Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions secrets/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,26 @@ func (a *awsSecretsManager) CheckSecretExists(ctx context.Context, key string) b
return err == nil
}

func (a *awsSecretsManager) GetSecretLabels(ctx context.Context, key string) (map[string]string, bool) {
secret, err := a.client.DescribeSecret(ctx, &secretsmanager.DescribeSecretInput{
SecretId: &key,
})
if err != nil {
return nil, false
}
return convert(secret), true
}

func convert(secret *secretsmanager.DescribeSecretOutput) map[string]string {
labels := make(map[string]string)
for _, tag := range secret.Tags {
if tag.Key != nil && tag.Value != nil {
labels[*tag.Key] = *tag.Value
}
}
return labels
}

func (a *awsSecretsManager) CreateSecret(ctx context.Context, key string, value string, tags map[string]string) error {
mergedTags := a.mergeTags(tags)

Expand Down
23 changes: 23 additions & 0 deletions secrets/az.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,29 @@ func (a *azSecretsManager) CheckSecretExists(ctx context.Context, key string) bo
return err == nil && len(page.Value) > 0
}

func (a *azSecretsManager) GetSecretLabels(ctx context.Context, key string) (map[string]string, bool) {
key = sanitize(key)
pager := a.client.NewListSecretVersionsPager(key, nil)
if !pager.More() {
return nil, false
}

page, err := pager.NextPage(ctx)
if err != nil || len(page.Value) == 0 {
return nil, false
}

// Get the latest version's properties (first item should be the latest)
secretProps := page.Value[0]
labels := make(map[string]string)
for k, v := range secretProps.Tags {
if v != nil {
labels[k] = *v
}
}
return labels, true
}

// CreateSecret creates a new secret.
func (a *azSecretsManager) CreateSecret(ctx context.Context, key string, value string, tags map[string]string) error {
key = sanitize(key)
Expand Down
10 changes: 10 additions & 0 deletions secrets/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@ func (g *gcpSecretsManager) CheckSecretExists(ctx context.Context, key string) b
return err == nil
}

func (g *gcpSecretsManager) GetSecretLabels(ctx context.Context, key string) (map[string]string, bool) {
secret, err := g.client.GetSecret(ctx, &secretmanagerpb.GetSecretRequest{
Name: g.getSecretID(key),
})
if err != nil {
return nil, false
}
return secret.Labels, true
}

func (g *gcpSecretsManager) CreateSecret(ctx context.Context, key string, value string, tags map[string]string) error {
secretID := g.getSecretID(key)
mergedTags := g.mergeTags(tags)
Expand Down
12 changes: 12 additions & 0 deletions secrets/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (
type SecretAPI interface {
GetSecretValue(ctx context.Context, key string) (string, bool)
CheckSecretExists(ctx context.Context, key string) bool
// GetSecretLabels gets the secret labels.
GetSecretLabels(context.Context, string) (map[string]string, bool)
// CreateSecret creates a new secret with the provided tags.
// Global tags will overwrite any provided tags with the same keys.
CreateSecret(ctx context.Context, key string, value string, tags map[string]string) error
Expand Down Expand Up @@ -75,6 +77,16 @@ func (s *secretProvider) CheckSecretExists(ctx context.Context, key string) bool
return s.SecretAPI.CheckSecretExists(ctx, secretName)
}

// GetSecretLabels gets the secret labels.
func (s *secretProvider) GetSecretLabels(ctx context.Context, key string) (map[string]string, bool) {
secretName, _, ok := s.trimPrefixAndSplit(key)
if !ok {
return nil, false
}

return s.SecretAPI.GetSecretLabels(ctx, secretName)
}

// CreateSecret creates a new secret.
func (s *secretProvider) CreateSecret(ctx context.Context, key string, value string, tags map[string]string) error {
secretName, _, ok := s.trimPrefixAndSplit(key)
Expand Down
10 changes: 10 additions & 0 deletions secrets/generic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ func (f *fakeSecretManager) CheckSecretExists(_ context.Context, key string) boo
return ok
}

func (f *fakeSecretManager) GetSecretLabels(_ context.Context, key string) (map[string]string, bool) {
_, ok := f.secrets[key]
return map[string]string{}, ok
}

func (f *fakeSecretManager) CreateSecret(_ context.Context, key string, value string, tags map[string]string) error {
if f.secrets == nil {
f.secrets = make(map[string]string)
Expand Down Expand Up @@ -423,6 +428,11 @@ func (f *fakeSecretManagerWithGlobalTags) CheckSecretExists(_ context.Context, k
return ok
}

func (f *fakeSecretManagerWithGlobalTags) GetSecretLabels(_ context.Context, key string) (map[string]string, bool) {
_, ok := f.secrets[key]
return map[string]string{}, ok
}

func (f *fakeSecretManagerWithGlobalTags) CreateSecret(_ context.Context, key string, value string, tags map[string]string) error {
if f.secrets == nil {
f.secrets = make(map[string]string)
Expand Down
Loading