-
Notifications
You must be signed in to change notification settings - Fork 36
/
runbook.go
117 lines (104 loc) · 2.37 KB
/
runbook.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"os/exec"
"syscall"
)
// runBook represents a collection of scripts.
type runBook struct {
Scripts []script `json:"scripts"`
AllowedNetworks Networks `json:"allowedNetworks,omitempty"`
}
type runBookResponse struct {
Results []result `json:"results"`
}
type result struct {
Stdout string `json:"stdout"`
Stderr string `json:"stderr"`
StatusCode int `json:"status_code"`
}
type script struct {
Command string `json:"command"`
Args []string `json:"args"`
}
// Networks is its own struct for JSON unmarshalling gymnastics
type Networks struct {
Networks []net.IPNet
}
// UnmarshalJSON for custom type Networks
func (nets *Networks) UnmarshalJSON(data []byte) error {
ns := []string{}
if err := json.Unmarshal(data, &ns); err != nil {
return err
}
nets.Networks = make([]net.IPNet, len(ns))
for i, nw := range ns {
_, ipnet, err := net.ParseCIDR(nw)
if err != nil {
return err
}
nets.Networks[i] = *ipnet
}
return nil
}
// NewRunBook returns the runBook identified by id.
func NewRunBook(id string) (*runBook, error) {
return getRunBookById(id)
}
func (r *runBook) AddrIsAllowed(remoteIP net.IP) bool {
if len(r.AllowedNetworks.Networks) == 0 {
return true
}
for _, nw := range r.AllowedNetworks.Networks {
if nw.Contains(remoteIP) {
return true
}
}
return false
}
func (r *runBook) execute() (*runBookResponse, error) {
results := make([]result, 0)
for _, x := range r.Scripts {
r, err := execScript(x)
if err != nil {
log.Println("ERROR :" + err.Error())
}
results = append(results, r)
}
return &runBookResponse{results}, nil
}
func execScript(s script) (result, error) {
cmd := exec.Command(s.Command, s.Args...)
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
r := result{
stdout.String(),
stderr.String(),
-1,
}
if err == nil {
r.StatusCode = cmd.ProcessState.Sys().(syscall.WaitStatus).ExitStatus()
}
return r, err
}
func getRunBookById(id string) (*runBook, error) {
var r = new(runBook)
runBookPath := fmt.Sprintf("%s/%s.json", configdir, id)
data, err := ioutil.ReadFile(runBookPath)
if err != nil {
return r, fmt.Errorf("cannot read run book %s: %s", runBookPath, err)
}
err = json.Unmarshal(data, r)
if err != nil {
return r, err
}
return r, nil
}