From bbc7f011322b2257da19825e82c7352c340aba9b Mon Sep 17 00:00:00 2001 From: shubhamvernekar Date: Mon, 6 Nov 2023 11:34:57 +0530 Subject: [PATCH] comment resolve --- client/client.go | 14 ++++++++------ client/gcs_service.go | 10 +++++++++- client/monart.go | 17 +++++++++++++---- client/service_test.go | 5 +++-- cmd/dolores/main.go | 2 +- secrets/manager.go | 21 +++++++++++++++------ store/google/gcs.go | 4 ++-- 7 files changed, 51 insertions(+), 22 deletions(-) diff --git a/client/client.go b/client/client.go index b4796fc..1d7b661 100644 --- a/client/client.go +++ b/client/client.go @@ -77,13 +77,15 @@ func (c *Client) GetOrgPublicKeys(env string) (OrgPublicKeys, error) { return OrgPublicKeys{Recipients: recps}, nil } -func (c *Client) GetSecretList() ([]google.SecretObject, error) { - resp, err := c.Service.store.ListObject(c.ctx, c.bucket, c.prefix) - if err != nil { - return nil, err - } +type GetSecretListConfig struct { + Environment string `json:"environment"` +} +type GetSecretListResponse struct { + Secrets []google.SecretObject `json:"secrets"` +} - return resp, nil +func (c *Client) GetSecretList(cfg GetSecretListConfig) ([]google.SecretObject, error) { + return c.Service.GetObjList(c.ctx, c.bucket, c.prefix) } func New(ctx context.Context, cfg config.Client) (*Client, error) { diff --git a/client/gcs_service.go b/client/gcs_service.go index 5dad991..c532b5d 100644 --- a/client/gcs_service.go +++ b/client/gcs_service.go @@ -78,7 +78,7 @@ func (s Service) GetOrgPublicKeys(ctx context.Context, env, bucketName, path str if pubKey != "" { return []string{pubKey}, nil } - resp, err := s.store.ListObject(ctx, bucketName, path) + resp, err := s.GetObjList(ctx, bucketName, path) if err != nil { return nil, fmt.Errorf("error listing objects: %w", err) } @@ -138,6 +138,14 @@ func (s Service) saveObject(ctx context.Context, bucket, fname string, md any) e return s.store.WriteToObject(ctx, bucket, fname, data) } +func (s Service) GetObjList(ctx context.Context, bucket, path string) ([]google.SecretObject, error) { + resp, err := s.store.ListObject(ctx, bucket, path) + if err != nil { + return nil, err + } + return resp, nil +} + func NewService(st gcsStore) Service { return Service{store: st} } diff --git a/client/monart.go b/client/monart.go index 6332cbc..ff60f49 100644 --- a/client/monart.go +++ b/client/monart.go @@ -87,6 +87,19 @@ func (s MonartClient) FetchSecrets(fetchReq FetchSecretRequest) ([]byte, error) return sec, nil } +func (s MonartClient) GetSecretList(cfg GetSecretListConfig) ([]google.SecretObject, error) { + path := fmt.Sprintf("/environment/%s/secrets", cfg.Environment) + req, err := http.NewRequest(http.MethodGet, s.serverURL(path), nil) + if err != nil { + return nil, fmt.Errorf("unable to build Get SecretList request: %w", err) + } + result := new(GetSecretListResponse) + if _, err := s.call(req, &result); err != nil { + return nil, err + } + return result.Secrets, nil +} + func (s MonartClient) serverURL(path string) string { return "https://relyonmetrics.com/api/" + path } @@ -110,10 +123,6 @@ func (s MonartClient) call(req *http.Request, dest any) (*http.Response, error) return resp, nil } -func (c MonartClient) GetSecretList() ([]google.SecretObject, error) { - return nil, nil -} - func NewMonart(ctx context.Context, cfg *config.Monart) MonartClient { cred := credentials{APIToken: cfg.APIToken, ID: cfg.ID} return MonartClient{cli: http.DefaultClient, cred: cred, ctx: ctx} diff --git a/client/service_test.go b/client/service_test.go index 2751b92..986830f 100644 --- a/client/service_test.go +++ b/client/service_test.go @@ -7,6 +7,7 @@ import ( "github.com/scalescape/dolores/client" "github.com/scalescape/dolores/config" + "github.com/scalescape/dolores/store/google" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" @@ -38,9 +39,9 @@ func (m *mockGCS) ReadObject(ctx context.Context, bucketName, fileName string) ( return args.Get(0).([]byte), args.Error(1) } -func (m *mockGCS) ListObject(ctx context.Context, bucketName, path string) ([]string, error) { +func (m *mockGCS) ListObject(ctx context.Context, bucketName, path string) ([]google.SecretObject, error) { args := m.Called(ctx, bucketName, path) - return args.Get(0).([]string), args.Error(1) + return args.Get(0).([]google.SecretObject), args.Error(1) } func (m *mockGCS) ExistsObject(ctx context.Context, bucketName, fileName string) (bool, error) { diff --git a/cmd/dolores/main.go b/cmd/dolores/main.go index 013ee0a..5205891 100644 --- a/cmd/dolores/main.go +++ b/cmd/dolores/main.go @@ -18,7 +18,7 @@ type secretsClient interface { FetchSecrets(req client.FetchSecretRequest) ([]byte, error) GetOrgPublicKeys(env string) (client.OrgPublicKeys, error) Init(ctx context.Context, bucket string, cfg client.Configuration) error - GetSecretList() ([]google.SecretObject, error) + GetSecretList(req client.GetSecretListConfig) ([]google.SecretObject, error) } type CtxKey string diff --git a/secrets/manager.go b/secrets/manager.go index 9cb0944..fb7d7e6 100644 --- a/secrets/manager.go +++ b/secrets/manager.go @@ -18,7 +18,7 @@ type secClient interface { FetchSecrets(req client.FetchSecretRequest) ([]byte, error) UploadSecrets(req client.EncryptedConfig) error GetOrgPublicKeys(env string) (client.OrgPublicKeys, error) - GetSecretList() ([]google.SecretObject, error) + GetSecretList(cfg client.GetSecretListConfig) ([]google.SecretObject, error) } type EncryptConfig struct { @@ -137,7 +137,7 @@ type ListSecretConfig struct { Out io.Writer } -func (c ListSecretConfig) Output() io.Writer { +func (c ListSecretConfig) output() io.Writer { if c.Out == nil { return os.Stdout } @@ -145,7 +145,8 @@ func (c ListSecretConfig) Output() io.Writer { } func (c ListSecretConfig) Valid() error { - if strings.ToLower(c.Environment) != "production" && strings.ToLower(c.Environment) != "staging" { + env := strings.ToLower(c.Environment) + if env != "production" && env != "staging" { return ErrInvalidEnvironment } return nil @@ -155,16 +156,24 @@ func (sm SecretManager) ListSecret(cfg ListSecretConfig) error { if err := cfg.Valid(); err != nil { return fmt.Errorf("invalid config: %w", err) } - resp, err := sm.client.GetSecretList() + req := client.GetSecretListConfig{Environment: cfg.Environment} + resp, err := sm.client.GetSecretList(req) if err != nil { return fmt.Errorf("failed to get secrets: %w", err) } - if _, err := cfg.Output().Write([]byte(fmt.Sprintf("%-20s %-40s %-40s %-40s\n", "Name", "Bucket", "Create At", "Updated At"))); err != nil { + if _, err := cfg.output().Write([]byte(fmt.Sprintf("%-10s %-50s %-30s %-30s\n", "Name", "Location", "Created At UTC", "Updated At UTC"))); err != nil { return err } for _, obj := range resp { if !strings.HasSuffix(obj.Name, ".key") && !strings.HasSuffix(obj.Name, "/") { - if _, err := cfg.Output().Write([]byte(fmt.Sprintf("%-20s %-40s %-40s %-40s\n", obj.Name, obj.Bucket, obj.CreatedAt, obj.UpdatedAt))); err != nil { + arr := strings.SplitN(obj.Name, "/", 2) + name := obj.Name + if len(arr) == 2 { + name = arr[1] + } + createdAt := obj.CreatedAt.Format("2000-01-02 15:04:05.999") + updatedAt := obj.UpdatedAt.Format("2000-01-02 15:04:05.999") + if _, err := cfg.output().Write([]byte(fmt.Sprintf("%-10s %-50s %-30s %-30s\n", name, obj.Location, createdAt, updatedAt))); err != nil { return err } } diff --git a/store/google/gcs.go b/store/google/gcs.go index 8c168bc..4635a21 100644 --- a/store/google/gcs.go +++ b/store/google/gcs.go @@ -28,7 +28,7 @@ type Config struct { type SecretObject struct { Name string `json:"name"` - Bucket string `json:"bucket"` + Location string `json:"location"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` } @@ -128,7 +128,7 @@ func (s StorageClient) ListObject(ctx context.Context, bucketName, path string) if err != nil { return nil, fmt.Errorf("failed to iterate object list: %w", err) } - o := SecretObject{Name: attrs.Name, CreatedAt: attrs.Created, UpdatedAt: attrs.Updated, Bucket: attrs.Bucket} + o := SecretObject{Name: attrs.Name, CreatedAt: attrs.Created, UpdatedAt: attrs.Updated, Location: fmt.Sprintf("%s/%s", attrs.Bucket, attrs.Name)} objs = append(objs, o) } log.Trace().Msgf("list of objects from path: %s %+v", path, objs)