forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin_secrets.go
161 lines (135 loc) · 4.81 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
package config
import (
"fmt"
"strings"
"crypto/sha1"
"encoding/base64"
"github.com/Pallinder/go-randomdata"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
)
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>local-nexus</id>
<username>admin</username>
<password>%s</password>
</server>
</servers>
<profiles>
<profile>
<id>nexus</id>
<properties>
<altDeploymentRepository>local-nexus::default::http://nexus/repository/maven-snapshots/</altDeploymentRepository>
<altReleaseDeploymentRepository>local-nexus::default::http://nexus/repository/maven-releases/</altReleaseDeploymentRepository>
<altSnapshotDeploymentRepository>local-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>
`
type IngressBasicAuth struct {
JXBasicAuth string `yaml:"JXBasicAuth"`
}
type ChartMuseum struct {
ChartMuseumSecret ChartMuseumSecret `yaml:"secret"`
}
type ChartMuseumSecret struct {
User string `yaml:"BASIC_AUTH_USER"`
Password string `yaml:"BASIC_AUTH_PASS"`
}
type Grafana struct {
GrafanaSecret GrafanaSecret `yaml:"server"`
}
type GrafanaSecret struct {
User string `yaml:"adminUser"`
Password string `yaml:"adminPassword"`
}
type Jenkins struct {
JenkinsSecret JenkinsAdminSecret `yaml:"Master"`
}
type JenkinsAdminSecret struct {
Password string `yaml:"AdminPassword"`
}
type PipelineSecrets struct {
MavenSettingsXML string `yaml:"MavenSettingsXML,omitempty"`
}
type AdminSecretsConfig struct {
IngressBasicAuth string `yaml:"JXBasicAuth,omitempty"`
ChartMuseum *ChartMuseum `yaml:"chartmuseum,omitempty"`
Grafana *Grafana `yaml:"grafana,omitempty"`
Jenkins *Jenkins `yaml:"jenkins,omitempty"`
Nexus *Nexus `yaml:"nexus,omitempty"`
PipelineSecrets *PipelineSecrets `yaml:"PipelineSecrets,omitempty"`
}
type Nexus struct {
DefaultAdminPassword string `yaml:"defaultAdminPassword,omitempty"`
}
type AdminSecretsService struct {
FileName string
Secrets AdminSecretsConfig
Flags AdminSecretsFlags
}
type AdminSecretsFlags struct {
DefaultAdminPassword 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")
if s.Flags.DefaultAdminPassword == "" {
s.Flags.DefaultAdminPassword = strings.ToLower(randomdata.SillyName())
}
}
func (c AdminSecretsConfig) String() (string, error) {
b, err := yaml.Marshal(c)
if err != nil {
return "", fmt.Errorf("failed to marshall helm values %v", err)
}
return string(b), nil
}
func (s *AdminSecretsService) NewAdminSecretsConfig() error {
s.Secrets = AdminSecretsConfig{
ChartMuseum: &ChartMuseum{},
Grafana: &Grafana{},
Jenkins: &Jenkins{},
PipelineSecrets: &PipelineSecrets{},
Nexus: &Nexus{},
}
s.Secrets.Jenkins.JenkinsSecret.Password = s.Flags.DefaultAdminPassword
s.Secrets.ChartMuseum.ChartMuseumSecret.User = "admin"
s.Secrets.ChartMuseum.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
s.Secrets.PipelineSecrets.MavenSettingsXML = fmt.Sprintf(defaultMavenSettings, s.Flags.DefaultAdminPassword)
hash := HashSha(s.Flags.DefaultAdminPassword)
s.Secrets.IngressBasicAuth = fmt.Sprintf("admin:{SHA}%s", hash)
return nil
}
func HashSha(password string) string {
s := sha1.New()
s.Write([]byte(password))
passwordSum := s.Sum(nil)
return base64.StdEncoding.EncodeToString(passwordSum)
}