Skip to content

Commit

Permalink
use google application credentials input for client creation in init
Browse files Browse the repository at this point in the history
  • Loading branch information
devdinu committed Jul 5, 2023
1 parent 77c7120 commit 13009c0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
12 changes: 7 additions & 5 deletions cmd/dolores/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ func (c *InitCommand) getData(env string) (*Input, error) {
Name: "location",
Validate: survey.Required,
Prompt: &survey.Input{
Message: "Enter the object location to store the secrets:",
Message: "Enter the storage path to store the config:",
Default: "secrets",
},
},
{
Expand Down Expand Up @@ -132,8 +133,8 @@ func (c *InitCommand) generateKey(fname string) (string, error) {
return pubKey, nil
}

func (c *InitCommand) initialize(ctx *cli.Context) error {
env := ctx.String("environment")
func (c *InitCommand) initialize(cctx *cli.Context) error {
env := cctx.String("environment")
if env == "" {
return fmt.Errorf("invalid environment: %w", ErrInvalidEnvironment)
}
Expand Down Expand Up @@ -161,9 +162,10 @@ func (c *InitCommand) initialize(ctx *cli.Context) error {
if err := d.SaveToDisk(); err != nil {
return fmt.Errorf("error saving dolores config: %w", err)
}
cli := c.rcli(ctx.Context)
ctx := context.WithValue(cctx.Context, config.CredsKey, inp.ApplicationCredentials)
cli := c.rcli(ctx)
cfg := client.Configuration{PublicKey: publicKey, Metadata: md, UserID: inp.UserID}
if err := cli.Init(ctx.Context, md.Bucket, cfg); err != nil {
if err := cli.Init(ctx, md.Bucket, cfg); err != nil {
return err
}
log.Info().Msgf("successfully initialized dolores")
Expand Down
2 changes: 1 addition & 1 deletion cmd/dolores/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func newClient(ctx context.Context) *client.Client {
if !ok || env == "" {
log.Fatal().Msgf("environment not passed properly")
}
cfg, err := config.LoadClient(env)
cfg, err := config.LoadClient(ctx, env)
if err != nil {
log.Fatal().Msgf("error loading config: %v", err)
return nil
Expand Down
16 changes: 15 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"context"
"errors"
"fmt"
"os"
Expand All @@ -15,6 +16,13 @@ var (
ErrInvalidStorageBucket = errors.New("invalid storage bucket")
)

type CtxKey string

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

var (
HomePath = os.Getenv("HOME")
Dir = filepath.Join(HomePath, ".config", "dolores")
Expand Down Expand Up @@ -52,7 +60,7 @@ func (c Client) Valid() error {
return nil
}

func LoadClient(env string) (Client, error) {
func LoadClient(ctx context.Context, env string) (Client, error) {
var cfg Client
d, err := LoadFromDisk()
if err != nil {
Expand All @@ -61,6 +69,12 @@ func LoadClient(env string) (Client, error) {
if err := envconfig.Process("GOOGLE", &cfg.Google); err != nil {
return Client{}, fmt.Errorf("processing config: %w", err)
}

if cfg.Google.ApplicationCredentials == "" {
if creds, ok := ctx.Value(CredsKey).(string); ok {
cfg.Google.ApplicationCredentials = creds
}
}
md := d.Environments[env].Metadata
bucket := md.Bucket
if bucket != "" {
Expand Down
16 changes: 16 additions & 0 deletions store/google/gcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,22 @@ func (s StorageClient) ReadObject(ctx context.Context, bucketName, fileName stri
return data, nil
}

func (s StorageClient) ListBuckets(ctx context.Context) ([]string, error) {
buckets := make([]string, 0)
iter := s.Client.Buckets(ctx, s.projectID)
for {
attrs, err := iter.Next()
if errors.Is(err, iterator.Done) {
break
}
if err != nil {
return nil, fmt.Errorf("failed to iterate bucket list: %w", err)
}
buckets = append(buckets, attrs.Name)
}
return buckets, nil
}

func (s StorageClient) ListOjbect(ctx context.Context, bucketName, path string) ([]string, error) {
bucket := s.Client.Bucket(bucketName)
if _, err := bucket.Attrs(ctx); err != nil {
Expand Down

0 comments on commit 13009c0

Please sign in to comment.