validate_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  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 esv1beta1.VaultAuth
  23. clientTLS esv1beta1.VaultClientTLS
  24. }
  25. tests := []struct {
  26. name string
  27. args args
  28. wantErr bool
  29. }{
  30. {
  31. name: "empty auth",
  32. args: args{},
  33. },
  34. {
  35. name: "invalid approle with namespace",
  36. args: args{
  37. auth: esv1beta1.VaultAuth{
  38. AppRole: &esv1beta1.VaultAppRole{
  39. SecretRef: esmeta.SecretKeySelector{
  40. Namespace: pointer.To("invalid"),
  41. },
  42. },
  43. },
  44. },
  45. wantErr: true,
  46. },
  47. {
  48. name: "invalid approle with roleId and no roleRef",
  49. args: args{
  50. auth: esv1beta1.VaultAuth{
  51. AppRole: &esv1beta1.VaultAppRole{
  52. RoleID: "",
  53. RoleRef: nil,
  54. },
  55. },
  56. },
  57. wantErr: true,
  58. },
  59. {
  60. name: "valid approle with roleId and no roleRef",
  61. args: args{
  62. auth: esv1beta1.VaultAuth{
  63. AppRole: &esv1beta1.VaultAppRole{
  64. RoleID: fakeValidationValue,
  65. },
  66. },
  67. },
  68. wantErr: false,
  69. },
  70. {
  71. name: "valid approle with roleId and no roleRef",
  72. args: args{
  73. auth: esv1beta1.VaultAuth{
  74. AppRole: &esv1beta1.VaultAppRole{
  75. RoleRef: &esmeta.SecretKeySelector{
  76. Name: fakeValidationValue,
  77. },
  78. },
  79. },
  80. },
  81. wantErr: false,
  82. },
  83. {
  84. name: "invalid clientcert",
  85. args: args{
  86. auth: esv1beta1.VaultAuth{
  87. Cert: &esv1beta1.VaultCertAuth{
  88. ClientCert: esmeta.SecretKeySelector{
  89. Namespace: pointer.To("invalid"),
  90. },
  91. },
  92. },
  93. },
  94. wantErr: true,
  95. },
  96. {
  97. name: "invalid cert secret",
  98. args: args{
  99. auth: esv1beta1.VaultAuth{
  100. Cert: &esv1beta1.VaultCertAuth{
  101. SecretRef: esmeta.SecretKeySelector{
  102. Namespace: pointer.To("invalid"),
  103. },
  104. },
  105. },
  106. },
  107. wantErr: true,
  108. },
  109. {
  110. name: "invalid jwt secret",
  111. args: args{
  112. auth: esv1beta1.VaultAuth{
  113. Jwt: &esv1beta1.VaultJwtAuth{
  114. SecretRef: &esmeta.SecretKeySelector{
  115. Namespace: pointer.To("invalid"),
  116. },
  117. },
  118. },
  119. },
  120. wantErr: true,
  121. },
  122. {
  123. name: "invalid kubernetes sa",
  124. args: args{
  125. auth: esv1beta1.VaultAuth{
  126. Kubernetes: &esv1beta1.VaultKubernetesAuth{
  127. ServiceAccountRef: &esmeta.ServiceAccountSelector{
  128. Namespace: pointer.To("invalid"),
  129. },
  130. },
  131. },
  132. },
  133. wantErr: true,
  134. },
  135. {
  136. name: "invalid kubernetes secret",
  137. args: args{
  138. auth: esv1beta1.VaultAuth{
  139. Kubernetes: &esv1beta1.VaultKubernetesAuth{
  140. SecretRef: &esmeta.SecretKeySelector{
  141. Namespace: pointer.To("invalid"),
  142. },
  143. },
  144. },
  145. },
  146. wantErr: true,
  147. },
  148. {
  149. name: "invalid ldap secret",
  150. args: args{
  151. auth: esv1beta1.VaultAuth{
  152. Ldap: &esv1beta1.VaultLdapAuth{
  153. SecretRef: esmeta.SecretKeySelector{
  154. Namespace: pointer.To("invalid"),
  155. },
  156. },
  157. },
  158. },
  159. wantErr: true,
  160. },
  161. {
  162. name: "invalid userpass secret",
  163. args: args{
  164. auth: esv1beta1.VaultAuth{
  165. UserPass: &esv1beta1.VaultUserPassAuth{
  166. SecretRef: esmeta.SecretKeySelector{
  167. Namespace: pointer.To("invalid"),
  168. },
  169. },
  170. },
  171. },
  172. wantErr: true,
  173. },
  174. {
  175. name: "invalid token secret",
  176. args: args{
  177. auth: esv1beta1.VaultAuth{
  178. TokenSecretRef: &esmeta.SecretKeySelector{
  179. Namespace: pointer.To("invalid"),
  180. },
  181. },
  182. },
  183. wantErr: true,
  184. },
  185. {
  186. name: "valid clientTls config",
  187. args: args{
  188. auth: esv1beta1.VaultAuth{
  189. AppRole: &esv1beta1.VaultAppRole{
  190. RoleRef: &esmeta.SecretKeySelector{
  191. Name: fakeValidationValue,
  192. },
  193. },
  194. },
  195. clientTLS: esv1beta1.VaultClientTLS{
  196. CertSecretRef: &esmeta.SecretKeySelector{
  197. Name: "tls-auth-certs",
  198. },
  199. KeySecretRef: &esmeta.SecretKeySelector{
  200. Name: "tls-auth-certs",
  201. },
  202. },
  203. },
  204. wantErr: false,
  205. },
  206. {
  207. name: "invalid clientTls config, missing SecretRef",
  208. args: args{
  209. auth: esv1beta1.VaultAuth{
  210. AppRole: &esv1beta1.VaultAppRole{
  211. RoleRef: &esmeta.SecretKeySelector{
  212. Name: fakeValidationValue,
  213. },
  214. },
  215. },
  216. clientTLS: esv1beta1.VaultClientTLS{
  217. CertSecretRef: &esmeta.SecretKeySelector{
  218. Name: "tls-auth-certs",
  219. },
  220. },
  221. },
  222. wantErr: true,
  223. },
  224. {
  225. name: "invalid clientTls config, missing ClientCert",
  226. args: args{
  227. auth: esv1beta1.VaultAuth{
  228. AppRole: &esv1beta1.VaultAppRole{
  229. RoleRef: &esmeta.SecretKeySelector{
  230. Name: fakeValidationValue,
  231. },
  232. },
  233. },
  234. clientTLS: esv1beta1.VaultClientTLS{
  235. KeySecretRef: &esmeta.SecretKeySelector{
  236. Name: "tls-auth-certs",
  237. },
  238. },
  239. },
  240. wantErr: true,
  241. },
  242. }
  243. for _, tt := range tests {
  244. t.Run(tt.name, func(t *testing.T) {
  245. c := &Provider{
  246. NewVaultClient: nil,
  247. }
  248. store := &esv1beta1.SecretStore{
  249. Spec: esv1beta1.SecretStoreSpec{
  250. Provider: &esv1beta1.SecretStoreProvider{
  251. Vault: &esv1beta1.VaultProvider{
  252. Auth: tt.args.auth,
  253. ClientTLS: tt.args.clientTLS,
  254. },
  255. },
  256. },
  257. }
  258. if _, err := c.ValidateStore(store); (err != nil) != tt.wantErr {
  259. t.Errorf("connector.ValidateStore() error = %v, wantErr %v", err, tt.wantErr)
  260. }
  261. })
  262. }
  263. }