-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
types.go
68 lines (55 loc) · 2.23 KB
/
types.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
/*
Copyright 2019 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 iam
import (
"encoding/json"
"fmt"
"strings"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/truncate"
)
// MaxLengthIAMRoleName defines the max length of an IAMRole name
const MaxLengthIAMRoleName = 64
// ParseStatements parses JSON into a list of Statements
func ParseStatements(policy string) ([]*Statement, error) {
statements := make([]*Statement, 0)
if err := json.Unmarshal([]byte(policy), &statements); err != nil {
return nil, fmt.Errorf("error parsing IAM statements: %v", err)
}
return statements, nil
}
type IAMModelContext struct {
// AWSAccountID holds the 12 digit AWS account ID, when running on AWS
AWSAccountID string
// AWSPartition defines the partition of the AWS account, typically "aws", "aws-cn", or "aws-us-gov"
AWSPartition string
Cluster *kops.Cluster
}
// IAMNameForServiceAccountRole determines the name of the IAM Role and Instance Profile to use for the service-account role
func (b *IAMModelContext) IAMNameForServiceAccountRole(role Subject) (string, error) {
serviceAccount, ok := role.ServiceAccount()
if !ok {
return "", fmt.Errorf("role %v does not have ServiceAccount", role)
}
name := IAMNameForServiceAccountRole(serviceAccount.Name, serviceAccount.Namespace, b.ClusterName())
return name, nil
}
// ClusterName returns the cluster name
func (b *IAMModelContext) ClusterName() string {
return b.Cluster.ObjectMeta.Name
}
func IAMNameForServiceAccountRole(name, namespace, clusterName string) string {
role := name + "." + strings.ReplaceAll(namespace, "*", "wildcard") + ".sa." + clusterName
role = truncate.TruncateString(role, truncate.TruncateStringOptions{MaxLength: MaxLengthIAMRoleName, AlwaysAddHash: false})
return role
}