@@ -13,8 +13,6 @@ import (
13
13
adminv20241113001 "go.mongodb.org/atlas-sdk/v20241113001/admin"
14
14
"go.mongodb.org/atlas/mongodbatlas"
15
15
"go.uber.org/zap"
16
- corev1 "k8s.io/api/core/v1"
17
- "sigs.k8s.io/controller-runtime/pkg/client"
18
16
19
17
"github.com/mongodb/mongodb-atlas-kubernetes/v2/api"
20
18
akov2 "github.com/mongodb/mongodb-atlas-kubernetes/v2/api/v1"
@@ -25,14 +23,11 @@ import (
25
23
26
24
const (
27
25
govAtlasDomain = "mongodbgov.com"
28
- orgIDKey = "orgId"
29
- publicAPIKey = "publicApiKey"
30
- privateAPIKey = "privateApiKey"
31
26
)
32
27
33
28
type Provider interface {
34
- Client (ctx context.Context , secretRef * client. ObjectKey , log * zap.SugaredLogger ) (* mongodbatlas.Client , string , error )
35
- SdkClientSet (ctx context.Context , secretRef * client. ObjectKey , log * zap.SugaredLogger ) (* ClientSet , string , error )
29
+ Client (ctx context.Context , creds * Credentials , log * zap.SugaredLogger ) (* mongodbatlas.Client , error )
30
+ SdkClientSet (ctx context.Context , creds * Credentials , log * zap.SugaredLogger ) (* ClientSet , error )
36
31
IsCloudGov () bool
37
32
IsResourceSupported (resource api.AtlasCustomResource ) bool
38
33
}
@@ -43,24 +38,33 @@ type ClientSet struct {
43
38
}
44
39
45
40
type ProductionProvider struct {
46
- k8sClient client.Client
47
- domain string
48
- globalSecretRef client.ObjectKey
49
- dryRun bool
41
+ domain string
42
+ dryRun bool
50
43
}
51
44
52
- type credentialsSecret struct {
53
- OrgID string
45
+ // ConnectionConfig is the type that contains connection configuration to Atlas, including credentials.
46
+ type ConnectionConfig struct {
47
+ OrgID string
48
+ Credentials * Credentials
49
+ }
50
+
51
+ // Credentials is the type that holds credentials to authenticate against the Atlas API.
52
+ // Currently, only API keys are support but more credential types could be added,
53
+ // see https://www.mongodb.com/docs/atlas/configure-api-access/.
54
+ type Credentials struct {
55
+ APIKeys * APIKeys
56
+ }
57
+
58
+ // APIKeys is the type that holds Public/Private API keys to authenticate against the Atlas API.
59
+ type APIKeys struct {
54
60
PublicKey string
55
61
PrivateKey string
56
62
}
57
63
58
- func NewProductionProvider (atlasDomain string , globalSecretRef client. ObjectKey , k8sClient client. Client , dryRun bool ) * ProductionProvider {
64
+ func NewProductionProvider (atlasDomain string , dryRun bool ) * ProductionProvider {
59
65
return & ProductionProvider {
60
- k8sClient : k8sClient ,
61
- domain : atlasDomain ,
62
- globalSecretRef : globalSecretRef ,
63
- dryRun : dryRun ,
66
+ domain : atlasDomain ,
67
+ dryRun : dryRun ,
64
68
}
65
69
}
66
70
@@ -102,35 +106,25 @@ func (p *ProductionProvider) IsResourceSupported(resource api.AtlasCustomResourc
102
106
return false
103
107
}
104
108
105
- func (p * ProductionProvider ) Client (ctx context.Context , secretRef * client.ObjectKey , log * zap.SugaredLogger ) (* mongodbatlas.Client , string , error ) {
106
- secretData , err := getSecrets (ctx , p .k8sClient , secretRef , & p .globalSecretRef )
107
- if err != nil {
108
- return nil , "" , err
109
- }
110
-
109
+ func (p * ProductionProvider ) Client (ctx context.Context , creds * Credentials , log * zap.SugaredLogger ) (* mongodbatlas.Client , error ) {
111
110
clientCfg := []httputil.ClientOpt {
112
- httputil .Digest (secretData . PublicKey , secretData .PrivateKey ),
111
+ httputil .Digest (creds . APIKeys . PublicKey , creds . APIKeys .PrivateKey ),
113
112
httputil .LoggingTransport (log ),
114
113
}
115
114
116
115
transport := p .newDryRunTransport (http .DefaultTransport )
117
116
httpClient , err := httputil .DecorateClient (& http.Client {Transport : transport }, clientCfg ... )
118
117
if err != nil {
119
- return nil , "" , err
118
+ return nil , err
120
119
}
121
120
122
121
c , err := mongodbatlas .New (httpClient , mongodbatlas .SetBaseURL (p .domain ), mongodbatlas .SetUserAgent (operatorUserAgent ()))
123
122
124
- return c , secretData . OrgID , err
123
+ return c , err
125
124
}
126
125
127
- func (p * ProductionProvider ) SdkClientSet (ctx context.Context , secretRef * client.ObjectKey , log * zap.SugaredLogger ) (* ClientSet , string , error ) {
128
- secretData , err := getSecrets (ctx , p .k8sClient , secretRef , & p .globalSecretRef )
129
- if err != nil {
130
- return nil , "" , err
131
- }
132
-
133
- var transport http.RoundTripper = digest .NewTransport (secretData .PublicKey , secretData .PrivateKey )
126
+ func (p * ProductionProvider ) SdkClientSet (ctx context.Context , creds * Credentials , log * zap.SugaredLogger ) (* ClientSet , error ) {
127
+ var transport http.RoundTripper = digest .NewTransport (creds .APIKeys .PublicKey , creds .APIKeys .PrivateKey )
134
128
transport = p .newDryRunTransport (transport )
135
129
transport = httputil .NewLoggingTransport (log , false , transport )
136
130
@@ -141,21 +135,21 @@ func (p *ProductionProvider) SdkClientSet(ctx context.Context, secretRef *client
141
135
adminv20231115008 .UseHTTPClient (httpClient ),
142
136
adminv20231115008 .UseUserAgent (operatorUserAgent ()))
143
137
if err != nil {
144
- return nil , "" , err
138
+ return nil , err
145
139
}
146
140
147
141
clientv20241113001 , err := adminv20241113001 .NewClient (
148
142
adminv20241113001 .UseBaseURL (p .domain ),
149
143
adminv20241113001 .UseHTTPClient (httpClient ),
150
144
adminv20241113001 .UseUserAgent (operatorUserAgent ()))
151
145
if err != nil {
152
- return nil , "" , err
146
+ return nil , err
153
147
}
154
148
155
149
return & ClientSet {
156
150
SdkClient20231115008 : clientv20231115008 ,
157
151
SdkClient20241113001 : clientv20241113001 ,
158
- }, secretData . OrgID , nil
152
+ }, nil
159
153
}
160
154
161
155
func (p * ProductionProvider ) newDryRunTransport (delegate http.RoundTripper ) http.RoundTripper {
@@ -166,51 +160,6 @@ func (p *ProductionProvider) newDryRunTransport(delegate http.RoundTripper) http
166
160
return delegate
167
161
}
168
162
169
- func getSecrets (ctx context.Context , k8sClient client.Client , secretRef , fallbackRef * client.ObjectKey ) (* credentialsSecret , error ) {
170
- if secretRef == nil {
171
- secretRef = fallbackRef
172
- }
173
-
174
- secret := & corev1.Secret {}
175
- if err := k8sClient .Get (ctx , * secretRef , secret ); err != nil {
176
- return nil , fmt .Errorf ("failed to read Atlas API credentials from the secret %s: %w" , secretRef .String (), err )
177
- }
178
-
179
- secretData := credentialsSecret {
180
- OrgID : string (secret .Data [orgIDKey ]),
181
- PublicKey : string (secret .Data [publicAPIKey ]),
182
- PrivateKey : string (secret .Data [privateAPIKey ]),
183
- }
184
-
185
- if missingFields , valid := validateSecretData (& secretData ); ! valid {
186
- return nil , fmt .Errorf ("the following fields are missing in the secret %v: %v" , secretRef , missingFields )
187
- }
188
-
189
- return & secretData , nil
190
- }
191
-
192
- func validateSecretData (secretData * credentialsSecret ) ([]string , bool ) {
193
- missingFields := make ([]string , 0 , 3 )
194
-
195
- if secretData .OrgID == "" {
196
- missingFields = append (missingFields , orgIDKey )
197
- }
198
-
199
- if secretData .PublicKey == "" {
200
- missingFields = append (missingFields , publicAPIKey )
201
- }
202
-
203
- if secretData .PrivateKey == "" {
204
- missingFields = append (missingFields , privateAPIKey )
205
- }
206
-
207
- if len (missingFields ) > 0 {
208
- return missingFields , false
209
- }
210
-
211
- return nil , true
212
- }
213
-
214
163
func operatorUserAgent () string {
215
164
return fmt .Sprintf ("%s/%s (%s;%s)" , "MongoDBAtlasKubernetesOperator" , version .Version , runtime .GOOS , runtime .GOARCH )
216
165
}
0 commit comments