forked from pingcap/kops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
authorizer.go
345 lines (287 loc) · 9.65 KB
/
authorizer.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
/*
Copyright 2018 The Kubernetes Authors.
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 aws
import (
"bytes"
"context"
"crypto/x509"
"encoding/base64"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"time"
"k8s.io/kops/node-authorizer/pkg/server"
"k8s.io/kops/node-authorizer/pkg/utils"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go/service/autoscaling/autoscalingiface"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
"github.com/fullsailor/pkcs7"
"go.uber.org/zap"
)
var (
// a collection of aws public signing certificates
publicCertificates []*x509.Certificate
)
var (
// CheckIAMProfile indicates we should validate the IAM profile
CheckIAMProfile = "verify-iam-profile"
// CheckIPAddress indicates we should validate the client ip address
CheckIPAddress = "verify-ip"
// CheckSignature indicates we validate the signature of the document
CheckSignature = "verify-signature"
)
// awsNodeAuthorizer is the implementation for a node authorizer
type awsNodeAuthorizer struct {
// client is the ec2 interface
client ec2iface.EC2API
// asgc is the autoscaling client
asgc autoscalingiface.AutoScalingAPI
// config is the service configuration
config *server.Config
// identity is the identity document for the instance we are running on
identity ec2metadata.EC2InstanceIdentityDocument
// instance is the ec2 instance we are running on
instance *ec2.Instance
// vpcID is our vpc id
vpcID string
}
// NewAuthorizer creates and returns a aws node authorizer
func NewAuthorizer(config *server.Config) (server.Authorizer, error) {
// @step: load the public certificates
if err := GetPublicCertificates(); err != nil {
return nil, err
}
// @step: get the identity document for the instance we are running
document, err := getInstanceIdentityDocument()
if err != nil {
return nil, err
}
utils.Logger.Info("running node authorizer on instance",
zap.String("instance-id", document.InstanceID),
zap.String("region", document.Region))
// @step: we create a ec2 and autoscaling client
client := ec2.New(session.New(&aws.Config{
Region: aws.String(document.Region),
}))
asgc := autoscaling.New(session.New(&aws.Config{
Region: aws.String(document.Region),
}))
// @step: get information on the instance we are running
instance, err := getInstance(client, document.InstanceID)
if err != nil {
return nil, err
}
return &awsNodeAuthorizer{
client: client,
asgc: asgc,
config: config,
identity: document,
instance: instance,
vpcID: aws.StringValue(instance.VpcId),
}, nil
}
// Authorize is responsible for accepting the request
func (a *awsNodeAuthorizer) Authorize(ctx context.Context, r *server.NodeRegistration) error {
identity := &ec2metadata.EC2InstanceIdentityDocument{}
// @step: decode the request
request, err := decodeRequest(r.Spec.Request)
if err != nil {
return err
}
// @step: extract and validate the document
if reason, err := func() (string, error) {
if a.config.UseFeature(CheckSignature) {
if reason, err := a.validateIdentityDocument(ctx, request.Document, identity); err != nil {
return "", err
} else if reason != "" {
return reason, nil
}
}
if reason, err := a.validateNodeInstance(ctx, identity, r); err != nil {
return "", err
} else if reason != "" {
return reason, nil
}
r.Status.Allowed = true
return "", nil
}(); err != nil {
return err
} else if reason != "" {
r.Deny(reason)
}
return nil
}
// validateNodeInstance is responsible for checking the instance exists and it part of the cluster
func (a *awsNodeAuthorizer) validateNodeInstance(ctx context.Context, doc *ec2metadata.EC2InstanceIdentityDocument, spec *server.NodeRegistration) (string, error) {
// @check we are in the same account
if a.identity.AccountID != doc.AccountID {
return "instance running in different account id", nil
}
// @check we found some instances
instance, err := getInstance(a.client, doc.InstanceID)
if err != nil {
return "", err
}
if aws.StringValue(instance.State.Name) != ec2.InstanceStateNameRunning {
return "instance is not running", nil
}
// @check the instance is running in our vpc
if aws.StringValue(instance.VpcId) != a.vpcID {
return "instance is not running in our VPC", nil
}
// @check the instance is tagged with our kubernetes cluster id
if !hasInstanceTags(a.config.ClusterTag, a.config.ClusterName, instance.Tags) {
return "missing cluster tag", nil
}
// @check the instance has access to the nodes IAM profile
if a.config.UseFeature(CheckIAMProfile) {
if instance.IamInstanceProfile == nil {
return "instance does not have an instance profile", nil
}
if aws.StringValue(instance.IamInstanceProfile.Arn) == "" {
return "instance profile arn is empty", nil
}
expectedArn := fmt.Sprintf("arn:aws:iam::%s:role/nodes.%s", a.identity.AccountID, a.config.ClusterName)
if expectedArn != aws.StringValue(instance.IamInstanceProfile.Arn) {
return fmt.Sprintf("invalid iam instance role, expected: %s, found: %s", expectedArn, aws.StringValue(instance.IamInstanceProfile.Arn)), nil
}
}
// @check the requester is as expected
if a.config.UseFeature(CheckIPAddress) {
if spec.Spec.RemoteAddr != aws.StringValue(instance.PrivateIpAddress) {
return fmt.Sprintf("ip address conflict, expected: %s, got: %s", aws.StringValue(instance.PrivateIpAddress), spec.Spec.RemoteAddr), nil
}
}
return "", nil
}
// validateIdentityDocument is responsible for validate the aws identity document
func (a *awsNodeAuthorizer) validateIdentityDocument(_ context.Context, signed []byte, document interface{}) (string, error) {
// @step: decode the signed document
decoded, err := base64.StdEncoding.DecodeString(string(signed))
if err != nil {
return "", err
}
// @step: get the digest
for _, x := range publicCertificates {
parsed, err := pkcs7.Parse(decoded)
if err != nil {
return "", err
}
parsed.Certificates = []*x509.Certificate{x}
if err := parsed.Verify(); err != nil {
utils.Logger.Warn("identity document not validated by certificates",
zap.String("common-name", x.Subject.CommonName),
zap.Error(err))
} else {
return "", json.NewDecoder(bytes.NewReader(parsed.Content)).Decode(document)
}
}
return "invalid signature", nil
}
// validateNodeRegistrationRequest is responsible for validating the request itself
func validateNodeRegistrationRequest(request *Request) error {
err := func() error {
if len(request.Document) <= 0 {
return errors.New("missing identity document")
}
return nil
}()
if err != nil {
return fmt.Errorf("invalid verification request: %s", err)
}
return nil
}
// decodeRequest is responsible for decoding the request
func decodeRequest(in []byte) (*Request, error) {
request := &Request{}
if err := json.NewDecoder(bytes.NewReader(in)).Decode(request); err != nil {
return nil, err
}
// @step: validate the node request
if err := validateNodeRegistrationRequest(request); err != nil {
return nil, err
}
return request, nil
}
func (a *awsNodeAuthorizer) Close() error {
return nil
}
// Name returns the name of the authozier
func (a *awsNodeAuthorizer) Name() string {
return "aws"
}
// hasInstanceTags checks the tags exists on the cluster
func hasInstanceTags(name, value string, tags []*ec2.Tag) bool {
for _, x := range tags {
if aws.StringValue(x.Key) == name && aws.StringValue(x.Value) == value {
return true
}
}
return false
}
// getInstanceIdentityDocument is responsible for retrieving the instance identity document
func getInstanceIdentityDocument() (ec2metadata.EC2InstanceIdentityDocument, error) {
var document ec2metadata.EC2InstanceIdentityDocument
client := ec2metadata.New(session.New())
maxInterval := 500 * time.Millisecond
maxTime := 5 * time.Second
err := utils.Retry(context.TODO(), maxInterval, maxTime, func() error {
x, err := client.GetInstanceIdentityDocument()
if err != nil {
return err
}
document = x
return nil
})
return document, err
}
// getInstance is responsible for getting the instance
func getInstance(client ec2iface.EC2API, instanceID string) (*ec2.Instance, error) {
// @step: describe the instance
resp, err := client.DescribeInstances(&ec2.DescribeInstancesInput{
InstanceIds: []*string{aws.String(instanceID)},
})
if err != nil {
return nil, err
}
// @check we found some instances
if len(resp.Reservations) <= 0 || len(resp.Reservations[0].Instances) <= 0 {
return nil, fmt.Errorf("missing instance id: %s", instanceID)
}
if len(resp.Reservations[0].Instances) > 1 {
return nil, fmt.Errorf("found multiple instances with instance id: %s", instanceID)
}
// @check the instance is running
instance := resp.Reservations[0].Instances[0]
if instance.State == nil {
return nil, errors.New("missing instance status")
}
return instance, nil
}
// GetPublicCertificates loads the certificates
func GetPublicCertificates() error {
for i := range awsCertificates {
block, _ := pem.Decode([]byte(awsCertificates[i]))
c, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return err
}
publicCertificates = append(publicCertificates, c)
}
return nil
}