/
validation.go
153 lines (122 loc) · 5.89 KB
/
validation.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
/*
* 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 apikey
import (
"context"
"fmt"
"time"
apiMachineryValidation "k8s.io/apimachinery/pkg/api/validation"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/validation/field"
authinternalclient "tkestack.io/tke/api/client/clientset/internalversion/typed/auth/internalversion"
"tkestack.io/tke/api/auth"
"tkestack.io/tke/pkg/auth/util"
"tkestack.io/tke/pkg/util/log"
)
var (
minExpire = time.Second
maxExpire = 100 * 365 * 24 * time.Hour
defaultAPIKeyTimeout = metav1.Duration{Duration: 7 * 24 * time.Hour}
)
// ValidateAPIkey tests if required fields in the signing key are set.
func ValidateAPIkey(ctx context.Context, apiKey *auth.APIKey, keySigner util.KeySigner, privilegedUsername string) field.ErrorList {
allErrs := apiMachineryValidation.ValidateObjectMeta(&apiKey.ObjectMeta, false, apiMachineryValidation.NameIsDNSSubdomain, field.NewPath("metadata"))
fldSpecPath := field.NewPath("spec")
if apiKey.Spec.APIkey == "" {
allErrs = append(allErrs, field.Required(fldSpecPath.Child("apiKey"), "must specify apiKey"))
}
if claims, err := keySigner.Verify(ctx, apiKey.Spec.APIkey); err != nil {
allErrs = append(allErrs, field.Invalid(fldSpecPath.Child("apiKey"), apiKey.Spec.APIkey, err.Error()))
} else {
// if not super admin, must specify tenantID
if apiKey.Spec.TenantID == "" && claims.UserName != privilegedUsername {
allErrs = append(allErrs, field.Required(fldSpecPath.Child("tenantID"), "must specify tenantID"))
}
if apiKey.Spec.Username != claims.UserName {
allErrs = append(allErrs, field.Invalid(fldSpecPath.Child("username"), apiKey.Spec.Username, "must be same with username of apikey"))
}
if claims.IssuedAt != apiKey.Spec.IssueAt.Unix() {
allErrs = append(allErrs, field.Invalid(fldSpecPath.Child("issue_at"), apiKey.Spec.IssueAt, "must be same with issue time of apiKey"))
}
if claims.ExpiresAt != apiKey.Spec.ExpireAt.Unix() {
allErrs = append(allErrs, field.Invalid(fldSpecPath.Child("expire_at"), apiKey.Spec.IssueAt, "must be same with expire time of apiKey"))
}
}
return allErrs
}
// ValidateAPIKeyUpdate tests if required fields in the session are set during
// an update.
func ValidateAPIKeyUpdate(ctx context.Context, apiKey *auth.APIKey, oldAPIKey *auth.APIKey) field.ErrorList {
allErrs := field.ErrorList{}
allErrs = append(allErrs, apiMachineryValidation.ValidateObjectMetaUpdate(&apiKey.ObjectMeta, &oldAPIKey.ObjectMeta, field.NewPath("metadata"))...)
fldSpecPath := field.NewPath("spec")
if apiKey.Spec.APIkey != oldAPIKey.Spec.APIkey {
allErrs = append(allErrs, field.Invalid(fldSpecPath.Child("apiKey"), apiKey.Spec.APIkey, "disallowed change the apiKey"))
}
if apiKey.Spec.TenantID != oldAPIKey.Spec.TenantID {
allErrs = append(allErrs, field.Invalid(fldSpecPath.Child("tenantID"), apiKey.Spec.TenantID, "disallowed change the tenantID"))
}
if apiKey.Spec.IssueAt != oldAPIKey.Spec.IssueAt {
allErrs = append(allErrs, field.Invalid(fldSpecPath.Child("issue_at"), apiKey.Spec.IssueAt, "disallowed change the issue_at"))
}
if apiKey.Spec.ExpireAt != oldAPIKey.Spec.ExpireAt {
allErrs = append(allErrs, field.Invalid(fldSpecPath.Child("expire_at"), apiKey.Spec.ExpireAt, "disallowed change the expire_at"))
}
if apiKey.Spec.Username != oldAPIKey.Spec.Username {
allErrs = append(allErrs, field.Invalid(fldSpecPath.Child("username"), apiKey.Spec.ExpireAt, "disallowed change the username"))
}
return allErrs
}
func validateAPIKeyExpire(expire metav1.Duration) error {
if expire.Duration < minExpire || expire.Duration > maxExpire {
return fmt.Errorf("expire %v must not shorter than %v or longer than %v", expire, minExpire, maxExpire)
}
return nil
}
// ValidateAPIKeyReq tests if required fields in the signing key are set.
func ValidateAPIKeyReq(apiKeyReq *auth.APIKeyReq) error {
if apiKeyReq.Expire.Duration == 0 {
apiKeyReq.Expire = defaultAPIKeyTimeout
}
if err := validateAPIKeyExpire(apiKeyReq.Expire); err != nil {
return err
}
return nil
}
// ValidateAPIkeyPassword tests if required fields in the signing key are set.
func ValidateAPIkeyPassword(ctx context.Context, apiKeyPass *auth.APIKeyReqPassword, authClient authinternalclient.AuthInterface) error {
allErrs := field.ErrorList{}
if apiKeyPass.Expire.Duration == 0 {
apiKeyPass.Expire = defaultAPIKeyTimeout
}
fldPath := field.NewPath("")
if err := validateAPIKeyExpire(apiKeyPass.Expire); err != nil {
allErrs = append(allErrs, field.Invalid(fldPath.Child("expire"), apiKeyPass.Expire, err.Error()))
}
localIdentity, err := util.GetLocalIdentity(ctx, authClient, apiKeyPass.TenantID, apiKeyPass.Username)
if err != nil {
log.Error("Get localidentity failed", log.String("localIdentity", apiKeyPass.Username), log.Err(err))
allErrs = append(allErrs, field.Invalid(fldPath.Child("username"), apiKeyPass.Username, err.Error()))
} else {
if err := util.VerifyDecodedPassword(apiKeyPass.Password, localIdentity.Spec.HashedPassword); err != nil {
log.Error("Invalid password", log.ByteString("input password", []byte(apiKeyPass.Password)), log.String("store password", localIdentity.Spec.HashedPassword), log.Err(err))
allErrs = append(allErrs, field.Invalid(fldPath.Child("password"), apiKeyPass.Password, err.Error()))
}
}
return allErrs.ToAggregate()
}