| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- /*
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- package vault
- import (
- "testing"
- pointer "k8s.io/utils/ptr"
- esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
- esmeta "github.com/external-secrets/external-secrets/apis/meta/v1"
- )
- const fakeValidationValue = "fake-value"
- func TestValidateStore(t *testing.T) {
- type args struct {
- auth esv1.VaultAuth
- clientTLS esv1.VaultClientTLS
- version esv1.VaultKVStoreVersion
- checkAndSet *esv1.VaultCheckAndSet
- }
- tests := []struct {
- name string
- args args
- wantErr bool
- }{
- {
- name: "empty auth",
- args: args{},
- },
- {
- name: "invalid approle with namespace",
- args: args{
- auth: esv1.VaultAuth{
- AppRole: &esv1.VaultAppRole{
- SecretRef: esmeta.SecretKeySelector{
- Namespace: pointer.To("invalid"),
- },
- },
- },
- },
- wantErr: true,
- },
- {
- name: "invalid approle with roleId and no roleRef",
- args: args{
- auth: esv1.VaultAuth{
- AppRole: &esv1.VaultAppRole{
- RoleID: "",
- RoleRef: nil,
- },
- },
- },
- wantErr: true,
- },
- {
- name: "valid approle with roleId and no roleRef",
- args: args{
- auth: esv1.VaultAuth{
- AppRole: &esv1.VaultAppRole{
- RoleID: fakeValidationValue,
- },
- },
- },
- wantErr: false,
- },
- {
- name: "valid approle with roleId and no roleRef",
- args: args{
- auth: esv1.VaultAuth{
- AppRole: &esv1.VaultAppRole{
- RoleRef: &esmeta.SecretKeySelector{
- Name: fakeValidationValue,
- },
- },
- },
- },
- wantErr: false,
- },
- {
- name: "invalid clientcert",
- args: args{
- auth: esv1.VaultAuth{
- Cert: &esv1.VaultCertAuth{
- ClientCert: esmeta.SecretKeySelector{
- Namespace: pointer.To("invalid"),
- },
- },
- },
- },
- wantErr: true,
- },
- {
- name: "invalid cert secret",
- args: args{
- auth: esv1.VaultAuth{
- Cert: &esv1.VaultCertAuth{
- SecretRef: esmeta.SecretKeySelector{
- Namespace: pointer.To("invalid"),
- },
- },
- },
- },
- wantErr: true,
- },
- {
- name: "invalid jwt secret",
- args: args{
- auth: esv1.VaultAuth{
- Jwt: &esv1.VaultJwtAuth{
- SecretRef: &esmeta.SecretKeySelector{
- Namespace: pointer.To("invalid"),
- },
- },
- },
- },
- wantErr: true,
- },
- {
- name: "invalid kubernetes sa",
- args: args{
- auth: esv1.VaultAuth{
- Kubernetes: &esv1.VaultKubernetesAuth{
- ServiceAccountRef: &esmeta.ServiceAccountSelector{
- Namespace: pointer.To("invalid"),
- },
- },
- },
- },
- wantErr: true,
- },
- {
- name: "invalid kubernetes secret",
- args: args{
- auth: esv1.VaultAuth{
- Kubernetes: &esv1.VaultKubernetesAuth{
- SecretRef: &esmeta.SecretKeySelector{
- Namespace: pointer.To("invalid"),
- },
- },
- },
- },
- wantErr: true,
- },
- {
- name: "invalid ldap secret",
- args: args{
- auth: esv1.VaultAuth{
- Ldap: &esv1.VaultLdapAuth{
- SecretRef: esmeta.SecretKeySelector{
- Namespace: pointer.To("invalid"),
- },
- },
- },
- },
- wantErr: true,
- },
- {
- name: "invalid userpass secret",
- args: args{
- auth: esv1.VaultAuth{
- UserPass: &esv1.VaultUserPassAuth{
- SecretRef: esmeta.SecretKeySelector{
- Namespace: pointer.To("invalid"),
- },
- },
- },
- },
- wantErr: true,
- },
- {
- name: "invalid token secret",
- args: args{
- auth: esv1.VaultAuth{
- TokenSecretRef: &esmeta.SecretKeySelector{
- Namespace: pointer.To("invalid"),
- },
- },
- },
- wantErr: true,
- },
- {
- name: "valid clientTls config",
- args: args{
- auth: esv1.VaultAuth{
- AppRole: &esv1.VaultAppRole{
- RoleRef: &esmeta.SecretKeySelector{
- Name: fakeValidationValue,
- },
- },
- },
- clientTLS: esv1.VaultClientTLS{
- CertSecretRef: &esmeta.SecretKeySelector{
- Name: "tls-auth-certs",
- },
- KeySecretRef: &esmeta.SecretKeySelector{
- Name: "tls-auth-certs",
- },
- },
- },
- wantErr: false,
- },
- {
- name: "invalid clientTls config, missing SecretRef",
- args: args{
- auth: esv1.VaultAuth{
- AppRole: &esv1.VaultAppRole{
- RoleRef: &esmeta.SecretKeySelector{
- Name: fakeValidationValue,
- },
- },
- },
- clientTLS: esv1.VaultClientTLS{
- CertSecretRef: &esmeta.SecretKeySelector{
- Name: "tls-auth-certs",
- },
- },
- },
- wantErr: true,
- },
- {
- name: "invalid clientTls config, missing ClientCert",
- args: args{
- auth: esv1.VaultAuth{
- AppRole: &esv1.VaultAppRole{
- RoleRef: &esmeta.SecretKeySelector{
- Name: fakeValidationValue,
- },
- },
- },
- clientTLS: esv1.VaultClientTLS{
- KeySecretRef: &esmeta.SecretKeySelector{
- Name: "tls-auth-certs",
- },
- },
- },
- wantErr: true,
- },
- {
- name: "valid CAS config with KV v2",
- args: args{
- auth: esv1.VaultAuth{
- AppRole: &esv1.VaultAppRole{
- RoleRef: &esmeta.SecretKeySelector{
- Name: fakeValidationValue,
- },
- },
- },
- version: esv1.VaultKVStoreV2,
- checkAndSet: &esv1.VaultCheckAndSet{
- Required: true,
- },
- },
- wantErr: false,
- },
- {
- name: "invalid CAS config with KV v1",
- args: args{
- auth: esv1.VaultAuth{
- AppRole: &esv1.VaultAppRole{
- RoleRef: &esmeta.SecretKeySelector{
- Name: fakeValidationValue,
- },
- },
- },
- version: esv1.VaultKVStoreV1,
- checkAndSet: &esv1.VaultCheckAndSet{
- Required: true,
- },
- },
- wantErr: true,
- },
- {
- name: "CAS config not required is valid with any version",
- args: args{
- auth: esv1.VaultAuth{
- AppRole: &esv1.VaultAppRole{
- RoleRef: &esmeta.SecretKeySelector{
- Name: fakeValidationValue,
- },
- },
- },
- version: esv1.VaultKVStoreV1,
- checkAndSet: &esv1.VaultCheckAndSet{
- Required: false,
- },
- },
- wantErr: false,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- c := &Provider{
- NewVaultClient: nil,
- }
- auth := tt.args.auth
- store := &esv1.SecretStore{
- Spec: esv1.SecretStoreSpec{
- Provider: &esv1.SecretStoreProvider{
- Vault: &esv1.VaultProvider{
- Auth: &auth,
- ClientTLS: tt.args.clientTLS,
- Version: tt.args.version,
- CheckAndSet: tt.args.checkAndSet,
- },
- },
- },
- }
- if _, err := c.ValidateStore(store); (err != nil) != tt.wantErr {
- t.Errorf("connector.ValidateStore() error = %v, wantErr %v", err, tt.wantErr)
- }
- })
- }
- }
|