Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GCP Secret Manager and Storage Bucket Binding implicit authentication support with Workload Identity #3711

Merged
Merged
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
43 changes: 36 additions & 7 deletions bindings/gcp/bucket/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,7 @@ func (g *GCPStorage) Init(ctx context.Context, metadata bindings.Metadata) error
return err
}

b, err := json.Marshal(m)
if err != nil {
return err
}

clientOptions := option.WithCredentialsJSON(b)
client, err := storage.NewClient(ctx, clientOptions)
client, err := g.getClient(ctx, m)
if err != nil {
return err
}
Expand All @@ -121,6 +115,41 @@ func (g *GCPStorage) Init(ctx context.Context, metadata bindings.Metadata) error
return nil
}

func (g *GCPStorage) getClient(ctx context.Context, m *gcpMetadata) (*storage.Client, error) {
var client *storage.Client
var err error

if m.Bucket == "" {
return nil, errors.New("missing property `bucket` in metadata")
}
if m.ProjectID == "" {
return nil, errors.New("missing property `project_id` in metadata")
}

// Explicit authentication
if m.PrivateKeyID != "" {
var b []byte
b, err = json.Marshal(m)
if err != nil {
return nil, err
}

clientOptions := option.WithCredentialsJSON(b)
client, err = storage.NewClient(ctx, clientOptions)
if err != nil {
return nil, err
}
} else {
// Implicit authentication, using GCP Application Default Credentials (ADC)
// Credentials search order: https://cloud.google.com/docs/authentication/application-default-credentials#order
client, err = storage.NewClient(ctx)
if err != nil {
return nil, err
}
}
return client, nil
}

func (g *GCPStorage) parseMetadata(meta bindings.Metadata) (*gcpMetadata, error) {
m := gcpMetadata{}
err := kitmd.DecodeMetadata(meta.Properties, &m)
Expand Down
25 changes: 25 additions & 0 deletions bindings/gcp/bucket/bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package bucket
import (
"context"
"encoding/json"
"errors"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -235,6 +236,30 @@ func TestMergeWithRequestMetadata(t *testing.T) {
})
}

func TestInit(t *testing.T) {
t.Run("Init missing bucket from metadata", func(t *testing.T) {
m := bindings.Metadata{}
m.Properties = map[string]string{
"projectID": "my_project_id",
}
gs := GCPStorage{logger: logger.NewLogger("test")}
err := gs.Init(context.Background(), m)
require.Error(t, err)
assert.Equal(t, err, errors.New("missing property `bucket` in metadata"))
})

t.Run("Init missing projectID from metadata", func(t *testing.T) {
m := bindings.Metadata{}
m.Properties = map[string]string{
"bucket": "my_bucket",
}
gs := GCPStorage{logger: logger.NewLogger("test")}
err := gs.Init(context.Background(), m)
require.Error(t, err)
assert.Equal(t, err, errors.New("missing property `project_id` in metadata"))
})
}

func TestGetOption(t *testing.T) {
gs := GCPStorage{logger: logger.NewLogger("test")}
gs.metadata = &gcpMetadata{}
Expand Down
45 changes: 31 additions & 14 deletions secretstores/gcp/secretmanager/secretmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,38 @@ func (s *Store) Init(ctx context.Context, metadataRaw secretstores.Metadata) err
}

func (s *Store) getClient(ctx context.Context, metadata *GcpSecretManagerMetadata) (*secretmanager.Client, error) {
b, _ := json.Marshal(metadata)
clientOptions := option.WithCredentialsJSON(b)
var client *secretmanager.Client
var err error

client, err := secretmanager.NewClient(ctx, clientOptions)
if err != nil {
return nil, err
if metadata.ProjectID == "" {
return nil, errors.New("missing property `project_id` in metadata")
}

// Explicit authentication
if metadata.PrivateKeyID != "" {
if metadata.Type == "" {
return nil, errors.New("missing property `type` in metadata")
}
if metadata.PrivateKey == "" {
return nil, errors.New("missing property `private_key` in metadata")
}
if metadata.ClientEmail == "" {
return nil, errors.New("missing property `client_email` in metadata")
}

b, _ := json.Marshal(metadata)
clientOptions := option.WithCredentialsJSON(b)
client, err = secretmanager.NewClient(ctx, clientOptions)
if err != nil {
return nil, err
}
} else {
// Implicit authentication, using GCP Application Default Credentials (ADC)
// Credentials search order: https://cloud.google.com/docs/authentication/application-default-credentials#order
client, err = secretmanager.NewClient(ctx)
if err != nil {
return nil, err
}
}

return client, nil
Expand Down Expand Up @@ -183,18 +209,9 @@ func (s *Store) parseSecretManagerMetadata(metadataRaw secretstores.Metadata) (*
return nil, fmt.Errorf("failed to decode metadata: %w", err)
}

if meta.Type == "" {
return nil, errors.New("missing property `type` in metadata")
}
if meta.ProjectID == "" {
return nil, errors.New("missing property `project_id` in metadata")
}
if meta.PrivateKey == "" {
return nil, errors.New("missing property `private_key` in metadata")
}
if meta.ClientEmail == "" {
return nil, errors.New("missing property `client_email` in metadata")
}

return &meta, nil
}
Expand Down
47 changes: 45 additions & 2 deletions secretstores/gcp/secretmanager/secretmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,47 @@ func TestInit(t *testing.T) {

t.Run("Init with missing `type` metadata", func(t *testing.T) {
m.Properties = map[string]string{
"dummy": "a",
"dummy": "a",
"private_key_id": "a",
"project_id": "a",
}
err := sm.Init(ctx, m)
require.Error(t, err)
assert.Equal(t, err, errors.New("missing property `type` in metadata"))
assert.Equal(t, errors.New("failed to setup secretmanager client: missing property `type` in metadata"), err)
})

t.Run("Init with missing `private_key` metadata", func(t *testing.T) {
m.Properties = map[string]string{
"dummy": "a",
"private_key_id": "a",
"type": "a",
"project_id": "a",
}
err := sm.Init(ctx, m)
require.Error(t, err)
assert.Equal(t, errors.New("failed to setup secretmanager client: missing property `private_key` in metadata"), err)
})

t.Run("Init with missing `client_email` metadata", func(t *testing.T) {
m.Properties = map[string]string{
"dummy": "a",
"private_key_id": "a",
"private_key": "a",
"type": "a",
"project_id": "a",
}
err := sm.Init(ctx, m)
require.Error(t, err)
assert.Equal(t, errors.New("failed to setup secretmanager client: missing property `client_email` in metadata"), err)
})

t.Run("Init with missing `project_id` metadata", func(t *testing.T) {
m.Properties = map[string]string{
"type": "service_account",
}
err := sm.Init(ctx, m)
require.Error(t, err)
assert.Equal(t, err, errors.New("missing property `project_id` in metadata"))
})

t.Run("Init with missing `project_id` metadata", func(t *testing.T) {
Expand All @@ -91,6 +127,13 @@ func TestInit(t *testing.T) {
require.Error(t, err)
assert.Equal(t, err, errors.New("missing property `project_id` in metadata"))
})

t.Run("Init with empty metadata", func(t *testing.T) {
m.Properties = map[string]string{}
err := sm.Init(ctx, m)
require.Error(t, err)
assert.Equal(t, err, errors.New("missing property `project_id` in metadata"))
})
}

func TestGetSecret(t *testing.T) {
Expand Down
Loading