-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_request.go
255 lines (220 loc) · 7.8 KB
/
config_request.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package dex
import (
"encoding/pem"
"fmt"
"strings"
google_protobuf "github.com/golang/protobuf/ptypes/wrappers"
"github.com/chef/automate/api/config/shared"
w "github.com/chef/automate/api/config/shared/wrappers"
)
// NewConfigRequest returns a new ConfigRequests instance with zero values.
func NewConfigRequest() *ConfigRequest {
return &ConfigRequest{
V1: &ConfigRequest_V1{
Sys: &ConfigRequest_V1_System{
Mlsa: &shared.Mlsa{},
Log: &ConfigRequest_V1_Log{},
Service: &ConfigRequest_V1_System_Service{},
Grpc: &ConfigRequest_V1_Grpc{},
Storage: &ConfigRequest_V1_Storage{},
Expiry: &ConfigRequest_V1_Expiry{},
Bootstrap: &ConfigRequest_V1_Bootstrap{},
Connectors: &ConfigRequest_V1_Connectors{},
Tls: &shared.TLSCredentials{},
},
Svc: &ConfigRequest_V1_Service{},
},
}
}
// NewLdapConnector returns a new instance of ConfigRequest_Ldap with zero
// values
func NewLdapConnector() *ConfigRequest_V1_Ldap {
return &ConfigRequest_V1_Ldap{}
}
// NewMsadLdapConnector returns a new instance of ConfigRequest_Msad_Ldap with
// zero values
func NewMsadLdapConnector() *ConfigRequest_V1_Msad_Ldap {
return &ConfigRequest_V1_Msad_Ldap{}
}
// NewSamlConnector returns a new instance of ConfigRequest_Saml with zero
// values
func NewSamlConnector() *ConfigRequest_V1_Saml {
return &ConfigRequest_V1_Saml{}
}
// DefaultConfigRequest returns a new ConfigRequest instance with default values.
func DefaultConfigRequest() *ConfigRequest {
c := NewConfigRequest()
c.V1.Sys.Grpc.Host = w.String("0.0.0.0")
c.V1.Sys.Grpc.Port = w.Int32(10116)
c.V1.Sys.Service.Host = w.String("0.0.0.0")
c.V1.Sys.Service.Port = w.Int32(10117)
c.V1.Sys.Expiry.IdTokens = w.String("3m")
c.V1.Sys.Bootstrap.InsecureAdmin = w.Bool(false)
c.V1.Sys.Log.Level = w.String("info")
return c
}
// Validate validates that the config is sufficient to start the service and returns true.
func (c *ConfigRequest) Validate() error {
cfgErr := shared.NewInvalidConfigError()
if conn := c.V1.Sys.Connectors; conn != nil {
// we can only have one connector: ldap, saml, or msad_ldap
if (conn.Saml != nil && conn.Ldap != nil) ||
(conn.MsadLdap != nil && conn.Ldap != nil) ||
(conn.MsadLdap != nil && conn.Saml != nil) {
cfgErr.AddInvalidValue("dex.v1.sys.connectors",
"auth config can only have one of the following connectors: [ldap, saml, msad_ldap]")
}
if ldap := conn.Ldap; ldap != nil {
for key, val := range map[string]*google_protobuf.StringValue{
"host": ldap.Host,
"base_user_search_dn": ldap.BaseUserSearchDn,
"username_attr": ldap.UsernameAttr,
"user_id_attr": ldap.UserIdAttr,
} {
if val.GetValue() == "" {
cfgErr.AddMissingKey("dex.v1.sys.connectors.ldap." + key)
}
}
if ldap.BindPassword.GetValue() != "" && ldap.BindDn.GetValue() == "" {
cfgErr.AddInvalidValue("dex.v1.sys.connectors.ldap", "bind_password with unset bind_dn is invalid")
}
// optionally verify ca_contents
if caContents := ldap.CaContents.GetValue(); caContents != "" {
checkCertsPEM(cfgErr, "dex.v1.sys.connectors.ldap.ca_contents", caContents)
}
}
if msad := conn.MsadLdap; msad != nil {
for key, val := range map[string]*google_protobuf.StringValue{
"host": msad.Host,
"base_user_search_dn": msad.BaseUserSearchDn,
} {
if val.GetValue() == "" {
cfgErr.AddMissingKey("dex.v1.sys.connectors.msad_ldap." + key)
}
}
if msad.BindPassword.GetValue() != "" && msad.BindDn.GetValue() == "" {
cfgErr.AddInvalidValue("dex.v1.sys.connectors.msad", "bind_password with unset bind_dn is invalid")
}
// optionally verify ca_contents
if caContents := msad.CaContents.GetValue(); caContents != "" {
checkCertsPEM(cfgErr, "dex.v1.sys.connectors.msad.ca_contents", caContents)
}
}
if saml := conn.Saml; saml != nil {
for key, val := range map[string]*google_protobuf.StringValue{
"ca_contents": saml.CaContents,
"sso_url": saml.SsoUrl,
"username_attr": saml.UsernameAttr,
"email_attr": saml.EmailAttr,
} {
if val.GetValue() == "" {
cfgErr.AddMissingKey("dex.v1.sys.connectors.saml." + key)
}
}
checkCertsPEM(cfgErr, "dex.v1.sys.connector.saml.ca_contents", saml.CaContents.GetValue())
}
}
if cfgErr.IsEmpty() {
return nil
}
return cfgErr
}
// PrepareSystemConfig returns a system configuration that can be used
// to start the service.
func (c *ConfigRequest) PrepareSystemConfig(creds *shared.TLSCredentials) (shared.PreparedSystemConfig, error) {
sys := c.V1.Sys
sys.Tls = creds
// Note: it's common to use a case variant for sAMAccountName that will cause
// issues only at login-time.
// For querying LDAP, attribute case doesn't matter. However, when
// constructing the user entry from the response, dex currently does a case-
// sensitive string match. To work around that issue, we ensure that commonly
// "odd-cased" attribute names are changed to reflect what's in their
// corresponding OID registration entry, and this should coincide with what
// any actual LDAP service should return.
//
// There's an upstream PR to fix this here: https://github.com/dexidp/dex/pull/1251
// However, this workaround will make the papercut go away faster.
c.V1.Sys.GetConnectors().GetLdap().fixCommonCaseIssues()
return c.V1.Sys, nil
}
// SetGlobalConfig imports settings from the global configuration
func (c *ConfigRequest) SetGlobalConfig(g *shared.GlobalConfig) {
c.V1.Sys.Mlsa = g.V1.Mlsa
c.V1.Sys.Service.ExternalFqdn = g.V1.Fqdn
if logLevel := g.GetV1().GetLog().GetLevel().GetValue(); logLevel != "" {
c.V1.Sys.Log.Level.Value = GlobalLogLevelToDexLevel(logLevel)
}
}
// Convert the accepted GlobalLogLevels to a log level accepted by
// Dex.
func GlobalLogLevelToDexLevel(level string) string {
switch level {
case "info":
return "info"
case "debug":
return "debug"
case "warning", "error", "fatal", "panic":
return "error"
default:
return "info"
}
}
func (ldapCfg *ConfigRequest_V1_Ldap) fixCommonCaseIssues() {
if ldapCfg == nil {
return
}
// Note: we fix every user-controlled ldap attribute, let's not assume too
// much and only fix half the issues
ldapCfg.UserIdAttr = fixCase(ldapCfg.UserIdAttr)
ldapCfg.UsernameAttr = fixCase(ldapCfg.UsernameAttr)
ldapCfg.UserDisplayNameAttr = fixCase(ldapCfg.UserDisplayNameAttr)
ldapCfg.GroupDisplayNameAttr = fixCase(ldapCfg.GroupDisplayNameAttr)
ldapCfg.FilterGroupsByUserAttr = fixCase(ldapCfg.FilterGroupsByUserAttr)
// This is also an attribute, albeit an oddly-named one
ldapCfg.FilterGroupsByUserValue = fixCase(ldapCfg.FilterGroupsByUserValue)
ldapCfg.EmailAttr = fixCase(ldapCfg.EmailAttr)
}
func fixCase(wrap *google_protobuf.StringValue) *google_protobuf.StringValue {
if wrap == nil {
return nil
}
return w.String(fix(wrap.Value))
}
func fix(in string) string {
// initialize commonAttrs
// key => val is samaccountname (all lowercase) => sAMAccountName (defined case)
commonAttrs := map[string]string{}
for _, attr := range []string{
"DN", // Note: DN is handled in a special way and thus needs to be uppercase
"sAMAccountName",
"cn",
"mail",
"uid",
"gidNumber",
"memberOf",
} {
commonAttrs[strings.ToLower(attr)] = attr
}
if out, ok := commonAttrs[strings.ToLower(in)]; ok {
return out
}
return in
}
func checkCertsPEM(cfgErr *shared.InvalidConfigError, key, data string) {
caData := []byte(data)
var block *pem.Block
for {
block, caData = pem.Decode(caData)
if block == nil {
if len(caData) > 0 { // nothing decoded but there's data left -- bad input
cfgErr.AddInvalidValue(key,
fmt.Sprintf("invalid certificate data: %q", string(caData)))
}
break
} else if block.Type != "CERTIFICATE" {
cfgErr.AddInvalidValue(key,
fmt.Sprintf("invalid PEM type: %q, expected \"CERTIFICATE\"", block.Type))
}
}
}