Skip to content

Commit

Permalink
Merge pull request #4 from shubhamvernekar/main
Browse files Browse the repository at this point in the history
Persisting Application Credentials file location in dolores.json
  • Loading branch information
devdinu authored Nov 4, 2023
2 parents 7ccc529 + 5a592be commit 587be66
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 26 deletions.
8 changes: 5 additions & 3 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,11 @@ func New(ctx context.Context, cfg config.Client) (*Client, error) {
return nil, err
}
cli := &Client{
ctx: ctx, Service: Service{store: st},
bucket: cfg.BucketName(), prefix: cfg.StoragePrefix,
log: log.With().Str("bucket", cfg.BucketName()).Str("prefix", cfg.StoragePrefix).Logger(),
ctx: ctx,
Service: Service{store: st},
bucket: cfg.BucketName(),
prefix: cfg.StoragePrefix,
log: log.With().Str("bucket", cfg.BucketName()).Str("prefix", cfg.StoragePrefix).Logger(),
}
return cli, nil
}
14 changes: 7 additions & 7 deletions cmd/dolores/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ type Input struct {

func (inp Input) ToMetadata(env string) config.Metadata {
return config.Metadata{
Bucket: inp.Bucket,
Location: inp.Location,
CreatedAt: time.Now(),
Environment: env,
Bucket: inp.Bucket,
Location: inp.Location,
CreatedAt: time.Now(),
Environment: env,
ApplicationCredentials: inp.ApplicationCredentials,
}
}

Expand Down Expand Up @@ -162,10 +163,9 @@ func (c *InitCommand) initialize(cctx *cli.Context) error {
if err := d.SaveToDisk(); err != nil {
return fmt.Errorf("error saving dolores config: %w", err)
}
ctx := context.WithValue(cctx.Context, config.CredsKey, inp.ApplicationCredentials)
cli := c.rcli(ctx)
cli := c.rcli(cctx.Context)
cfg := client.Configuration{PublicKey: publicKey, Metadata: md, UserID: inp.UserID}
if err := cli.Init(ctx, md.Bucket, cfg); err != nil {
if err := cli.Init(cctx.Context, md.Bucket, cfg); err != nil {
return err
}
log.Info().Msgf("successfully initialized dolores")
Expand Down
15 changes: 13 additions & 2 deletions cmd/dolores/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,17 @@ import (
"github.com/urfave/cli/v2"
)

func parseKeyConfig(ctx *cli.Context, cfg *secrets.DecryptConfig) {
func parseKeyConfig(ctx *cli.Context, cfg *secrets.DecryptConfig) error {
log.Trace().Msgf("parsing configuration required to decrypt config")
key := ctx.String("key")
keyFile := ctx.String("key-file")
if keyFile == "" {
d, err := config.LoadFromDisk()
if err != nil {
return fmt.Errorf("dolores not initialized yet: %w", err)
}
keyFile = d.Environments[cfg.Environment].KeyFile
}
if keyFile == "" {
keyFile = os.Getenv("DOLORES_SECRETS_KEY_FILE")
}
Expand All @@ -24,6 +31,8 @@ func parseKeyConfig(ctx *cli.Context, cfg *secrets.DecryptConfig) {
}
cfg.KeyFile = keyFile
cfg.Key = key

return nil
}

func parseDecryptConfig(ctx *cli.Context) (secrets.DecryptConfig, error) {
Expand All @@ -34,7 +43,9 @@ func parseDecryptConfig(ctx *cli.Context) (secrets.DecryptConfig, error) {
Name: name,
Out: os.Stdout,
}
parseKeyConfig(ctx, &req)
if err := parseKeyConfig(ctx, &req); err != nil {
return secrets.DecryptConfig{}, fmt.Errorf("unable to load key-file from config: %w", err)
}
if err := req.Valid(); err != nil {
return secrets.DecryptConfig{}, fmt.Errorf("pass appropriate key or key-file to decrypt: %w", err)
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/dolores/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ func (c *Runner) parse(ctx *cli.Context) error {
Environment: c.environment,
Out: c.configBuffer,
}
parseKeyConfig(ctx, &c.DecryptConfig)
if err := parseKeyConfig(ctx, &c.DecryptConfig); err != nil {
return err
}
if err := c.DecryptConfig.Valid(); err != nil {
return err
}
Expand Down
29 changes: 16 additions & 13 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ import (
var (
ErrInvalidGoogleCreds = errors.New("invalid google application credentials")
ErrInvalidStorageBucket = errors.New("invalid storage bucket")
ErrInvalidKeyFile = errors.New("invalid key file")
)

type CtxKey string

var (
EnvKey CtxKey = "ctx_environment"
CredsKey CtxKey = "ctx_application_creds"
)
var EnvKey CtxKey = "ctx_environment"

var (
HomePath = os.Getenv("HOME")
Expand All @@ -36,10 +34,11 @@ type Google struct {
}

type Metadata struct {
Bucket string `json:"bucket"`
Location string `json:"location"`
Environment string `json:"environment"`
CreatedAt time.Time `json:"created_at"`
Bucket string `json:"bucket"`
Location string `json:"location"`
Environment string `json:"environment"`
CreatedAt time.Time `json:"created_at"`
ApplicationCredentials string `json:"application_credentials"`
}

type Client struct {
Expand Down Expand Up @@ -70,17 +69,21 @@ func LoadClient(ctx context.Context, env string) (Client, error) {
return Client{}, fmt.Errorf("processing config: %w", err)
}

md := d.Environments[env].Metadata
if cfg.Google.ApplicationCredentials == "" {
if creds, ok := ctx.Value(CredsKey).(string); ok {
if creds := md.ApplicationCredentials; creds != "" {
cfg.Google.ApplicationCredentials = creds
}
}
md := d.Environments[env].Metadata
bucket := md.Bucket
if bucket != "" {

if bucket := md.Bucket; bucket != "" {
cfg.Google.StorageBucket = bucket
cfg.Google.StoragePrefix = md.Location
}

if location := md.Location; location != "" {
cfg.Google.StoragePrefix = location
}

if err := cfg.Valid(); err != nil {
return Client{}, err
}
Expand Down

0 comments on commit 587be66

Please sign in to comment.