-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathauth_methods.go
176 lines (150 loc) · 5.36 KB
/
auth_methods.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
package main
import (
"encoding/json"
"fmt"
VaultApi "github.com/hashicorp/vault/api"
log "github.com/sirupsen/logrus"
"io/ioutil"
"path"
"path/filepath"
)
type authMethod struct {
Name string
Path string `json:"path"`
AuthOptions VaultApi.EnableAuthOptions `json:"auth_options"`
Config map[string]interface{} `json:"config"`
AdditionalConfig interface{} `json:"additional_config"`
}
type authMethodList map[string]authMethod
func SyncAuthMethods() {
authMethodList := authMethodList{}
log.Info("Syncing Auth Methods")
getAuthMethods(authMethodList)
configureAuthMethods(authMethodList)
cleanupAuthMethods(authMethodList)
}
func getAuthMethods(authMethodList authMethodList) {
files, err := ioutil.ReadDir(Spec.ConfigurationPath + "/auth_methods/")
if err != nil {
log.Warn("No auth methods found: ", err)
}
for _, file := range files {
if checkExt(file.Name(), ".json") {
content, err := ioutil.ReadFile(Spec.ConfigurationPath + "/auth_methods/" + file.Name())
if err != nil {
log.Fatal(err)
}
if !isJSON(string(content)) {
log.Fatal("Auth method configuration not valid JSON: ", file.Name())
}
var m authMethod
// Use the filename as the mount path
filename := file.Name()
m.Name = filename[0 : len(filename)-len(filepath.Ext(filename))]
m.Path = m.Name + "/"
err = json.Unmarshal([]byte(content), &m)
if err != nil {
log.Fatal("Error parsing auth method configuration: ", file.Name(), " ", err)
}
authMethodList[m.Path] = m
} else {
log.Warn("Auth file has wrong extension. Will not be processed: ", Spec.ConfigurationPath+"auth_methods/"+file.Name())
}
}
}
func configureAuthMethods(authMethodList authMethodList) {
for _, mount := range authMethodList {
// Check if mount is enabled
existing_mounts, _ := VaultSys.ListAuth()
if _, ok := existing_mounts[mount.Path]; ok {
if existing_mounts[mount.Path].Type != mount.AuthOptions.Type {
log.Fatal("Auth mount path "+mount.Path+" exists but doesn't match type: ", existing_mounts[mount.Path].Type, "!=", mount.AuthOptions.Type)
}
var mc VaultApi.MountConfigInput
mc.DefaultLeaseTTL = mount.AuthOptions.Config.DefaultLeaseTTL
mc.MaxLeaseTTL = mount.AuthOptions.Config.MaxLeaseTTL
mc.ListingVisibility = mount.AuthOptions.Config.ListingVisibility
mc.Description = &mount.AuthOptions.Description
tunePath := path.Join("sys/auth", mount.Path, "tune")
task := taskWrite{
Path: tunePath,
Description: fmt.Sprintf("Auth mount tune for [%s]", tunePath),
Data: structToMap(mc),
}
wg.Add(1)
taskChan <- task
} else {
log.Debug("Auth mount path " + mount.Path + " is not enabled, enabling")
err := VaultSys.EnableAuthWithOptions(mount.Path, &mount.AuthOptions)
if err != nil {
log.Fatal("Error enabling mount: ", mount.Path, " ", mount.AuthOptions.Type, " ", err)
}
log.Info("Auth enabled: ", mount.Path, " ", mount.AuthOptions.Type)
}
// Write the auth configuration (if set)
if mount.Config != nil {
// Here we transform to json in order to do string substitution
jsondata, err := json.Marshal(mount.Config)
if err != nil {
log.Fatal(err)
}
contentstring := string(jsondata)
success, errMsg := performSubstitutions(&contentstring, "auth/"+mount.Name)
if !success {
log.Warn(errMsg)
log.Warn("Secret substitution failed for [%s], skipping auth method configuration", mount.Path)
return
} else {
if !isJSON(contentstring) {
log.Fatal("Auth engine [%s] is not a valid JSON after secret substitution", mount.Path)
}
var configMap map[string]interface{}
if err := json.Unmarshal([]byte(contentstring), &configMap); err != nil {
log.Fatalf("Auth engine [%s] failed to unmarshall after secret substitution", mount.Path)
}
configPath := path.Join("auth", mount.Path, "config")
task := taskWrite{
Path: configPath,
Description: fmt.Sprintf("Auth mount config for [%s]", configPath),
Data: configMap,
}
wg.Add(1)
taskChan <- task
}
}
if mount.AuthOptions.Type == "userpass" {
log.Info("Running additional configuration for ", mount.Path)
configureUserpassAuth(mount)
} else if mount.AuthOptions.Type == "ldap" {
log.Info("Running additional configuration for ", mount.Path)
configureLDAPAuth(mount)
} else if mount.AuthOptions.Type == "jwt" || mount.AuthOptions.Type == "oidc" {
authMethodJWT := AuthMethodJWT{
Path: path.Join("auth", mount.Path),
AdditionalConfig: mount.AdditionalConfig,
}
log.Infof("Running additional configuration for [%s]", authMethodJWT.Path)
authMethodJWT.Configure()
} else {
log.Warn("Auth types other than LDAP not currently configurable, please open PR!")
}
}
}
func cleanupAuthMethods(authMethodList authMethodList) {
existing_mounts, _ := VaultSys.ListAuth()
for mountPath, mount := range existing_mounts {
// Ignore default token auth mount
if !(mountPath == "token/" && mount.Type == "token") {
if _, ok := authMethodList[mountPath]; ok {
log.Debug(mountPath + " exists in configuration, no cleanup necessary")
} else {
authPath := path.Join("sys/auth", mountPath)
task := taskDelete{
Description: fmt.Sprintf("Auth method [%s]", authPath),
Path: authPath,
}
taskPromptChan <- task
}
}
}
}