/
compliance_profile.go
197 lines (167 loc) · 4.9 KB
/
compliance_profile.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
package integration
import (
"archive/zip"
"bytes"
"fmt"
"io/ioutil"
"text/template"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/pkg/errors"
"github.com/chef/automate/components/automate-cli/pkg/diagnostics"
"github.com/chef/automate/components/automate-cli/pkg/diagnostics/lbrequest"
)
const complianceProfileInspecYamlTemplateStr = `
name: {{ .Name }}
title: {{ .Name }}
maintainer: Chef Software, Inc.
copyright: Chef Software, Inc.
copyright_email: support@chef.io
license: Proprietary, All rights reserved
summary: Integration Diagnostic Test
version: {{ .Version }}
supports:
- os-family: unix
`
const complianceProfileControlStr = `
title 'Filesystem Configuration'
control 'fs-1' do
impact 0.5
title 'Create a separate partition for /tmp'
desc '
Since /tmp is world-writable, avoid resource
exhaustion on the host by mounting this folder
to a separate partition.
'
describe mount('/tmp') do
it { should be_mounted }
end
end
`
type complianceProfileSave struct {
Name string `json:"name"`
Version string `json:"version"`
Owner string `json:"owner"`
}
func createComplianceProfileZip(inspecYaml string, sampleControl string) ([]byte, error) {
buf := new(bytes.Buffer)
w := zip.NewWriter(buf)
var files = []struct {
Name, Body string
}{
{"inspec.yml", inspecYaml},
{"controls/", ""},
{"controls/sample_spec.rb", sampleControl},
}
for _, file := range files {
f, err := w.Create(file.Name)
if err != nil {
return nil, errors.Wrapf(err, "Failed to create %s in zip", file.Name)
}
if file.Body != "" {
_, err = f.Write([]byte(file.Body))
if err != nil {
return nil, errors.Wrapf(err, "Failed to write data %s in zip", file.Name)
}
}
}
err := w.Close()
if err != nil {
return nil, errors.Wrap(err, "Failed to close zip writer")
}
return buf.Bytes(), nil
}
// CreateComplianceProfileDiagnostic create the diagnostic struct for compliance profile
func CreateComplianceProfileDiagnostic() diagnostics.Diagnostic {
tmpl := template.Must(template.New("compliance-profile").Parse(complianceProfileInspecYamlTemplateStr))
return diagnostics.Diagnostic{
Name: "compliance-profile",
Tags: diagnostics.Tags{"compliance"},
Generate: func(tstCtx diagnostics.TestContext) error {
buf := bytes.NewBuffer([]byte{})
ts := time.Now()
owner := "integration-diagnostic"
name := "integration-diagnostic-" + ts.Format("20060102150405")
version := "1.1.1"
err := tmpl.Execute(buf, struct {
Owner string
Name string
Version string
}{
Owner: owner,
Name: name,
Version: version,
})
if err != nil {
return err
}
zipData, err := createComplianceProfileZip(buf.String(), complianceProfileControlStr)
if err != nil {
return err
}
reqPath := fmt.Sprintf("/api/v0/compliance/profiles?contentType=application/zip&owner=%s", owner)
resp, err := tstCtx.DoLBRequest(
reqPath,
lbrequest.WithMethod("POST"),
lbrequest.WithFormFile("profile.zip", zipData),
)
if err != nil {
return errors.Wrapf(err, "Request POST %s failed", reqPath)
}
defer func() {
_ = resp.Body.Close()
}()
if resp.StatusCode != 200 {
err = errors.New("Status code not 200")
respBody, _ := ioutil.ReadAll(resp.Body)
return errors.Wrapf(err, "Request POST %s failed with %d\nResponse Body:\n%s", reqPath, resp.StatusCode, respBody)
}
tstCtx.SetValue("compliance-profile", complianceProfileSave{
Owner: owner,
Name: name,
Version: version,
})
return err
},
Verify: func(tstCtx diagnostics.VerificationTestContext) {
loaded := complianceProfileSave{}
err := tstCtx.GetValue("compliance-profile", &loaded)
require.NoError(tstCtx, err, "Generated context was not found")
reqPath := fmt.Sprintf("/api/v0/compliance/profiles/read/%s/%s/version/%s", loaded.Owner, loaded.Name, loaded.Version)
resp, err := tstCtx.DoLBRequest(reqPath)
require.NoError(tstCtx, err, "Failed to GET %s", reqPath)
defer func() {
_ = resp.Body.Close()
}()
assert.Equal(tstCtx, 200, resp.StatusCode, "GET %s did not respond with 200", reqPath)
},
Cleanup: func(tstCtx diagnostics.TestContext) error {
loaded := complianceProfileSave{}
err := tstCtx.GetValue("compliance-profile", &loaded)
if err != nil {
return errors.Wrap(err, "Generated context was not found")
}
reqPath := fmt.Sprintf("/api/v0/compliance/profiles/%s/%s/version/%s", loaded.Owner, loaded.Name, loaded.Version)
resp, err := tstCtx.DoLBRequest(
reqPath,
lbrequest.WithMethod("DELETE"),
)
if err != nil {
return errors.Wrapf(err, "Failed to DELETE %s", reqPath)
}
defer func() {
_ = resp.Body.Close()
}()
if resp.StatusCode != 200 {
return errors.New("Unexpected status code")
}
return nil
},
}
}
func init() {
diagnostics.RegisterDiagnostic(
CreateComplianceProfileDiagnostic(),
)
}