provider_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or impliec.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package infisical
  13. import (
  14. "context"
  15. "errors"
  16. "testing"
  17. "github.com/stretchr/testify/assert"
  18. "github.com/stretchr/testify/require"
  19. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  20. esv1meta "github.com/external-secrets/external-secrets/apis/meta/v1"
  21. "github.com/external-secrets/external-secrets/pkg/provider/infisical/api"
  22. )
  23. type storeModifier func(*esv1.SecretStore) *esv1.SecretStore
  24. var apiScope = InfisicalClientScope{
  25. SecretPath: "/",
  26. ProjectSlug: "first-project",
  27. EnvironmentSlug: "dev",
  28. }
  29. type TestCases struct {
  30. Name string
  31. MockStatusCode int
  32. MockResponse any
  33. Property string
  34. Error error
  35. Output any
  36. }
  37. func TestGetSecret(t *testing.T) {
  38. key := "foo"
  39. testCases := []TestCases{
  40. {
  41. Name: "Get_valid_key",
  42. MockStatusCode: 200,
  43. MockResponse: api.GetSecretByKeyV3Response{
  44. Secret: api.SecretsV3{
  45. SecretKey: key,
  46. SecretValue: "bar",
  47. },
  48. },
  49. Output: []byte("bar"),
  50. },
  51. {
  52. Name: "Get_property_key",
  53. MockStatusCode: 200,
  54. MockResponse: api.GetSecretByKeyV3Response{
  55. Secret: api.SecretsV3{
  56. SecretKey: key,
  57. SecretValue: `{"bar": "value"}`,
  58. },
  59. },
  60. Property: "bar",
  61. Output: []byte("value"),
  62. },
  63. {
  64. Name: "Key_not_found",
  65. MockStatusCode: 404,
  66. MockResponse: api.InfisicalAPIError{
  67. StatusCode: 404,
  68. Err: "Not Found",
  69. Message: "Secret not found",
  70. },
  71. Error: esv1.NoSecretError{},
  72. Output: "",
  73. },
  74. }
  75. for _, tc := range testCases {
  76. t.Run(tc.Name, func(t *testing.T) {
  77. apiClient, closeFunc := api.NewMockClient(tc.MockStatusCode, tc.MockResponse)
  78. defer closeFunc()
  79. p := &Provider{
  80. apiClient: apiClient,
  81. apiScope: &apiScope,
  82. }
  83. output, err := p.GetSecret(context.Background(), esv1.ExternalSecretDataRemoteRef{
  84. Key: key,
  85. Property: tc.Property,
  86. })
  87. if tc.Error == nil {
  88. assert.NoError(t, err)
  89. assert.Equal(t, tc.Output, output)
  90. } else {
  91. assert.ErrorAs(t, err, &tc.Error)
  92. }
  93. })
  94. }
  95. }
  96. func TestGetSecretMap(t *testing.T) {
  97. key := "foo"
  98. testCases := []TestCases{
  99. {
  100. Name: "Get_valid_key_map",
  101. MockStatusCode: 200,
  102. MockResponse: api.GetSecretByKeyV3Response{
  103. Secret: api.SecretsV3{
  104. SecretKey: key,
  105. SecretValue: `{"bar": "value"}`,
  106. },
  107. },
  108. Output: map[string][]byte{
  109. "bar": []byte("value"),
  110. },
  111. },
  112. {
  113. Name: "Get_invalid_map",
  114. MockStatusCode: 200,
  115. MockResponse: []byte(``),
  116. Error: errors.New("unable to unmarshal secret foo"),
  117. },
  118. }
  119. for _, tc := range testCases {
  120. t.Run(tc.Name, func(t *testing.T) {
  121. apiClient, closeFunc := api.NewMockClient(tc.MockStatusCode, tc.MockResponse)
  122. defer closeFunc()
  123. p := &Provider{
  124. apiClient: apiClient,
  125. apiScope: &apiScope,
  126. }
  127. output, err := p.GetSecretMap(context.Background(), esv1.ExternalSecretDataRemoteRef{
  128. Key: key,
  129. })
  130. if tc.Error == nil {
  131. assert.NoError(t, err)
  132. assert.Equal(t, tc.Output, output)
  133. } else {
  134. assert.ErrorAs(t, err, &tc.Error)
  135. }
  136. })
  137. }
  138. }
  139. func makeSecretStore(projectSlug, environment, secretPath string, fn ...storeModifier) *esv1.SecretStore {
  140. store := &esv1.SecretStore{
  141. Spec: esv1.SecretStoreSpec{
  142. Provider: &esv1.SecretStoreProvider{
  143. Infisical: &esv1.InfisicalProvider{
  144. Auth: esv1.InfisicalAuth{
  145. UniversalAuthCredentials: &esv1.UniversalAuthCredentials{},
  146. },
  147. SecretsScope: esv1.MachineIdentityScopeInWorkspace{
  148. SecretsPath: secretPath,
  149. EnvironmentSlug: environment,
  150. ProjectSlug: projectSlug,
  151. },
  152. },
  153. },
  154. },
  155. }
  156. for _, f := range fn {
  157. store = f(store)
  158. }
  159. return store
  160. }
  161. func withClientID(name, key string, namespace *string) storeModifier {
  162. return func(store *esv1.SecretStore) *esv1.SecretStore {
  163. store.Spec.Provider.Infisical.Auth.UniversalAuthCredentials.ClientID = esv1meta.SecretKeySelector{
  164. Name: name,
  165. Key: key,
  166. Namespace: namespace,
  167. }
  168. return store
  169. }
  170. }
  171. func withClientSecret(name, key string, namespace *string) storeModifier {
  172. return func(store *esv1.SecretStore) *esv1.SecretStore {
  173. store.Spec.Provider.Infisical.Auth.UniversalAuthCredentials.ClientSecret = esv1meta.SecretKeySelector{
  174. Name: name,
  175. Key: key,
  176. Namespace: namespace,
  177. }
  178. return store
  179. }
  180. }
  181. type ValidateStoreTestCase struct {
  182. store *esv1.SecretStore
  183. assertError func(t *testing.T, err error)
  184. }
  185. func TestValidateStore(t *testing.T) {
  186. const randomID = "some-random-id"
  187. const authType = "universal-auth"
  188. var authCredMissingErr = errors.New("universalAuthCredentials.clientId and universalAuthCredentials.clientSecret cannot be empty")
  189. var authScopeMissingErr = errors.New("secretsScope.projectSlug and secretsScope.environmentSlug cannot be empty")
  190. testCases := []ValidateStoreTestCase{
  191. {
  192. store: makeSecretStore("", "", ""),
  193. assertError: func(t *testing.T, err error) {
  194. require.ErrorAs(t, err, &authScopeMissingErr)
  195. },
  196. },
  197. {
  198. store: makeSecretStore(apiScope.ProjectSlug, apiScope.EnvironmentSlug, apiScope.SecretPath, withClientID(authType, randomID, nil)),
  199. assertError: func(t *testing.T, err error) {
  200. require.ErrorAs(t, err, &authCredMissingErr)
  201. },
  202. },
  203. {
  204. store: makeSecretStore(apiScope.ProjectSlug, apiScope.EnvironmentSlug, apiScope.SecretPath, withClientSecret(authType, randomID, nil)),
  205. assertError: func(t *testing.T, err error) {
  206. require.ErrorAs(t, err, &authCredMissingErr)
  207. },
  208. },
  209. {
  210. store: makeSecretStore(apiScope.ProjectSlug, apiScope.EnvironmentSlug, apiScope.SecretPath, withClientID(authType, randomID, nil), withClientSecret(authType, randomID, nil)),
  211. assertError: func(t *testing.T, err error) { require.NoError(t, err) },
  212. },
  213. }
  214. p := Provider{}
  215. for _, tc := range testCases {
  216. _, err := p.ValidateStore(tc.store)
  217. tc.assertError(t, err)
  218. }
  219. }