-
Notifications
You must be signed in to change notification settings - Fork 786
/
admin_secrets.go
275 lines (231 loc) · 8.06 KB
/
admin_secrets.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
package config
import (
"fmt"
"github.com/ghodss/yaml"
"io/ioutil"
"strings"
"github.com/jenkins-x/jx/v2/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/pkg/errors"
"github.com/sethvargo/go-password/password"
"github.com/spf13/cobra"
)
const defaultMavenSettings = `<settings>
<!-- sets the local maven repository outside of the ~/.m2 folder for easier mounting of secrets and repo -->
<localRepository>${user.home}/.mvnrepository</localRepository>
<!-- lets disable the download progress indicator that fills up logs -->
<interactiveMode>false</interactiveMode>
<mirrors>
<mirror>
<id>nexus</id>
<mirrorOf>external:*</mirrorOf>
<url>http://nexus/repository/maven-group/</url>
</mirror>
</mirrors>
<servers>
<server>
<id>nexus</id>
<username>admin</username>
<password>%s</password>
</server>
</servers>
<profiles>
<profile>
<id>nexus</id>
<properties>
<altDeploymentRepository>nexus::default::http://nexus/repository/maven-snapshots/</altDeploymentRepository>
<altReleaseDeploymentRepository>nexus::default::http://nexus/repository/maven-releases/</altReleaseDeploymentRepository>
<altSnapshotDeploymentRepository>nexus::default::http://nexus/repository/maven-snapshots/</altSnapshotDeploymentRepository>
</properties>
</profile>
<profile>
<id>release</id>
<properties>
<gpg.executable>gpg</gpg.executable>
<gpg.passphrase>mysecretpassphrase</gpg.passphrase>
</properties>
</profile>
</profiles>
<activeProfiles>
<!--make the profile active all the time -->
<activeProfile>nexus</activeProfile>
</activeProfiles>
</settings>
`
const allowedSymbols = "~!#%^_+-=?,."
type ChartMuseum struct {
ChartMuseumEnv ChartMuseumEnv `json:"env"`
}
type ChartMuseumEnv struct {
ChartMuseumSecret ChartMuseumSecret `json:"secret"`
}
type ChartMuseumSecret struct {
User string `json:"BASIC_AUTH_USER"`
Password string `json:"BASIC_AUTH_PASS"`
}
type Grafana struct {
GrafanaSecret GrafanaSecret `json:"server"`
}
type GrafanaSecret struct {
User string `json:"adminUser"`
Password string `json:"adminPassword"`
}
type Jenkins struct {
JenkinsSecret JenkinsAdminSecret `json:"Master"`
}
type JenkinsAdminSecret struct {
Password string `json:"AdminPassword"`
}
type PipelineSecrets struct {
MavenSettingsXML string `json:"MavenSettingsXML,omitempty"`
}
// KanikoSecret store the kaniko service account
type KanikoSecret struct {
Data string `json:"Data,omitempty"`
}
type AdminSecretsConfig struct {
IngressBasicAuth string `json:"JXBasicAuth,omitempty"`
ChartMuseum *ChartMuseum `json:"chartmuseum,omitempty"`
Grafana *Grafana `json:"grafana,omitempty"`
Jenkins *Jenkins `json:"jenkins,omitempty"`
Nexus *Nexus `json:"nexus,omitempty"`
PipelineSecrets *PipelineSecrets `json:"PipelineSecrets,omitempty"`
KanikoSecret *KanikoSecret `json:"KanikoSecret,omitempty"`
}
type Nexus struct {
DefaultAdminPassword string `json:"defaultAdminPassword,omitempty"`
}
type AdminSecretsService struct {
FileName string
Secrets AdminSecretsConfig
Flags AdminSecretsFlags
ingressAuth BasicAuth
}
type AdminSecretsFlags struct {
DefaultAdminUsername string
DefaultAdminPassword string
KanikoSecret string
}
func (s *AdminSecretsService) AddAdminSecretsValues(cmd *cobra.Command) {
cmd.Flags().StringVarP(&s.Flags.DefaultAdminPassword, "default-admin-password", "", "", "the default admin password to access Jenkins, Kubernetes Dashboard, ChartMuseum and Nexus")
cmd.Flags().StringVarP(&s.Flags.DefaultAdminUsername, "default-admin-username", "", "admin", "the default admin username to access Jenkins, Kubernetes Dashboard, ChartMuseum and Nexus")
}
func (s *AdminSecretsService) NewAdminSecretsConfig() error {
s.Secrets = AdminSecretsConfig{
ChartMuseum: &ChartMuseum{},
Grafana: &Grafana{},
Jenkins: &Jenkins{},
PipelineSecrets: &PipelineSecrets{},
Nexus: &Nexus{},
KanikoSecret: &KanikoSecret{},
}
if s.Flags.DefaultAdminPassword == "" {
log.Logger().Debugf("No default password set, generating a random one")
input := password.GeneratorInput{
Symbols: allowedSymbols,
}
generator, err := password.NewGenerator(&input)
if err != nil {
return errors.Wrap(err, "unable to create password generator")
}
s.Flags.DefaultAdminPassword, _ = generator.Generate(20, 4, 2, false, true)
}
err := s.setDefaultSecrets()
if err != nil {
return errors.Wrap(err, "unable to set default secrets")
}
err = s.NewMavenSettingsXML()
if err != nil {
return errors.Wrap(err, "unable to generate maven settings")
}
s.newIngressBasicAuth()
s.newKanikoSecret()
return nil
}
func (s *AdminSecretsService) setDefaultSecrets() error {
s.Secrets.Jenkins.JenkinsSecret.Password = s.Flags.DefaultAdminPassword
s.Secrets.ChartMuseum.ChartMuseumEnv.ChartMuseumSecret.User = "admin"
s.Secrets.ChartMuseum.ChartMuseumEnv.ChartMuseumSecret.Password = s.Flags.DefaultAdminPassword
s.Secrets.Grafana.GrafanaSecret.User = "admin"
s.Secrets.Grafana.GrafanaSecret.Password = s.Flags.DefaultAdminPassword
s.Secrets.Nexus.DefaultAdminPassword = s.Flags.DefaultAdminPassword
return nil
}
// NewMavenSettingsXML generates the maven settings
func (s *AdminSecretsService) NewMavenSettingsXML() error {
s.Secrets.PipelineSecrets.MavenSettingsXML = fmt.Sprintf(defaultMavenSettings, s.Flags.DefaultAdminPassword)
return nil
}
func (s *AdminSecretsService) NewAdminSecretsConfigFromSecret(decryptedSecretsFile string) error {
a := AdminSecretsConfig{}
data, err := ioutil.ReadFile(decryptedSecretsFile)
if err != nil {
return errors.Wrap(err, "unable to read file")
}
err = yaml.Unmarshal(data, &a)
if err != nil {
return errors.Wrap(err, "unable to unmarshall secrets")
}
s.Secrets = a
s.Flags.DefaultAdminPassword = s.Secrets.Jenkins.JenkinsSecret.Password
err = s.setDefaultSecrets()
if err != nil {
return errors.Wrap(err, "unable to set default secrets")
}
s.updateIngressBasicAuth()
return nil
}
func (s *AdminSecretsService) newKanikoSecret() {
s.Secrets.KanikoSecret.Data = s.Flags.KanikoSecret
}
func (s *AdminSecretsService) newIngressBasicAuth() {
password := s.Flags.DefaultAdminPassword
username := "admin"
s.ingressAuth = BasicAuth{
Username: "admin",
Password: password,
}
hash := util.HashPassword(password)
s.Secrets.IngressBasicAuth = fmt.Sprintf("%s:{SHA}%s", username, hash)
}
func (s *AdminSecretsService) updateIngressBasicAuth() {
password := s.Flags.DefaultAdminPassword
parts := strings.Split(s.Secrets.IngressBasicAuth, ":")
username := parts[0]
s.ingressAuth = BasicAuth{
Username: username,
Password: password,
}
}
// JenkinsAuth returns the current basic auth credentials for Jenkins
func (s *AdminSecretsService) JenkinsAuth() BasicAuth {
return BasicAuth{
Username: "admin",
Password: s.Secrets.Jenkins.JenkinsSecret.Password,
}
}
// IngressAuth returns the current basic auth credentials for Ingress
func (s *AdminSecretsService) IngressAuth() BasicAuth {
return s.ingressAuth
}
// ChartMuseumAuth returns the current credentials for ChartMuseum
func (s *AdminSecretsService) ChartMuseumAuth() BasicAuth {
return BasicAuth{
Username: s.Secrets.ChartMuseum.ChartMuseumEnv.ChartMuseumSecret.User,
Password: s.Secrets.ChartMuseum.ChartMuseumEnv.ChartMuseumSecret.Password,
}
}
// GrafanaAuth returns the current credentials for Grafana
func (s *AdminSecretsService) GrafanaAuth() BasicAuth {
return BasicAuth{
Username: s.Secrets.Grafana.GrafanaSecret.User,
Password: s.Secrets.Grafana.GrafanaSecret.Password,
}
}
// NexusAuth returns the current credentials for Nexus
func (s *AdminSecretsService) NexusAuth() BasicAuth {
return BasicAuth{
Username: "admin",
Password: s.Secrets.Nexus.DefaultAdminPassword,
}
}