Skip to content

Commit 23f26bd

Browse files
committed
VenConnClient: fix failing tests and use a single envtest process
1 parent fc4d3ae commit 23f26bd

File tree

4 files changed

+153
-52
lines changed

4 files changed

+153
-52
lines changed

pkg/agent/config_test.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func Test_ValidateAndCombineConfig(t *testing.T) {
155155
)
156156
assert.EqualError(t, err, testutil.Undent(`
157157
no auth mode specified. You can use one of four auth modes:
158-
- Use --venafi-cloud with --credentials-file or --client-id with --private-key-path to use the Venafi Cloud Key Pair Service Account mode.
158+
- Use (--venafi-cloud with --credentials-file) or (--client-id with --private-key-path) to use the Venafi Cloud Key Pair Service Account mode.
159159
- Use --venafi-connection for the Venafi Cloud VenafiConnection mode.
160160
- Use --credentials-file alone if you want to use the Jetstack Secure OAuth mode.
161161
- Use --api-token if you want to use the Jetstack Secure API Token mode.
@@ -196,6 +196,7 @@ func Test_ValidateAndCombineConfig(t *testing.T) {
196196
Server: "http://example.com",
197197
OrganizationID: "example",
198198
EndpointPath: "api/v1/data",
199+
BackoffMaxTime: 10 * time.Minute,
199200
}
200201
require.NoError(t, err)
201202
assert.Equal(t, expect, got)
@@ -221,7 +222,7 @@ func Test_ValidateAndCombineConfig(t *testing.T) {
221222
uploader_id: test-agent
222223
upload_path: "/testing/path"
223224
`)),
224-
withCmdLineFlags("--venafi-cloud", "--credentials-file", credsPath, "--backoff-max-time", "5m"),
225+
withCmdLineFlags("--venafi-cloud", "--credentials-file", credsPath, "--backoff-max-time", "99m"),
225226
)
226227
expect := CombinedConfig{
227228
Server: "http://localhost:8080",
@@ -234,7 +235,7 @@ func Test_ValidateAndCombineConfig(t *testing.T) {
234235
UploadPath: "/testing/path",
235236
AuthMode: VenafiCloudKeypair,
236237
ClusterID: "the cluster name",
237-
BackoffMaxTime: 5 * time.Minute,
238+
BackoffMaxTime: 99 * time.Minute,
238239
}
239240
require.NoError(t, err)
240241
assert.Equal(t, expect, got)
@@ -337,7 +338,7 @@ func Test_ValidateAndCombineConfig(t *testing.T) {
337338
`)),
338339
withCmdLineFlags("--credentials-file", path))
339340
require.NoError(t, err)
340-
assert.Equal(t, CombinedConfig{Server: "https://api.venafi.eu", Period: time.Hour, OrganizationID: "foo", ClusterID: "bar", AuthMode: JetstackSecureOAuth}, got)
341+
assert.Equal(t, CombinedConfig{Server: "https://api.venafi.eu", Period: time.Hour, OrganizationID: "foo", ClusterID: "bar", AuthMode: JetstackSecureOAuth, BackoffMaxTime: 10 * time.Minute}, got)
341342
assert.IsType(t, &client.OAuthClient{}, cl)
342343
})
343344

@@ -411,7 +412,7 @@ func Test_ValidateAndCombineConfig(t *testing.T) {
411412
`)),
412413
withCmdLineFlags("--client-id", "5bc7d07c-45da-11ef-a878-523f1e1d7de1", "--private-key-path", path))
413414
require.NoError(t, err)
414-
assert.Equal(t, CombinedConfig{Server: "https://api.venafi.eu", Period: time.Hour, AuthMode: VenafiCloudKeypair, ClusterID: "the cluster name", UploadPath: "/foo/bar"}, got)
415+
assert.Equal(t, CombinedConfig{Server: "https://api.venafi.eu", Period: time.Hour, AuthMode: VenafiCloudKeypair, ClusterID: "the cluster name", UploadPath: "/foo/bar", BackoffMaxTime: 10 * time.Minute}, got)
415416
assert.IsType(t, &client.VenafiCloudClient{}, cl)
416417
})
417418

