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

Commit

Permalink
Merge pull request #234 from secrethub/fix/credential-errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jpcoenen authored Feb 8, 2021
2 parents 50206dd + 4291882 commit 5cd68e5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
41 changes: 21 additions & 20 deletions pkg/secrethub/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@ func (i AppInfo) ValidateName() error {
// If no key credential could be found, a Client is returned that can only be used for unauthenticated routes.
func NewClient(with ...ClientOption) (*Client, error) {
client := &Client{
httpClient: http.NewClient(),
repoIndexKeys: make(map[api.RepoPath]*crypto.SymmetricKey),
appInfo: []*AppInfo{},
defaultPassphraseReader: credentials.FromEnv("SECRETHUB_CREDENTIAL_PASSPHRASE"),
httpClient: http.NewClient(),
repoIndexKeys: make(map[api.RepoPath]*crypto.SymmetricKey),
appInfo: []*AppInfo{},
}

err := client.with(with...)
if err != nil {
return nil, err
Expand Down Expand Up @@ -157,26 +157,12 @@ func NewClient(with ...ClientOption) (*Client, error) {
}

err := client.with(WithCredentials(provider))
// nolint: staticcheck
if err != nil {
// TODO: log that default credential was not loaded.
// Do go on because we want to allow an unauthenticated client.
}
}

appName := os.Getenv("SECRETHUB_APP_INFO_NAME")
if appName != "" {
appVersion := os.Getenv("SECRETHUB_APP_INFO_VERSION")
topLevelAppInfo := &AppInfo{
Name: appName,
Version: appVersion,
}
// Ignore app info from environment variable if name is invalid
if err = topLevelAppInfo.ValidateName(); err == nil {
client.appInfo = append(client.appInfo, topLevelAppInfo)
return nil, err
}
}

client.loadAppInfoFromEnv()
userAgent := client.userAgent()

client.httpClient.Options(http.WithUserAgent(userAgent))
Expand Down Expand Up @@ -288,6 +274,21 @@ func (c *Client) isKeyed() bool {
return c.decrypter != nil
}

func (c *Client) loadAppInfoFromEnv() {
appName := os.Getenv("SECRETHUB_APP_INFO_NAME")
if appName != "" {
appVersion := os.Getenv("SECRETHUB_APP_INFO_VERSION")
topLevelAppInfo := &AppInfo{
Name: appName,
Version: appVersion,
}
// Ignore app info from environment variable if name is invalid
if err := topLevelAppInfo.ValidateName(); err == nil {
c.appInfo = append(c.appInfo, topLevelAppInfo)
}
}
}

func (c *Client) userAgent() string {
userAgent := userAgentPrefix
for _, info := range c.appInfo {
Expand Down
8 changes: 4 additions & 4 deletions pkg/secrethub/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ func TestClient_userAgent(t *testing.T) {
for _, info := range tc.appInfo {
opts = append(opts, WithAppInfo(info))
}
client, err := NewClient(opts...)
client := &Client{}
err := client.with(opts...)
assert.Equal(t, err, tc.err)
if err != nil {
return
}

client.loadAppInfoFromEnv()

userAgent := client.userAgent()
pattern := tc.expected + " \\(.*\\)"
Expand Down
3 changes: 3 additions & 0 deletions pkg/secrethub/credentials/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ func ImportKey(credentialReader, passphraseReader Reader) (Key, error) {
if envPassphrase != "" {
credential, err := decryptKey([]byte(envPassphrase), encoded)
if err != nil {
if crypto.IsWrongKey(err) {
err = ErrCannotDecryptCredential
}
return Key{}, fmt.Errorf("decrypting credential with passphrase read from $%s: %v", credentialPassphraseEnvVar, err)
}
return Key{key: credential}, nil
Expand Down

0 comments on commit 5cd68e5

Please sign in to comment.