-
Notifications
You must be signed in to change notification settings - Fork 88
/
identity.go
338 lines (264 loc) · 10.1 KB
/
identity.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package cli
import (
"context"
"fmt"
"io/ioutil"
"github.com/pkg/errors"
kotsv1beta1 "github.com/replicatedhq/kots/kotskinds/apis/kots/v1beta1"
"github.com/replicatedhq/kots/pkg/identity"
ingress "github.com/replicatedhq/kots/pkg/ingress"
"github.com/replicatedhq/kots/pkg/k8sutil"
kotsadmtypes "github.com/replicatedhq/kots/pkg/kotsadm/types"
"github.com/replicatedhq/kots/pkg/kotsutil"
"github.com/replicatedhq/kots/pkg/logger"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"k8s.io/client-go/kubernetes"
)
func IdentityServiceCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "identity-service",
Short: "KOTS Identity Service",
Hidden: true,
}
cmd.AddCommand(IdentityServiceInstallCmd())
cmd.AddCommand(IdentityServiceConfigureCmd())
cmd.AddCommand(IdentityServiceUninstallCmd())
cmd.AddCommand(IdentityServiceEnableSharedPasswordCmd())
cmd.AddCommand(IdentityServiceOIDCCallbackURLCmd())
return cmd
}
func IdentityServiceInstallCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "install",
Short: "Install the KOTS Identity Service",
SilenceUsage: true,
SilenceErrors: false,
PreRun: func(cmd *cobra.Command, args []string) {
viper.BindPFlags(cmd.Flags())
},
RunE: func(cmd *cobra.Command, args []string) error {
v := viper.GetViper()
log := logger.NewLogger()
clientset, err := k8sutil.GetClientset(kubernetesConfigFlags)
if err != nil {
return errors.Wrap(err, "failed to get clientset")
}
namespace := v.GetString("namespace")
if err := validateNamespace(namespace); err != nil {
return err
}
ingressConfig, err := ingress.GetConfig(cmd.Context(), namespace)
if err != nil {
return errors.Wrap(err, "failed to get ingress config")
}
identityConfig := kotsv1beta1.IdentityConfig{}
if identityConfigPath := v.GetString("identity-config"); identityConfigPath != "" {
content, err := ioutil.ReadFile(identityConfigPath)
if err != nil {
return errors.Wrap(err, "failed to read identity service config file")
}
s, err := kotsutil.LoadIdentityConfigFromContents(content)
if err != nil {
return errors.Wrap(err, "failed to decoce identity service config")
}
identityConfig = *s
}
registryConfig, err := getRegistryConfig(v)
if err != nil {
return errors.Wrap(err, "failed to get registry config")
}
return identityServiceDeploy(cmd.Context(), log, clientset, namespace, identityConfig, *ingressConfig, registryConfig)
},
}
cmd.Flags().String("identity-config", "", "path to a kots.Identity resource file")
// random other registry flags
cmd.Flags().String("license-file", "", "path to a license file to use when download a replicated app")
cmd.Flags().String("airgap-bundle", "", "path to the application airgap bundle where application metadata will be loaded from")
cmd.Flags().Bool("airgap", false, "set to true to run install in airgapped mode. setting --airgap-bundle implies --airgap=true.")
registryFlags(cmd.Flags())
return cmd
}
func IdentityServiceConfigureCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "configure",
Short: "Configure the KOTS Identity Service",
SilenceUsage: true,
SilenceErrors: false,
PreRun: func(cmd *cobra.Command, args []string) {
viper.BindPFlags(cmd.Flags())
},
RunE: func(cmd *cobra.Command, args []string) error {
v := viper.GetViper()
log := logger.NewLogger()
clientset, err := k8sutil.GetClientset(kubernetesConfigFlags)
if err != nil {
return errors.Wrap(err, "failed to get clientset")
}
namespace := v.GetString("namespace")
if err := validateNamespace(namespace); err != nil {
return err
}
ingressConfig, err := ingress.GetConfig(cmd.Context(), namespace)
if err != nil {
return errors.Wrap(err, "failed to get ingress config")
}
identityConfig := kotsv1beta1.IdentityConfig{}
if identityConfigPath := v.GetString("identity-config"); identityConfigPath != "" {
content, err := ioutil.ReadFile(identityConfigPath)
if err != nil {
return errors.Wrap(err, "failed to read identity service config file")
}
s, err := kotsutil.LoadIdentityConfigFromContents(content)
if err != nil {
return errors.Wrap(err, "failed to decoce identity service config")
}
identityConfig = *s
}
return identityServiceConfigure(cmd.Context(), log, clientset, namespace, identityConfig, *ingressConfig)
},
}
cmd.Flags().String("identity-config", "", "path to a yaml file containing the KOTS identity service configuration")
return cmd
}
func IdentityServiceUninstallCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "uninstall",
Short: "Uninstall the KOTS identity service",
Long: "Uninstall the KOTS identity service. This will re-enable shared password authentication.",
SilenceUsage: true,
SilenceErrors: false,
PreRun: func(cmd *cobra.Command, args []string) {
viper.BindPFlags(cmd.Flags())
},
RunE: func(cmd *cobra.Command, args []string) error {
v := viper.GetViper()
log := logger.NewLogger()
clientset, err := k8sutil.GetClientset(kubernetesConfigFlags)
if err != nil {
return errors.Wrap(err, "failed to get clientset")
}
namespace := v.GetString("namespace")
if err := validateNamespace(namespace); err != nil {
return err
}
log.ChildActionWithSpinner("Updating the Identity Service configuration")
identityConfig, err := identity.GetConfig(cmd.Context(), namespace)
if err != nil {
return errors.Wrap(err, "failed to get identity config")
}
identityConfig.Spec.Enabled = false
identityConfig.Spec.DisablePasswordAuth = false
if err := identity.SetConfig(cmd.Context(), namespace, *identityConfig); err != nil {
return errors.Wrap(err, "failed to set identity config")
}
log.FinishSpinner()
log.ChildActionWithSpinner("Uninstalling the Identity Service")
if err := identity.Undeploy(cmd.Context(), log, clientset, namespace); err != nil {
return errors.Wrap(err, "failed to uninstall the identity service")
}
log.FinishSpinner()
return nil
},
}
return cmd
}
func IdentityServiceEnableSharedPasswordCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "enable-shared-password",
Short: "Enable shared password",
Long: "Enable the shared password login option in the KOTS Admin Console.",
SilenceUsage: true,
SilenceErrors: false,
PreRun: func(cmd *cobra.Command, args []string) {
viper.BindPFlags(cmd.Flags())
},
RunE: func(cmd *cobra.Command, args []string) error {
v := viper.GetViper()
log := logger.NewLogger()
namespace := v.GetString("namespace")
if err := validateNamespace(namespace); err != nil {
return err
}
log.ChildActionWithSpinner("Updating the Identity Service configuration")
identityConfig, err := identity.GetConfig(cmd.Context(), namespace)
if err != nil {
return errors.Wrap(err, "failed to get identity config")
}
identityConfig.Spec.DisablePasswordAuth = false
if err := identity.SetConfig(cmd.Context(), namespace, *identityConfig); err != nil {
return errors.Wrap(err, "failed to set identity config")
}
log.FinishSpinner()
return nil
},
}
return cmd
}
func IdentityServiceOIDCCallbackURLCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "oidc-callback-url",
Short: "Prints OICD callback URL",
SilenceUsage: true,
SilenceErrors: false,
PreRun: func(cmd *cobra.Command, args []string) {
viper.BindPFlags(cmd.Flags())
},
RunE: func(cmd *cobra.Command, args []string) error {
v := viper.GetViper()
namespace := v.GetString("namespace")
if err := validateNamespace(namespace); err != nil {
return err
}
identityConfig, err := identity.GetConfig(cmd.Context(), namespace)
if err != nil {
return errors.Wrap(err, "failed to get identity config")
}
fmt.Fprintln(cmd.OutOrStdout(), identity.DexCallbackURL(identityConfig.Spec))
return nil
},
}
return cmd
}
func identityServiceDeploy(ctx context.Context, log *logger.Logger, clientset kubernetes.Interface, namespace string, identityConfig kotsv1beta1.IdentityConfig, ingressConfig kotsv1beta1.IngressConfig, registryConfig *kotsadmtypes.KotsadmOptions) error {
log.ChildActionWithSpinner("Deploying the Identity Service")
identityConfig.Spec.Enabled = true
identityConfig.Spec.DisablePasswordAuth = true
if identityConfig.Spec.IngressConfig == (kotsv1beta1.IngressConfigSpec{}) {
identityConfig.Spec.IngressConfig.Enabled = false
} else {
identityConfig.Spec.IngressConfig.Enabled = true
}
if err := identity.ValidateConfig(ctx, namespace, identityConfig, ingressConfig); err != nil {
return errors.Wrap(err, "failed to validate identity config")
}
if err := identity.SetConfig(ctx, namespace, identityConfig); err != nil {
return errors.Wrap(err, "failed to set identity config")
}
if err := identity.Deploy(ctx, clientset, namespace, identityConfig, ingressConfig, registryConfig); err != nil {
return errors.Wrap(err, "failed to deploy the identity service")
}
log.FinishSpinner()
return nil
}
func identityServiceConfigure(ctx context.Context, log *logger.Logger, clientset kubernetes.Interface, namespace string, identityConfig kotsv1beta1.IdentityConfig, ingressConfig kotsv1beta1.IngressConfig) error {
log.ChildActionWithSpinner("Configuring the Identity Service")
identityConfig.Spec.Enabled = true
identityConfig.Spec.DisablePasswordAuth = true
if identityConfig.Spec.IngressConfig == (kotsv1beta1.IngressConfigSpec{}) {
identityConfig.Spec.IngressConfig.Enabled = false
} else {
identityConfig.Spec.IngressConfig.Enabled = true
}
if err := identity.ValidateConfig(ctx, namespace, identityConfig, ingressConfig); err != nil {
return errors.Wrap(err, "failed to validate identity config")
}
if err := identity.SetConfig(ctx, namespace, identityConfig); err != nil {
return errors.Wrap(err, "failed to set identity config")
}
if err := identity.Configure(ctx, clientset, namespace, identityConfig, ingressConfig); err != nil {
return errors.Wrap(err, "failed to configure identity service")
}
log.FinishSpinner()
return nil
}