@@ -88,12 +88,38 @@ func (s *Store) Init(ctx context.Context, metadataRaw secretstores.Metadata) err
8888}
8989
9090func (s * Store ) getClient (ctx context.Context , metadata * GcpSecretManagerMetadata ) (* secretmanager.Client , error ) {
91- b , _ := json . Marshal ( metadata )
92- clientOptions := option . WithCredentialsJSON ( b )
91+ var client * secretmanager. Client
92+ var err error
9393
94- client , err := secretmanager .NewClient (ctx , clientOptions )
95- if err != nil {
96- return nil , err
94+ if metadata .ProjectID == "" {
95+ return nil , errors .New ("missing property `project_id` in metadata" )
96+ }
97+
98+ // Explicit authentication
99+ if metadata .PrivateKeyID != "" {
100+ if metadata .Type == "" {
101+ return nil , errors .New ("missing property `type` in metadata" )
102+ }
103+ if metadata .PrivateKey == "" {
104+ return nil , errors .New ("missing property `private_key` in metadata" )
105+ }
106+ if metadata .ClientEmail == "" {
107+ return nil , errors .New ("missing property `client_email` in metadata" )
108+ }
109+
110+ b , _ := json .Marshal (metadata )
111+ clientOptions := option .WithCredentialsJSON (b )
112+ client , err = secretmanager .NewClient (ctx , clientOptions )
113+ if err != nil {
114+ return nil , err
115+ }
116+ } else {
117+ // Implicit authentication, using GCP Application Default Credentials (ADC)
118+ // Credentials search order: https://cloud.google.com/docs/authentication/application-default-credentials#order
119+ client , err = secretmanager .NewClient (ctx )
120+ if err != nil {
121+ return nil , err
122+ }
97123 }
98124
99125 return client , nil
@@ -183,18 +209,9 @@ func (s *Store) parseSecretManagerMetadata(metadataRaw secretstores.Metadata) (*
183209 return nil , fmt .Errorf ("failed to decode metadata: %w" , err )
184210 }
185211
186- if meta .Type == "" {
187- return nil , errors .New ("missing property `type` in metadata" )
188- }
189212 if meta .ProjectID == "" {
190213 return nil , errors .New ("missing property `project_id` in metadata" )
191214 }
192- if meta .PrivateKey == "" {
193- return nil , errors .New ("missing property `private_key` in metadata" )
194- }
195- if meta .ClientEmail == "" {
196- return nil , errors .New ("missing property `client_email` in metadata" )
197- }
198215
199216 return & meta , nil
200217}
0 commit comments