forked from step-security/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
93 lines (83 loc) · 2.7 KB
/
config.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
package main
import (
"encoding/json"
"io/ioutil"
"strconv"
"strings"
"github.com/miekg/dns"
"github.com/pkg/errors"
)
type config struct {
Repo string
CorrelationId string
RunId string
WorkingDirectory string
APIURL string
OneTimeKey string
Endpoints map[string][]Endpoint
EgressPolicy string
DisableTelemetry bool
DisableSudo bool
DisableFileMonitoring bool
Private bool
}
type Endpoint struct {
domainName string
port int
}
type configFile struct {
Repo string `json:"repo"`
CorrelationId string `json:"correlation_id"`
RunId string `json:"run_id"`
WorkingDirectory string `json:"working_directory"`
APIURL string `json:"api_url"`
OneTimeKey string `json:"one_time_key"`
AllowedEndpoints string `json:"allowed_endpoints"`
EgressPolicy string `json:"egress_policy"`
DisableTelemetry bool `json:"disable_telemetry"`
DisableSudo bool `json:"disable_sudo"`
DisableFileMonitoring bool `json:"disable_file_monitoring"`
Private bool `json:"private"`
}
// init reads the config file for the agent and initializes config settings
func (c *config) init(configFilePath string) error {
var configFile configFile
data, err := ioutil.ReadFile(configFilePath)
if err != nil {
return errors.Wrap(err, "failed to read config file")
}
err = json.Unmarshal([]byte(data), &configFile)
if err != nil {
return errors.Wrap(err, "failed to unmarshal config file")
}
c.CorrelationId = configFile.CorrelationId
c.Repo = configFile.Repo
c.RunId = configFile.RunId
c.WorkingDirectory = configFile.WorkingDirectory
c.APIURL = configFile.APIURL
c.Endpoints = parseEndpoints(configFile.AllowedEndpoints)
c.EgressPolicy = configFile.EgressPolicy
c.DisableTelemetry = configFile.DisableTelemetry
c.DisableSudo = configFile.DisableSudo
c.DisableFileMonitoring = configFile.DisableFileMonitoring
c.Private = configFile.Private
c.OneTimeKey = configFile.OneTimeKey
return nil
}
func parseEndpoints(allowedEndpoints string) map[string][]Endpoint {
endpoints := make(map[string][]Endpoint)
endpointsArray := strings.Split(allowedEndpoints, " ")
for _, endpoint := range endpointsArray {
if len(endpoint) > 0 {
endpointParts := strings.Split(endpoint, ":")
domainName := endpointParts[0]
domainName = dns.Fqdn(domainName)
port := 443 // default to 443
if len(endpointParts) > 1 {
port, _ = strconv.Atoi(endpointParts[1])
}
endpoints[domainName] = append(endpoints[domainName], Endpoint{domainName: domainName, port: port})
}
}
return endpoints
}