/
hosts_api.go
89 lines (74 loc) · 2.97 KB
/
hosts_api.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
// Copyright (c) 2020 Sorint.lab S.p.A.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package controller
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/ercole-io/ercole/model"
"github.com/ercole-io/ercole/utils"
"github.com/goji/httpauth"
"github.com/xeipuuv/gojsonschema"
)
// AuthenticateMiddleware return the middleware used to authenticate (request) users
func (ctrl *HostDataController) AuthenticateMiddleware() func(http.Handler) http.Handler {
return httpauth.SimpleBasicAuth(ctrl.Config.DataService.AgentUsername, ctrl.Config.DataService.AgentPassword)
}
// UpdateHostInfo update the informations about a host using the HostData in the request
func (ctrl *HostDataController) UpdateHostInfo(w http.ResponseWriter, r *http.Request) {
var hostdata model.HostDataBE
var aerr utils.AdvancedErrorInterface
//Read all bytes for the request
raw, err := ioutil.ReadAll(r.Body)
if err != nil {
utils.WriteAndLogError(ctrl.Log, w, http.StatusBadRequest, utils.NewAdvancedErrorPtr(err, http.StatusText(http.StatusBadRequest)))
return
}
defer r.Body.Close()
//Validate the data
documentLoader := gojsonschema.NewBytesLoader(raw)
schemaLoader := gojsonschema.NewStringLoader(model.FrontendHostdataSchemaValidator)
if result, err := gojsonschema.Validate(schemaLoader, documentLoader); err != nil {
utils.WriteAndLogError(ctrl.Log, w, http.StatusUnprocessableEntity, utils.NewAdvancedErrorPtr(err, "HOSTDATA_VALIDATION"))
return
} else if !result.Valid() {
var errorMsg strings.Builder
errorMsg.WriteString("Invalid schema! The input hostdata is not valid!\n")
for _, err := range result.Errors() {
value := fmt.Sprintf("%v", err.Value())
if len(value) > 80 {
value = value[:78] + ".."
}
errorMsg.WriteString(fmt.Sprintf("- %s. Value: [%v]\n", err, value))
}
errorMsg.WriteString(fmt.Sprintf("hostdata:\n%v", string(raw)))
utils.WriteAndLogError(ctrl.Log, w, http.StatusUnprocessableEntity,
utils.NewAdvancedErrorPtr(errors.New(errorMsg.String()), "HOSTDATA_VALIDATION"))
return
}
//Unmarshal raw to hostdata. The err checking is not needed
_ = json.Unmarshal(raw, &hostdata)
//Save the HostData
id, aerr := ctrl.Service.UpdateHostInfo(hostdata)
if aerr != nil {
utils.WriteAndLogError(ctrl.Log, w, http.StatusInternalServerError, aerr)
return
}
//Write the created id
utils.WriteJSONResponse(w, http.StatusOK, id)
}