-
Notifications
You must be signed in to change notification settings - Fork 6
/
templates.go
191 lines (167 loc) · 4.67 KB
/
templates.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 main
import (
"encoding/json"
"errors"
"github.com/nu7hatch/gouuid"
"io/ioutil"
"sync"
)
// Templates used to be executed on hosts
type Template struct {
Id string
Title string // Short title
Description string // Full description that explains in layman's terms what this does, so everyone can help as part of the authorization process
Command string // Command to be executed
Enabled bool // Is this available for running?
Timeout int // Seconds of execution before the command is killed
Acl *TemplateACL
ExecutionStrategy *ExecutionStrategy
ValidationRules []*ExecutionValidation // Validation rules
mux sync.RWMutex
}
type TemplateACL struct {
MinAuth uint // Minimum amount of authorization before the template is actually executed (eg 3 = requester + 2 additional approvers)
IncludedTags []string
ExcludedTags []string
}
type TemplateStore struct {
ConfFile string
Templates map[string]*Template
templateMux sync.RWMutex
}
// Add a validation rule
func (s *Template) AddValidationRule(r *ExecutionValidation) {
s.ValidationRules = append(s.ValidationRules, r)
}
// Delete a validation rule
func (s *Template) DeleteValidationRule(id string) {
s.mux.Lock()
defer s.mux.Unlock()
var tmp = make([]*ExecutionValidation, 0)
for _, r := range s.ValidationRules {
if r.Id == id {
continue
}
tmp = append(tmp, r)
}
s.ValidationRules = tmp
}
// Validate the setup of a template
func (s *Template) IsValid() (bool, error) {
if len(s.Title) < 1 {
return false, errors.New("Fill in a title")
}
// Title must be unique
server.templateStore.templateMux.RLock()
defer server.templateStore.templateMux.RUnlock()
for _, template := range server.templateStore.Templates {
if template.Title == s.Title {
return false, errors.New("Title is not unique")
}
}
if len(s.Description) < 1 {
return false, errors.New("Fill in a description")
}
if len(s.Command) < 1 {
return false, errors.New("Fill in a command")
}
return true, nil
}
func (s *TemplateStore) Remove(templateId string) {
s.templateMux.Lock()
defer s.templateMux.Unlock()
delete(s.Templates, templateId)
}
func (s *TemplateStore) Get(templateId string) *Template {
s.templateMux.RLock()
defer s.templateMux.RUnlock()
return s.Templates[templateId]
}
func (s *TemplateStore) Add(template *Template) {
s.templateMux.Lock()
defer s.templateMux.Unlock()
s.Templates[template.Id] = template
}
func (s *TemplateStore) save() bool {
s.templateMux.Lock()
defer s.templateMux.Unlock()
bytes, je := json.Marshal(s.Templates)
if je != nil {
log.Printf("Failed to write templates: %s", je)
return false
}
err := ioutil.WriteFile(s.ConfFile, bytes, 0644)
if err != nil {
log.Printf("Failed to write templates: %s", err)
return false
}
return true
}
func (s *TemplateStore) load() {
s.templateMux.Lock()
defer s.templateMux.Unlock()
// Read file and load into user store
bytes, err := ioutil.ReadFile(s.ConfFile)
if err == nil {
var v map[string]*Template
je := json.Unmarshal(bytes, &v)
if je != nil {
log.Printf("Invalid templates.json: %s", je)
return
}
s.Templates = v
}
}
// Execution strategy of the template
func (t *Template) GetExecutionStrategy() *ExecutionStrategy {
t.mux.RLock()
defer t.mux.RUnlock()
// Default strategy
if t.ExecutionStrategy == nil {
return newExecutionStrategy(SimpleExecutionStrategy)
}
return t.ExecutionStrategy
}
func newTemplateStore() *TemplateStore {
s := &TemplateStore{
ConfFile: conf.HomeFile("templates.conf"),
Templates: make(map[string]*Template),
}
s.load()
return s
}
func newTemplateAcl() *TemplateACL {
return &TemplateACL{
IncludedTags: make([]string, 0),
ExcludedTags: make([]string, 0),
}
}
func newTemplate(title string, description string, command string, enabled bool, includedTags []string, excludedTags []string, minAuth uint, timeout int, executionStrategy *ExecutionStrategy) *Template {
// Unique ID
id, _ := uuid.NewV4()
// ACL
acl := newTemplateAcl()
acl.IncludedTags = includedTags
acl.ExcludedTags = excludedTags
acl.MinAuth = minAuth
// Tags
if len(acl.IncludedTags) == 1 && acl.IncludedTags[0] == "" {
acl.IncludedTags = make([]string, 0)
}
if len(acl.ExcludedTags) == 1 && acl.ExcludedTags[0] == "" {
acl.ExcludedTags = make([]string, 0)
}
// Instantiate
t := &Template{
Id: id.String(),
Title: title,
Description: description,
Command: command,
Enabled: enabled,
Acl: acl,
Timeout: timeout,
ExecutionStrategy: executionStrategy,
ValidationRules: make([]*ExecutionValidation, 0),
}
return t
}