-
Notifications
You must be signed in to change notification settings - Fork 786
/
create_addon_sso.go
255 lines (220 loc) · 7.62 KB
/
create_addon_sso.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 cmd
import (
"fmt"
"io"
"sort"
"strings"
"github.com/pkg/errors"
"github.com/spf13/cobra"
survey "gopkg.in/AlecAivazis/survey.v1"
"gopkg.in/AlecAivazis/survey.v1/terminal"
"github.com/jenkins-x/jx/pkg/gits"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/kube/pki"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
)
const (
defaultSSONamesapce = "sso"
defaultSSOReleaseNamePrefix = "jx-sso"
repoName = "jenkinsxio"
repoURL = "https://chartmuseum.jx.cd.jenkins-x.io"
dexServiceName = "dex"
operatorServiceName = "operator"
githubNewOAuthAppURL = "https://github.com/settings/applications/new"
defaultDexVersion = "2.13.4"
defaultOperatorVersion = "1.2.5"
)
var (
CreateAddonSSOLong = templates.LongDesc(`
Creates the Single Sign-On addon
This addon will install and configure the dex identity provider, sso-operator and cert-manager.
`)
CreateAddonSSOExample = templates.Examples(`
# Create the sso addon
jx create addon sso
`)
)
// CreateAddonSSOptions the options for the create sso addon
type CreateAddonSSOOptions struct {
CreateAddonOptions
DexVersion string
}
// NewCmdCreateAddonSSO creates a command object for the "create addon sso" command
func NewCmdCreateAddonSSO(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
commonOptions := CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
}
options := &CreateAddonSSOOptions{
CreateAddonOptions: CreateAddonOptions{
CreateOptions: CreateOptions{
CommonOptions: commonOptions,
},
},
}
cmd := &cobra.Command{
Use: "sso",
Short: "Create a SSO addon for Single Sign-On",
Long: CreateAddonSSOLong,
Example: CreateAddonSSOExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
options.addCommonFlags(cmd)
cmd.Flags().StringVarP(&options.DexVersion, "dex-version", "", defaultDexVersion, "The dex chart version to install)")
options.addFlags(cmd, defaultSSONamesapce, defaultSSOReleaseNamePrefix, defaultOperatorVersion)
return cmd
}
// Run implements the command
func (o *CreateAddonSSOOptions) Run() error {
client, err := o.KubeClient()
if err != nil {
return fmt.Errorf("cannot connect to Kubernetes cluster: %v", err)
}
o.devNamespace, _, err = kube.GetDevNamespace(client, o.currentNamespace)
if err != nil {
return errors.Wrap(err, "retrieving the development namespace")
}
err = o.ensureCertmanager()
if err != nil {
return errors.Wrap(err, "ensuring cert-manager is installed")
}
log.Infof("Installing %s...\n", util.ColorInfo("dex identity provider"))
ingressConfig, err := kube.GetIngressConfig(client, o.devNamespace)
if err != nil {
return errors.Wrap(err, "retrieving existing ingress configuration")
}
domain, err := util.PickValue("Domain:", ingressConfig.Domain, true, "", o.In, o.Out, o.Err)
if err != nil {
return err
}
log.Infof("Configuring %s connector\n", util.ColorInfo("GitHub"))
log.Infof("Please go to %s and create a new OAuth application with an Authorization Callback URL of %s.\nChoose a suitable Application name and Homepage URL.\n",
util.ColorInfo(githubNewOAuthAppURL), util.ColorInfo(o.dexCallback(domain)))
log.Infof("Copy the %s and the %s and paste them into the form below:\n",
util.ColorInfo("Client ID"), util.ColorInfo("Client Secret"))
clientID, err := util.PickValue("Client ID:", "", true, "", o.In, o.Out, o.Err)
if err != nil {
return err
}
clientSecret, err := util.PickPassword("Client Secret:", "", o.In, o.Out, o.Err)
if err != nil {
return err
}
authorizedOrgs, err := o.getAuthorizedOrgs()
if err != nil {
return err
}
err = o.ensureHelm()
if err != nil {
return errors.Wrap(err, "checking if helm is installed")
}
err = o.addHelmRepoIfMissing(repoURL, repoName, "", "")
if err != nil {
return errors.Wrap(err, "adding dex chart helm repository")
}
err = o.installDex(o.dexDomain(domain), clientID, clientSecret, authorizedOrgs)
if err != nil {
return errors.Wrap(err, "installing dex")
}
log.Infof("Installing %s...\n", util.ColorInfo("sso-operator"))
dexGrpcService := fmt.Sprintf("%s.%s", dexServiceName, o.Namespace)
err = o.installSSOOperator(dexGrpcService)
if err != nil {
return errors.Wrap(err, "installing sso-operator")
}
log.Infof("Exposing services with %s enabled...\n", util.ColorInfo("TLS"))
return o.exposeSSO()
}
func (o *CreateAddonSSOOptions) dexDomain(domain string) string {
return fmt.Sprintf("%s.%s.%s", dexServiceName, o.Namespace, domain)
}
func (o *CreateAddonSSOOptions) dexCallback(domain string) string {
return fmt.Sprintf("https://%s/callback", o.dexDomain(domain))
}
func (o *CreateAddonSSOOptions) getAuthorizedOrgs() ([]string, error) {
surveyOpts := survey.WithStdio(o.In, o.Out, o.Err)
authConfigSvc, err := o.CreateGitAuthConfigService()
if err != nil {
return nil, err
}
config := authConfigSvc.Config()
server := config.GetOrCreateServer(gits.GitHubURL)
userAuth, err := config.PickServerUserAuth(server, "Git user name", true, "", o.In, o.Out, o.Err)
if err != nil {
return nil, err
}
provider, err := gits.CreateProvider(server, userAuth, o.Git())
if err != nil {
return nil, err
}
orgs := gits.GetOrganizations(provider, userAuth.Username)
if len(orgs) == 0 {
return nil, fmt.Errorf("user '%s' does not have any GitHub organizations", userAuth.Username)
}
orgChecker, ok := provider.(gits.OrganisationChecker)
if !ok || orgChecker == nil {
return nil, errors.New("failed to create the GitHub organisation checker")
}
orgsWithMembers := []string{}
for _, org := range orgs {
member, err := orgChecker.IsUserInOrganisation(userAuth.Username, org)
if err != nil {
continue
}
if member {
orgsWithMembers = append(orgsWithMembers, org)
}
}
if len(orgsWithMembers) == 0 {
return nil, fmt.Errorf("user '%s' is not member of any GitHub organizations", userAuth.Username)
}
sort.Strings(orgsWithMembers)
promt := &survey.MultiSelect{
Message: "Select GitHub organizations to authorize users from:",
Options: orgsWithMembers,
}
authorizedOrgs := []string{}
err = survey.AskOne(promt, &authorizedOrgs, nil, surveyOpts)
return authorizedOrgs, err
}
func (o *CreateAddonSSOOptions) installDex(domain string, clientID string, clientSecret string, authorizedOrgs []string) error {
values := []string{
"connectors.github.config.clientID=" + clientID,
"connectors.github.config.clientSecret=" + clientSecret,
fmt.Sprintf("connectors.github.config.orgs={%s}", strings.Join(authorizedOrgs, ",")),
"domain=" + domain,
"certs.grpc.ca.namespace=" + pki.CertManagerNamespace,
}
setValues := strings.Split(o.SetValues, ",")
values = append(values, setValues...)
releaseName := o.ReleaseName + "-" + dexServiceName
return o.installChart(releaseName, kube.ChartSsoDex, o.DexVersion, o.Namespace, true, values, nil, "")
}
func (o *CreateAddonSSOOptions) installSSOOperator(dexGrpcService string) error {
values := []string{
"dex.grpcHost=" + dexGrpcService,
}
setValues := strings.Split(o.SetValues, ",")
values = append(values, setValues...)
releaseName := o.ReleaseName + "-" + operatorServiceName
return o.installChart(releaseName, kube.ChartSsoOperator, o.Version, o.Namespace, true, values, nil, "")
}
func (o *CreateAddonSSOOptions) exposeSSO() error {
upgradeIngOpts := &UpgradeIngressOptions{
CreateOptions: CreateOptions{
CommonOptions: o.CommonOptions,
},
Namespaces: []string{o.Namespace},
WaitForCerts: true,
}
return upgradeIngOpts.Run()
}