-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration.go
116 lines (95 loc) · 2.45 KB
/
configuration.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
package main
import (
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strings"
"sync"
"time"
)
func registerAgent(endPoint, name string, key string, insecure bool) (agentDetails, error) {
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: insecure}
var body []byte
payload := strings.NewReader(fmt.Sprintf(`{"agentName": "%s"}`, name))
regReq, err := http.NewRequest("POST", endPoint+REGISTRATIONENDPOINT, payload)
if err != nil {
return agentDetails{}, err
}
regReq.Header.Add("Content-Type", "application/json")
regReq.Header.Add("UTM-Token", key)
regRes, err := http.DefaultClient.Do(regReq)
if err != nil {
return agentDetails{}, err
}
defer regRes.Body.Close()
body, err = io.ReadAll(regRes.Body)
if err != nil {
return agentDetails{}, err
}
if regRes.StatusCode != 200 {
keyReq, err := http.NewRequest("GET", endPoint+GETIDANDKEYENDPOINT+fmt.Sprintf("?agentName=%s", name), nil)
if err != nil {
return agentDetails{}, err
}
keyReq.Header.Add("UTM-Token", key)
keyRes, err := http.DefaultClient.Do(keyReq)
if err != nil {
return agentDetails{}, err
}
defer keyRes.Body.Close()
body, err = io.ReadAll(keyRes.Body)
if err != nil {
return agentDetails{}, err
}
var agentList []agentDetails
err = json.Unmarshal(body, &agentList)
if err != nil {
h.Error("can't decode agent details: %v", err)
h.Debug("query result: %s", body)
time.Sleep(10 * time.Second)
os.Exit(1)
}
h.Debug("Successful registered. Agent Details: %v", agentList)
return agentList[0], nil
}
var agent agentDetails
err = json.Unmarshal(body, &agent)
if err != nil {
h.Error("can't decode agent details: %v", err)
h.Debug("query result: %s", body)
time.Sleep(10 * time.Second)
os.Exit(1)
}
h.Debug("Agent Details: %v", agent)
return agent, nil
}
type config struct {
Server string `yaml:"server"`
AgentID string `yaml:"agent-id"`
AgentKey string `yaml:"agent-key"`
SkipCertValidation bool `yaml:"skip-cert-validation"`
}
var oneConfigRead sync.Once
var cnf config
func readConfig() {
err := readYAML("config.yml", &cnf)
if err != nil {
h.Error("error reading config %v", err)
time.Sleep(10 * time.Second)
os.Exit(1)
}
}
func getConfig() config {
oneConfigRead.Do(func() { readConfig() })
return cnf
}
func writeConfig(cnf config) error {
err := writeYAML("config.yml", cnf)
if err != nil {
return err
}
return nil
}