-
Notifications
You must be signed in to change notification settings - Fork 62
/
backend.go
158 lines (135 loc) · 3.83 KB
/
backend.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
package kubeauth
import (
"context"
"encoding/json"
"fmt"
"strings"
"sync"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
const (
configPath = "config"
rolePrefix = "role/"
// aliasNameSourceUnset provides backwards compatibility with preexisting roles.
aliasNameSourceUnset = ""
aliasNameSourceSAToken = "sa_token"
aliasNameSourceSAPath = "sa_path"
aliasNameSourceDefault = aliasNameSourceSAToken
)
var (
// when adding new alias name sources make sure to update the corresponding FieldSchema description in path_role.go
aliasNameSources = []string{aliasNameSourceSAToken, aliasNameSourceSAPath}
errInvalidAliasNameSource = fmt.Errorf(`invalid alias_name_source, must be one of: %s`, strings.Join(aliasNameSources, ", "))
)
// kubeAuthBackend implements logical.Backend
type kubeAuthBackend struct {
*framework.Backend
// reviewFactory is used to configure the strategy for doing a token review.
// Currently the only options are using the kubernetes API or mocking the
// review. Mocks should only be used in tests.
reviewFactory tokenReviewFactory
l sync.RWMutex
}
// Factory returns a new backend as logical.Backend.
func Factory(ctx context.Context, conf *logical.BackendConfig) (logical.Backend, error) {
b := Backend()
if err := b.Setup(ctx, conf); err != nil {
return nil, err
}
return b, nil
}
func Backend() *kubeAuthBackend {
b := &kubeAuthBackend{}
b.Backend = &framework.Backend{
AuthRenew: b.pathLoginRenew(),
BackendType: logical.TypeCredential,
Help: backendHelp,
PathsSpecial: &logical.Paths{
Unauthenticated: []string{
"login",
},
SealWrapStorage: []string{
configPath,
},
},
Paths: framework.PathAppend(
[]*framework.Path{
pathConfig(b),
pathLogin(b),
},
pathsRole(b),
),
}
// Set the review factory to default to calling into the kubernetes API.
b.reviewFactory = tokenReviewAPIFactory
return b
}
// config takes a storage object and returns a kubeConfig object
func (b *kubeAuthBackend) config(ctx context.Context, s logical.Storage) (*kubeConfig, error) {
raw, err := s.Get(ctx, configPath)
if err != nil {
return nil, err
}
if raw == nil {
return nil, nil
}
conf := &kubeConfig{}
if err := json.Unmarshal(raw.Value, conf); err != nil {
return nil, err
}
// Parse the public keys from the CertificatesBytes
conf.PublicKeys = make([]interface{}, len(conf.PEMKeys))
for i, cert := range conf.PEMKeys {
conf.PublicKeys[i], err = parsePublicKeyPEM([]byte(cert))
if err != nil {
return nil, err
}
}
return conf, nil
}
// role takes a storage backend and the name and returns the role's storage
// entry
func (b *kubeAuthBackend) role(ctx context.Context, s logical.Storage, name string) (*roleStorageEntry, error) {
raw, err := s.Get(ctx, fmt.Sprintf("%s%s", rolePrefix, strings.ToLower(name)))
if err != nil {
return nil, err
}
if raw == nil {
return nil, nil
}
role := &roleStorageEntry{}
if err := json.Unmarshal(raw.Value, role); err != nil {
return nil, err
}
if role.TokenTTL == 0 && role.TTL > 0 {
role.TokenTTL = role.TTL
}
if role.TokenMaxTTL == 0 && role.MaxTTL > 0 {
role.TokenMaxTTL = role.MaxTTL
}
if role.TokenPeriod == 0 && role.Period > 0 {
role.TokenPeriod = role.Period
}
if role.TokenNumUses == 0 && role.NumUses > 0 {
role.TokenNumUses = role.NumUses
}
if len(role.TokenPolicies) == 0 && len(role.Policies) > 0 {
role.TokenPolicies = role.Policies
}
if len(role.TokenBoundCIDRs) == 0 && len(role.BoundCIDRs) > 0 {
role.TokenBoundCIDRs = role.BoundCIDRs
}
return role, nil
}
func validateAliasNameSource(source string) error {
for _, s := range aliasNameSources {
if s == source {
return nil
}
}
return errInvalidAliasNameSource
}
var backendHelp string = `
The Kubernetes Auth Backend allows authentication for Kubernetes service accounts.
`