-
Notifications
You must be signed in to change notification settings - Fork 331
/
clustercredential.go
108 lines (98 loc) · 4.17 KB
/
clustercredential.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
/*
* Tencent is pleased to support the open source community by making TKEStack
* available.
*
* Copyright (C) 2012-2019 Tencent. All Rights Reserved.
*
* 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
*
* https://opensource.org/licenses/Apache-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 OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package validation
import (
"context"
"errors"
"time"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/validation"
apimachineryvalidation "k8s.io/apimachinery/pkg/api/validation"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/client-go/rest"
platforminternalclient "tkestack.io/tke/api/client/clientset/internalversion/typed/platform/internalversion"
"tkestack.io/tke/api/platform"
utilvalidation "tkestack.io/tke/pkg/util/validation"
)
// ValidateClusterCredential validates a given ClusterCredential.
func ValidateClusterCredential(ctx context.Context, credential *platform.ClusterCredential, platformClient platforminternalclient.PlatformInterface) field.ErrorList {
allErrs := validation.ValidateObjectMeta(&credential.ObjectMeta, false, apimachineryvalidation.NameIsDNSLabel, field.NewPath("metadata"))
if credential.ClusterName != "" {
cluster, err := platformClient.Clusters().Get(ctx, credential.ClusterName, metav1.GetOptions{})
if err != nil {
return allErrs
}
// Deprecated: will remove in next release
if cluster.Spec.Type == "Imported" {
if credential.ClientCert == nil && credential.ClientKey != nil ||
credential.ClientCert != nil && credential.ClientKey == nil {
allErrs = append(allErrs, field.Required(field.NewPath("clientCert"),
"`clientCert` and `clientKey` must provide togther"))
}
host, err := cluster.Host()
if err != nil {
allErrs = append(allErrs, field.Invalid(field.NewPath("clusterName"), credential.ClusterName, err.Error()))
} else {
restConfig := &rest.Config{
Host: host,
Timeout: 5 * time.Second,
}
if credential.CACert != nil {
restConfig.CAData = credential.CACert
if err = utilvalidation.ValidateRESTConfig(ctx, restConfig); err != nil {
if status := apierrors.APIStatus(nil); !errors.As(err, &status) {
allErrs = append(allErrs, field.Invalid(field.NewPath("caCert"), "", err.Error()))
}
}
} else {
restConfig.Insecure = true
}
if credential.Token != nil {
config := rest.CopyConfig(restConfig)
config.BearerToken = *credential.Token
if err = utilvalidation.ValidateRESTConfig(ctx, config); err != nil {
if apierrors.IsUnauthorized(err) {
allErrs = append(allErrs, field.Invalid(field.NewPath("token"), *credential.Token, err.Error()))
} else {
allErrs = append(allErrs, field.InternalError(field.NewPath("token"), err))
}
}
}
if credential.ClientCert != nil && credential != nil {
config := rest.CopyConfig(restConfig)
config.TLSClientConfig.CertData = credential.ClientCert
config.TLSClientConfig.KeyData = credential.ClientKey
if err = utilvalidation.ValidateRESTConfig(ctx, config); err != nil {
if apierrors.IsUnauthorized(err) {
allErrs = append(allErrs, field.Invalid(field.NewPath("clientCert"), "", err.Error()))
} else {
allErrs = append(allErrs, field.InternalError(field.NewPath("clientCert"), err))
}
}
}
}
}
}
return allErrs
}
// ValidateUpdateClusterCredential tests if an update to a ClusterCredential is valid.
func ValidateUpdateClusterCredential(ctx context.Context, newObj *platform.ClusterCredential, oldObj *platform.ClusterCredential, platformClient platforminternalclient.PlatformInterface) field.ErrorList {
allErrs := validation.ValidateObjectMetaUpdate(&newObj.ObjectMeta, &oldObj.ObjectMeta, field.NewPath("metadata"))
return allErrs
}