-
Notifications
You must be signed in to change notification settings - Fork 2k
/
config.go
205 lines (172 loc) · 5.54 KB
/
config.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
/*
Copyright 2021 The Vitess 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 rbac
import (
"fmt"
"strings"
"github.com/spf13/viper"
"k8s.io/apimachinery/pkg/util/sets"
"vitess.io/vitess/go/vt/concurrency"
"vitess.io/vitess/go/vt/log"
)
// Config is the RBAC configuration representation. The public fields are
// populated by viper during LoadConfig, and the private fields are set during
// cfg.Reify. A config must be reified before first use.
type Config struct {
Authenticator string
Rules []*struct {
Resource string
Actions []string
Subjects []string
Clusters []string
}
reified bool
cfg map[string][]*Rule
authenticator Authenticator
authorizer *Authorizer
}
// LoadConfig reads the file at path into a Config struct, and then reifies
// the config so its autheticator and authorizer may be used. Errors during
// loading/parsing, or validation errors during reification are returned to the
// caller.
//
// Any file format supported by viper is supported. Currently this is yaml, json
// or toml.
func LoadConfig(path string) (*Config, error) {
v := viper.New()
v.SetConfigFile(path)
if err := v.ReadInConfig(); err != nil {
return nil, err
}
var cfg Config
if err := v.UnmarshalExact(&cfg); err != nil {
return nil, err
}
if err := cfg.Reify(); err != nil {
return nil, err
}
return &cfg, nil
}
// Reify makes a config that was loaded from a file usable, by validating the
// rules and constructing its (optional) authenticator and authorizer. A config
// must be reified before first use. Calling Reify multiple times has no effect
// after the first call. Reify is called by LoadConfig, so a config loaded that
// way does not need to be manually reified.
func (c *Config) Reify() error {
if c.reified {
return nil
}
// reify the rules
byResource := map[string][]*Rule{}
rec := concurrency.AllErrorRecorder{}
for i, rule := range c.Rules {
resourceRules := byResource[rule.Resource]
actions := sets.NewString(rule.Actions...)
if actions.Has("*") && actions.Len() > 1 {
// error to have wildcard and something else
rec.RecordError(fmt.Errorf("rule %d: actions list cannot include wildcard and other actions, have %v", i, actions.List()))
}
subjects := sets.NewString(rule.Subjects...)
if subjects.Has("*") && subjects.Len() > 1 {
// error to have wildcard and something else
rec.RecordError(fmt.Errorf("rule %d: subjects list cannot include wildcard and other subjects, have %v", i, subjects.List()))
}
clusters := sets.NewString(rule.Clusters...)
if clusters.Has("*") && clusters.Len() > 1 {
// error to have wildcard and something else
rec.RecordError(fmt.Errorf("rule %d: clusters list cannot include wildcard and other clusters, have %v", i, clusters.List()))
}
resourceRules = append(resourceRules, &Rule{
actions: actions,
subjects: subjects,
clusters: clusters,
})
byResource[rule.Resource] = resourceRules
}
if rec.HasErrors() {
return rec.Error()
}
log.Infof("[rbac]: loaded authorizer with %d rules", len(c.Rules))
c.cfg = byResource
c.authorizer = &Authorizer{
policies: c.cfg,
}
// reify the authenticator
switch {
case strings.HasSuffix(c.Authenticator, ".so"):
authn, err := loadAuthenticatorPlugin(c.Authenticator)
if err != nil {
return err
}
c.authenticator = authn
case c.Authenticator != "":
factory, ok := authenticators[c.Authenticator]
if !ok {
return fmt.Errorf("%w %s", ErrUnregisteredAuthenticationImpl, c.Authenticator)
}
c.authenticator = factory()
default:
log.Info("[rbac]: no authenticator implementation specified")
c.authenticator = nil // Technically a no-op, but being super explicit about it.
}
c.reified = true
return nil
}
// GetAuthenticator returns the Authenticator implementation specified by the
// config. It returns nil if the Authenticator string field is the empty string,
// or if a call to Reify has not been made.
func (c *Config) GetAuthenticator() Authenticator {
return c.authenticator
}
// GetAuthorizer returns the Authorizer using the rules specified in the config.
// It returns nil if a call to Reify has not been made.
func (c *Config) GetAuthorizer() *Authorizer {
return c.authorizer
}
// DefaultConfig returns a default config that allows all actions on all resources
// It is mainly used in the case where users explicitly pass --no-rbac flag.
func DefaultConfig() *Config {
log.Info("[rbac]: using default rbac configuration")
actions := []string{string(GetAction), string(CreateAction), string(DeleteAction), string(PutAction), string(PingAction)}
subjects := []string{"*"}
clusters := []string{"*"}
cfg := map[string][]*Rule{
"*": {
{
clusters: sets.NewString(clusters...),
actions: sets.NewString(actions...),
subjects: sets.NewString(subjects...),
},
},
}
return &Config{
Rules: []*struct {
Resource string
Actions []string
Subjects []string
Clusters []string
}{
{
Resource: "*",
Actions: actions,
Subjects: subjects,
Clusters: clusters,
},
},
cfg: cfg,
authorizer: &Authorizer{
policies: cfg,
},
authenticator: nil,
}
}