This repository was archived by the owner on Mar 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 236
Expand file tree
/
Copy pathpolicy.go
More file actions
176 lines (145 loc) · 4.65 KB
/
policy.go
File metadata and controls
176 lines (145 loc) · 4.65 KB
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
// Copyright 2017 uSwitch
//
// 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 server
import (
"context"
"fmt"
"regexp"
v1 "k8s.io/api/core/v1"
"github.com/uswitch/kiam/pkg/aws/sts"
"github.com/uswitch/kiam/pkg/k8s"
)
// AssumeRolePolicy allows for policy to check whether pods can assume the role being
// requested
type AssumeRolePolicy interface {
IsAllowedAssumeRole(ctx context.Context, roleName string, pod *v1.Pod) (Decision, error)
}
// CompositeAssumeRolePolicy allows multiple policies to be checked
type CompositeAssumeRolePolicy struct {
policies []AssumeRolePolicy
}
func (p *CompositeAssumeRolePolicy) IsAllowedAssumeRole(ctx context.Context, role string, pod *v1.Pod) (Decision, error) {
for _, policy := range p.policies {
decision, err := policy.IsAllowedAssumeRole(ctx, role, pod)
if err != nil {
return nil, err
}
if !decision.IsAllowed() {
return decision, nil
}
}
return &allowed{}, nil
}
// Creates a AssumeRolePolicy that tests all policies pass.
func Policies(p ...AssumeRolePolicy) *CompositeAssumeRolePolicy {
return &CompositeAssumeRolePolicy{
policies: p,
}
}
// RequestingAnnotatedRolePolicy ensures the pod is requesting the role that it's
// currently annotated with.
type RequestingAnnotatedRolePolicy struct {
pods k8s.PodGetter
resolver sts.ARNResolver
}
func NewRequestingAnnotatedRolePolicy(p k8s.PodGetter, resolver sts.ARNResolver) *RequestingAnnotatedRolePolicy {
return &RequestingAnnotatedRolePolicy{pods: p, resolver: resolver}
}
func (p *RequestingAnnotatedRolePolicy) IsAllowedAssumeRole(ctx context.Context, role string, pod *v1.Pod) (Decision, error) {
annotatedIdentiy, err := p.resolver.Resolve(k8s.PodRole(pod))
if err != nil {
return nil, err
}
requestedIdentity, err := p.resolver.Resolve(role)
if err != nil {
return nil, err
}
if annotatedIdentiy.Equals(requestedIdentity) {
return &allowed{}, nil
}
return &forbidden{requested: role, annotated: annotatedIdentiy.Name}, nil
}
// NamespacePermittedRoleNamePolicy ensures the pod is requesting a role that
// the namespace permits in its regexp annotation.
type NamespacePermittedRoleNamePolicy struct {
namespaces k8s.NamespaceFinder
resolver sts.ARNResolver
strict bool
}
func NewNamespacePermittedRoleNamePolicy(strictRegexp bool, n k8s.NamespaceFinder, resolver sts.ARNResolver) *NamespacePermittedRoleNamePolicy {
return &NamespacePermittedRoleNamePolicy{namespaces: n, resolver: resolver, strict: strictRegexp}
}
func (p *NamespacePermittedRoleNamePolicy) IsAllowedAssumeRole(ctx context.Context, role string, pod *v1.Pod) (Decision, error) {
requestedIdentity, err := p.resolver.Resolve(role)
if err != nil {
return nil, err
}
ns, err := p.namespaces.FindNamespace(ctx, pod.GetObjectMeta().GetNamespace())
if err != nil {
return nil, err
}
expression := ns.GetAnnotations()[k8s.AnnotationPermittedKey]
if expression == "" {
return &namespacePolicyForbidden{expression: "(empty)", role: role}, nil
}
var re *regexp.Regexp
if p.strict {
re, err = regexp.Compile("^" + expression + "$")
if err != nil {
return nil, err
}
} else {
re, err = regexp.Compile(expression)
if err != nil {
return nil, err
}
}
if !re.MatchString(requestedIdentity.ARN) {
return &namespacePolicyForbidden{expression: expression, role: requestedIdentity.ARN}, nil
}
return &allowed{}, nil
}
// Decision reports (with message) as to whether the assume role is permitted.
type Decision interface {
IsAllowed() bool
Explanation() string
}
type allowed struct {
}
func (a *allowed) IsAllowed() bool {
return true
}
func (a *allowed) Explanation() string {
return ""
}
type forbidden struct {
requested string
annotated string
}
func (f *forbidden) IsAllowed() bool {
return false
}
func (f *forbidden) Explanation() string {
return fmt.Sprintf("requested '%s' but annotated with '%s', forbidden", f.requested, f.annotated)
}
type namespacePolicyForbidden struct {
expression string
role string
}
func (f *namespacePolicyForbidden) IsAllowed() bool {
return false
}
func (f *namespacePolicyForbidden) Explanation() string {
return fmt.Sprintf("namespace policy expression '%s' forbids role '%s'", f.expression, f.role)
}