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

Commit ddbed30

Browse files
committed
Make the --credential flag a credential reader and source
1 parent d91d66f commit ddbed30

4 files changed

Lines changed: 32 additions & 10 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.31.0
20+
github.com/secrethub/secrethub-go v0.30.1-0.20201215150659-e5f45b9d8a0d
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/sys v0.0.0-20200501052902-10377860bb8e

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ github.com/secrethub/secrethub-go v0.29.1-0.20200728110331-9d7b31301226/go.mod h
182182
github.com/secrethub/secrethub-go v0.29.1-0.20200728110331-9d7b31301226/go.mod h1:tDeBtyjfFQX3UqgaZfY+H4dYkcGfiVzrwLDf0XtfOrw=
183183
github.com/secrethub/secrethub-go v0.30.0 h1:Nh1twPDwPbYQj/cYc1NG+j7sv76LZiXLPovyV83tZj0=
184184
github.com/secrethub/secrethub-go v0.30.0/go.mod h1:tDeBtyjfFQX3UqgaZfY+H4dYkcGfiVzrwLDf0XtfOrw=
185+
github.com/secrethub/secrethub-go v0.30.1-0.20201215150659-e5f45b9d8a0d h1:5HtPCmZWsK3hLHyT825lhp6361uu3gRFJFN7MLr36ec=
186+
github.com/secrethub/secrethub-go v0.30.1-0.20201215150659-e5f45b9d8a0d/go.mod h1:ZIco8Y0G0Pi0Vb7pQROjvEKgSreZiRMLhAbzWUneUSQ=
185187
github.com/secrethub/secrethub-go v0.31.0 h1:0KoG0KHBOa5knkvf3K0f6sKuPSQ5VGPXLD4ttC9Eul8=
186188
github.com/secrethub/secrethub-go v0.31.0/go.mod h1:ZIco8Y0G0Pi0Vb7pQROjvEKgSreZiRMLhAbzWUneUSQ=
187189
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=

internals/secrethub/credential_store.go

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ func NewCredentialConfig(io ui.IO) CredentialConfig {
3636

3737
type credentialConfig struct {
3838
configDir ConfigDir
39-
AccountCredential string
40-
credentialFlag *cli.Flag
39+
credentialReader *flagCredentialReader
4140
credentialPassphrase string
4241
CredentialPassphraseCacheTTL time.Duration
4342
io ui.IO
@@ -55,8 +54,7 @@ func (store *credentialConfig) IsPassphraseSet() bool {
5554
// The environment variables of these flags are also checked on the client, but checking them here allows us to fail fast.
5655
func (store *credentialConfig) Register(r FlagRegisterer) {
5756
r.Flag("config-dir", "The absolute path to a custom configuration directory. Defaults to $HOME/.secrethub").Default("").PlaceHolder("CONFIG-DIR").SetValue(&store.configDir)
58-
store.credentialFlag = r.Flag("credential", "Use a specific account credential to authenticate to the API. This overrides the credential stored in the configuration directory.")
59-
store.credentialFlag.StringVar(&store.AccountCredential)
57+
store.credentialReader = credentialReader(r.Flag("credential", "Use a specific account credential to authenticate to the API. This overrides the credential stored in the configuration directory."))
6058
r.Flag("p", "").Short('p').Hidden().NoEnvar().StringVar(&store.credentialPassphrase) // Shorthand -p is deprecated. Use --credential-passphrase instead.
6159
r.Flag("credential-passphrase", "The passphrase to unlock your credential file. When set, it will not prompt for the passphrase, nor cache it in the OS keyring. Please only use this if you know what you're doing and ensure your passphrase doesn't end up in bash history.").StringVar(&store.credentialPassphrase)
6260
r.Flag("credential-passphrase-cache-ttl", "Cache the credential passphrase in the OS keyring for this duration. The cache is automatically cleared after the timer runs out. Each time the passphrase is read from the cache the timer is reset. Passphrase caching is turned on by default for 5 minutes. Turn it off by setting the duration to 0.").Default("5m").DurationVar(&store.CredentialPassphraseCacheTTL)
@@ -74,16 +72,38 @@ func (store *credentialConfig) Import() (credentials.Key, error) {
7472
}
7573

7674
func (store *credentialConfig) getCredentialReader() credentials.Reader {
77-
if store.AccountCredential == "" {
75+
if store.credentialReader.value == "" {
7876
return store.configDir.Credential()
7977
}
80-
if store.credentialFlag.HasEnvarValue() {
81-
return credentials.FromEnv("SECRETHUB_CREDENTIAL")
82-
}
83-
return credentials.FromString(store.AccountCredential)
78+
return store.credentialReader
8479
}
8580

8681
// PassphraseReader returns a PassphraseReader configured by the flags.
8782
func (store *credentialConfig) PassphraseReader() credentials.Reader {
8883
return NewPassphraseReader(store.io, store.credentialPassphrase, store.CredentialPassphraseCacheTTL)
8984
}
85+
86+
// credentialReader returns a credential reader and source that reads from the given flag (and its corresponding env var).
87+
func credentialReader(flag *cli.Flag) *flagCredentialReader {
88+
reader := flagCredentialReader{Flag: flag}
89+
flag.StringVar(&reader.value)
90+
flag.IsSetByUser(&reader.setByUser)
91+
return &reader
92+
}
93+
94+
type flagCredentialReader struct {
95+
*cli.Flag
96+
value string
97+
setByUser bool
98+
}
99+
100+
func (f *flagCredentialReader) Read() ([]byte, error) {
101+
return []byte(f.value), nil
102+
}
103+
104+
func (f *flagCredentialReader) Source() string {
105+
if f.HasEnvarValue() && !f.setByUser {
106+
return "$SECRETHUB_CREDENTIAL"
107+
}
108+
return "--credential"
109+
}

secrethub

22 MB
Binary file not shown.

0 commit comments

Comments
 (0)