validate_test.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 vault
  14. import (
  15. "testing"
  16. pointer "k8s.io/utils/ptr"
  17. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  18. esmeta "github.com/external-secrets/external-secrets/apis/meta/v1"
  19. )
  20. const fakeValidationValue = "fake-value"
  21. func TestValidateStore(t *testing.T) {
  22. type args struct {
  23. auth esv1.VaultAuth
  24. clientTLS esv1.VaultClientTLS
  25. version esv1.VaultKVStoreVersion
  26. checkAndSet *esv1.VaultCheckAndSet
  27. }
  28. tests := []struct {
  29. name string
  30. args args
  31. wantErr bool
  32. }{
  33. {
  34. name: "empty auth",
  35. args: args{},
  36. },
  37. {
  38. name: "invalid approle with namespace",
  39. args: args{
  40. auth: esv1.VaultAuth{
  41. AppRole: &esv1.VaultAppRole{
  42. SecretRef: esmeta.SecretKeySelector{
  43. Namespace: pointer.To("invalid"),
  44. },
  45. },
  46. },
  47. },
  48. wantErr: true,
  49. },
  50. {
  51. name: "invalid approle with roleId and no roleRef",
  52. args: args{
  53. auth: esv1.VaultAuth{
  54. AppRole: &esv1.VaultAppRole{
  55. RoleID: "",
  56. RoleRef: nil,
  57. },
  58. },
  59. },
  60. wantErr: true,
  61. },
  62. {
  63. name: "valid approle with roleId and no roleRef",
  64. args: args{
  65. auth: esv1.VaultAuth{
  66. AppRole: &esv1.VaultAppRole{
  67. RoleID: fakeValidationValue,
  68. },
  69. },
  70. },
  71. wantErr: false,
  72. },
  73. {
  74. name: "valid approle with roleId and no roleRef",
  75. args: args{
  76. auth: esv1.VaultAuth{
  77. AppRole: &esv1.VaultAppRole{
  78. RoleRef: &esmeta.SecretKeySelector{
  79. Name: fakeValidationValue,
  80. },
  81. },
  82. },
  83. },
  84. wantErr: false,
  85. },
  86. {
  87. name: "invalid clientcert",
  88. args: args{
  89. auth: esv1.VaultAuth{
  90. Cert: &esv1.VaultCertAuth{
  91. ClientCert: esmeta.SecretKeySelector{
  92. Namespace: pointer.To("invalid"),
  93. },
  94. },
  95. },
  96. },
  97. wantErr: true,
  98. },
  99. {
  100. name: "invalid cert secret",
  101. args: args{
  102. auth: esv1.VaultAuth{
  103. Cert: &esv1.VaultCertAuth{
  104. SecretRef: esmeta.SecretKeySelector{
  105. Namespace: pointer.To("invalid"),
  106. },
  107. },
  108. },
  109. },
  110. wantErr: true,
  111. },
  112. {
  113. name: "invalid jwt secret",
  114. args: args{
  115. auth: esv1.VaultAuth{
  116. Jwt: &esv1.VaultJwtAuth{
  117. SecretRef: &esmeta.SecretKeySelector{
  118. Namespace: pointer.To("invalid"),
  119. },
  120. },
  121. },
  122. },
  123. wantErr: true,
  124. },
  125. {
  126. name: "invalid kubernetes sa",
  127. args: args{
  128. auth: esv1.VaultAuth{
  129. Kubernetes: &esv1.VaultKubernetesAuth{
  130. ServiceAccountRef: &esmeta.ServiceAccountSelector{
  131. Namespace: pointer.To("invalid"),
  132. },
  133. },
  134. },
  135. },
  136. wantErr: true,
  137. },
  138. {
  139. name: "invalid kubernetes secret",
  140. args: args{
  141. auth: esv1.VaultAuth{
  142. Kubernetes: &esv1.VaultKubernetesAuth{
  143. SecretRef: &esmeta.SecretKeySelector{
  144. Namespace: pointer.To("invalid"),
  145. },
  146. },
  147. },
  148. },
  149. wantErr: true,
  150. },
  151. {
  152. name: "invalid ldap secret",
  153. args: args{
  154. auth: esv1.VaultAuth{
  155. Ldap: &esv1.VaultLdapAuth{
  156. SecretRef: esmeta.SecretKeySelector{
  157. Namespace: pointer.To("invalid"),
  158. },
  159. },
  160. },
  161. },
  162. wantErr: true,
  163. },
  164. {
  165. name: "invalid userpass secret",
  166. args: args{
  167. auth: esv1.VaultAuth{
  168. UserPass: &esv1.VaultUserPassAuth{
  169. SecretRef: esmeta.SecretKeySelector{
  170. Namespace: pointer.To("invalid"),
  171. },
  172. },
  173. },
  174. },
  175. wantErr: true,
  176. },
  177. {
  178. name: "invalid token secret",
  179. args: args{
  180. auth: esv1.VaultAuth{
  181. TokenSecretRef: &esmeta.SecretKeySelector{
  182. Namespace: pointer.To("invalid"),
  183. },
  184. },
  185. },
  186. wantErr: true,
  187. },
  188. {
  189. name: "valid clientTls config",
  190. args: args{
  191. auth: esv1.VaultAuth{
  192. AppRole: &esv1.VaultAppRole{
  193. RoleRef: &esmeta.SecretKeySelector{
  194. Name: fakeValidationValue,
  195. },
  196. },
  197. },
  198. clientTLS: esv1.VaultClientTLS{
  199. CertSecretRef: &esmeta.SecretKeySelector{
  200. Name: "tls-auth-certs",
  201. },
  202. KeySecretRef: &esmeta.SecretKeySelector{
  203. Name: "tls-auth-certs",
  204. },
  205. },
  206. },
  207. wantErr: false,
  208. },
  209. {
  210. name: "invalid clientTls config, missing SecretRef",
  211. args: args{
  212. auth: esv1.VaultAuth{
  213. AppRole: &esv1.VaultAppRole{
  214. RoleRef: &esmeta.SecretKeySelector{
  215. Name: fakeValidationValue,
  216. },
  217. },
  218. },
  219. clientTLS: esv1.VaultClientTLS{
  220. CertSecretRef: &esmeta.SecretKeySelector{
  221. Name: "tls-auth-certs",
  222. },
  223. },
  224. },
  225. wantErr: true,
  226. },
  227. {
  228. name: "invalid clientTls config, missing ClientCert",
  229. args: args{
  230. auth: esv1.VaultAuth{
  231. AppRole: &esv1.VaultAppRole{
  232. RoleRef: &esmeta.SecretKeySelector{
  233. Name: fakeValidationValue,
  234. },
  235. },
  236. },
  237. clientTLS: esv1.VaultClientTLS{
  238. KeySecretRef: &esmeta.SecretKeySelector{
  239. Name: "tls-auth-certs",
  240. },
  241. },
  242. },
  243. wantErr: true,
  244. },
  245. {
  246. name: "valid CAS config with KV v2",
  247. args: args{
  248. auth: esv1.VaultAuth{
  249. AppRole: &esv1.VaultAppRole{
  250. RoleRef: &esmeta.SecretKeySelector{
  251. Name: fakeValidationValue,
  252. },
  253. },
  254. },
  255. version: esv1.VaultKVStoreV2,
  256. checkAndSet: &esv1.VaultCheckAndSet{
  257. Required: true,
  258. },
  259. },
  260. wantErr: false,
  261. },
  262. {
  263. name: "invalid CAS config with KV v1",
  264. args: args{
  265. auth: esv1.VaultAuth{
  266. AppRole: &esv1.VaultAppRole{
  267. RoleRef: &esmeta.SecretKeySelector{
  268. Name: fakeValidationValue,
  269. },
  270. },
  271. },
  272. version: esv1.VaultKVStoreV1,
  273. checkAndSet: &esv1.VaultCheckAndSet{
  274. Required: true,
  275. },
  276. },
  277. wantErr: true,
  278. },
  279. {
  280. name: "CAS config not required is valid with any version",
  281. args: args{
  282. auth: esv1.VaultAuth{
  283. AppRole: &esv1.VaultAppRole{
  284. RoleRef: &esmeta.SecretKeySelector{
  285. Name: fakeValidationValue,
  286. },
  287. },
  288. },
  289. version: esv1.VaultKVStoreV1,
  290. checkAndSet: &esv1.VaultCheckAndSet{
  291. Required: false,
  292. },
  293. },
  294. wantErr: false,
  295. },
  296. }
  297. for _, tt := range tests {
  298. t.Run(tt.name, func(t *testing.T) {
  299. c := &Provider{
  300. NewVaultClient: nil,
  301. }
  302. auth := tt.args.auth
  303. store := &esv1.SecretStore{
  304. Spec: esv1.SecretStoreSpec{
  305. Provider: &esv1.SecretStoreProvider{
  306. Vault: &esv1.VaultProvider{
  307. Auth: &auth,
  308. ClientTLS: tt.args.clientTLS,
  309. Version: tt.args.version,
  310. CheckAndSet: tt.args.checkAndSet,
  311. },
  312. },
  313. },
  314. }
  315. if _, err := c.ValidateStore(store); (err != nil) != tt.wantErr {
  316. t.Errorf("connector.ValidateStore() error = %v, wantErr %v", err, tt.wantErr)
  317. }
  318. })
  319. }
  320. }