provider_test.go 11 KB

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