-
-
Notifications
You must be signed in to change notification settings - Fork 739
/
validate.go
100 lines (87 loc) · 3 KB
/
validate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
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 conjur provides a Conjur provider for External Secrets.
package conjur
import (
"fmt"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
"github.com/external-secrets/external-secrets/pkg/provider/conjur/util"
"github.com/external-secrets/external-secrets/pkg/utils"
)
// ValidateStore validates the store.
func (p *Provider) ValidateStore(store esv1beta1.GenericStore) (admission.Warnings, error) {
prov, err := util.GetConjurProvider(store)
if err != nil {
return nil, err
}
if prov.URL == "" {
return nil, fmt.Errorf("conjur URL cannot be empty")
}
if prov.Auth.APIKey != nil {
err := validateAPIKeyStore(store, *prov.Auth.APIKey)
if err != nil {
return nil, err
}
}
if prov.Auth.Jwt != nil {
err := validateJWTStore(store, *prov.Auth.Jwt)
if err != nil {
return nil, err
}
}
// At least one auth must be configured
if prov.Auth.APIKey == nil && prov.Auth.Jwt == nil {
return nil, fmt.Errorf("missing Auth.* configuration")
}
return nil, nil
}
func validateAPIKeyStore(store esv1beta1.GenericStore, auth esv1beta1.ConjurAPIKey) error {
if auth.Account == "" {
return fmt.Errorf("missing Auth.ApiKey.Account")
}
if auth.UserRef == nil {
return fmt.Errorf("missing Auth.Apikey.UserRef")
}
if auth.APIKeyRef == nil {
return fmt.Errorf("missing Auth.Apikey.ApiKeyRef")
}
if err := utils.ValidateReferentSecretSelector(store, *auth.UserRef); err != nil {
return fmt.Errorf("invalid Auth.Apikey.UserRef: %w", err)
}
if err := utils.ValidateReferentSecretSelector(store, *auth.APIKeyRef); err != nil {
return fmt.Errorf("invalid Auth.Apikey.ApiKeyRef: %w", err)
}
return nil
}
func validateJWTStore(store esv1beta1.GenericStore, auth esv1beta1.ConjurJWT) error {
if auth.Account == "" {
return fmt.Errorf("missing Auth.Jwt.Account")
}
if auth.ServiceID == "" {
return fmt.Errorf("missing Auth.Jwt.ServiceID")
}
if auth.ServiceAccountRef == nil && auth.SecretRef == nil {
return fmt.Errorf("must specify Auth.Jwt.SecretRef or Auth.Jwt.ServiceAccountRef")
}
if auth.SecretRef != nil {
if err := utils.ValidateReferentSecretSelector(store, *auth.SecretRef); err != nil {
return fmt.Errorf("invalid Auth.Jwt.SecretRef: %w", err)
}
}
if auth.ServiceAccountRef != nil {
if err := utils.ValidateReferentServiceAccountSelector(store, *auth.ServiceAccountRef); err != nil {
return fmt.Errorf("invalid Auth.Jwt.ServiceAccountRef: %w", err)
}
}
return nil
}