This repository has been archived by the owner on Mar 28, 2022. It is now read-only.
/
hGlobalSettings.go
300 lines (244 loc) · 8.57 KB
/
hGlobalSettings.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package system
import (
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/seknox/trasa/server/api/providers/vault/tsxvault"
"github.com/seknox/trasa/server/consts"
"github.com/seknox/trasa/server/global"
"github.com/seknox/trasa/server/models"
"github.com/seknox/trasa/server/utils"
"github.com/sirupsen/logrus"
)
type GlobalSettingsResp struct {
DynamicAccess models.GlobalSettings `json:"dynamicAccess"`
PasswordPolicy models.GlobalSettings `json:"passPolicy"`
EmailSettings models.GlobalSettings `json:"emailSettings"`
DeviceHygiene models.GlobalSettings `json:"deviceHygiene"`
}
//GlobalSettings returns all global settings
func GlobalSettings(w http.ResponseWriter, r *http.Request) {
logrus.Trace("request received")
userContext := r.Context().Value("user").(models.UserContext)
var resp GlobalSettingsResp
passPolicy, err := Store.GetGlobalSetting(userContext.User.OrgID, consts.GLOBAL_PASSWORD_CONFIG)
if err != nil {
logrus.Error(err)
}
emailConfigs, err := Store.GetGlobalSetting(userContext.User.OrgID, consts.GLOBAL_EMAIL_CONFIG)
if err != nil {
logrus.Error(err)
}
resp.DeviceHygiene, err = Store.GetGlobalSetting(userContext.User.OrgID, consts.GLOBAL_DEVICE_HYGIENE_CHECK)
if err != nil {
logrus.Error(err)
}
resp.DynamicAccess, err = Store.GetGlobalSetting(userContext.Org.ID, consts.GLOBAL_DYNAMIC_ACCESS)
if err != nil {
logrus.Error(err)
}
resp.PasswordPolicy = passPolicy
resp.EmailSettings = emailConfigs
utils.TrasaResponse(w, 200, "success", "global settings fetched", "", resp)
}
type updatePassPolicyReq struct {
Policy models.PasswordPolicy `json:"policy"`
Enable bool `json:"enable"`
}
//UpdatePasswordPolicy updates password policy in global settings
func UpdatePasswordPolicy(w http.ResponseWriter, r *http.Request) {
logrus.Trace("request received")
userContext := r.Context().Value("user").(models.UserContext)
var req updatePassPolicyReq
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
logrus.Error(err)
utils.TrasaResponse(w, 200, "failed", "error parsing request", "password policy not updated", nil, nil)
return
}
var store models.GlobalSettings
encodePolicy, _ := json.Marshal(req.Policy)
store.SettingValue = string(encodePolicy)
store.Status = req.Enable
store.OrgID = userContext.User.OrgID
store.SettingType = consts.GLOBAL_PASSWORD_CONFIG
store.UpdatedBy = userContext.User.ID
store.UpdatedOn = time.Now().Unix()
err := Store.UpdateGlobalSetting(store)
if err != nil {
logrus.Error(err)
utils.TrasaResponse(w, 200, "failed", "error updating global settings", "password policy not updated", nil, nil)
return
}
reason := "password policy enabled"
if req.Enable == false {
reason = "password policy disabled"
}
utils.TrasaResponse(w, 200, "success", reason, "password policy updated", nil, nil)
}
//UpdateDeviceHygieneSetting updates device hygiene enforce settings
func UpdateDeviceHygieneSetting(w http.ResponseWriter, r *http.Request) {
logrus.Trace("device hygeiene req")
uc := r.Context().Value("user").(models.UserContext)
var req struct {
EnableDeviceHygieneCheck bool `json:"enableDeviceHygieneCheck"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
logrus.Error(err)
utils.TrasaResponse(w, 200, "failed", "error parsing request", "device hygiene setting not updated", nil, nil)
return
}
err := Store.UpdateGlobalSetting(models.GlobalSettings{
SettingID: utils.GetUUID(),
OrgID: uc.Org.ID,
Status: req.EnableDeviceHygieneCheck,
SettingType: consts.GLOBAL_DEVICE_HYGIENE_CHECK,
SettingValue: "{}",
UpdatedBy: uc.User.ID,
UpdatedOn: time.Now().Unix(),
})
if err != nil {
logrus.Error(err)
utils.TrasaResponse(w, 200, "failed", "Could not update setting", "device hygiene setting not updated", nil, nil)
return
}
utils.TrasaResponse(w, 200, "success", "Setting updated", "device hygiene setting updated", nil, nil)
}
//UpdateDynamicAccessSetting updates dynamic access settings
func UpdateDynamicAccessSetting(w http.ResponseWriter, r *http.Request) {
uc := r.Context().Value("user").(models.UserContext)
var req models.GlobalDynamicAccessSettings
if err := utils.ParseAndValidateRequest(r, &req); err != nil {
logrus.Error(err)
utils.TrasaResponse(w, 200, "failed", "error parsing request", "dynamic access setting not updated", nil, nil)
return
}
settVal, err := json.Marshal(req)
if err != nil {
logrus.Error(err)
}
err = Store.UpdateGlobalSetting(models.GlobalSettings{
SettingID: utils.GetUUID(),
OrgID: uc.Org.ID,
Status: req.Status,
SettingType: consts.GLOBAL_DYNAMIC_ACCESS,
SettingValue: string(settVal),
UpdatedBy: uc.User.ID,
UpdatedOn: time.Now().Unix(),
})
if err != nil {
logrus.Error(err)
utils.TrasaResponse(w, 200, "failed", "Could not update setting", "dynamic access setting not updated", nil, nil)
return
}
utils.TrasaResponse(w, 200, "success", "Setting updated", "dynamic access setting updated", nil, nil)
}
//UpdateEmailSetting updates email settings
func UpdateEmailSetting(w http.ResponseWriter, r *http.Request) {
logrus.Trace("UpdateEmailSetting request received")
uc := r.Context().Value("user").(models.UserContext)
var req models.EmailIntegrationConfig
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
logrus.Error(err)
utils.TrasaResponse(w, 200, "failed", "error parsing request", "email setting not updated", nil, nil)
return
}
// switch req.IntegrationType {
// case "EMAIL_SMTP":
// req.IntegrationType = string(consts.EMAIL_SMTP)
// case "mailgun":
// req.IntegrationType = string(consts.EMAIL_MAILGUN)
// }
var key models.KeysHolder
start := ""
if len(req.AuthPass) > 4 {
start = req.AuthPass[0:4]
}
key.OrgID = uc.User.OrgID
key.KeyID = utils.GetRandomString(5)
key.KeyTag = fmt.Sprintf("%sxxxx-xxxx...", start)
key.AddedBy = uc.User.ID
key.AddedAt = time.Now().Unix()
key.KeyName = consts.GLOBAL_EMAIL_CONFIG_SECRET
key.KeyVal = []byte(req.AuthPass)
_, err := EncryptAndStoreKeyOrToken(key)
if err != nil {
logrus.Error(err)
utils.TrasaResponse(w, 200, "failed", "failed to update email settings", "email settings not updated")
return
}
req.AuthPass = key.KeyTag
j, err := json.Marshal(req)
if err != nil {
}
var store models.GlobalSettings
store.SettingValue = string(j)
store.Status = true
store.OrgID = uc.User.OrgID
store.SettingType = consts.GLOBAL_EMAIL_CONFIG
store.UpdatedBy = uc.User.ID
store.UpdatedOn = time.Now().Unix()
err = Store.UpdateGlobalSetting(store)
if err != nil {
logrus.Error(err)
utils.TrasaResponse(w, 200, "failed", "error updating global settings", "email settings not updated", nil, nil)
return
}
utils.TrasaResponse(w, 200, "success", "successfully updated email setting", "password policy updated", nil, nil)
}
// StoreCloudProxyKey handles signed TRASA cloud proxy access key storage.
func StoreCloudProxyKey(w http.ResponseWriter, r *http.Request) {
uc := r.Context().Value("user").(models.UserContext)
type storekey struct {
TsxCPxyAddr string `json:"tsxCPxyAddr"`
APIKey string `json:"apiKey"`
}
var req storekey
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
logrus.Error(err)
utils.TrasaResponse(w, 200, "failed", "error parsing request", "cloud proxy key", nil, nil)
return
}
var key models.KeysHolder
start := ""
if len(req.APIKey) > 4 {
start = req.APIKey[0:4]
}
key.OrgID = uc.User.OrgID
key.KeyID = utils.GetRandomString(5)
key.KeyTag = fmt.Sprintf("%sxxxx-xxxx...", start)
key.AddedBy = uc.User.ID
key.AddedAt = time.Now().Unix()
key.KeyName = consts.GLOBAL_CLOUDPROXY_APIKEY
key.KeyVal = []byte(req.APIKey)
_, err := EncryptAndStoreKeyOrToken(key)
if err != nil {
logrus.Error(err)
utils.TrasaResponse(w, 200, "failed", "failed to encrypt key", "cloud proxy key not updated")
return
}
// update config
global.UpdateTRASACPxyAddr(req.TsxCPxyAddr)
tsxvault.Store.SetTsxCPxyKey(req.APIKey)
// proceed updating global setting
req.APIKey = key.KeyTag
j, err := json.Marshal(req)
if err != nil {
logrus.Error(err)
}
var store models.GlobalSettings
store.SettingValue = string(j)
store.Status = true
store.OrgID = uc.User.OrgID
store.SettingType = consts.GLOBAL_CLOUDPROXY_APIKEY
store.UpdatedBy = uc.User.ID
store.UpdatedOn = time.Now().Unix()
err = Store.UpdateGlobalSetting(store)
if err != nil {
logrus.Error(err)
utils.TrasaResponse(w, 200, "failed", "failed to store key", "cloud proxy key not updated", nil, nil)
return
}
logrus.Trace("CLOUDPROXYADDR: ", global.GetConfig().Trasa.CloudServer)
utils.TrasaResponse(w, 200, "success", "key stored", "cloud proxy key updated", nil, nil)
}