From 12f823abef1491bfd35a7c8381363dfc7f00bbfd Mon Sep 17 00:00:00 2001 From: Vivek Singh Chauhan Date: Tue, 8 Aug 2023 12:55:19 -0700 Subject: [PATCH] APIGOV-24312 - Fix tests --- pkg/apic/auth/apicauth_test.go | 68 ---------------------------- pkg/authz/oauth/authclient_test.go | 4 +- pkg/authz/oauth/keyreader_test.go | 71 ++++++++++++++++++++++++++++++ pkg/authz/oauth/provider.go | 3 ++ 4 files changed, 76 insertions(+), 70 deletions(-) create mode 100644 pkg/authz/oauth/keyreader_test.go diff --git a/pkg/apic/auth/apicauth_test.go b/pkg/apic/auth/apicauth_test.go index d1563fd31..cb785f045 100644 --- a/pkg/apic/auth/apicauth_test.go +++ b/pkg/apic/auth/apicauth_test.go @@ -45,74 +45,6 @@ func TestChannelTokenGetterCloses(t *testing.T) { } } -func TestGetKey(t *testing.T) { - cases := []struct { - description string - kr *keyReader - }{ - { - "no password", - &keyReader{ - privKey: "testdata/private_key.pem", - }, - }, - { - "with empty password file", - &keyReader{ - privKey: "testdata/private_key.pem", - password: "testdata/password_empty", - }, - }, - { - "with password", - &keyReader{ - privKey: "testdata/private_key_with_pwd.pem", - password: "testdata/password", - }, - }, - } - - for _, testCase := range cases { - if _, err := testCase.kr.getPrivateKey(); err != nil { - t.Errorf("testcase: %s: failed to read rsa key %s", testCase.description, err) - } - } -} - -func TestGetPublicKey(t *testing.T) { - cases := []struct { - description string - kr *keyReader - }{ - { - "with public key", - &keyReader{ - publicKey: "testdata/public_key", - }, - }, - { - "with private and public key", - &keyReader{ - privKey: "testdata/private_key.pem", - publicKey: "testdata/public_key", - }, - }, - { - "with private, public key, and password", - &keyReader{ - privKey: "testdata/private_key_with_pwd.pem", - password: "testdata/password", - publicKey: "testdata/public_key", - }, - }, - } - for _, testCase := range cases { - if _, err := testCase.kr.getPublicKey(); err != nil { - t.Errorf("testcase: %s: failed to read public key %s", testCase.description, err) - } - } -} - func TestNetAuthenticate(t *testing.T) { aa := NewWithStatic("12345", "abcde") if aa.tenantID != "12345" { diff --git a/pkg/authz/oauth/authclient_test.go b/pkg/authz/oauth/authclient_test.go index 3ff1d8df5..fd414a2c4 100644 --- a/pkg/authz/oauth/authclient_test.go +++ b/pkg/authz/oauth/authclient_test.go @@ -66,7 +66,7 @@ func TestGetPlatformTokensHttpError(t *testing.T) { s.SetTokenResponse("", 0, http.StatusBadRequest) ac, err := NewAuthClient(s.GetTokenURL(), apiClient, WithServerName("testServer"), - WithClientSecretAuth("invalid_client", "invalid-secrt", "")) + WithClientSecretPostAuth("invalid_client", "invalid-secrt", "")) assert.Nil(t, err) assert.NotNil(t, ac) @@ -106,7 +106,7 @@ func TestGetPlatformTokensTimeout(t *testing.T) { apiClient := api.NewClientWithTimeout(config.NewTLSConfig(), "", time.Second) ac, err := NewAuthClient(s.URL, apiClient, WithServerName("testServer"), - WithClientSecretAuth("invalid_client", "invalid-secrt", "")) + WithClientSecretPostAuth("invalid_client", "invalid-secrt", "")) assert.Nil(t, err) assert.NotNil(t, ac) diff --git a/pkg/authz/oauth/keyreader_test.go b/pkg/authz/oauth/keyreader_test.go new file mode 100644 index 000000000..14d4145b3 --- /dev/null +++ b/pkg/authz/oauth/keyreader_test.go @@ -0,0 +1,71 @@ +package oauth + +import "testing" + +func TestGetKey(t *testing.T) { + cases := []struct { + description string + kr *keyReader + }{ + { + "no password", + &keyReader{ + privKey: "../../apic/auth/testdata/private_key.pem", + }, + }, + { + "with empty password file", + &keyReader{ + privKey: "../../apic/auth/testdata/private_key.pem", + password: "../../apic/auth/testdata/password_empty", + }, + }, + { + "with password", + &keyReader{ + privKey: "../../apic/auth/testdata/private_key_with_pwd.pem", + password: "../../apic/auth/testdata/password", + }, + }, + } + + for _, testCase := range cases { + if _, err := testCase.kr.GetPrivateKey(); err != nil { + t.Errorf("testcase: %s: failed to read rsa key %s", testCase.description, err) + } + } +} + +func TestGetPublicKey(t *testing.T) { + cases := []struct { + description string + kr *keyReader + }{ + { + "with public key", + &keyReader{ + publicKey: "../../apic/auth/testdata/public_key", + }, + }, + { + "with private and public key", + &keyReader{ + privKey: "../../apic/auth/testdata/private_key.pem", + publicKey: "../../apic/auth/testdata/public_key", + }, + }, + { + "with private, public key, and password", + &keyReader{ + privKey: "../../apic/auth/testdata/private_key_with_pwd.pem", + password: "../../apic/auth/testdata/password", + publicKey: "../../apic/auth/testdata/public_key", + }, + }, + } + for _, testCase := range cases { + if _, err := testCase.kr.GetPublicKey(); err != nil { + t.Errorf("testcase: %s: failed to read public key %s", testCase.description, err) + } + } +} diff --git a/pkg/authz/oauth/provider.go b/pkg/authz/oauth/provider.go index cf5401f49..234ba1df9 100644 --- a/pkg/authz/oauth/provider.go +++ b/pkg/authz/oauth/provider.go @@ -187,6 +187,9 @@ func (p *provider) GetIssuer() string { } func (p *provider) useTLSAuth() bool { + if p.cfg.GetAuthConfig() == nil { + return false + } return p.cfg.GetAuthConfig().GetType() == IDPAuthTypeTLSClientAuth || p.cfg.GetAuthConfig().GetType() == IDPAuthTypeSelfSignedTLSClientAuth }