/
eula.go
73 lines (58 loc) · 1.84 KB
/
eula.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
package rest
import (
"encoding/json"
"io/ioutil"
"net/http"
"github.com/julienschmidt/httprouter"
log "github.com/sirupsen/logrus"
"github.com/zhyocean/neuvector/controller/api"
"github.com/zhyocean/neuvector/share"
"github.com/zhyocean/neuvector/share/cluster"
)
func handlerEULAShow(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
log.WithFields(log.Fields{"URL": r.URL.String()}).Debug("")
defer r.Body.Close()
acc, login := getAccessControl(w, r, "")
if acc == nil {
return
}
var resp api.RESTEULAData
key := share.CLUSConfigEULAKey
value, _ := cluster.Get(key)
if value != nil {
var eula share.CLUSEULA
json.Unmarshal(value, &eula)
resp.EULA = &api.RESTEULA{Accepted: eula.Accepted}
} else {
resp.EULA = &api.RESTEULA{Accepted: false}
}
restRespSuccess(w, r, &resp, acc, login, nil, "Get EULA")
}
func handlerEULAConfig(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
log.WithFields(log.Fields{"URL": r.URL.String()}).Debug("")
defer r.Body.Close()
acc, login := getAccessControl(w, r, "")
if acc == nil {
return
}
body, _ := ioutil.ReadAll(r.Body)
var rconf api.RESTEULAData
err := json.Unmarshal(body, &rconf)
if err != nil || rconf.EULA == nil {
log.WithFields(log.Fields{"error": err}).Error("Request error")
restRespError(w, http.StatusBadRequest, api.RESTErrInvalidRequest)
return
}
var cconf share.CLUSEULA
cconf.Accepted = rconf.EULA.Accepted
key := share.CLUSConfigEULAKey
value, _ := json.Marshal(&cconf)
if err := cluster.Put(key, value); err != nil {
log.WithFields(log.Fields{"error": err}).Error("")
// cache.AuthLog(cache.LOGEV_USER_CONFIG_FAILED,
// auth.Password.Username, auth.Password.Domain)
restRespError(w, http.StatusInternalServerError, api.RESTErrFailWriteCluster)
return
}
restRespSuccess(w, r, nil, acc, login, &rconf, "Configure EULA")
}