-
-
Notifications
You must be signed in to change notification settings - Fork 790
/
provider.go
172 lines (141 loc) · 4.8 KB
/
provider.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
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 scaleway
import (
"context"
"fmt"
smapi "github.com/scaleway/scaleway-sdk-go/api/secret/v1beta1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/scaleway-sdk-go/validation"
ctrl "sigs.k8s.io/controller-runtime"
kubeClient "sigs.k8s.io/controller-runtime/pkg/client"
"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/utils"
"github.com/external-secrets/external-secrets/pkg/utils/resolvers"
)
var (
defaultAPIURL = "https://api.scaleway.com"
log = ctrl.Log.WithName("provider").WithName("scaleway")
)
type Provider struct{}
// Capabilities return the provider supported capabilities (ReadOnly, WriteOnly, ReadWrite).
func (p *Provider) Capabilities() esv1beta1.SecretStoreCapabilities {
return esv1beta1.SecretStoreReadWrite
}
func (p *Provider) NewClient(ctx context.Context, store esv1beta1.GenericStore, kube kubeClient.Client, namespace string) (esv1beta1.SecretsClient, error) {
cfg, err := getConfig(store)
if err != nil {
return nil, err
}
if store.GetKind() == esv1beta1.ClusterSecretStoreKind && doesConfigDependOnNamespace(cfg) {
// we are not attached to a specific namespace, but some config values are dependent on it
return nil, fmt.Errorf("when using a ClusterSecretStore, namespaces must be explicitly set")
}
accessKey, err := loadConfigSecret(ctx, cfg.AccessKey, kube, namespace, store.GetKind())
if err != nil {
return nil, err
}
secretKey, err := loadConfigSecret(ctx, cfg.SecretKey, kube, namespace, store.GetKind())
if err != nil {
return nil, err
}
scwClient, err := scw.NewClient(
scw.WithAPIURL(cfg.APIURL),
scw.WithDefaultRegion(scw.Region(cfg.Region)),
scw.WithDefaultProjectID(cfg.ProjectID),
scw.WithAuth(accessKey, secretKey),
scw.WithUserAgent("external-secrets"),
)
if err != nil {
return nil, err
}
return &client{
api: smapi.NewAPI(scwClient),
projectID: cfg.ProjectID,
cache: newCache(),
}, nil
}
func loadConfigSecret(ctx context.Context, ref *esv1beta1.ScalewayProviderSecretRef, kube kubeClient.Client, defaultNamespace, storeKind string) (string, error) {
if ref.SecretRef == nil {
return ref.Value, nil
}
return resolvers.SecretKeyRef(
ctx,
kube,
storeKind,
defaultNamespace,
ref.SecretRef,
)
}
func validateSecretRef(store esv1beta1.GenericStore, ref *esv1beta1.ScalewayProviderSecretRef) error {
if ref.SecretRef != nil {
if ref.Value != "" {
return fmt.Errorf("cannot specify both secret reference and value")
}
err := utils.ValidateReferentSecretSelector(store, *ref.SecretRef)
if err != nil {
return err
}
} else if ref.Value == "" {
return fmt.Errorf("must specify either secret reference or direct value")
}
return nil
}
func doesConfigDependOnNamespace(cfg *esv1beta1.ScalewayProvider) bool {
if cfg.AccessKey.SecretRef != nil && cfg.AccessKey.SecretRef.Namespace == nil {
return true
}
if cfg.SecretKey.SecretRef != nil && cfg.SecretKey.SecretRef.Namespace == nil {
return true
}
return false
}
func getConfig(store esv1beta1.GenericStore) (*esv1beta1.ScalewayProvider, error) {
if store == nil {
return nil, fmt.Errorf("missing store specification")
}
storeSpec := store.GetSpec()
if storeSpec == nil || storeSpec.Provider == nil || storeSpec.Provider.Scaleway == nil {
return nil, fmt.Errorf("invalid specification for scaleway provider")
}
cfg := storeSpec.Provider.Scaleway
if cfg.APIURL == "" {
cfg.APIURL = defaultAPIURL
} else if !validation.IsURL(cfg.APIURL) {
return nil, fmt.Errorf("invalid api url: %q", cfg.APIURL)
}
if !validation.IsRegion(cfg.Region) {
return nil, fmt.Errorf("invalid region: %q", cfg.Region)
}
if !validation.IsProjectID(cfg.ProjectID) {
return nil, fmt.Errorf("invalid project id: %q", cfg.ProjectID)
}
err := validateSecretRef(store, cfg.AccessKey)
if err != nil {
return nil, err
}
err = validateSecretRef(store, cfg.SecretKey)
if err != nil {
return nil, err
}
return cfg, nil
}
func (p *Provider) ValidateStore(store esv1beta1.GenericStore) (admission.Warnings, error) {
_, err := getConfig(store)
return nil, err
}
func init() {
esv1beta1.Register(&Provider{}, &esv1beta1.SecretStoreProvider{
Scaleway: &esv1beta1.ScalewayProvider{},
})
}