-
Notifications
You must be signed in to change notification settings - Fork 51
/
systemdutil.go
191 lines (146 loc) · 4.38 KB
/
systemdutil.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
package systemdutil
import (
"errors"
"fmt"
"log"
"net"
"net/rpc/jsonrpc"
"os"
"os/exec"
"path"
"strconv"
"strings"
"syscall"
"github.com/aporeto-inc/trireme/constants"
"github.com/aporeto-inc/trireme/monitor"
"github.com/aporeto-inc/trireme/monitor/rpcmonitor"
)
const (
remoteMethodCall = "Server.HandleEvent"
)
// ExecuteCommand executes a command in a cgroup and programs Trireme
func ExecuteCommand(arguments map[string]interface{}) error {
var err error
stderrlogger := log.New(os.Stderr, "", 0)
if arguments["<cgroup>"] != nil && len(arguments["<cgroup>"].(string)) > 0 {
exitingCgroup := arguments["<cgroup>"].(string)
if err = HandleCgroupStop(exitingCgroup); err != nil {
err = fmt.Errorf("cannot connect to policy process %s. Resources not deleted", err)
stderrlogger.Print(err)
return err
}
return nil
}
if !arguments["run"].(bool) {
return fmt.Errorf("Bad arguments - no run command")
}
metadata := []string{}
servicename := ""
ports := "0"
command := arguments["<command>"].(string)
if !path.IsAbs(command) {
command, err = exec.LookPath(command)
if err != nil {
return err
}
}
if args, ok := arguments["--label"]; ok && args != nil {
metadata = args.([]string)
}
if args, ok := arguments["--service-name"]; ok && args != nil {
servicename = args.(string)
}
params := []string{command}
if args, ok := arguments["<params>"]; ok && args != nil {
params = append(params, args.([]string)...)
}
if args, ok := arguments["--ports"]; ok && args != nil {
ports = args.(string)
}
name, metadatamap, err := createMetadata(servicename, command, ports, metadata)
if err != nil {
err = fmt.Errorf("Invalid metadata: %s", err)
stderrlogger.Print(err)
return err
}
// Make RPC call
client, err := net.Dial("unix", rpcmonitor.DefaultRPCAddress)
if err != nil {
err = fmt.Errorf("Cannot connect to policy process %s", err)
stderrlogger.Print(err)
return err
}
//This is added since the release_notification comes in this format
//Easier to massage it while creation rather than change at the receiving end depending on event
request := &rpcmonitor.EventInfo{
PUType: constants.LinuxProcessPU,
PUID: "/" + strconv.Itoa(os.Getpid()),
Name: name,
Tags: metadatamap,
PID: strconv.Itoa(os.Getpid()),
EventType: "start",
}
response := &rpcmonitor.RPCResponse{}
rpcClient := jsonrpc.NewClient(client)
err = rpcClient.Call(remoteMethodCall, request, response)
if err != nil {
err = fmt.Errorf("Policy Server call failed %s", err.Error())
stderrlogger.Print(err)
return err
}
if len(response.Error) > 0 {
err = fmt.Errorf("Your policy does not allow you to run this command")
stderrlogger.Print(err)
return err
}
return syscall.Exec(command, params, os.Environ())
}
// createMetadata extracts the relevant metadata
func createMetadata(servicename string, command string, ports string, metadata []string) (string, map[string]string, error) {
metadatamap := map[string]string{}
for _, element := range metadata {
keyvalue := strings.Split(element, "=")
if len(keyvalue) != 2 {
return "", nil, fmt.Errorf("Invalid metadata")
}
if keyvalue[0][0] == []byte("$")[0] || keyvalue[0][0] == []byte("@")[0] {
return "", nil, fmt.Errorf("Metadata cannot start with $ or @")
}
if keyvalue[0] == "port" || keyvalue[0] == "execpath" {
return "", nil, fmt.Errorf("Metadata key cannot be port or execpath ")
}
metadatamap[keyvalue[0]] = keyvalue[1]
}
metadatamap["port"] = ports
metadatamap["execpath"] = command
name := command
if servicename != "" {
name = servicename
}
return name, metadatamap, nil
}
// HandleCgroupStop handles the deletion of a cgroup
func HandleCgroupStop(cgroupName string) error {
client, err := net.Dial("unix", rpcmonitor.DefaultRPCAddress)
if err != nil {
return err
}
request := &rpcmonitor.EventInfo{
PUType: constants.LinuxProcessPU,
PUID: cgroupName,
Name: cgroupName,
Tags: nil,
PID: strconv.Itoa(os.Getpid()),
EventType: monitor.EventStop,
}
response := &rpcmonitor.RPCResponse{}
rpcClient := jsonrpc.NewClient(client)
if rpcClient == nil {
return errors.New("Failed to connect to policy server")
}
if err := rpcClient.Call(remoteMethodCall, request, response); err != nil {
return err
}
request.EventType = monitor.EventDestroy
return rpcClient.Call(remoteMethodCall, request, response)
}