/
rancher.go
526 lines (458 loc) · 19.3 KB
/
rancher.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
// Copyright (c) 2022, 2023, Oracle and/or its affiliates.
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
package pkg
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
urlpkg "net/url"
"strconv"
"strings"
"github.com/hashicorp/go-retryablehttp"
"github.com/onsi/gomega"
"github.com/verrazzano/verrazzano/pkg/constants"
"github.com/verrazzano/verrazzano/pkg/httputil"
"github.com/verrazzano/verrazzano/pkg/k8sutil"
"github.com/verrazzano/verrazzano/pkg/rancherutil"
"github.com/verrazzano/verrazzano/platform-operator/controllers/verrazzano/component/common"
"github.com/verrazzano/verrazzano/platform-operator/controllers/verrazzano/component/rancher"
"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
)
type Payload struct {
ClusterID string `json:"clusterID"`
TTL int `json:"ttl"`
}
type TokenPostResponse struct {
Token string `json:"token"`
Created string `json:"created"`
}
type ListOfTokenOutputFromRancher struct {
Data []struct {
ClusterID string `json:"clusterId"`
Name string `json:"name"`
} `json:"data"`
}
func EventuallyGetURLForIngress(log *zap.SugaredLogger, api *APIEndpoint, namespace string, name string, scheme string) string {
ingressHost := EventuallyGetIngressHost(log, api, namespace, name)
gomega.Expect(ingressHost).ToNot(gomega.BeEmpty())
return fmt.Sprintf("%s://%s", scheme, ingressHost)
}
func EventuallyGetIngressHost(log *zap.SugaredLogger, api *APIEndpoint, namespace string, name string) string {
var ingressHost string
gomega.Eventually(func() error {
ingress, err := api.GetIngress(namespace, name)
if err != nil {
return err
}
if len(ingress.Spec.Rules) == 0 {
return fmt.Errorf("no rules found in ingress %s/%s", namespace, name)
}
ingressHost = ingress.Spec.Rules[0].Host
log.Info(fmt.Sprintf("Found ingress host: %s", ingressHost))
return nil
}, waitTimeout, pollingInterval).Should(gomega.BeNil())
return ingressHost
}
func GetURLForIngress(log *zap.SugaredLogger, api *APIEndpoint, namespace string, name string, scheme string) (string, error) {
ingress, err := api.GetIngress(namespace, name)
if err != nil {
return "", err
}
ingressURL := fmt.Sprintf("%s://%s", scheme, ingress.Spec.Rules[0].Host)
log.Info(fmt.Sprintf("Found ingress URL: %s", ingressURL))
return ingressURL, err
}
func eventuallyGetRancherAdminPassword(log *zap.SugaredLogger) (string, error) {
var err error
var secret *corev1.Secret
gomega.Eventually(func() error {
secret, err = GetSecret("cattle-system", "rancher-admin-secret")
if err != nil {
log.Error(fmt.Sprintf("Error getting rancher-admin-secret, retrying: %v", err))
}
return err
}, waitTimeout, pollingInterval).Should(gomega.BeNil())
if secret == nil {
return "", fmt.Errorf("Unable to get rancher admin secret")
}
var rancherAdminPassword []byte
var ok bool
if rancherAdminPassword, ok = secret.Data["password"]; !ok {
return "", fmt.Errorf("Error getting rancher admin credentials")
}
return string(rancherAdminPassword), nil
}
func GetRancherAdminToken(log *zap.SugaredLogger, httpClient *retryablehttp.Client, rancherURL string) string {
rancherAdminPassword, err := eventuallyGetRancherAdminPassword(log)
if err != nil {
log.Error(fmt.Sprintf("Error getting rancher admin password: %v", err))
return ""
}
token, err := getRancherUserToken(log, httpClient, rancherURL, "admin", string(rancherAdminPassword))
if err != nil {
log.Error(fmt.Sprintf("Error getting user token from rancher: %v", err))
return ""
}
return token
}
func getRancherUserToken(log *zap.SugaredLogger, httpClient *retryablehttp.Client, rancherURL string, username string, password string) (string, error) {
rancherLoginURL := fmt.Sprintf("%s/%s", rancherURL, "v3-public/localProviders/local?action=login")
payload := `{"Username": "` + username + `", "Password": "` + password + `"}`
response, err := httpClient.Post(rancherLoginURL, "application/json", strings.NewReader(payload))
if err != nil {
log.Error(fmt.Sprintf("Error getting rancher admin token: %v", err))
return "", err
}
err = httputil.ValidateResponseCode(response, http.StatusCreated)
if err != nil {
log.Errorf("Invalid response code when fetching Rancher token: %v", err)
return "", err
}
defer response.Body.Close()
// extract the response body
body, err := io.ReadAll(response.Body)
if err != nil {
log.Errorf("Failed to read Rancher token response: %v", err)
return "", err
}
token, err := httputil.ExtractFieldFromResponseBodyOrReturnError(string(body), "token", "unable to find token in Rancher response")
if err != nil {
log.Errorf("Failed to extract token from Rancher response: %v", err)
return "", err
}
return token, nil
}
// This function adds an access token to Rancher gven that a ttl and clusterID string is provided
func AddAccessTokenToRancherForLoggedInUser(log *zap.SugaredLogger, adminKubeConfig, managedClusterName, usernameForRancher, ttl string) (string, error) {
responseBody, err := ExecutePostRequestToAddAToken(log, adminKubeConfig, managedClusterName, usernameForRancher, ttl)
if err != nil {
return "", err
}
var tokenPostResponse TokenPostResponse
err = json.Unmarshal(responseBody, &tokenPostResponse)
if err != nil {
return "", err
}
return tokenPostResponse.Created, nil
}
// This function returns the list of token names that correspond to the cluster ID and this user before when this is called
// If no error occurs, this means that these tokens were found and deleted in Rancher
func GetAndDeleteTokenNamesForLoggedInUserBasedOnClusterID(log *zap.SugaredLogger, adminKubeConfig, managedClusterName, usernameForRancher string) error {
responseBody, err := ExecuteGetRequestToReturnAllTokens(log, adminKubeConfig, managedClusterName, usernameForRancher)
if err != nil {
return err
}
var listOfTokenOutputFromRancher = ListOfTokenOutputFromRancher{}
err = json.Unmarshal(responseBody, &listOfTokenOutputFromRancher)
if err != nil {
return err
}
listOfTokens := listOfTokenOutputFromRancher.Data
clusterID, err := getClusterIDForManagedCluster(adminKubeConfig, managedClusterName)
if err != nil {
return err
}
for _, token := range listOfTokens {
//Check that it is not the same name as the user access token and it has the same cluster ID
if token.ClusterID != clusterID {
continue
}
err = ExecuteDeleteRequestForToken(log, adminKubeConfig, managedClusterName, usernameForRancher, token.Name)
if err != nil {
return err
}
}
return nil
}
// VerifyRancherAccess verifies that Rancher is accessible.
func VerifyRancherAccess(log *zap.SugaredLogger) error {
kubeconfigPath, err := k8sutil.GetKubeConfigLocation()
if err != nil {
log.Error(fmt.Sprintf("Error getting kubeconfig: %v", err))
return err
}
api := EventuallyGetAPIEndpoint(kubeconfigPath)
rancherURL := EventuallyGetURLForIngress(log, api, "cattle-system", "rancher", "https")
httpClient := EventuallyVerrazzanoRetryableHTTPClient()
var httpResponse *HTTPResponse
gomega.Eventually(func() (*HTTPResponse, error) {
httpResponse, err = GetWebPageWithClient(httpClient, rancherURL, "")
return httpResponse, err
}, waitTimeout, pollingInterval).Should(HasStatus(http.StatusOK))
gomega.Expect(CheckNoServerHeader(httpResponse)).To(gomega.BeTrue(), "Found unexpected server header in response")
return nil
}
// VerifyRancherKeycloakAuthConfig verifies that Rancher/Keycloak AuthConfig is correctly populated
func VerifyRancherKeycloakAuthConfig(log *zap.SugaredLogger) error {
kubeconfigPath, err := k8sutil.GetKubeConfigLocation()
if err != nil {
log.Error(fmt.Sprintf("Error getting kubeconfig: %v", err))
return err
}
log.Info("Verify Keycloak AuthConfig")
gomega.Eventually(func() (bool, error) {
api, err := GetAPIEndpoint(kubeconfigPath)
if err != nil {
log.Error(fmt.Sprintf("Error getting API endpoint: %v", err))
return false, err
}
keycloakURL, err := GetURLForIngress(log, api, "keycloak", "keycloak", "https")
if err != nil {
log.Error(fmt.Sprintf("Error getting API endpoint: %v", err))
return false, err
}
rancherURL, err := GetURLForIngress(log, api, "cattle-system", "rancher", "https")
if err != nil {
return false, err
}
k8sClient, err := GetDynamicClientInCluster(kubeconfigPath)
if err != nil {
log.Error(fmt.Sprintf("Error getting dynamic client: %v", err))
return false, err
}
authConfigData, err := k8sClient.Resource(GvkToGvr(common.GVKAuthConfig)).Get(context.Background(), common.AuthConfigKeycloak, v1.GetOptions{})
if err != nil {
log.Error(fmt.Sprintf("error getting keycloak oidc authConfig: %v", err))
return false, err
}
authConfigAttributes := authConfigData.UnstructuredContent()
if err = verifyAuthConfigAttribute(rancher.AuthConfigKeycloakAttributeAccessMode, authConfigAttributes[rancher.AuthConfigKeycloakAttributeAccessMode].(string), rancher.AuthConfigKeycloakAccessMode); err != nil {
log.Error(err)
return false, err
}
if err = verifyAuthConfigAttribute(rancher.AuthConfigKeycloakAttributeClientID, authConfigAttributes[rancher.AuthConfigKeycloakAttributeClientID].(string), rancher.AuthConfigKeycloakClientIDRancher); err != nil {
log.Error(err)
return false, err
}
if err = verifyAuthConfigAttribute(rancher.AuthConfigKeycloakAttributeGroupSearchEnabled, authConfigAttributes[rancher.AuthConfigKeycloakAttributeGroupSearchEnabled].(bool), true); err != nil {
return false, err
}
if err = verifyAuthConfigAttribute(rancher.AuthConfigKeycloakAttributeAuthEndpoint, authConfigAttributes[rancher.AuthConfigKeycloakAttributeAuthEndpoint].(string), keycloakURL+rancher.AuthConfigKeycloakURLPathAuthEndPoint); err != nil {
log.Error(err)
return false, err
}
if err = verifyAuthConfigAttribute(rancher.AuthConfigKeycloakAttributeRancherURL, authConfigAttributes[rancher.AuthConfigKeycloakAttributeRancherURL].(string), rancherURL+rancher.AuthConfigKeycloakURLPathVerifyAuth); err != nil {
log.Error(err)
return false, err
}
authConfigClientSecret := authConfigAttributes[common.AuthConfigKeycloakAttributeClientSecret].(string)
if authConfigClientSecret == "" {
err = fmt.Errorf("keycloak auth config attribute %s not correctly configured, value is empty", common.AuthConfigKeycloakAttributeClientSecret)
log.Error(err)
return false, err
}
return true, nil
}, waitTimeout, pollingInterval).Should(gomega.Equal(true), "keycloak oidc authconfig not configured correctly")
return nil
}
// GvkToGvr converts a GroupVersionKind to corresponding GroupVersionResource
func GvkToGvr(gvk schema.GroupVersionKind) schema.GroupVersionResource {
resource := strings.ToLower(gvk.Kind)
if strings.HasSuffix(resource, "s") {
resource = resource + "es"
} else {
resource = resource + "s"
}
return schema.GroupVersionResource{Group: gvk.Group,
Version: gvk.Version,
Resource: resource,
}
}
func verifyAuthConfigAttribute(name string, actual interface{}, expected interface{}) error {
if expected != actual {
return fmt.Errorf("keycloak auth config attribute %s not correctly configured, expected %v, actual %v", name, expected, actual)
}
return nil
}
func EventuallyGetRancherHost(log *zap.SugaredLogger, api *APIEndpoint) (string, error) {
rancherHost := EventuallyGetIngressHost(log, api, rancher.ComponentNamespace, common.RancherName)
if rancherHost == "" {
return "", fmt.Errorf("got empty Rancher ingress host")
}
return rancherHost, nil
}
func CreateNewRancherConfig(log *zap.SugaredLogger, kubeconfigPath string) (*rancherutil.RancherConfig, error) {
rancherAdminPassword, err := eventuallyGetRancherAdminPassword(log)
if err != nil {
return nil, err
}
return CreateNewRancherConfigForUser(log, kubeconfigPath, "admin", rancherAdminPassword)
}
func CreateNewRancherConfigForUser(log *zap.SugaredLogger, kubeconfigPath string, username string, password string) (*rancherutil.RancherConfig, error) {
apiEndpoint := EventuallyGetAPIEndpoint(kubeconfigPath)
rancherHost, err := EventuallyGetRancherHost(log, apiEndpoint)
if err != nil {
return nil, err
}
rancherURL := fmt.Sprintf("https://%s", rancherHost)
caCert, err := GetCACertFromSecret(common.RancherIngressCAName, constants.RancherSystemNamespace, "ca.crt", kubeconfigPath)
if err != nil {
return nil, fmt.Errorf("failed to get caCert: %v", err)
}
// the tls-ca secret is optional, and contains the private CA bundle configured for Rancher
additionalCA, _ := GetCACertFromSecret(constants.RancherTLSCA, constants.RancherSystemNamespace, constants.RancherTLSCAKey, kubeconfigPath)
httpClient, err := GetVerrazzanoHTTPClient(kubeconfigPath)
if err != nil {
return nil, err
}
token, err := getRancherUserToken(log, httpClient, rancherURL, username, password)
if err != nil {
return nil, fmt.Errorf("failed to get user token from Rancher: %v", err)
}
rc := rancherutil.RancherConfig{
// populate Rancher config from the functions available in this file,adding as necessary
BaseURL: rancherURL,
Host: rancherHost,
APIAccessToken: token,
CertificateAuthorityData: caCert,
AdditionalCA: additionalCA,
}
return &rc, nil
}
func GetClusterKubeconfig(log *zap.SugaredLogger, httpClient *retryablehttp.Client, rc *rancherutil.RancherConfig, clusterID string) (string, error) {
reqURL := rc.BaseURL + "/v3/clusters/" + clusterID + "?action=generateKubeconfig"
req, err := retryablehttp.NewRequest("POST", reqURL, nil)
if err != nil {
return "", err
}
req.Header.Set("Authorization", "Bearer "+rc.APIAccessToken)
response, err := httpClient.Do(req)
if err != nil {
log.Error(fmt.Sprintf("Error getting managed cluster kubeconfig: %v", err))
return "", err
}
err = httputil.ValidateResponseCode(response, http.StatusOK)
if err != nil {
log.Errorf("Invalid response code when fetching cluster kubeconfig: %v", err)
return "", err
}
defer response.Body.Close()
// extract the response body
responseBody, err := io.ReadAll(response.Body)
if err != nil {
log.Errorf("Failed to read Rancher kubeconfig response: %v", err)
return "", err
}
return httputil.ExtractFieldFromResponseBodyOrReturnError(string(responseBody), "config", "")
}
// This function is a wrapper function that executes a GET Request to return all tokens in Rancher for a given user
func ExecuteGetRequestToReturnAllTokens(log *zap.SugaredLogger, adminKubeconfig, managedClusterName, usernameForRancher string) ([]byte, error) {
getReq, err := retryablehttp.NewRequest("GET", "", nil)
if err != nil {
return nil, err
}
getReq.Header = map[string][]string{"Content-Type": {"application/json"}, "Accept": {"application/json"}}
return sendTokenRequestToRancher(log, adminKubeconfig, managedClusterName, usernameForRancher, getReq, http.StatusOK, "/v3/tokens")
}
// This function is a wrapper function that executes a POST Request to add a token in Rancher for a given user
func ExecutePostRequestToAddAToken(log *zap.SugaredLogger, adminKubeconfig, managedClusterName, usernameForRancher, ttl string) ([]byte, error) {
clusterID, err := getClusterIDForManagedCluster(adminKubeconfig, managedClusterName)
if err != nil {
return nil, err
}
val, _ := strconv.Atoi(ttl)
payload := &Payload{
ClusterID: clusterID,
TTL: val * 60000,
}
data, err := json.Marshal(payload)
if err != nil {
return nil, err
}
postReq, err := retryablehttp.NewRequest("POST", "", data)
if err != nil {
return nil, err
}
postReq.Header = map[string][]string{"Content-Type": {"application/json"}}
return sendTokenRequestToRancher(log, adminKubeconfig, managedClusterName, usernameForRancher, postReq, http.StatusCreated, "/v3/tokens")
}
// This function is a wrapper function to delete a given token for a specified user in Rancher
func ExecuteDeleteRequestForToken(log *zap.SugaredLogger, adminKubeconfig, managedClusterName, usernameForRancher, tokenName string) error {
deleteReq, err := retryablehttp.NewRequest("DELETE", "", nil)
if err != nil {
return err
}
deleteReq.Header = map[string][]string{"Accept": {"application/json"}}
_, err = sendTokenRequestToRancher(log, adminKubeconfig, managedClusterName, usernameForRancher, deleteReq, http.StatusNoContent, "/v3/tokens/"+tokenName)
return err
}
// This function is a helper function that sends a Token Request to Rancher for a specified user
// This function expects a retryable HTTP Request object
func sendTokenRequestToRancher(log *zap.SugaredLogger, adminKubeconfig, managedClusterName, usernameForRancher string, requestObject *retryablehttp.Request, expectedReturnCode int, requestPath string) ([]byte, error) {
httpClient, APIAccessToken, err := getRequiredInfoToPreformTokenOperationsInRancherForArgoCD(log, adminKubeconfig, managedClusterName, usernameForRancher)
if err != nil {
return nil, err
}
api, err := GetAPIEndpoint(adminKubeconfig)
if err != nil {
log.Errorf("API Endpoint not successfully received based on KubeConfig Path")
return nil, err
}
rancherURL, err := GetURLForIngress(log, api, "cattle-system", "rancher", "https")
if err != nil {
log.Errorf("URL For Rancher not successfully found")
return nil, err
}
reqURL := rancherURL + requestPath
URLForRequest, err := urlpkg.Parse(reqURL)
if err != nil {
return nil, err
}
requestObject.URL = URLForRequest
requestObject.Header["Authorization"] = []string{"Bearer " + APIAccessToken}
response, err := httpClient.Do(requestObject)
if err != nil {
return nil, err
}
responseBody, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}
err = httputil.ValidateResponseCode(response, expectedReturnCode)
if err != nil {
return nil, err
}
return responseBody, err
}
// This function gets the necessary information required to access the token API resources in Rancher for ArgoCD
func getRequiredInfoToPreformTokenOperationsInRancherForArgoCD(log *zap.SugaredLogger, adminKubeconfig, managedClusterName, argoCDUsernameForRancher string) (httpClient *retryablehttp.Client, APIAccessToken string, err error) {
argoCDPasswordForRancher, err := RetrieveArgoCDPassword("verrazzano-mc", "verrazzano-argocd-secret")
if err != nil {
return nil, "", err
}
rancherConfigForArgoCD, err := CreateNewRancherConfigForUser(log, adminKubeconfig, argoCDUsernameForRancher, argoCDPasswordForRancher)
if err != nil {
Log(Error, "Error occurred when created a Rancher Config for ArgoCD")
return nil, "", err
}
httpClientForRancher, err := GetVerrazzanoHTTPClient(adminKubeconfig)
if err != nil {
Log(Error, "Error getting the Verrazzano http client")
return nil, "", err
}
return httpClientForRancher, rancherConfigForArgoCD.APIAccessToken, nil
}
func getClusterIDForManagedCluster(adminKubeConfig, managedClusterName string) (string, error) {
client, err := GetClusterOperatorClientset(adminKubeConfig)
if err != nil {
Log(Error, "Error creating the client set used by the cluster operator")
return "", err
}
managedCluster, err := client.ClustersV1alpha1().VerrazzanoManagedClusters(constants.VerrazzanoMultiClusterNamespace).Get(context.TODO(), managedClusterName, v1.GetOptions{})
if err != nil {
Log(Error, "Error getting the current managed cluster resource")
return "", err
}
clusterID := managedCluster.Status.RancherRegistration.ClusterID
if clusterID == "" {
Log(Error, "The managed cluster does not have a clusterID value")
err := fmt.Errorf("ClusterID value is not yet populated for the managed cluster")
return "", err
}
return clusterID, nil
}