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

Commit 5cd68e5

Browse files
authored
Merge pull request #234 from secrethub/fix/credential-errors
2 parents 50206dd + 4291882 commit 5cd68e5

3 files changed

Lines changed: 28 additions & 24 deletions

File tree

pkg/secrethub/client.go

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,11 @@ func (i AppInfo) ValidateName() error {
121121
// If no key credential could be found, a Client is returned that can only be used for unauthenticated routes.
122122
func NewClient(with ...ClientOption) (*Client, error) {
123123
client := &Client{
124-
httpClient: http.NewClient(),
125-
repoIndexKeys: make(map[api.RepoPath]*crypto.SymmetricKey),
126-
appInfo: []*AppInfo{},
127-
defaultPassphraseReader: credentials.FromEnv("SECRETHUB_CREDENTIAL_PASSPHRASE"),
124+
httpClient: http.NewClient(),
125+
repoIndexKeys: make(map[api.RepoPath]*crypto.SymmetricKey),
126+
appInfo: []*AppInfo{},
128127
}
128+
129129
err := client.with(with...)
130130
if err != nil {
131131
return nil, err
@@ -157,26 +157,12 @@ func NewClient(with ...ClientOption) (*Client, error) {
157157
}
158158

159159
err := client.with(WithCredentials(provider))
160-
// nolint: staticcheck
161160
if err != nil {
162-
// TODO: log that default credential was not loaded.
163-
// Do go on because we want to allow an unauthenticated client.
164-
}
165-
}
166-
167-
appName := os.Getenv("SECRETHUB_APP_INFO_NAME")
168-
if appName != "" {
169-
appVersion := os.Getenv("SECRETHUB_APP_INFO_VERSION")
170-
topLevelAppInfo := &AppInfo{
171-
Name: appName,
172-
Version: appVersion,
173-
}
174-
// Ignore app info from environment variable if name is invalid
175-
if err = topLevelAppInfo.ValidateName(); err == nil {
176-
client.appInfo = append(client.appInfo, topLevelAppInfo)
161+
return nil, err
177162
}
178163
}
179164

165+
client.loadAppInfoFromEnv()
180166
userAgent := client.userAgent()
181167

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

277+
func (c *Client) loadAppInfoFromEnv() {
278+
appName := os.Getenv("SECRETHUB_APP_INFO_NAME")
279+
if appName != "" {
280+
appVersion := os.Getenv("SECRETHUB_APP_INFO_VERSION")
281+
topLevelAppInfo := &AppInfo{
282+
Name: appName,
283+
Version: appVersion,
284+
}
285+
// Ignore app info from environment variable if name is invalid
286+
if err := topLevelAppInfo.ValidateName(); err == nil {
287+
c.appInfo = append(c.appInfo, topLevelAppInfo)
288+
}
289+
}
290+
}
291+
291292
func (c *Client) userAgent() string {
292293
userAgent := userAgentPrefix
293294
for _, info := range c.appInfo {

pkg/secrethub/client_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ func TestClient_userAgent(t *testing.T) {
6262
for _, info := range tc.appInfo {
6363
opts = append(opts, WithAppInfo(info))
6464
}
65-
client, err := NewClient(opts...)
65+
client := &Client{}
66+
err := client.with(opts...)
6667
assert.Equal(t, err, tc.err)
67-
if err != nil {
68-
return
69-
}
68+
69+
client.loadAppInfoFromEnv()
7070

7171
userAgent := client.userAgent()
7272
pattern := tc.expected + " \\(.*\\)"

pkg/secrethub/credentials/key.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ func ImportKey(credentialReader, passphraseReader Reader) (Key, error) {
8585
if envPassphrase != "" {
8686
credential, err := decryptKey([]byte(envPassphrase), encoded)
8787
if err != nil {
88+
if crypto.IsWrongKey(err) {
89+
err = ErrCannotDecryptCredential
90+
}
8891
return Key{}, fmt.Errorf("decrypting credential with passphrase read from $%s: %v", credentialPassphraseEnvVar, err)
8992
}
9093
return Key{key: credential}, nil

0 commit comments

Comments
 (0)