provider_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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 ovh
  14. import (
  15. "context"
  16. "testing"
  17. corev1 "k8s.io/api/core/v1"
  18. v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/apimachinery/pkg/runtime"
  20. kclient "sigs.k8s.io/controller-runtime/pkg/client"
  21. fakeBuilder "sigs.k8s.io/controller-runtime/pkg/client/fake"
  22. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  23. esmeta "github.com/external-secrets/external-secrets/apis/meta/v1"
  24. "github.com/external-secrets/external-secrets/providers/v1/ovh/fake"
  25. )
  26. var (
  27. namespace = "namespace"
  28. scheme = runtime.NewScheme()
  29. _ = corev1.AddToScheme(scheme)
  30. kube = fakeBuilder.NewClientBuilder().
  31. WithScheme(scheme).
  32. WithObjects(&corev1.Secret{
  33. ObjectMeta: v1.ObjectMeta{
  34. Name: "my-secret",
  35. Namespace: "default",
  36. },
  37. Data: map[string][]byte{
  38. "key": []byte("value"),
  39. },
  40. }).Build()
  41. okmsId = "11111111-1111-1111-1111-111111111111"
  42. validTokenAuth = "Valid token auth"
  43. validClientCert = "Valid mtls client certificate"
  44. validClientKey = "Valid mtls client key"
  45. fillingStr = "string"
  46. )
  47. func TestNewClient(t *testing.T) {
  48. tests := map[string]struct {
  49. errshould string
  50. kube kclient.Client
  51. store *esv1.SecretStore
  52. }{
  53. "Nil store": {
  54. errshould: "failed to create new ovh provider client: store validation failed: store is nil",
  55. kube: kube,
  56. },
  57. "Nil provider": {
  58. errshould: "failed to create new ovh provider client: store validation failed: store provider is nil",
  59. kube: kube,
  60. store: &esv1.SecretStore{
  61. Spec: esv1.SecretStoreSpec{
  62. Provider: nil,
  63. },
  64. },
  65. },
  66. "Nil ovh provider": {
  67. errshould: "failed to create new ovh provider client: store validation failed: ovh store provider is nil",
  68. kube: kube,
  69. store: &esv1.SecretStore{
  70. Spec: esv1.SecretStoreSpec{
  71. Provider: &esv1.SecretStoreProvider{
  72. AWS: &esv1.AWSProvider{},
  73. },
  74. },
  75. },
  76. },
  77. "Nil controller-runtime client": {
  78. errshould: "failed to create new ovh provider client: controller-runtime client is nil",
  79. store: &esv1.SecretStore{
  80. Spec: esv1.SecretStoreSpec{
  81. Provider: &esv1.SecretStoreProvider{
  82. OVHcloud: &esv1.OvhProvider{
  83. Auth: esv1.OvhAuth{
  84. ClientToken: &esv1.OvhClientToken{
  85. ClientTokenSecret: esmeta.SecretKeySelector{
  86. Name: validTokenAuth,
  87. Namespace: &namespace,
  88. Key: fillingStr,
  89. },
  90. },
  91. },
  92. Server: fillingStr,
  93. OkmsID: okmsId,
  94. },
  95. },
  96. },
  97. },
  98. },
  99. "Authentication method conflict": {
  100. errshould: "failed to create new ovh provider client: store validation failed: only one authentication method allowed (mtls | token)",
  101. kube: kube,
  102. store: &esv1.SecretStore{
  103. Spec: esv1.SecretStoreSpec{
  104. Provider: &esv1.SecretStoreProvider{
  105. OVHcloud: &esv1.OvhProvider{
  106. Server: fillingStr,
  107. OkmsID: okmsId,
  108. Auth: esv1.OvhAuth{
  109. ClientMTLS: &esv1.OvhClientMTLS{
  110. ClientCertificate: esmeta.SecretKeySelector{
  111. Name: fillingStr,
  112. Namespace: &namespace,
  113. Key: fillingStr,
  114. },
  115. ClientKey: esmeta.SecretKeySelector{
  116. Name: fillingStr,
  117. Namespace: &namespace,
  118. Key: fillingStr,
  119. },
  120. },
  121. ClientToken: &esv1.OvhClientToken{
  122. ClientTokenSecret: esmeta.SecretKeySelector{
  123. Name: fillingStr,
  124. Namespace: &namespace,
  125. Key: fillingStr,
  126. },
  127. },
  128. },
  129. },
  130. },
  131. },
  132. },
  133. },
  134. "Authentication method empty": {
  135. errshould: "failed to create new ovh provider client: store validation failed: missing authentication method",
  136. kube: kube,
  137. store: &esv1.SecretStore{
  138. Spec: esv1.SecretStoreSpec{
  139. Provider: &esv1.SecretStoreProvider{
  140. OVHcloud: &esv1.OvhProvider{
  141. Server: fillingStr,
  142. OkmsID: okmsId,
  143. Auth: esv1.OvhAuth{},
  144. },
  145. },
  146. },
  147. },
  148. },
  149. "Valid token auth": {
  150. errshould: "",
  151. kube: kube,
  152. store: &esv1.SecretStore{
  153. Spec: esv1.SecretStoreSpec{
  154. Provider: &esv1.SecretStoreProvider{
  155. OVHcloud: &esv1.OvhProvider{
  156. Server: fillingStr,
  157. OkmsID: okmsId,
  158. Auth: esv1.OvhAuth{
  159. ClientToken: &esv1.OvhClientToken{
  160. ClientTokenSecret: esmeta.SecretKeySelector{
  161. Name: validTokenAuth,
  162. Namespace: &namespace,
  163. Key: fillingStr,
  164. },
  165. },
  166. },
  167. },
  168. },
  169. },
  170. },
  171. },
  172. "Empty token auth": {
  173. errshould: "failed to create new ovh provider client: store validation failed: missing token secret for token authentication",
  174. kube: kube,
  175. store: &esv1.SecretStore{
  176. Spec: esv1.SecretStoreSpec{
  177. Provider: &esv1.SecretStoreProvider{
  178. OVHcloud: &esv1.OvhProvider{
  179. Server: fillingStr,
  180. OkmsID: okmsId,
  181. Auth: esv1.OvhAuth{
  182. ClientToken: &esv1.OvhClientToken{
  183. ClientTokenSecret: esmeta.SecretKeySelector{},
  184. },
  185. },
  186. },
  187. },
  188. },
  189. },
  190. },
  191. "Valid mtls auth": {
  192. errshould: "",
  193. kube: kube,
  194. store: &esv1.SecretStore{
  195. Spec: esv1.SecretStoreSpec{
  196. Provider: &esv1.SecretStoreProvider{
  197. OVHcloud: &esv1.OvhProvider{
  198. Server: fillingStr,
  199. OkmsID: okmsId,
  200. Auth: esv1.OvhAuth{
  201. ClientMTLS: &esv1.OvhClientMTLS{
  202. ClientCertificate: esmeta.SecretKeySelector{
  203. Name: validClientCert,
  204. Namespace: &namespace,
  205. Key: fillingStr,
  206. },
  207. ClientKey: esmeta.SecretKeySelector{
  208. Name: validClientKey,
  209. Namespace: &namespace,
  210. Key: fillingStr,
  211. },
  212. },
  213. },
  214. },
  215. },
  216. },
  217. },
  218. },
  219. "Empty mtls client certificate": {
  220. errshould: "failed to create new ovh provider client: store validation failed: missing tls certificate or key for mtls authentication",
  221. kube: kube,
  222. store: &esv1.SecretStore{
  223. Spec: esv1.SecretStoreSpec{
  224. Provider: &esv1.SecretStoreProvider{
  225. OVHcloud: &esv1.OvhProvider{
  226. Server: fillingStr,
  227. OkmsID: okmsId,
  228. Auth: esv1.OvhAuth{
  229. ClientMTLS: &esv1.OvhClientMTLS{
  230. ClientKey: esmeta.SecretKeySelector{
  231. Name: validClientKey,
  232. Namespace: &namespace,
  233. Key: fillingStr,
  234. },
  235. },
  236. },
  237. },
  238. },
  239. },
  240. },
  241. },
  242. }
  243. ctx := context.Background()
  244. for name, testCase := range tests {
  245. t.Run(name, func(t *testing.T) {
  246. provider := Provider{
  247. secretKeyResolver: &fake.FakeSecretKeyResolver{},
  248. }
  249. _, err := provider.NewClient(ctx, testCase.store, testCase.kube, "namespace")
  250. if testCase.errshould != "" {
  251. if err == nil {
  252. t.Errorf("\nexpected error: %s\nactual error: <nil>\n\n", testCase.errshould)
  253. } else if err.Error() != testCase.errshould {
  254. t.Errorf("\nexpected error: %s\nactual error: %v\n\n", testCase.errshould, err)
  255. }
  256. } else if err != nil {
  257. t.Errorf("\nunexpected error: %v\n\n", err)
  258. }
  259. })
  260. }
  261. }
  262. func TestValidateStore(t *testing.T) {
  263. var namespace string = "namespace"
  264. tests := map[string]struct {
  265. errshould string
  266. kube kclient.Client
  267. store *esv1.SecretStore
  268. }{
  269. "Nil store": {
  270. errshould: "store provider is nil",
  271. kube: kube,
  272. store: &esv1.SecretStore{
  273. Spec: esv1.SecretStoreSpec{
  274. Provider: nil,
  275. },
  276. },
  277. },
  278. "Nil ovh provider": {
  279. errshould: "ovh store provider is nil",
  280. kube: kube,
  281. store: &esv1.SecretStore{
  282. Spec: esv1.SecretStoreSpec{
  283. Provider: &esv1.SecretStoreProvider{
  284. AWS: &esv1.AWSProvider{},
  285. },
  286. },
  287. },
  288. },
  289. "Authentication method conflict": {
  290. errshould: "only one authentication method allowed (mtls | token)",
  291. kube: kube,
  292. store: &esv1.SecretStore{
  293. Spec: esv1.SecretStoreSpec{
  294. Provider: &esv1.SecretStoreProvider{
  295. OVHcloud: &esv1.OvhProvider{
  296. Server: fillingStr,
  297. OkmsID: okmsId,
  298. Auth: esv1.OvhAuth{
  299. ClientMTLS: &esv1.OvhClientMTLS{
  300. ClientCertificate: esmeta.SecretKeySelector{
  301. Name: fillingStr,
  302. Namespace: &namespace,
  303. Key: fillingStr,
  304. },
  305. ClientKey: esmeta.SecretKeySelector{
  306. Name: fillingStr,
  307. Namespace: &namespace,
  308. Key: fillingStr,
  309. },
  310. },
  311. ClientToken: &esv1.OvhClientToken{
  312. ClientTokenSecret: esmeta.SecretKeySelector{
  313. Name: fillingStr,
  314. Namespace: &namespace,
  315. Key: fillingStr,
  316. },
  317. },
  318. },
  319. },
  320. },
  321. },
  322. },
  323. },
  324. "Valid token auth": {
  325. errshould: "",
  326. kube: kube,
  327. store: &esv1.SecretStore{
  328. Spec: esv1.SecretStoreSpec{
  329. Provider: &esv1.SecretStoreProvider{
  330. OVHcloud: &esv1.OvhProvider{
  331. Server: fillingStr,
  332. OkmsID: okmsId,
  333. Auth: esv1.OvhAuth{
  334. ClientToken: &esv1.OvhClientToken{
  335. ClientTokenSecret: esmeta.SecretKeySelector{
  336. Name: fillingStr,
  337. Namespace: &namespace,
  338. Key: fillingStr,
  339. },
  340. },
  341. },
  342. },
  343. },
  344. },
  345. },
  346. },
  347. "Valid mtls auth": {
  348. errshould: "",
  349. kube: kube,
  350. store: &esv1.SecretStore{
  351. Spec: esv1.SecretStoreSpec{
  352. Provider: &esv1.SecretStoreProvider{
  353. OVHcloud: &esv1.OvhProvider{
  354. Server: fillingStr,
  355. OkmsID: okmsId,
  356. Auth: esv1.OvhAuth{
  357. ClientMTLS: &esv1.OvhClientMTLS{
  358. ClientCertificate: esmeta.SecretKeySelector{
  359. Name: fillingStr,
  360. Namespace: &namespace,
  361. Key: fillingStr,
  362. },
  363. ClientKey: esmeta.SecretKeySelector{
  364. Name: fillingStr,
  365. Namespace: &namespace,
  366. Key: fillingStr,
  367. },
  368. },
  369. },
  370. },
  371. },
  372. },
  373. },
  374. },
  375. "Invalid mtls auth: missing client certificate": {
  376. errshould: "missing tls certificate or key for mtls authentication",
  377. kube: kube,
  378. store: &esv1.SecretStore{
  379. Spec: esv1.SecretStoreSpec{
  380. Provider: &esv1.SecretStoreProvider{
  381. OVHcloud: &esv1.OvhProvider{
  382. Server: fillingStr,
  383. OkmsID: okmsId,
  384. Auth: esv1.OvhAuth{
  385. ClientMTLS: &esv1.OvhClientMTLS{
  386. ClientKey: esmeta.SecretKeySelector{
  387. Name: fillingStr,
  388. Namespace: &namespace,
  389. Key: fillingStr,
  390. },
  391. },
  392. },
  393. },
  394. },
  395. },
  396. },
  397. },
  398. "Empty auth": {
  399. errshould: "missing authentication method",
  400. kube: kube,
  401. store: &esv1.SecretStore{
  402. Spec: esv1.SecretStoreSpec{
  403. Provider: &esv1.SecretStoreProvider{
  404. OVHcloud: &esv1.OvhProvider{
  405. Server: fillingStr,
  406. OkmsID: okmsId,
  407. },
  408. },
  409. },
  410. },
  411. },
  412. "Invalid token auth: missing token secret": {
  413. errshould: "missing token secret for token authentication",
  414. kube: kube,
  415. store: &esv1.SecretStore{
  416. Spec: esv1.SecretStoreSpec{
  417. Provider: &esv1.SecretStoreProvider{
  418. OVHcloud: &esv1.OvhProvider{
  419. Server: fillingStr,
  420. OkmsID: okmsId,
  421. Auth: esv1.OvhAuth{
  422. ClientToken: &esv1.OvhClientToken{},
  423. },
  424. },
  425. },
  426. },
  427. },
  428. },
  429. }
  430. for name, testCase := range tests {
  431. t.Run(name, func(t *testing.T) {
  432. provider := Provider{}
  433. _, err := provider.ValidateStore(testCase.store)
  434. if testCase.errshould != "" {
  435. if err == nil {
  436. t.Errorf("\nexpected error: %s\nactual error: <nil>\n\n", testCase.errshould)
  437. } else if err.Error() != testCase.errshould {
  438. t.Errorf("\nexpected error: %s\nactual error: %v\n\n", testCase.errshould, err)
  439. }
  440. } else if err != nil {
  441. t.Errorf("\nunexpected error: %v\n\n", err)
  442. }
  443. })
  444. }
  445. }