| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- /*
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- package api
- import (
- "errors"
- "reflect"
- "testing"
- "github.com/stretchr/testify/assert"
- esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
- )
- const (
- fakeClientID = "client-id"
- fakeClientSecret = "client-secret"
- fakeToken = "token"
- fakeProjectSlug = "first-project"
- fakeEnvironmentSlug = "dev"
- )
- func TestAPIClientDo(t *testing.T) {
- apiURL := "foo"
- httpMethod := "bar"
- testCases := []struct {
- Name string
- MockStatusCode int
- MockResponse any
- ExpectedResponse any
- ExpectedError error
- }{
- {
- Name: "Success",
- MockStatusCode: 200,
- MockResponse: MachineIdentityDetailsResponse{
- AccessToken: "foobar",
- },
- ExpectedResponse: MachineIdentityDetailsResponse{
- AccessToken: "foobar",
- },
- ExpectedError: nil,
- },
- {
- Name: "Error when response cannot be unmarshalled",
- MockStatusCode: 500,
- MockResponse: []byte("not-json"),
- ExpectedError: errors.New("API error (500), could not unmarshal error response: json: cannot unmarshal string into Go value of type api.InfisicalAPIErrorResponse"),
- },
- {
- Name: "Error when non-Infisical error response received",
- MockStatusCode: 500,
- MockResponse: map[string]string{"foo": "bar"},
- ExpectedError: errors.New("API error (500): {\"foo\":\"bar\"}"),
- },
- {
- Name: "Do: Error when non-200 response received",
- MockStatusCode: 401,
- MockResponse: InfisicalAPIErrorResponse{
- StatusCode: 401,
- Error: "Unauthorized",
- },
- ExpectedError: &InfisicalAPIError{StatusCode: 401, Err: "Unauthorized", Message: ""},
- },
- {
- Name: "Error when arbitrary details are returned",
- MockStatusCode: 401,
- MockResponse: InfisicalAPIErrorResponse{
- StatusCode: 401,
- Error: "Unauthorized",
- Details: map[string]string{"foo": "details"},
- },
- ExpectedError: &InfisicalAPIError{StatusCode: 401, Err: "Unauthorized", Message: "", Details: map[string]string{"foo": "details"}},
- },
- }
- for _, tc := range testCases {
- t.Run(tc.Name, func(t *testing.T) {
- apiClient, closeFunc := NewMockClient(tc.MockStatusCode, tc.MockResponse)
- defer closeFunc()
- // Automatically pluck out the expected response type using reflection to create a new empty value for unmarshalling.
- var actualResponse any
- if tc.ExpectedResponse != nil {
- actualResponse = reflect.New(reflect.TypeOf(tc.ExpectedResponse)).Interface()
- }
- err := apiClient.do(apiURL, httpMethod, nil, nil, actualResponse)
- if tc.ExpectedError != nil {
- assert.Error(t, err)
- assert.Equal(t, tc.ExpectedError.Error(), err.Error())
- } else {
- assert.NoError(t, err)
- assert.Equal(t, tc.ExpectedResponse, reflect.ValueOf(actualResponse).Elem().Interface())
- }
- })
- }
- }
- // TestAPIClientDoInvalidResponse tests the case where the response is a 200 but does not unmarshal
- // correctly.
- func TestAPIClientDoInvalidResponse(t *testing.T) {
- apiClient, closeFunc := NewMockClient(200, []byte("not-json"))
- defer closeFunc()
- err := apiClient.do("foo", "bar", nil, nil, nil)
- assert.ErrorIs(t, err, errJSONUnmarshal)
- }
- func TestSetTokenViaMachineIdentity(t *testing.T) {
- t.Run("Success", func(t *testing.T) {
- apiClient, closeFunc := NewMockClient(200, MachineIdentityDetailsResponse{
- AccessToken: "foobar",
- })
- defer closeFunc()
- err := apiClient.SetTokenViaMachineIdentity(fakeClientID, fakeClientSecret)
- assert.NoError(t, err)
- assert.Equal(t, apiClient.token, "foobar")
- })
- t.Run("SetTokenViaMachineIdentity: Error when non-200 response received", func(t *testing.T) {
- apiClient, closeFunc := NewMockClient(401, InfisicalAPIErrorResponse{
- StatusCode: 401,
- Error: "Unauthorized",
- })
- defer closeFunc()
- err := apiClient.SetTokenViaMachineIdentity(fakeClientID, fakeClientSecret)
- assert.Error(t, err)
- var apiErr *InfisicalAPIError
- assert.True(t, errors.As(err, &apiErr))
- assert.Equal(t, 401, apiErr.StatusCode)
- assert.Equal(t, "Unauthorized", apiErr.Err)
- })
- t.Run("Error when token already set", func(t *testing.T) {
- apiClient, closeFunc := NewMockClient(401, nil)
- defer closeFunc()
- apiClient.token = fakeToken
- err := apiClient.SetTokenViaMachineIdentity(fakeClientID, fakeClientSecret)
- assert.ErrorIs(t, err, errAccessTokenAlreadyRetrieved)
- })
- }
- func TestRevokeAccessToken(t *testing.T) {
- t.Run("Success", func(t *testing.T) {
- apiClient, closeFunc := NewMockClient(200, RevokeMachineIdentityAccessTokenResponse{
- Message: "Success",
- })
- defer closeFunc()
- apiClient.token = fakeToken
- err := apiClient.RevokeAccessToken()
- assert.NoError(t, err)
- // Verify that the access token was unset.
- assert.Equal(t, apiClient.token, "")
- })
- t.Run("RevokeAccessToken: Error when non-200 response received", func(t *testing.T) {
- apiClient, closeFunc := NewMockClient(401, InfisicalAPIErrorResponse{
- StatusCode: 401,
- Error: "Unauthorized",
- })
- defer closeFunc()
- apiClient.token = fakeToken
- err := apiClient.RevokeAccessToken()
- assert.Error(t, err)
- var apiErr *InfisicalAPIError
- assert.True(t, errors.As(err, &apiErr))
- assert.Equal(t, 401, apiErr.StatusCode)
- assert.Equal(t, "Unauthorized", apiErr.Err)
- })
- t.Run("Error when no access token is set", func(t *testing.T) {
- apiClient, closeFunc := NewMockClient(401, nil)
- defer closeFunc()
- err := apiClient.RevokeAccessToken()
- assert.ErrorIs(t, err, errNoAccessToken)
- })
- }
- func TestGetSecretsV3(t *testing.T) {
- t.Run("Works with secrets", func(t *testing.T) {
- apiClient, closeFunc := NewMockClient(200, GetSecretsV3Response{
- Secrets: []SecretsV3{
- {SecretKey: "foo", SecretValue: "bar"},
- },
- })
- defer closeFunc()
- secrets, err := apiClient.GetSecretsV3(GetSecretsV3Request{
- ProjectSlug: fakeProjectSlug,
- EnvironmentSlug: fakeEnvironmentSlug,
- SecretPath: "/",
- Recursive: true,
- })
- assert.NoError(t, err)
- assert.Equal(t, secrets, map[string]string{"foo": "bar"})
- })
- t.Run("Works with imported secrets", func(t *testing.T) {
- apiClient, closeFunc := NewMockClient(200, GetSecretsV3Response{
- ImportedSecrets: []ImportedSecretV3{{
- Secrets: []SecretsV3{{SecretKey: "foo", SecretValue: "bar"}},
- }},
- })
- defer closeFunc()
- secrets, err := apiClient.GetSecretsV3(GetSecretsV3Request{
- ProjectSlug: fakeProjectSlug,
- EnvironmentSlug: fakeEnvironmentSlug,
- SecretPath: "/",
- Recursive: true,
- })
- assert.NoError(t, err)
- assert.Equal(t, secrets, map[string]string{"foo": "bar"})
- })
- t.Run("GetSecretsV3: Error when non-200 response received", func(t *testing.T) {
- apiClient, closeFunc := NewMockClient(401, InfisicalAPIErrorResponse{
- StatusCode: 401,
- Error: "Unauthorized",
- })
- defer closeFunc()
- _, err := apiClient.GetSecretsV3(GetSecretsV3Request{
- ProjectSlug: fakeProjectSlug,
- EnvironmentSlug: fakeEnvironmentSlug,
- SecretPath: "/",
- Recursive: true,
- })
- assert.Error(t, err)
- var apiErr *InfisicalAPIError
- assert.True(t, errors.As(err, &apiErr))
- assert.Equal(t, 401, apiErr.StatusCode)
- assert.Equal(t, "Unauthorized", apiErr.Err)
- })
- }
- func TestGetSecretByKeyV3(t *testing.T) {
- t.Run("Works", func(t *testing.T) {
- apiClient, closeFunc := NewMockClient(200, GetSecretByKeyV3Response{
- Secret: SecretsV3{
- SecretKey: "foo",
- SecretValue: "bar",
- },
- })
- defer closeFunc()
- secret, err := apiClient.GetSecretByKeyV3(GetSecretByKeyV3Request{
- ProjectSlug: fakeProjectSlug,
- EnvironmentSlug: fakeEnvironmentSlug,
- SecretPath: "/",
- SecretKey: "foo",
- })
- assert.NoError(t, err)
- assert.Equal(t, "bar", secret)
- })
- t.Run("Error when secret is not found", func(t *testing.T) {
- apiClient, closeFunc := NewMockClient(404, InfisicalAPIErrorResponse{
- StatusCode: 404,
- Error: "Not Found",
- })
- defer closeFunc()
- _, err := apiClient.GetSecretByKeyV3(GetSecretByKeyV3Request{
- ProjectSlug: fakeProjectSlug,
- EnvironmentSlug: fakeEnvironmentSlug,
- SecretPath: "/",
- SecretKey: "foo",
- })
- assert.Error(t, err)
- // Importantly, we return the standard error for no secrets found.
- assert.ErrorIs(t, err, esv1.NoSecretError{})
- })
- // Test case where the request is unauthorized
- t.Run("ErrorHandlingUnauthorized", func(t *testing.T) {
- apiClient, closeFunc := NewMockClient(401, InfisicalAPIErrorResponse{
- StatusCode: 401,
- Error: "Unauthorized",
- })
- defer closeFunc()
- _, err := apiClient.GetSecretByKeyV3(GetSecretByKeyV3Request{
- ProjectSlug: fakeProjectSlug,
- EnvironmentSlug: fakeEnvironmentSlug,
- SecretPath: "/",
- SecretKey: "foo",
- })
- assert.Error(t, err)
- var apiErr *InfisicalAPIError
- assert.True(t, errors.As(err, &apiErr))
- assert.Equal(t, 401, apiErr.StatusCode)
- assert.Equal(t, "Unauthorized", apiErr.Err)
- })
- }
|