-
Notifications
You must be signed in to change notification settings - Fork 2
/
ignored.go
200 lines (172 loc) · 4.96 KB
/
ignored.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
/*
* Copyright 2023 Comcast Cable Communications Management, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package common
import (
"context"
"crypto/tls"
"encoding/json"
"io"
"net"
"net/http"
"time"
"go.uber.org/zap"
)
var (
IgnoredDevices = make(map[string]IgnoredDevice)
)
type host struct {
H string `json:"host"`
}
type IgnoredDevice struct {
Name string
Endpoint string
Module string
CredentialProfile string
}
func TestConn(w http.ResponseWriter, r *http.Request) {
var h host
var path string
response := make(map[string]interface{})
response["connectionTest"] = false
log = zap.L()
body, err := getBody(r)
if err != nil {
response["error"] = err.Error()
resp, _ := marshalResponse(&response, r)
w.WriteHeader(http.StatusInternalServerError)
w.Write(resp)
return
}
err = unmarshalBody(body, &h, r)
if err != nil {
log.Error("failed to unmarshal body from frontend", zap.Error(err), zap.String("path", r.URL.Path))
response["error"] = err.Error()
resp, _ := marshalResponse(&response, r)
w.WriteHeader(http.StatusInternalServerError)
w.Write(resp)
return
}
if _, ok := IgnoredDevices[h.H]; !ok {
log.Error("missing host from ignored hosts list", zap.Error(err), zap.String("path", r.URL.Path))
response["error"] = "missing host from ignored hosts list"
resp, _ := marshalResponse(&response, r)
w.WriteHeader(http.StatusInternalServerError)
w.Write(resp)
return
}
path = IgnoredDevices[h.H].Endpoint
credProfile := IgnoredDevices[h.H].CredentialProfile
// get credentials from vault
credential, err := ChassisCreds.GetCredentials(context.Background(), credProfile, h.H)
if err != nil {
log.Error("issue retrieving credentials from vault using target "+h.H, zap.Error(err), zap.String("path", r.URL.Path))
response["error"] = err.Error()
resp, _ := marshalResponse(&response, r)
w.WriteHeader(http.StatusInternalServerError)
w.Write(resp)
return
}
req, err := http.NewRequest(http.MethodGet, path, nil)
if err != nil {
log.Error("failed to build test connection request", zap.Error(err), zap.String("path", r.URL.Path))
response["error"] = err.Error()
resp, _ := marshalResponse(&response, r)
w.WriteHeader(http.StatusInternalServerError)
w.Write(resp)
return
}
req.SetBasicAuth(credential.User, credential.Pass)
tr := &http.Transport{
Dial: (&net.Dialer{
Timeout: 3 * time.Second,
}).Dial,
MaxIdleConns: 1,
MaxConnsPerHost: 1,
MaxIdleConnsPerHost: 1,
IdleConnTimeout: 90 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
TLSHandshakeTimeout: 10 * time.Second,
}
client := &http.Client{
Timeout: 10 * time.Second,
Transport: tr,
}
res, err := client.Do(req)
if err != nil {
log.Error("request failed for test connection call", zap.Error(err), zap.String("path", r.URL.Path))
response["error"] = err.Error()
resp, _ := marshalResponse(&response, r)
w.WriteHeader(http.StatusInternalServerError)
w.Write(resp)
return
}
if res.StatusCode != 401 {
response["connectionTest"] = true
} else {
response["error"] = res.Status
}
resp, _ := marshalResponse(&response, r)
w.WriteHeader(http.StatusOK)
w.Write(resp)
}
func RemoveHost(w http.ResponseWriter, r *http.Request) {
var h host
log = zap.L()
body, err := getBody(r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
err = unmarshalBody(body, &h, r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
delete(IgnoredDevices, h.H)
log.Info("remove host " + h.H + " from ignored list")
w.WriteHeader(http.StatusOK)
}
func getBody(r *http.Request) ([]byte, error) {
var body []byte
log = zap.L()
body, err := io.ReadAll(r.Body)
if err != nil {
log.Error("could not parse request body", zap.Error(err), zap.String("path", r.URL.Path))
return body, err
}
return body, nil
}
func unmarshalBody(b []byte, h *host, r *http.Request) error {
var err error
log = zap.L()
err = json.Unmarshal(b, h)
if err != nil {
log.Error("could not unmarshal host struct", zap.Error(err), zap.String("path", r.URL.Path))
return err
}
return nil
}
func marshalResponse(p *map[string]interface{}, r *http.Request) ([]byte, error) {
var resp []byte
log = zap.L()
resp, err := json.Marshal(p)
if err != nil {
log.Error("could not marshal response", zap.Error(err), zap.String("path", r.URL.Path))
return resp, err
}
return resp, nil
}