provider_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /*
  2. Copyright © The ESO Authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package ngrok
  14. import (
  15. "fmt"
  16. "testing"
  17. "github.com/ngrok/ngrok-api-go/v7"
  18. corev1 "k8s.io/api/core/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. kubeClient "sigs.k8s.io/controller-runtime/pkg/client"
  21. clientfake "sigs.k8s.io/controller-runtime/pkg/client/fake"
  22. "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
  23. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  24. v1 "github.com/external-secrets/external-secrets/apis/meta/v1"
  25. "github.com/external-secrets/external-secrets/providers/v1/ngrok/fake"
  26. . "github.com/onsi/ginkgo/v2"
  27. . "github.com/onsi/gomega"
  28. )
  29. func newTestClusterSecretStore(provider *esv1.SecretStoreProvider) esv1.GenericStore {
  30. return &esv1.ClusterSecretStore{
  31. TypeMeta: metav1.TypeMeta{
  32. Kind: "ClusterSecretStore",
  33. },
  34. Spec: esv1.SecretStoreSpec{
  35. Provider: provider,
  36. },
  37. }
  38. }
  39. func newTestNgrokClusterSecretStore(ngrokProv *esv1.NgrokProvider) esv1.GenericStore {
  40. return newTestClusterSecretStore(&esv1.SecretStoreProvider{
  41. Ngrok: ngrokProv,
  42. })
  43. }
  44. func newTestSecretStore(provider *esv1.SecretStoreProvider) esv1.GenericStore {
  45. return &esv1.SecretStore{
  46. TypeMeta: metav1.TypeMeta{
  47. Kind: "SecretStore",
  48. },
  49. Spec: esv1.SecretStoreSpec{
  50. Provider: provider,
  51. },
  52. }
  53. }
  54. func newTestNgrokSecretStore(ngrokProv *esv1.NgrokProvider) esv1.GenericStore {
  55. return newTestSecretStore(&esv1.SecretStoreProvider{
  56. Ngrok: ngrokProv,
  57. })
  58. }
  59. func newNgrokAPICredentials(name, namespace, apiKey string) *corev1.Secret {
  60. return &corev1.Secret{
  61. ObjectMeta: metav1.ObjectMeta{
  62. Name: name,
  63. Namespace: namespace,
  64. },
  65. Data: map[string][]byte{
  66. "API_KEY": []byte(apiKey),
  67. },
  68. }
  69. }
  70. var _ = Describe("Provider", func() {
  71. var (
  72. provider *Provider
  73. )
  74. BeforeEach(func() {
  75. provider = &Provider{}
  76. })
  77. Describe("Capabilities", func() {
  78. It("should return write-only capability", func() {
  79. cap := provider.Capabilities()
  80. Expect(cap).To(Equal(esv1.SecretStoreWriteOnly))
  81. })
  82. })
  83. Describe("NewClient", func() {
  84. var (
  85. store esv1.GenericStore
  86. ngrokStore *fake.Store
  87. namespace string
  88. kubeClient kubeClient.Client
  89. ngrokCredentials *corev1.Secret
  90. vaultName string
  91. // Injected errors
  92. vaultListErr error
  93. // Outputs
  94. err error
  95. client esv1.SecretsClient
  96. )
  97. BeforeEach(func() {
  98. namespace = "default"
  99. vaultName = "vault-" + fake.GenerateRandomString(5)
  100. ngrokCredentials = newNgrokAPICredentials("ngrok-credentials", namespace, "secret-api-key")
  101. kubeClient = clientfake.NewClientBuilder().WithObjects(ngrokCredentials).Build()
  102. ngrokStore = fake.NewStore()
  103. vaultListErr = nil
  104. })
  105. JustBeforeEach(func() {
  106. getVaultsClient = func(_ *ngrok.ClientConfig) VaultClient {
  107. return ngrokStore.VaultClient().WithListError(vaultListErr)
  108. }
  109. getSecretsClient = func(_ *ngrok.ClientConfig) SecretsClient {
  110. return ngrokStore.SecretsClient()
  111. }
  112. client, err = provider.NewClient(GinkgoT().Context(), store, kubeClient, namespace)
  113. })
  114. Context("SecretStore", func() {
  115. When("the secret does not exist", func() {
  116. BeforeEach(func() {
  117. store = newTestNgrokSecretStore(&esv1.NgrokProvider{
  118. Vault: esv1.NgrokVault{
  119. Name: vaultName,
  120. },
  121. Auth: esv1.NgrokAuth{
  122. APIKey: &esv1.NgrokProviderSecretRef{
  123. SecretRef: &v1.SecretKeySelector{
  124. Key: "API_KEY",
  125. Name: "non-existent-secret",
  126. },
  127. },
  128. },
  129. })
  130. })
  131. It("should return an error that the secret does not exist", func() {
  132. Expect(err).To(HaveOccurred())
  133. Expect(err.Error()).To(ContainSubstring("secrets \"non-existent-secret\" not found"))
  134. Expect(client).To(BeNil())
  135. })
  136. })
  137. When("the store is valid", func() {
  138. BeforeEach(func() {
  139. store = newTestNgrokSecretStore(&esv1.NgrokProvider{
  140. Vault: esv1.NgrokVault{
  141. Name: vaultName,
  142. },
  143. Auth: esv1.NgrokAuth{
  144. APIKey: &esv1.NgrokProviderSecretRef{
  145. SecretRef: &v1.SecretKeySelector{
  146. Key: "API_KEY",
  147. Name: ngrokCredentials.Name,
  148. },
  149. },
  150. },
  151. })
  152. })
  153. When("the vault does not exist", func() {
  154. It("Should return an error that the vault is not found", func() {
  155. Expect(err).To(HaveOccurred())
  156. Expect(err.Error()).To(ContainSubstring(fmt.Sprintf("vault %q not found", vaultName)))
  157. Expect(client).To(BeNil())
  158. })
  159. })
  160. When("the vault exists", func() {
  161. BeforeEach(func() {
  162. _, createErr := ngrokStore.CreateVault(&ngrok.VaultCreate{
  163. Name: vaultName,
  164. })
  165. Expect(createErr).To(BeNil())
  166. })
  167. It("should not return an error", func() {
  168. Expect(err).To(BeNil())
  169. })
  170. It("should return a non-nil client", func() {
  171. Expect(client).NotTo(BeNil())
  172. })
  173. })
  174. When("there is an error listing vaults", func() {
  175. BeforeEach(func() {
  176. vaultListErr = fmt.Errorf("some error listing vaults")
  177. })
  178. It("should return the list error", func() {
  179. Expect(err).To(HaveOccurred())
  180. Expect(err.Error()).To(ContainSubstring("some error listing vaults"))
  181. Expect(client).To(BeNil())
  182. })
  183. })
  184. })
  185. })
  186. Context("ClusterSecretStore", func() {
  187. When("the store does not specify a namespace", func() {
  188. BeforeEach(func() {
  189. store = newTestNgrokClusterSecretStore(&esv1.NgrokProvider{
  190. Vault: esv1.NgrokVault{
  191. Name: vaultName,
  192. },
  193. Auth: esv1.NgrokAuth{
  194. APIKey: &esv1.NgrokProviderSecretRef{
  195. SecretRef: &v1.SecretKeySelector{
  196. Key: "API_KEY",
  197. Name: ngrokCredentials.Name,
  198. },
  199. },
  200. },
  201. })
  202. })
  203. It("should return an error that the cluster store requires a namespace", func() {
  204. Expect(err).To(MatchError(errClusterStoreRequiresNamespace))
  205. Expect(client).To(BeNil())
  206. })
  207. })
  208. When("the secret does not exist", func() {
  209. BeforeEach(func() {
  210. store = newTestNgrokClusterSecretStore(&esv1.NgrokProvider{
  211. Vault: esv1.NgrokVault{
  212. Name: vaultName,
  213. },
  214. Auth: esv1.NgrokAuth{
  215. APIKey: &esv1.NgrokProviderSecretRef{
  216. SecretRef: &v1.SecretKeySelector{
  217. Key: "API_KEY",
  218. Name: "non-existent-secret",
  219. Namespace: new("some-other-namespace"),
  220. },
  221. },
  222. },
  223. })
  224. })
  225. It("should return an error that the secret does not exist", func() {
  226. Expect(err).To(HaveOccurred())
  227. Expect(err.Error()).To(ContainSubstring("secrets \"non-existent-secret\" not found"))
  228. Expect(client).To(BeNil())
  229. })
  230. })
  231. When("the store is valid", func() {
  232. BeforeEach(func() {
  233. store = newTestNgrokClusterSecretStore(&esv1.NgrokProvider{
  234. Vault: esv1.NgrokVault{
  235. Name: vaultName,
  236. },
  237. Auth: esv1.NgrokAuth{
  238. APIKey: &esv1.NgrokProviderSecretRef{
  239. SecretRef: &v1.SecretKeySelector{
  240. Key: "API_KEY",
  241. Name: ngrokCredentials.Name,
  242. Namespace: new(namespace),
  243. },
  244. },
  245. },
  246. })
  247. })
  248. When("the vault does not exist", func() {
  249. It("Should return an error that the vault is not found", func() {
  250. Expect(err).To(HaveOccurred())
  251. Expect(err.Error()).To(ContainSubstring(fmt.Sprintf("vault %q not found", vaultName)))
  252. Expect(client).To(BeNil())
  253. })
  254. })
  255. When("the vault exists", func() {
  256. BeforeEach(func() {
  257. _, createErr := ngrokStore.CreateVault(&ngrok.VaultCreate{
  258. Name: vaultName,
  259. })
  260. Expect(createErr).To(BeNil())
  261. })
  262. It("should not return an error", func() {
  263. Expect(err).To(BeNil())
  264. })
  265. It("should return a non-nil client", func() {
  266. Expect(client).NotTo(BeNil())
  267. })
  268. })
  269. })
  270. })
  271. })
  272. Describe("ValidateStore", func() {
  273. var (
  274. store esv1.GenericStore
  275. err error
  276. warnings admission.Warnings
  277. )
  278. JustBeforeEach(func() {
  279. warnings, err = provider.ValidateStore(store)
  280. })
  281. When("the store is nil", func() {
  282. BeforeEach(func() { store = nil })
  283. It("Should return an invalid store error", func() {
  284. Expect(err).To(MatchError(errInvalidStore))
  285. Expect(warnings).To(BeNil())
  286. })
  287. })
  288. When("the provider is nil", func() {
  289. BeforeEach(func() { store = newTestSecretStore(nil) })
  290. It("Should return an invalid ngrok provider error", func() {
  291. Expect(err).To(MatchError(errInvalidStoreProv))
  292. Expect(warnings).To(BeNil())
  293. })
  294. })
  295. When("the ngrok provider is nil", func() {
  296. BeforeEach(func() { store = newTestNgrokSecretStore(nil) })
  297. It("Should return an invalid ngrok provider error", func() {
  298. Expect(err).To(MatchError(errInvalidNgrokProv))
  299. Expect(warnings).To(BeNil())
  300. })
  301. })
  302. When("the API URL is invalid", func() {
  303. BeforeEach(func() {
  304. store = newTestNgrokSecretStore(&esv1.NgrokProvider{
  305. APIURL: "http://example.com/path\n",
  306. })
  307. })
  308. It("Should return an invalid API URL error", func() {
  309. Expect(err).To(MatchError(errInvalidAPIURL))
  310. Expect(warnings).To(BeNil())
  311. })
  312. })
  313. When("the auth APIKey is missing", func() {
  314. BeforeEach(func() {
  315. store = newTestNgrokSecretStore(&esv1.NgrokProvider{
  316. Vault: esv1.NgrokVault{
  317. Name: "test-vault",
  318. },
  319. Auth: esv1.NgrokAuth{
  320. APIKey: nil,
  321. },
  322. })
  323. })
  324. It("Should return an invalid auth APIKey required error", func() {
  325. Expect(err).To(MatchError(errInvalidAuthAPIKeyRequired))
  326. Expect(warnings).To(BeNil())
  327. })
  328. })
  329. When("the vault name is missing", func() {
  330. BeforeEach(func() {
  331. store = newTestNgrokSecretStore(&esv1.NgrokProvider{
  332. Auth: esv1.NgrokAuth{
  333. APIKey: &esv1.NgrokProviderSecretRef{
  334. SecretRef: &v1.SecretKeySelector{
  335. Key: "apiKey",
  336. Name: "ngrok-credentials",
  337. },
  338. },
  339. },
  340. Vault: esv1.NgrokVault{},
  341. })
  342. })
  343. It("Should return a missing vault name error", func() {
  344. Expect(err).To(MatchError(errMissingVaultName))
  345. Expect(warnings).To(BeNil())
  346. })
  347. })
  348. When("the store is valid", func() {
  349. BeforeEach(func() {
  350. store = newTestNgrokSecretStore(&esv1.NgrokProvider{
  351. Vault: esv1.NgrokVault{
  352. Name: "test-vault",
  353. },
  354. Auth: esv1.NgrokAuth{
  355. APIKey: &esv1.NgrokProviderSecretRef{
  356. SecretRef: &v1.SecretKeySelector{
  357. Key: "apiKey",
  358. Name: "ngrok-credentials",
  359. },
  360. },
  361. },
  362. })
  363. })
  364. It("Should not return an error", func() {
  365. Expect(err).To(BeNil())
  366. Expect(warnings).To(BeNil())
  367. })
  368. })
  369. })
  370. // Add more Ginkgo tests here for ValidateStore, NewClient, etc.
  371. })
  372. func TestNgrokProvider(t *testing.T) {
  373. RegisterFailHandler(Fail)
  374. RunSpecs(t, "Ngrok Provider Suite")
  375. }