@@ -432,7 +433,7 @@ func Test_ValidateAndCombineConfig(t *testing.T) {
432433
`)),
433434
withCmdLineFlags("--venafi-cloud", "--credentials-file", credsPath))
434435
require.NoError(t, err)
435-
assert.Equal(t, CombinedConfig{Server: "https://api.venafi.eu", Period: time.Hour, AuthMode: VenafiCloudKeypair, ClusterID: "the cluster name", UploadPath: "/foo/bar"}, got)
436+
assert.Equal(t, CombinedConfig{Server: "https://api.venafi.eu", Period: time.Hour, AuthMode: VenafiCloudKeypair, ClusterID: "the cluster name", UploadPath: "/foo/bar", BackoffMaxTime: 10 * time.Minute}, got)
436437
})
437438

438439
t.Run("venafi-cloud-keypair-auth: venafi-cloud.upload_path field is required", func(t *testing.T) {
@@ -503,12 +504,13 @@ func Test_ValidateAndCombineConfig(t *testing.T) {
503504
withCmdLineFlags("--install-namespace", "venafi", "--venafi-connection", "venafi-components"))
504505
require.NoError(t, err)
505506
assert.Equal(t, CombinedConfig{
506-
Period: 1 * time.Hour,
507-
ClusterID: "the cluster name",
508-
AuthMode: VenafiCloudVenafiConnection,
509-
VenConnName: "venafi-components",
510-
VenConnNS: "venafi",
511-
InstallNS: "venafi",
507+
Period: 1 * time.Hour,
508+
ClusterID: "the cluster name",
509+
AuthMode: VenafiCloudVenafiConnection,
510+
VenConnName: "venafi-components",
511+
VenConnNS: "venafi",
512+
InstallNS: "venafi",
513+
BackoffMaxTime: 10 * time.Minute,
512514
}, got)
513515
assert.IsType(t, &client.VenConnClient{}, cl)
514516
})

pkg/client/client_venafi_cloud.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ const (
8484
// to authenticate to the backend API.
8585
func NewVenafiCloudClient(agentMetadata *api.AgentMetadata, credentials *VenafiSvcAccountCredentials, baseURL string, uploaderID string, uploadPath string) (*VenafiCloudClient, error) {
8686
if err := credentials.Validate(); err != nil {
87-
return nil, fmt.Errorf("cannot create VenafiCloudClient: %v", err)
87+
return nil, fmt.Errorf("cannot create VenafiCloudClient: %w", err)
8888
}
8989
privateKey, jwtSigningAlg, err := parsePrivateKeyAndExtractSigningMethod(credentials.PrivateKeyFile)
9090
if err != nil {
91-
return nil, fmt.Errorf("error parsing private key file %v", err)
91+
return nil, fmt.Errorf("while parsing private key file: %w", err)
9292
}
9393
if baseURL == "" {
9494
return nil, fmt.Errorf("cannot create VenafiCloudClient: baseURL cannot be empty")
@@ -380,7 +380,7 @@ func parsePrivateKeyFromPemFile(privateKeyFilePath string) (crypto.PrivateKey, e
380380

381381
der, _ := pem.Decode(pkBytes)
382382
if der == nil {
383-
return nil, fmt.Errorf("error decoding private key from pem file %q", privateKeyFilePath)
383+
return nil, fmt.Errorf("while decoding the PEM-encoded private key %v, its content were: %s", privateKeyFilePath, string(pkBytes))
384384
}
385385

386386
if key, err := x509.ParsePKCS1PrivateKey(der.Bytes); err == nil {
@@ -391,13 +391,13 @@ func parsePrivateKeyFromPemFile(privateKeyFilePath string) (crypto.PrivateKey, e
391391
case *rsa.PrivateKey, *ecdsa.PrivateKey, ed25519.PrivateKey:
392392
return key, nil
393393
default:
394-
return nil, fmt.Errorf("found unknown private key type in PKCS#8 wrapping")
394+
return nil, fmt.Errorf("found unknown private key type in PKCS#8 wrapping: %T", key)
395395
}
396396
}
397397
if key, err := x509.ParseECPrivateKey(der.Bytes); err == nil {
398398
return key, nil
399399
}
400-
return nil, fmt.Errorf("failed to parse private key")
400+
return nil, fmt.Errorf("while parsing EC private: %w", err)
401401
}
402402

403403
func parsePrivateKeyAndExtractSigningMethod(privateKeyFile string) (crypto.PrivateKey, jwt.SigningMethod, error) {

0 commit comments

Comments
 (0)