validate_test.go 6.8 KB

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