-
Notifications
You must be signed in to change notification settings - Fork 7
/
settings.go
150 lines (143 loc) · 4.58 KB
/
settings.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
package kube
import (
"crypto/rand"
"fmt"
"github.com/devtron-labs/authenticator/client"
passwordutil "github.com/devtron-labs/authenticator/password"
v1 "k8s.io/api/core/v1"
"math/big"
"time"
)
const letters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-"
func InitialiseSettings(k8sClient *client.K8sClient) error {
restClient, err := k8sClient.GetRestClient()
if err != nil {
return err
}
kubeutil := NewKubeUtil(restClient)
dexConfig, err := client.DexConfigConfigFromEnv()
if err != nil {
return err
}
secret, err := k8sClient.GetDevtronConfig()
if err != nil {
return err
}
var hashedPassword string
newPassword := false
if oldPassword, ok := secret.Data[client.ADMIN_PASSWORD]; !ok || len(oldPassword) == 0 {
randBytes := make([]byte, client.InitialPasswordLength)
for i := 0; i < client.InitialPasswordLength; i++ {
num, err := rand.Int(rand.Reader, big.NewInt(int64(len(letters))))
if err != nil {
return err
}
randBytes[i] = letters[num.Int64()]
}
initialPassword := string(randBytes)
hashedPassword, err = passwordutil.HashPassword(initialPassword)
err = kubeutil.CreateOrUpdateSecretField(client.DevtronDefaultNamespaceName, dexConfig.DevtronSecretName, client.ADMIN_PASSWORD, initialPassword)
if err != nil {
return err
}
newPassword = true
}
passwordTime := time.Now()
err = kubeutil.CreateOrUpdateSecret(client.DevtronDefaultNamespaceName, dexConfig.DevtronSecretName, func(s *v1.Secret, new bool) error {
if s.Data == nil {
s.Data = make(map[string][]byte)
}
if newPassword {
s.Data[client.SettingAdminPasswordHashKey] = []byte(hashedPassword)
s.Data[client.SettingAdminPasswordMtimeKey] = []byte(passwordTime.Format(time.RFC3339))
}
signature := s.Data[client.SettingServerSignatureKey]
if len(signature) == 0 {
signature, err := MakeSignature(32)
if err != nil {
return err
}
s.Data[client.SettingServerSignatureKey] = signature
}
return nil
})
if err != nil {
return err
}
return nil
}
func MigrateDexConfigFromAcdToDevtronSecret(k8sClient *client.K8sClient) (bool, error) {
restClient, err := k8sClient.GetRestClient()
if err != nil {
return false, err
}
kubeutil := NewKubeUtil(restClient)
dexConfig, err := client.DexConfigConfigFromEnv()
if err != nil {
return false, err
}
devtronSecret, err := k8sClient.GetDevtronConfig()
if err != nil {
return false, err
}
acdSecret, acdConfigMap, err := k8sClient.GetArgocdConfig()
if err != nil {
return false, err
}
operationSuccess := false
retryCount := 0
updateRequired := false
for !operationSuccess && retryCount < 3 {
retryCount = retryCount + 1
if devtronSecret.Data == nil {
data := make(map[string][]byte)
devtronSecret.Data = data
}
if acdConfigMap.Data != nil {
if _, ok := devtronSecret.Data[client.SettingDexConfigKey]; !ok {
devtronSecret.Data[client.SettingDexConfigKey] = []byte(acdConfigMap.Data[client.SettingDexConfigKey])
updateRequired = true
}
if _, ok := devtronSecret.Data[client.SettingURLKey]; !ok {
devtronSecret.Data[client.SettingURLKey] = []byte(acdConfigMap.Data[client.SettingURLKey])
updateRequired = true
}
}
if acdSecret.Data != nil {
if _, ok := devtronSecret.Data[client.SettingAdminPasswordHashKey]; !ok {
devtronSecret.Data[client.SettingAdminPasswordHashKey] = acdSecret.Data[client.SettingAdminPasswordHashKey]
updateRequired = true
}
if _, ok := devtronSecret.Data[client.SettingAdminPasswordMtimeKey]; !ok {
devtronSecret.Data[client.SettingAdminPasswordMtimeKey] = acdSecret.Data[client.SettingAdminPasswordMtimeKey]
updateRequired = true
}
if _, ok := devtronSecret.Data[client.SettingServerSignatureKey]; !ok {
devtronSecret.Data[client.SettingServerSignatureKey] = acdSecret.Data[client.SettingServerSignatureKey]
updateRequired = true
}
}
if _, ok := devtronSecret.Data[client.ADMIN_PASSWORD]; !ok {
oldPassword := devtronSecret.Data[client.SettingAdminAcdPasswordKey]
if len(oldPassword) > 0 {
devtronSecret.Data[client.ADMIN_PASSWORD] = oldPassword
updateRequired = true
}
}
// here create or update devtron secret and migrate and store config for dex
if updateRequired {
err = kubeutil.CreateOrUpdateSecret(client.DevtronDefaultNamespaceName, dexConfig.DevtronSecretName, func(s *v1.Secret, new bool) error {
if s.Data == nil {
s.Data = make(map[string][]byte)
}
s.Data = devtronSecret.Data
return nil
})
}
operationSuccess = true
}
if updateRequired && !operationSuccess {
return false, fmt.Errorf("unable to update devtron secret")
}
return true, nil
}