-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
201 lines (179 loc) · 4.92 KB
/
main.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
201
package main
import (
"encoding/json"
"fmt"
"os"
seccomp "github.com/grantseltzer/Manhattan/oci-seccomp-gen"
types "github.com/opencontainers/runtime-spec/specs-go"
"github.com/Sirupsen/logrus"
"github.com/urfave/cli"
)
const (
defaultSeccompProfile = "/etc/sysconfig/manhattan.json"
version = "0.0.1"
)
func main() {
var (
input string
kill string
trap string
errno string
trace string
allow string
remove string
defaultAction string
arch string
name string
nameforce string
)
app := cli.NewApp()
app.Name = "manhattan"
app.Usage = "Create seccomp json configurations for use with OCI or Docker"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: inputOptions,
Value: defaultSeccompProfile,
Usage: "Specify location of base configuration file",
Destination: &input,
},
cli.StringFlag{
Name: killOptions,
Value: emptyString,
Usage: "Respond to system call with KILL",
Destination: &kill,
},
cli.StringFlag{
Name: trapOptions,
Value: emptyString,
Usage: "Respond to system call with TRAP",
Destination: &trap,
},
cli.StringFlag{
Name: errnoOptions,
Value: emptyString,
Usage: "Respond to system call with ERRNO",
Destination: &errno,
},
cli.StringFlag{
Name: traceOptions,
Value: emptyString,
Usage: "Respond to system call with TRACE",
Destination: &trace,
},
cli.StringFlag{
Name: allowOptions,
Value: emptyString,
Usage: "Respond to system call with ALLOW",
Destination: &allow,
},
cli.StringFlag{
Name: removeOptions,
Value: emptyString,
Usage: "Remove a syscall",
Destination: &remove,
},
cli.StringFlag{
Name: defaultOptions,
Value: defaultDefault,
Usage: "Set the default action for syscalls not specified",
Destination: &defaultAction,
},
cli.StringFlag{
Name: archOptions,
Value: archDefault,
Usage: "Set supported architectures",
Destination: &arch,
},
cli.StringFlag{
Name: nameOptions,
Value: seccomp.DefaultFullPath(),
Usage: "Set name of output file",
Destination: &name,
},
cli.StringFlag{
Name: nameForceOptions,
Value: nameForceDefault,
Usage: "Set name of output file, force write",
Destination: &nameforce,
},
}
app.Version = version
app.EnableBashCompletion = true
app.Action = func(c *cli.Context) error {
return nil
}
app.Run(os.Args)
var SeccompProfile types.Seccomp
configFile, err := os.Open(input)
if err != nil {
fmt.Println("[*] Could not open seccomp profile at", input)
fmt.Println("[*] Creating new Profile")
} else {
jsonParser := json.NewDecoder(configFile)
err = jsonParser.Decode(&SeccompProfile)
if err != nil {
logrus.Fatal("Error parsing Configuration File", err)
}
defer configFile.Close()
}
err = seccomp.ParseDefaultAction(defaultAction, &SeccompProfile)
if err != nil {
logrus.Fatal("Error parsing default action argument", err)
}
err = seccomp.ParseSyscallFlag("kill", kill, &SeccompProfile)
if err != nil {
logrus.Fatal("Error parsing kill argument", err)
}
err = seccomp.ParseSyscallFlag("trap", trap, &SeccompProfile)
if err != nil {
logrus.Fatal("Error parsing trap argument", err)
}
err = seccomp.ParseSyscallFlag("errno", errno, &SeccompProfile)
if err != nil {
logrus.Fatal("Error parsing errno argument", err)
}
err = seccomp.ParseSyscallFlag("trace", trace, &SeccompProfile)
if err != nil {
logrus.Fatal("Error parsing trace argument", err)
}
err = seccomp.ParseSyscallFlag("allow", allow, &SeccompProfile)
if err != nil {
logrus.Fatal("Error parsing allow argument", err)
}
err = seccomp.ParseArchitectureFlag(arch, &SeccompProfile)
if err != nil {
logrus.Fatal("Error parsing architecture agument", err)
}
err = seccomp.RemoveAction(remove, &SeccompProfile)
if err != nil {
logrus.Fatal("Error parsing remove action argument", err)
}
b, err := json.MarshalIndent(SeccompProfile, emptyString, " ")
if err != nil {
logrus.Fatal("Error creating Seccomp Profile", err)
}
if _, erro := os.Stat(name); erro == nil && nameforce == "not-specified" {
logrus.Fatal("File destination already exists. Use --name-force to overwrite. ", name, erro)
}
if nameforce != "not-specified" {
name = nameforce
}
newConfigFile, err := os.Create(name)
if err != nil {
logrus.Fatal("Error creating config file", err)
}
if _, err := newConfigFile.Write(b); err != nil {
logrus.Fatal("Error writing config to file", err)
}
}
func autocomplete(c *cli.Context) {
tasks := []string{"kill", "trap", "errno", "trace", "allow", "remove",
"default", "arch", "name"}
// This will complete if no args are passed
if c.NArg() > 0 {
return
}
for _, t := range tasks {
fmt.Println(t)
}
}