This repository has been archived by the owner on Apr 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathinstall.go
197 lines (176 loc) · 5.65 KB
/
install.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 harbor
import (
"fmt"
pgapi "github.com/flanksource/karina/pkg/api/postgres"
"github.com/flanksource/karina/pkg/phases/postgresoperator"
"github.com/flanksource/karina/pkg/platform"
"github.com/flanksource/konfigadm/pkg/utils"
"golang.org/x/crypto/bcrypt"
)
const (
HaborRegistryUsername = "harbor_registry_user"
)
var manifests = []string{"core", "portal", "registry", "exporter", "redis", "jobservice", "chartmuseum", "trivy"}
func Deploy(p *platform.Platform) error {
if p.Harbor.IsDisabled() {
for _, spec := range manifests {
if err := p.DeleteSpecs("", fmt.Sprintf("harbor/%s.yaml", spec)); err != nil {
p.Warnf("failed to delete specs: %v", err)
}
}
return nil
}
if p.Harbor.S3 == nil {
p.Harbor.S3 = &p.S3.S3Connection
}
if p.Harbor.Bucket != "" {
if err := p.GetOrCreateBucketFor(*p.Harbor.S3, p.Harbor.Bucket); err != nil {
return err
}
}
if err := p.CreateOrUpdateNamespace(Namespace, nil, nil); err != nil {
return err
}
var nonce string
if p.HasSecret(Namespace, "harbor-secret") {
nonce = string((*p.GetSecret(Namespace, "harbor-secret"))["secret"])
} else {
nonce = utils.RandomString(16)
if err := p.CreateOrUpdateSecret("harbor-secret", Namespace, map[string][]byte{
"secret": []byte(nonce),
}); err != nil {
return err
}
}
harborCore := p.GetSecret(Namespace, "harbor-core")
harborRegistry := p.GetSecret(Namespace, "harbor-registry")
var registryPassword []byte
var registryHtPassword []byte
var err error
if harborCore != nil && (*harborCore)["REGISTRY_CREDENTIAL_PASSWORD"] != nil {
registryPassword = (*harborCore)["REGISTRY_CREDENTIAL_PASSWORD"]
registryHtPassword = (*harborRegistry)["REGISTRY_HTPASSWD"]
} else {
registryPassword = []byte(utils.RandomString(16))
registryHtPassword, err = getHtPasswd(string(registryPassword))
if err != nil {
return err
}
}
var csrfKey []byte
if harborCore != nil && (*harborCore)["CSRF_KEY"] != nil {
csrfKey = (*harborCore)["CSRF_KEY"]
} else {
csrfKey = []byte(utils.RandomString(32))
}
if !p.HasSecret(Namespace, "token-key") {
var tokenKey, tokenCrt []byte
if harborCore != nil && (*harborCore)["tls.key"] != nil {
// migrate key over from existing installation
tokenKey = (*harborCore)["tls.key"]
tokenCrt = (*harborCore)["tls.crt"]
} else {
token := p.NewSelfSigned("registry-token")
tokenKey = token.EncodedPrivateKey()
tokenCrt = token.EncodedCertificate()
}
if err := p.CreateOrUpdateSecret("token-key", Namespace, map[string][]byte{
"tls.key": tokenKey,
"tls.crt": tokenCrt,
}); err != nil {
return err
}
}
if p.Harbor.DB == nil {
if !p.ApplyDryRun {
dbConfig := pgapi.NewClusterConfig(dbCluster, dbNames...)
db, err := postgresoperator.GetOrCreateDB(p, dbConfig)
if err != nil {
return fmt.Errorf("deploy: failed to get/update db: %v", err)
}
p.Harbor.DB = db
} else {
p.Debugf("Creating postgres database %s", dbCluster)
}
}
if !p.ApplyDryRun {
chartSecret := map[string][]byte{
"CACHE_REDIS_PASSWORD": []byte{},
}
if p.Harbor.ChartPVC == "" {
chartSecret["AWS_SECRET_ACCESS_KEY"] = []byte(p.Harbor.S3.SecretKey)
}
if err := p.CreateOrUpdateSecret("harbor-chartmuseum", Namespace, chartSecret); err != nil {
return err
}
if err := p.CreateOrUpdateSecret("harbor-trivy", Namespace, map[string][]byte{
"gitHubToken": []byte{},
"redisURL": []byte("redis://harbor-redis:6379/4"),
}); err != nil {
return err
}
if err := p.CreateOrUpdateSecret("harbor-core", Namespace, map[string][]byte{
"HARBOR_ADMIN_PASSWORD": []byte(p.Harbor.AdminPassword),
"POSTGRESQL_PASSWORD": []byte(p.Harbor.DB.Password),
"REGISTRY_CREDENTIAL_PASSWORD": registryPassword,
"secretKey": []byte("not-a-secure-key"),
"secret": []byte(nonce),
"CSRF_KEY": csrfKey,
}); err != nil {
return err
}
registrySecret := map[string][]byte{
"REGISTRY_HTPASSWD": registryHtPassword,
"REGISTRY_HTTP_SECRET": []byte(nonce),
"REGISTRY_REDIS_PASSWORD": []byte(""),
}
if p.Harbor.RegistryPVC == "" {
registrySecret["REGISTRY_STORAGE_S3_ACCESSKEY"] = []byte(p.Harbor.S3.AccessKey)
registrySecret["REGISTRY_STORAGE_S3_SECRETKEY"] = []byte(p.Harbor.S3.SecretKey)
}
if err := p.CreateOrUpdateSecret("harbor-registry", Namespace, registrySecret); err != nil {
return err
}
if err := p.CreateOrUpdateSecret("harbor-jobservice", Namespace, map[string][]byte{
"secret": []byte(nonce),
}); err != nil {
return err
}
if err := p.CreateOrUpdateConfigMap("trusted-certs", Namespace, map[string]string{
"ca.crt": string(p.GetIngressCA().GetPublicChain()[0].EncodedCertificate()),
}); err != nil {
return err
}
exporterSecret := map[string][]byte{
"HARBOR_ADMIN_PASSWORD": []byte(p.Harbor.AdminPassword),
"HARBOR_DATABASE_PASSWORD": []byte(p.Harbor.DB.Password),
}
if err := p.CreateOrUpdateSecret("harbor-exporter", Namespace, exporterSecret); err != nil {
return err
}
}
for _, spec := range manifests {
if err := p.ApplySpecs("", fmt.Sprintf("harbor/%s.yaml", spec)); err != nil {
return err
}
}
// Skip connecting if in dry run mode
if p.ApplyDryRun {
return nil
}
client, err := NewClient(p)
if err != nil {
return err
}
if err := client.UpdateSettings(*p.Harbor.Settings); err != nil {
p.Errorf("Failed to update harbor settings: %v", err)
}
return nil
}
func getHtPasswd(password string) ([]byte, error) {
passwordBytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return nil, err
}
return []byte(fmt.Sprintf("%s:%s", HaborRegistryUsername, string(passwordBytes))), nil
}