-
-
Notifications
You must be signed in to change notification settings - Fork 125
/
newInstall.go
209 lines (187 loc) · 6.53 KB
/
newInstall.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
package main
import (
"net/http"
"encoding/json"
"time"
"os"
"golang.org/x/crypto/bcrypt"
"github.com/azukaar/cosmos-server/src/utils"
"github.com/azukaar/cosmos-server/src/docker"
)
func waitForDB() {
time.Sleep(1 * time.Second)
err := utils.DB()
if err != nil {
utils.Warn("DB Not ready yet")
waitForDB()
}
}
type NewInstallJSON struct {
MongoDBMode string `json:"mongodbMode"`
MongoDB string `json:"mongodb"`
HTTPSCertificateMode string `json:"httpsCertificateMode"`
TLSCert string `json:"tlsCert"`
TLSKey string `json:"tlsKey"`
Nickname string `json:"nickname"`
Password string `json:"password"`
Email string `json:"omitempty,email"`
Hostname string `json:"hostname"`
Step string `json:"step"`
SSLEmail string `json:"sslEmail",validate:"omitempty,email"`
UseWildcardCertificate bool `json:"useWildcardCertificate",validate:"omitempty"`
DNSChallengeProvider string `json:"dnsChallengeProvider",validate:"omitempty"`
DNSChallengeConfig map[string]string
}
type AdminJSON struct {
Nickname string `validate:"required,min=3,max=32,alphanum"`
Password string `validate:"required,min=9,max=128,containsany=~!@#$%^&*()_+=-{[}]:;"'<>.?/,containsany=ABCDEFGHIJKLMNOPQRSTUVWXYZ,containsany=abcdefghijklmnopqrstuvwxyz,containsany=0123456789"`
}
func NewInstallRoute(w http.ResponseWriter, req *http.Request) {
if !utils.GetMainConfig().NewInstall {
utils.Error("Status: not a new New install", nil)
utils.HTTPError(w, "New install", http.StatusForbidden, "NI001")
return
}
if(req.Method == "POST") {
var request NewInstallJSON
err1 := json.NewDecoder(req.Body).Decode(&request)
if err1 != nil {
utils.Error("NewInstall: Invalid User Request", err1)
utils.HTTPError(w, "New Install: Invalid User Request" + err1.Error(),
http.StatusInternalServerError, "NI001")
return
}
errV := utils.Validate.Struct(request)
if errV != nil {
utils.Error("NewInstall: Invalid User Request", errV)
utils.HTTPError(w, "New Install: Invalid User Request " + errV.Error(),
http.StatusInternalServerError, "NI001")
return
}
newConfig := utils.GetBaseMainConfig()
if(request.Step == "-1") {
// remove everythin in /config
utils.Log("NewInstall: Step cleanup")
utils.Log("NewInstall: Removing config file")
configFile := utils.GetConfigFileName()
os.Remove(configFile)
if(os.Getenv("HOSTNAME") != "") {
utils.Log("NewInstall: Emptying /config")
os.RemoveAll("/config")
os.Mkdir("/config", 0700)
}
utils.DisconnectDB()
LoadConfig()
}
if(request.Step == "2") {
utils.Log("NewInstall: Step Database")
// User Management & Mongo DB
if(request.MongoDBMode == "DisableUserManagement") {
utils.Log("NewInstall: Disable User Management")
newConfig.DisableUserManagement = true
utils.SaveConfigTofile(newConfig)
utils.LoadBaseMainConfig(newConfig)
} else if (request.MongoDBMode == "Provided") {
utils.Log("NewInstall: DB Provided")
newConfig.DisableUserManagement = false
newConfig.MongoDB = request.MongoDB
utils.SaveConfigTofile(newConfig)
utils.LoadBaseMainConfig(newConfig)
} else if (request.MongoDBMode == "Create") {
utils.Log("NewInstall: Create DB")
newConfig.DisableUserManagement = false
strco, err := docker.NewDB(w, req)
if err != nil {
utils.Error("NewInstall: Error creating MongoDB", err)
return
}
newConfig.MongoDB = strco
utils.SaveConfigTofile(newConfig)
utils.LoadBaseMainConfig(newConfig)
utils.Log("NewInstall: MongoDB created, waiting for it to be ready")
waitForDB()
w.WriteHeader(http.StatusOK)
return
} else {
utils.Log("NewInstall: Invalid MongoDBMode")
utils.Error("NewInstall: Invalid MongoDBMode", nil)
utils.HTTPError(w, "New Install: Invalid MongoDBMode",
http.StatusInternalServerError, "NI001")
return
}
} else if (request.Step == "3") {
// HTTPS Certificate Mode & Certs & Let's Encrypt
newConfig.HTTPConfig.HTTPSCertificateMode = request.HTTPSCertificateMode
newConfig.HTTPConfig.SSLEmail = request.SSLEmail
newConfig.HTTPConfig.UseWildcardCertificate = request.UseWildcardCertificate
newConfig.HTTPConfig.DNSChallengeProvider = request.DNSChallengeProvider
newConfig.HTTPConfig.DNSChallengeConfig = request.DNSChallengeConfig
newConfig.HTTPConfig.TLSCert = request.TLSCert
newConfig.HTTPConfig.TLSKey = request.TLSKey
// Hostname
newConfig.HTTPConfig.Hostname = request.Hostname
utils.SaveConfigTofile(newConfig)
utils.LoadBaseMainConfig(newConfig)
} else if (request.Step == "4") {
adminObj := AdminJSON{
Nickname: request.Nickname,
Password: request.Password,
}
errV2 := utils.Validate.Struct(adminObj)
if errV2 != nil {
utils.Error("NewInstall: Invalid User Request", errV2)
utils.HTTPError(w, errV2.Error(), http.StatusInternalServerError, "UL001")
return
}
// Admin User
c, errCo := utils.GetCollection(utils.GetRootAppId(), "users")
if errCo != nil {
utils.Error("Database Connect", errCo)
utils.HTTPError(w, "Database", http.StatusInternalServerError, "DB001")
return
}
nickname := utils.Sanitize(request.Nickname)
hashedPassword, err2 := bcrypt.GenerateFromPassword([]byte(request.Password), 14)
if err2 != nil {
utils.Error("NewInstall: Error hashing password", err2)
utils.HTTPError(w, "New Install: Error hashing password " + err2.Error(),
http.StatusInternalServerError, "NI001")
return
}
// pre-remove every users
_, err4 := c.DeleteMany(nil, map[string]interface{}{})
if err4 != nil {
utils.Error("NewInstall: Error deleting users", err4)
utils.HTTPError(w, "New Install: Error deleting users " + err4.Error(),
http.StatusInternalServerError, "NI001")
return
}
_, err3 := c.InsertOne(nil, map[string]interface{}{
"Nickname": nickname,
"Email": request.Email,
"Password": hashedPassword,
"Role": utils.ADMIN,
"PasswordCycle": 0,
"CreatedAt": time.Now(),
"RegisteredAt": time.Now(),
})
if err3 != nil {
utils.Error("NewInstall: Error creating admin user", err3)
utils.HTTPError(w, "New Install: Error creating admin user " + err3.Error(),
http.StatusInternalServerError, "NI001")
return
}
} else if (request.Step == "5") {
newConfig.NewInstall = false
utils.SaveConfigTofile(newConfig)
os.Exit(0)
}
json.NewEncoder(w).Encode(map[string]interface{}{
"status": "OK",
})
} else {
utils.Error("UserList: Method not allowed" + req.Method, nil)
utils.HTTPError(w, "Method not allowed", http.StatusMethodNotAllowed, "HTTP001")
return
}
}