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

Commit bdadd27

Browse files
Merge pull request #358 from secrethub/feature/improve-credential-error
Improve credential errors
2 parents 5e40148 + ddbed30 commit bdadd27

5 files changed

Lines changed: 37 additions & 6 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/client_factory.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ type clientFactory struct {
4040
}
4141

4242
// Register the flags for configuration on a cli application.
43+
// The environment variables of these flags are also checked on the client, but checking them here allows us to fail fast.
4344
func (f *clientFactory) Register(r FlagRegisterer) {
4445
r.Flag("api-remote", "The SecretHub API address, don't set this unless you know what you're doing.").Hidden().URLVar(&f.ServerURL)
4546
r.Flag("identity-provider", "Enable native authentication with a trusted identity provider. Options are `aws` (IAM + KMS), `gcp` (IAM + KMS) and `key`. When you run the CLI on one of the platforms, you can leverage their respective identity providers to do native keyless authentication. Defaults to key, which uses the default credential sourced from a file, command-line flag, or environment variable. ").Default("key").StringVar(&f.identityProvider)

internals/secrethub/credential_store.go

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package secrethub
33
import (
44
"time"
55

6+
"github.com/secrethub/secrethub-cli/internals/cli"
7+
68
"github.com/secrethub/secrethub-go/pkg/secrethub/configdir"
79
"github.com/secrethub/secrethub-go/pkg/secrethub/credentials"
810

@@ -34,7 +36,7 @@ func NewCredentialConfig(io ui.IO) CredentialConfig {
3436

3537
type credentialConfig struct {
3638
configDir ConfigDir
37-
AccountCredential string
39+
credentialReader *flagCredentialReader
3840
credentialPassphrase string
3941
CredentialPassphraseCacheTTL time.Duration
4042
io ui.IO
@@ -49,9 +51,10 @@ func (store *credentialConfig) IsPassphraseSet() bool {
4951
}
5052

5153
// Register registers the flags for configuring the store on the provided Registerer.
54+
// The environment variables of these flags are also checked on the client, but checking them here allows us to fail fast.
5255
func (store *credentialConfig) Register(r FlagRegisterer) {
5356
r.Flag("config-dir", "The absolute path to a custom configuration directory. Defaults to $HOME/.secrethub").Default("").PlaceHolder("CONFIG-DIR").SetValue(&store.configDir)
54-
r.Flag("credential", "Use a specific account credential to authenticate to the API. This overrides the credential stored in the configuration directory.").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."))
5558
r.Flag("p", "").Short('p').Hidden().NoEnvar().StringVar(&store.credentialPassphrase) // Shorthand -p is deprecated. Use --credential-passphrase instead.
5659
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)
5760
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)
@@ -69,13 +72,38 @@ func (store *credentialConfig) Import() (credentials.Key, error) {
6972
}
7073

7174
func (store *credentialConfig) getCredentialReader() credentials.Reader {
72-
if store.AccountCredential != "" {
73-
return credentials.FromString(store.AccountCredential)
75+
if store.credentialReader.value == "" {
76+
return store.configDir.Credential()
7477
}
75-
return store.configDir.Credential()
78+
return store.credentialReader
7679
}
7780

7881
// PassphraseReader returns a PassphraseReader configured by the flags.
7982
func (store *credentialConfig) PassphraseReader() credentials.Reader {
8083
return NewPassphraseReader(store.io, store.credentialPassphrase, store.CredentialPassphraseCacheTTL)
8184
}
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)