-
Notifications
You must be signed in to change notification settings - Fork 88
/
dex.go
197 lines (174 loc) · 5.32 KB
/
dex.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
package deploy
import (
"context"
"encoding/json"
"fmt"
"github.com/dexidp/dex/connector/oidc"
"github.com/dexidp/dex/server"
dexserver "github.com/dexidp/dex/server"
dexstorage "github.com/dexidp/dex/storage"
ghodssyaml "github.com/ghodss/yaml"
"github.com/pkg/errors"
kotsv1beta1 "github.com/replicatedhq/kots/kotskinds/apis/kots/v1beta1"
dextypes "github.com/replicatedhq/kots/pkg/identity/types/dex"
"github.com/replicatedhq/kots/pkg/template"
)
func getDexConfig(ctx context.Context, issuerURL string, options Options) ([]byte, error) {
identitySpec := options.IdentitySpec
identityConfigSpec := options.IdentityConfigSpec
builder := options.Builder
cipher := options.Cipher
redirectURIs, err := buildIdentitySpecOIDCRedirectURIs(identitySpec.OIDCRedirectURIs, builder)
if err != nil {
return nil, errors.Wrap(err, "failed to build identity spec oicd redirect uris")
}
webConfigIssuer := options.NamePrefix
if identitySpec.WebConfig != nil && identitySpec.WebConfig.Title != "" {
webConfigIssuer = identitySpec.WebConfig.Title
}
frontend := dexserver.WebConfig{
Issuer: webConfigIssuer,
LogoURL: "theme/logo.png",
}
if identitySpec.WebConfig != nil {
frontend.Theme = "kots"
if identitySpec.WebConfig.Theme != nil && identitySpec.WebConfig.Theme.LogoURL != "" {
frontend.LogoURL = identitySpec.WebConfig.Theme.LogoURL
}
}
config := dextypes.Config{
Issuer: issuerURL,
Storage: dextypes.Storage{
Type: "postgres",
Config: dextypes.Postgres{
SSL: dextypes.SSL{
Mode: "disable", // TODO ssl
},
},
},
Web: dextypes.Web{
HTTP: "0.0.0.0:5556",
},
Frontend: frontend,
OAuth2: dextypes.OAuth2{
SkipApprovalScreen: true,
AlwaysShowLoginScreen: identitySpec.OAUTH2AlwaysShowLoginScreen,
},
Expiry: dextypes.Expiry{
IDTokens: identitySpec.IDTokensExpiration,
SigningKeys: identitySpec.SigningKeysExpiration,
},
StaticClients: []dexstorage.Client{
{
ID: identityConfigSpec.ClientID,
Name: identityConfigSpec.ClientID,
SecretEnv: "DEX_CLIENT_SECRET",
RedirectURIs: redirectURIs,
},
},
EnablePasswordDB: false,
}
dexConnectors := []kotsv1beta1.DexConnector{}
if cipher != nil {
dexConnectors, err = identityConfigSpec.DexConnectors.GetValue(*cipher)
if err != nil {
return nil, errors.Wrap(err, "failed to decrypt dex connectors")
}
} else if identityConfigSpec.DexConnectors.ValueEncrypted != "" {
return nil, errors.New("cipher required")
} else {
// NOTE: we do not encrypt kotsadm config
dexConnectors = identityConfigSpec.DexConnectors.Value
}
connectors := []kotsv1beta1.DexConnector{}
for _, connector := range dexConnectors {
if len(identitySpec.SupportedProviders) == 0 || stringInSlice(connector.Type, identitySpec.SupportedProviders) {
connectors = append(connectors, connector)
}
}
if len(connectors) == 0 {
return nil, errors.New("at lease one dex connector is required")
}
if len(connectors) > 0 {
dexConnectors, err := DexConnectorsToDexTypeConnectors(connectors)
if err != nil {
return nil, errors.Wrap(err, "failed to unmarshal dex connectors")
}
config.StaticConnectors = dexConfigReplaceDynamicValues(issuerURL, dexConnectors)
}
if err := config.Validate(); err != nil {
return nil, errors.Wrap(err, "failed to validate dex config")
}
marshalledConfig, err := ghodssyaml.Marshal(config)
if err != nil {
return nil, errors.Wrap(err, "failed to marshal dex config")
}
return marshalledConfig, nil
}
func dexConfigReplaceDynamicValues(issuerURL string, connectors []dextypes.Connector) []dextypes.Connector {
next := make([]dextypes.Connector, len(connectors))
for i, connector := range connectors {
switch c := connector.Config.(type) {
case *oidc.Config:
c.RedirectURI = dexCallbackURL(issuerURL)
}
next[i] = connector
}
return next
}
func DexConnectorsToDexTypeConnectors(conns []kotsv1beta1.DexConnector) ([]dextypes.Connector, error) {
dexConnectors := []dextypes.Connector{}
for _, conn := range conns {
f, ok := server.ConnectorsConfig[conn.Type]
if !ok {
return nil, errors.Errorf("unknown connector type %q", conn.Type)
}
connConfig := f()
if len(conn.Config.Raw) != 0 {
if err := json.Unmarshal(conn.Config.Raw, connConfig); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal connector config")
}
}
dexConnectors = append(dexConnectors, dextypes.Connector{
Type: conn.Type,
Name: conn.Name,
ID: conn.ID,
Config: connConfig,
})
}
return dexConnectors, nil
}
func dexIssuerURL(identitySpec kotsv1beta1.IdentitySpec, builder *template.Builder) (string, error) {
// TODO: ingress
if builder == nil {
return identitySpec.IdentityIssuerURL, nil
}
return builder.String(identitySpec.IdentityIssuerURL)
}
func dexCallbackURL(issuerURL string) string {
return fmt.Sprintf("%s/callback", issuerURL)
}
func buildIdentitySpecOIDCRedirectURIs(uris []string, builder *template.Builder) ([]string, error) {
if builder == nil {
return uris, nil
}
next := []string{}
for _, uri := range uris {
rendered, err := builder.String(uri)
if err != nil {
return nil, errors.Wrapf(err, "build %q", uri)
}
if rendered != "" {
next = append(next, rendered)
}
}
return next, nil
}
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}