Skip to content

Commit

Permalink
comment resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhamvernekar committed Nov 6, 2023
1 parent a706a54 commit bbc7f01
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 22 deletions.
14 changes: 8 additions & 6 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 9 additions & 1 deletion client/gcs_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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}
}
17 changes: 13 additions & 4 deletions client/monart.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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}
Expand Down
5 changes: 3 additions & 2 deletions client/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/dolores/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 15 additions & 6 deletions secrets/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -137,15 +137,16 @@ 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
}
return c.Out
}

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
Expand All @@ -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
}
}
Expand Down
4 changes: 2 additions & 2 deletions store/google/gcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit bbc7f01

Please sign in to comment.