|
@@ -16,145 +16,86 @@ package api
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
"errors"
|
|
"errors"
|
|
|
- "reflect"
|
|
|
|
|
|
|
+ "fmt"
|
|
|
|
|
+ "regexp"
|
|
|
|
|
+ "strconv"
|
|
|
"testing"
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
+ infisical "github.com/infisical/go-sdk"
|
|
|
"github.com/stretchr/testify/assert"
|
|
"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 parseInfisicalAPIError(err error, t *testing.T) (int, string, error) {
|
|
|
|
|
+ var apiErr *infisical.APIError
|
|
|
|
|
+ assert.True(t, errors.As(err, &apiErr))
|
|
|
|
|
+
|
|
|
|
|
+ // Regex to extract status-code
|
|
|
|
|
+ statusRegex := regexp.MustCompile(`\[status-code=(\d+)\]`)
|
|
|
|
|
+ statusMatch := statusRegex.FindStringSubmatch(apiErr.Error())
|
|
|
|
|
|
|
|
-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"}},
|
|
|
|
|
- },
|
|
|
|
|
|
|
+ // Regex to extract message (handles quoted content)
|
|
|
|
|
+ messageRegex := regexp.MustCompile(`\[message="([^"]*)"\]`)
|
|
|
|
|
+ messageMatch := messageRegex.FindStringSubmatch(apiErr.Error())
|
|
|
|
|
+
|
|
|
|
|
+ if len(statusMatch) < 2 {
|
|
|
|
|
+ return 0, "", fmt.Errorf("status-code not found in error string")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- 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())
|
|
|
|
|
- }
|
|
|
|
|
- })
|
|
|
|
|
|
|
+ if len(messageMatch) < 2 {
|
|
|
|
|
+ return 0, "", fmt.Errorf("message not found in error string")
|
|
|
}
|
|
}
|
|
|
-}
|
|
|
|
|
|
|
|
|
|
-// 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()
|
|
|
|
|
|
|
+ statusCode, err := strconv.Atoi(statusMatch[1])
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return 0, "", fmt.Errorf("invalid status code: %w", err)
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- err := apiClient.do("foo", "bar", nil, nil, nil)
|
|
|
|
|
- assert.ErrorIs(t, err, errJSONUnmarshal)
|
|
|
|
|
|
|
+ return statusCode, messageMatch[1], nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+const errNoAccessToken = "sdk client is not authenticated, cannot revoke access token"
|
|
|
|
|
+
|
|
|
|
|
+const (
|
|
|
|
|
+ fakeClientID = "client-id"
|
|
|
|
|
+ fakeClientSecret = "client-secret"
|
|
|
|
|
+ fakeToken = "token"
|
|
|
|
|
+ fakeProjectSlug = "first-project"
|
|
|
|
|
+ fakeEnvironmentSlug = "dev"
|
|
|
|
|
+)
|
|
|
|
|
+
|
|
|
func TestSetTokenViaMachineIdentity(t *testing.T) {
|
|
func TestSetTokenViaMachineIdentity(t *testing.T) {
|
|
|
t.Run("Success", func(t *testing.T) {
|
|
t.Run("Success", func(t *testing.T) {
|
|
|
apiClient, closeFunc := NewMockClient(200, MachineIdentityDetailsResponse{
|
|
apiClient, closeFunc := NewMockClient(200, MachineIdentityDetailsResponse{
|
|
|
- AccessToken: "foobar",
|
|
|
|
|
|
|
+ AccessToken: "foobar",
|
|
|
|
|
+ ExpiresIn: 2592000,
|
|
|
|
|
+ AccessTokenMaxTTL: 2592000,
|
|
|
|
|
+ TokenType: "Bearer",
|
|
|
})
|
|
})
|
|
|
defer closeFunc()
|
|
defer closeFunc()
|
|
|
|
|
|
|
|
- err := apiClient.SetTokenViaMachineIdentity(fakeClientID, fakeClientSecret)
|
|
|
|
|
|
|
+ _, err := apiClient.Auth().UniversalAuthLogin(fakeClientID, fakeClientSecret)
|
|
|
|
|
+
|
|
|
assert.NoError(t, err)
|
|
assert.NoError(t, err)
|
|
|
- assert.Equal(t, apiClient.token, "foobar")
|
|
|
|
|
|
|
+ assert.Equal(t, apiClient.Auth().GetAccessToken(), "foobar")
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
t.Run("SetTokenViaMachineIdentity: Error when non-200 response received", func(t *testing.T) {
|
|
t.Run("SetTokenViaMachineIdentity: Error when non-200 response received", func(t *testing.T) {
|
|
|
apiClient, closeFunc := NewMockClient(401, InfisicalAPIErrorResponse{
|
|
apiClient, closeFunc := NewMockClient(401, InfisicalAPIErrorResponse{
|
|
|
StatusCode: 401,
|
|
StatusCode: 401,
|
|
|
- Error: "Unauthorized",
|
|
|
|
|
|
|
+ Message: "Unauthorized",
|
|
|
})
|
|
})
|
|
|
defer closeFunc()
|
|
defer closeFunc()
|
|
|
|
|
|
|
|
- err := apiClient.SetTokenViaMachineIdentity(fakeClientID, fakeClientSecret)
|
|
|
|
|
|
|
+ _, err := apiClient.Auth().UniversalAuthLogin(fakeClientID, fakeClientSecret)
|
|
|
assert.Error(t, err)
|
|
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
|
|
|
|
|
|
|
+ apiErrorStatusCode, apiErrorMessage, err := parseInfisicalAPIError(err, t)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ t.Fatalf("Error parsing infisical API error: %v", err)
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- err := apiClient.SetTokenViaMachineIdentity(fakeClientID, fakeClientSecret)
|
|
|
|
|
- assert.ErrorIs(t, err, errAccessTokenAlreadyRetrieved)
|
|
|
|
|
|
|
+ assert.Equal(t, 401, apiErrorStatusCode)
|
|
|
|
|
+ assert.Equal(t, "Unauthorized", apiErrorMessage)
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -165,153 +106,208 @@ func TestRevokeAccessToken(t *testing.T) {
|
|
|
})
|
|
})
|
|
|
defer closeFunc()
|
|
defer closeFunc()
|
|
|
|
|
|
|
|
- apiClient.token = fakeToken
|
|
|
|
|
|
|
+ apiClient.Auth().SetAccessToken(fakeToken)
|
|
|
|
|
+
|
|
|
|
|
+ err := apiClient.Auth().RevokeAccessToken()
|
|
|
|
|
|
|
|
- err := apiClient.RevokeAccessToken()
|
|
|
|
|
assert.NoError(t, err)
|
|
assert.NoError(t, err)
|
|
|
// Verify that the access token was unset.
|
|
// Verify that the access token was unset.
|
|
|
- assert.Equal(t, apiClient.token, "")
|
|
|
|
|
|
|
+ assert.Equal(t, apiClient.Auth().GetAccessToken(), "")
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
t.Run("RevokeAccessToken: Error when non-200 response received", func(t *testing.T) {
|
|
t.Run("RevokeAccessToken: Error when non-200 response received", func(t *testing.T) {
|
|
|
apiClient, closeFunc := NewMockClient(401, InfisicalAPIErrorResponse{
|
|
apiClient, closeFunc := NewMockClient(401, InfisicalAPIErrorResponse{
|
|
|
StatusCode: 401,
|
|
StatusCode: 401,
|
|
|
- Error: "Unauthorized",
|
|
|
|
|
|
|
+ Message: "Unauthorized",
|
|
|
})
|
|
})
|
|
|
defer closeFunc()
|
|
defer closeFunc()
|
|
|
|
|
|
|
|
- apiClient.token = fakeToken
|
|
|
|
|
|
|
+ apiClient.Auth().SetAccessToken(fakeToken)
|
|
|
|
|
|
|
|
- err := apiClient.RevokeAccessToken()
|
|
|
|
|
|
|
+ err := apiClient.Auth().RevokeAccessToken()
|
|
|
assert.Error(t, err)
|
|
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)
|
|
|
|
|
|
|
+
|
|
|
|
|
+ apiErrorStatusCode, apiErrorMessage, err := parseInfisicalAPIError(err, t)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ t.Fatalf("Error parsing infisical API error: %v", err)
|
|
|
|
|
+ }
|
|
|
|
|
+ assert.Equal(t, 401, apiErrorStatusCode)
|
|
|
|
|
+ assert.Equal(t, "Unauthorized", apiErrorMessage)
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
t.Run("Error when no access token is set", func(t *testing.T) {
|
|
t.Run("Error when no access token is set", func(t *testing.T) {
|
|
|
apiClient, closeFunc := NewMockClient(401, nil)
|
|
apiClient, closeFunc := NewMockClient(401, nil)
|
|
|
defer closeFunc()
|
|
defer closeFunc()
|
|
|
|
|
|
|
|
- err := apiClient.RevokeAccessToken()
|
|
|
|
|
- assert.ErrorIs(t, err, errNoAccessToken)
|
|
|
|
|
|
|
+ err := apiClient.Auth().RevokeAccessToken()
|
|
|
|
|
+
|
|
|
|
|
+ assert.EqualError(t, err, errNoAccessToken)
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func TestGetSecretsV3(t *testing.T) {
|
|
func TestGetSecretsV3(t *testing.T) {
|
|
|
t.Run("Works with secrets", func(t *testing.T) {
|
|
t.Run("Works with secrets", func(t *testing.T) {
|
|
|
|
|
+ secrets := []SecretsV3{
|
|
|
|
|
+ {SecretKey: "foo", SecretValue: "bar"},
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
apiClient, closeFunc := NewMockClient(200, GetSecretsV3Response{
|
|
apiClient, closeFunc := NewMockClient(200, GetSecretsV3Response{
|
|
|
- Secrets: []SecretsV3{
|
|
|
|
|
- {SecretKey: "foo", SecretValue: "bar"},
|
|
|
|
|
- },
|
|
|
|
|
|
|
+ Secrets: secrets,
|
|
|
})
|
|
})
|
|
|
|
|
+
|
|
|
|
|
+ var sdkFormattedSecrets []infisical.Secret
|
|
|
|
|
+
|
|
|
|
|
+ for _, secret := range secrets {
|
|
|
|
|
+ sdkFormattedSecrets = append(sdkFormattedSecrets, infisical.Secret{
|
|
|
|
|
+ SecretKey: secret.SecretKey,
|
|
|
|
|
+ SecretValue: secret.SecretValue,
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
defer closeFunc()
|
|
defer closeFunc()
|
|
|
|
|
|
|
|
- secrets, err := apiClient.GetSecretsV3(GetSecretsV3Request{
|
|
|
|
|
- ProjectSlug: fakeProjectSlug,
|
|
|
|
|
- EnvironmentSlug: fakeEnvironmentSlug,
|
|
|
|
|
- SecretPath: "/",
|
|
|
|
|
- Recursive: true,
|
|
|
|
|
|
|
+ sdkSecrets, err := apiClient.Secrets().List(infisical.ListSecretsOptions{
|
|
|
|
|
+ ProjectSlug: fakeProjectSlug,
|
|
|
|
|
+ Environment: fakeEnvironmentSlug,
|
|
|
|
|
+ SecretPath: "/",
|
|
|
|
|
+ Recursive: true,
|
|
|
})
|
|
})
|
|
|
assert.NoError(t, err)
|
|
assert.NoError(t, err)
|
|
|
- assert.Equal(t, secrets, map[string]string{"foo": "bar"})
|
|
|
|
|
|
|
+ assert.Equal(t, sdkSecrets, sdkFormattedSecrets)
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
t.Run("Works with imported secrets", func(t *testing.T) {
|
|
t.Run("Works with imported secrets", func(t *testing.T) {
|
|
|
|
|
+ secrets := []SecretsV3{
|
|
|
|
|
+ {SecretKey: "foo", SecretValue: "bar"},
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
apiClient, closeFunc := NewMockClient(200, GetSecretsV3Response{
|
|
apiClient, closeFunc := NewMockClient(200, GetSecretsV3Response{
|
|
|
ImportedSecrets: []ImportedSecretV3{{
|
|
ImportedSecrets: []ImportedSecretV3{{
|
|
|
- Secrets: []SecretsV3{{SecretKey: "foo", SecretValue: "bar"}},
|
|
|
|
|
|
|
+ Secrets: secrets,
|
|
|
}},
|
|
}},
|
|
|
})
|
|
})
|
|
|
defer closeFunc()
|
|
defer closeFunc()
|
|
|
|
|
|
|
|
- secrets, err := apiClient.GetSecretsV3(GetSecretsV3Request{
|
|
|
|
|
- ProjectSlug: fakeProjectSlug,
|
|
|
|
|
- EnvironmentSlug: fakeEnvironmentSlug,
|
|
|
|
|
- SecretPath: "/",
|
|
|
|
|
- Recursive: true,
|
|
|
|
|
|
|
+ var sdkFormattedSecrets []infisical.Secret
|
|
|
|
|
+
|
|
|
|
|
+ for _, secret := range secrets {
|
|
|
|
|
+ sdkFormattedSecrets = append(sdkFormattedSecrets, infisical.Secret{
|
|
|
|
|
+ SecretKey: secret.SecretKey,
|
|
|
|
|
+ SecretValue: secret.SecretValue,
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ sdkSecrets, err := apiClient.Secrets().List(infisical.ListSecretsOptions{
|
|
|
|
|
+ ProjectSlug: fakeProjectSlug,
|
|
|
|
|
+ Environment: fakeEnvironmentSlug,
|
|
|
|
|
+ IncludeImports: true,
|
|
|
|
|
+ SecretPath: "/",
|
|
|
|
|
+ Recursive: true,
|
|
|
})
|
|
})
|
|
|
assert.NoError(t, err)
|
|
assert.NoError(t, err)
|
|
|
- assert.Equal(t, secrets, map[string]string{"foo": "bar"})
|
|
|
|
|
|
|
+ assert.Equal(t, sdkSecrets, sdkFormattedSecrets)
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
t.Run("GetSecretsV3: Error when non-200 response received", func(t *testing.T) {
|
|
t.Run("GetSecretsV3: Error when non-200 response received", func(t *testing.T) {
|
|
|
apiClient, closeFunc := NewMockClient(401, InfisicalAPIErrorResponse{
|
|
apiClient, closeFunc := NewMockClient(401, InfisicalAPIErrorResponse{
|
|
|
StatusCode: 401,
|
|
StatusCode: 401,
|
|
|
- Error: "Unauthorized",
|
|
|
|
|
|
|
+ Message: "Unauthorized",
|
|
|
})
|
|
})
|
|
|
defer closeFunc()
|
|
defer closeFunc()
|
|
|
|
|
|
|
|
- _, err := apiClient.GetSecretsV3(GetSecretsV3Request{
|
|
|
|
|
- ProjectSlug: fakeProjectSlug,
|
|
|
|
|
- EnvironmentSlug: fakeEnvironmentSlug,
|
|
|
|
|
- SecretPath: "/",
|
|
|
|
|
- Recursive: true,
|
|
|
|
|
|
|
+ _, err := apiClient.Secrets().List(infisical.ListSecretsOptions{
|
|
|
|
|
+ ProjectSlug: fakeProjectSlug,
|
|
|
|
|
+ Environment: fakeEnvironmentSlug,
|
|
|
|
|
+ SecretPath: "/",
|
|
|
|
|
+ Recursive: true,
|
|
|
})
|
|
})
|
|
|
assert.Error(t, err)
|
|
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)
|
|
|
|
|
|
|
+
|
|
|
|
|
+ apiErrorStatusCode, apiErrorMessage, err := parseInfisicalAPIError(err, t)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ t.Fatalf("Error parsing infisical API error: %v", err)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ assert.Equal(t, 401, apiErrorStatusCode)
|
|
|
|
|
+ assert.Equal(t, "Unauthorized", apiErrorMessage)
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
func TestGetSecretByKeyV3(t *testing.T) {
|
|
func TestGetSecretByKeyV3(t *testing.T) {
|
|
|
t.Run("Works", func(t *testing.T) {
|
|
t.Run("Works", func(t *testing.T) {
|
|
|
|
|
+ secret := SecretsV3{
|
|
|
|
|
+ SecretKey: "foo",
|
|
|
|
|
+ SecretValue: "bar",
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ sdkFormattedSecret := infisical.Secret{
|
|
|
|
|
+ SecretKey: secret.SecretKey,
|
|
|
|
|
+ SecretValue: secret.SecretValue,
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
apiClient, closeFunc := NewMockClient(200, GetSecretByKeyV3Response{
|
|
apiClient, closeFunc := NewMockClient(200, GetSecretByKeyV3Response{
|
|
|
- Secret: SecretsV3{
|
|
|
|
|
- SecretKey: "foo",
|
|
|
|
|
- SecretValue: "bar",
|
|
|
|
|
- },
|
|
|
|
|
|
|
+ Secret: secret,
|
|
|
})
|
|
})
|
|
|
defer closeFunc()
|
|
defer closeFunc()
|
|
|
|
|
|
|
|
- secret, err := apiClient.GetSecretByKeyV3(GetSecretByKeyV3Request{
|
|
|
|
|
- ProjectSlug: fakeProjectSlug,
|
|
|
|
|
- EnvironmentSlug: fakeEnvironmentSlug,
|
|
|
|
|
- SecretPath: "/",
|
|
|
|
|
- SecretKey: "foo",
|
|
|
|
|
|
|
+ sdkSecret, err := apiClient.Secrets().Retrieve(infisical.RetrieveSecretOptions{
|
|
|
|
|
+ ProjectSlug: fakeProjectSlug,
|
|
|
|
|
+ Environment: fakeEnvironmentSlug,
|
|
|
|
|
+ SecretPath: "/",
|
|
|
|
|
+ IncludeImports: true,
|
|
|
|
|
+ SecretKey: "foo",
|
|
|
})
|
|
})
|
|
|
assert.NoError(t, err)
|
|
assert.NoError(t, err)
|
|
|
- assert.Equal(t, "bar", secret)
|
|
|
|
|
|
|
+ assert.Equal(t, sdkSecret, sdkFormattedSecret)
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
t.Run("Error when secret is not found", func(t *testing.T) {
|
|
t.Run("Error when secret is not found", func(t *testing.T) {
|
|
|
apiClient, closeFunc := NewMockClient(404, InfisicalAPIErrorResponse{
|
|
apiClient, closeFunc := NewMockClient(404, InfisicalAPIErrorResponse{
|
|
|
StatusCode: 404,
|
|
StatusCode: 404,
|
|
|
- Error: "Not Found",
|
|
|
|
|
|
|
+ Message: "Not Found",
|
|
|
})
|
|
})
|
|
|
defer closeFunc()
|
|
defer closeFunc()
|
|
|
|
|
|
|
|
- _, err := apiClient.GetSecretByKeyV3(GetSecretByKeyV3Request{
|
|
|
|
|
- ProjectSlug: fakeProjectSlug,
|
|
|
|
|
- EnvironmentSlug: fakeEnvironmentSlug,
|
|
|
|
|
- SecretPath: "/",
|
|
|
|
|
- SecretKey: "foo",
|
|
|
|
|
|
|
+ _, err := apiClient.Secrets().Retrieve(infisical.RetrieveSecretOptions{
|
|
|
|
|
+ ProjectSlug: fakeProjectSlug,
|
|
|
|
|
+ Environment: fakeEnvironmentSlug,
|
|
|
|
|
+ SecretPath: "/",
|
|
|
|
|
+ IncludeImports: true,
|
|
|
|
|
+ SecretKey: "foo",
|
|
|
})
|
|
})
|
|
|
assert.Error(t, err)
|
|
assert.Error(t, err)
|
|
|
- // Importantly, we return the standard error for no secrets found.
|
|
|
|
|
- assert.ErrorIs(t, err, esv1.NoSecretError{})
|
|
|
|
|
|
|
+
|
|
|
|
|
+ apiErrorStatusCode, apiErrorMessage, err := parseInfisicalAPIError(err, t)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ t.Fatalf("Error parsing infisical API error: %v", err)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ assert.Equal(t, 404, apiErrorStatusCode)
|
|
|
|
|
+ assert.Equal(t, "Not Found", apiErrorMessage)
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
// Test case where the request is unauthorized
|
|
// Test case where the request is unauthorized
|
|
|
t.Run("ErrorHandlingUnauthorized", func(t *testing.T) {
|
|
t.Run("ErrorHandlingUnauthorized", func(t *testing.T) {
|
|
|
apiClient, closeFunc := NewMockClient(401, InfisicalAPIErrorResponse{
|
|
apiClient, closeFunc := NewMockClient(401, InfisicalAPIErrorResponse{
|
|
|
StatusCode: 401,
|
|
StatusCode: 401,
|
|
|
- Error: "Unauthorized",
|
|
|
|
|
|
|
+ Message: "Unauthorized",
|
|
|
})
|
|
})
|
|
|
defer closeFunc()
|
|
defer closeFunc()
|
|
|
|
|
|
|
|
- _, err := apiClient.GetSecretByKeyV3(GetSecretByKeyV3Request{
|
|
|
|
|
- ProjectSlug: fakeProjectSlug,
|
|
|
|
|
- EnvironmentSlug: fakeEnvironmentSlug,
|
|
|
|
|
- SecretPath: "/",
|
|
|
|
|
- SecretKey: "foo",
|
|
|
|
|
|
|
+ _, err := apiClient.Secrets().Retrieve(infisical.RetrieveSecretOptions{
|
|
|
|
|
+ ProjectSlug: fakeProjectSlug,
|
|
|
|
|
+ Environment: fakeEnvironmentSlug,
|
|
|
|
|
+ SecretPath: "/",
|
|
|
|
|
+ IncludeImports: true,
|
|
|
|
|
+ SecretKey: "foo",
|
|
|
})
|
|
})
|
|
|
assert.Error(t, err)
|
|
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)
|
|
|
|
|
|
|
+
|
|
|
|
|
+ apiErrorStatusCode, apiErrorMessage, err := parseInfisicalAPIError(err, t)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ t.Fatalf("Error parsing infisical API error: %v", err)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ assert.Equal(t, 401, apiErrorStatusCode)
|
|
|
|
|
+ assert.Equal(t, "Unauthorized", apiErrorMessage)
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|