-
Notifications
You must be signed in to change notification settings - Fork 90
/
admin-console.go
177 lines (151 loc) · 4.96 KB
/
admin-console.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
package upstream
import (
"io/ioutil"
"os"
"path"
"github.com/google/uuid"
"github.com/manifoldco/promptui"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/kotsadm"
kotsadmtypes "github.com/replicatedhq/kots/pkg/kotsadm/types"
"github.com/replicatedhq/kots/pkg/upstream/types"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes/scheme"
)
type UpstreamSettings struct {
SharedPassword string
SharedPasswordBcrypt string
S3AccessKey string
S3SecretKey string
JWT string
PostgresPassword string
APIEncryptionKey string
AutoCreateClusterToken string
}
func generateAdminConsoleFiles(renderDir string, sharedPassword string) ([]types.UpstreamFile, error) {
if _, err := os.Stat(path.Join(renderDir, "admin-console")); os.IsNotExist(err) {
settings := &UpstreamSettings{
SharedPassword: sharedPassword,
AutoCreateClusterToken: uuid.New().String(),
}
return generateNewAdminConsoleFiles(settings)
}
existingFiles, err := ioutil.ReadDir(path.Join(renderDir, "admin-console"))
if err != nil {
return nil, errors.Wrap(err, "failed to read existing files")
}
settings := &UpstreamSettings{
AutoCreateClusterToken: uuid.New().String(),
}
if err := loadUpstreamSettingsFromFiles(settings, renderDir, existingFiles); err != nil {
return nil, errors.Wrap(err, "failed to find existing settings")
}
return generateNewAdminConsoleFiles(settings)
}
func loadUpstreamSettingsFromFiles(settings *UpstreamSettings, renderDir string, files []os.FileInfo) error {
for _, fi := range files {
data, err := ioutil.ReadFile(path.Join(renderDir, "admin-console", fi.Name()))
if err != nil {
return errors.Wrap(err, "failed to read file")
}
decode := scheme.Codecs.UniversalDeserializer().Decode
obj, gvk, err := decode(data, nil, nil)
if err != nil {
continue
}
if gvk.Group == "" && gvk.Version == "v1" && gvk.Kind == "Secret" {
loadUpstreamSettingsFromSecret(settings, obj.(*corev1.Secret))
} else if gvk.Group == "apps" && gvk.Version == "v1" && gvk.Kind == "Deployment" {
loadUpstreamSettingsFromDeployment(settings, obj.(*appsv1.Deployment))
}
}
return nil
}
func loadUpstreamSettingsFromSecret(settings *UpstreamSettings, secret *corev1.Secret) {
switch secret.Name {
case "kotsadm-password":
settings.SharedPasswordBcrypt = string(secret.Data["passwordBcrypt"])
case "kotsadm-minio":
settings.S3AccessKey = string(secret.Data["accesskey"])
settings.S3SecretKey = string(secret.Data["secretkey"])
case "kotsadm-session":
settings.JWT = string(secret.Data["key"])
case "kotsadm-postgres":
settings.PostgresPassword = string(secret.Data["password"])
case "kotsadm-encryption":
settings.APIEncryptionKey = string(secret.Data["encryptionKey"])
}
}
func loadUpstreamSettingsFromDeployment(settings *UpstreamSettings, deployment *appsv1.Deployment) {
for _, c := range deployment.Spec.Template.Spec.Containers {
for _, e := range c.Env {
switch e.Name {
case "AUTO_CREATE_CLUSTER_TOKEN", "KOTSADM_TOKEN":
settings.AutoCreateClusterToken = e.Value
}
}
}
}
func generateNewAdminConsoleFiles(settings *UpstreamSettings) ([]types.UpstreamFile, error) {
upstreamFiles := []types.UpstreamFile{}
deployOptions := kotsadmtypes.DeployOptions{
Namespace: "default",
SharedPassword: settings.SharedPassword,
SharedPasswordBcrypt: settings.SharedPasswordBcrypt,
S3AccessKey: settings.S3AccessKey,
S3SecretKey: settings.S3SecretKey,
JWT: settings.JWT,
PostgresPassword: settings.PostgresPassword,
APIEncryptionKey: settings.APIEncryptionKey,
AutoCreateClusterToken: settings.AutoCreateClusterToken,
}
if deployOptions.SharedPasswordBcrypt == "" && deployOptions.SharedPassword == "" {
p, err := promptForSharedPassword()
if err != nil {
return nil, errors.Wrap(err, "failed to prompt for shared password")
}
deployOptions.SharedPassword = p
}
adminConsoleDocs, err := kotsadm.YAML(deployOptions)
if err != nil {
return nil, errors.Wrap(err, "failed to get minio yaml")
}
for n, v := range adminConsoleDocs {
upstreamFile := types.UpstreamFile{
Path: path.Join("admin-console", n),
Content: v,
}
upstreamFiles = append(upstreamFiles, upstreamFile)
}
return upstreamFiles, nil
}
func promptForSharedPassword() (string, error) {
templates := &promptui.PromptTemplates{
Prompt: "{{ . | bold }} ",
Valid: "{{ . | green }} ",
Invalid: "{{ . | red }} ",
Success: "{{ . | bold }} ",
}
prompt := promptui.Prompt{
Label: "Enter a new password to be used for the Admin Console:",
Templates: templates,
Mask: rune('•'),
Validate: func(input string) error {
if len(input) < 6 {
return errors.New("please enter a longer password")
}
return nil
},
}
for {
result, err := prompt.Run()
if err != nil {
if err == promptui.ErrInterrupt {
os.Exit(-1)
}
continue
}
return result, nil
}
}