Skip to content
This repository was archived by the owner on Feb 16, 2023. It is now read-only.

Commit 063b8ef

Browse files
Merge pull request #313 from secrethub/feature/user-managed-sa-only
Only list user-managed GCP Service Accounts in service gcp init prompt
2 parents 4cb0578 + 56f38ba commit 063b8ef

3 files changed

Lines changed: 13 additions & 7 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ require (
1717
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7
1818
github.com/pkg/errors v0.9.1 // indirect
1919
github.com/secrethub/demo-app v0.1.0
20-
github.com/secrethub/secrethub-go v0.29.1-0.20200703150346-411544a71e9d
20+
github.com/secrethub/secrethub-go v0.29.1-0.20200707154958-5e5602145597
2121
github.com/zalando/go-keyring v0.0.0-20190208082241-fbe81aec3a07
2222
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550
2323
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ github.com/secrethub/secrethub-go v0.29.1-0.20200703092019-9f5d3de9b0e4 h1:TszZ+
174174
github.com/secrethub/secrethub-go v0.29.1-0.20200703092019-9f5d3de9b0e4/go.mod h1:tDeBtyjfFQX3UqgaZfY+H4dYkcGfiVzrwLDf0XtfOrw=
175175
github.com/secrethub/secrethub-go v0.29.1-0.20200703150346-411544a71e9d h1:tADItWP+YXaGLD1ZMFocxDaKKVcu8wXgEulbcUmX4Ec=
176176
github.com/secrethub/secrethub-go v0.29.1-0.20200703150346-411544a71e9d/go.mod h1:tDeBtyjfFQX3UqgaZfY+H4dYkcGfiVzrwLDf0XtfOrw=
177+
github.com/secrethub/secrethub-go v0.29.1-0.20200707154958-5e5602145597 h1:uC9ODMKaqBo1k8fxmFSWGkLr05TgEd3t4mHqJ8Jo9Gc=
178+
github.com/secrethub/secrethub-go v0.29.1-0.20200707154958-5e5602145597/go.mod h1:tDeBtyjfFQX3UqgaZfY+H4dYkcGfiVzrwLDf0XtfOrw=
177179
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
178180
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
179181
github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48=

internals/secrethub/service_gcp_init.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (cmd *ServiceGCPInitCommand) Run() error {
7676
serviceAccountLister := gcpServiceAccountOptionLister{
7777
ProjectID: projectID,
7878
}
79-
serviceAccountEmail, err := ui.ChooseDynamicOptionsValidate(cmd.io, "What is the email of the service account you want to use?", serviceAccountLister.Options, "service account", api.ValidateGCPServiceAccountEmail)
79+
serviceAccountEmail, err := ui.ChooseDynamicOptionsValidate(cmd.io, "What is the email of the service account you want to use?", serviceAccountLister.Options, "service account", api.ValidateGCPUserManagedServiceAccountEmail)
8080
if err != nil {
8181
return err
8282
}
@@ -98,7 +98,7 @@ func (cmd *ServiceGCPInitCommand) Run() error {
9898
}
9999

100100
if cmd.serviceAccountEmail == "" {
101-
serviceAccountEmail, err := ui.AskAndValidate(cmd.io, "What is the email of the GCP Service Account that should have access to the service?\n", 3, api.ValidateGCPServiceAccountEmail)
101+
serviceAccountEmail, err := ui.AskAndValidate(cmd.io, "What is the email of the GCP Service Account that should have access to the service?\n", 3, api.ValidateGCPUserManagedServiceAccountEmail)
102102
if err != nil {
103103
return err
104104
}
@@ -224,16 +224,20 @@ func (l *gcpServiceAccountOptionLister) Options() ([]ui.Option, bool, error) {
224224
return nil, false, gcp.HandleError(err)
225225
}
226226

227-
options := make([]ui.Option, len(resp.Accounts))
228-
for i, account := range resp.Accounts {
227+
options := make([]ui.Option, 0, len(resp.Accounts))
228+
for _, account := range resp.Accounts {
229+
// Only list user-managed service accounts
230+
if err := api.ValidateGCPUserManagedServiceAccountEmail(account.Email); err != nil {
231+
continue
232+
}
229233
display := account.Email
230234
if account.Description != "" {
231235
display += " (" + account.Description + ")"
232236
}
233-
options[i] = ui.Option{
237+
options = append(options, ui.Option{
234238
Value: account.Email,
235239
Display: display,
236-
}
240+
})
237241
}
238242

239243
l.nextPage = resp.NextPageToken

0 commit comments

Comments
 (0)