Skip to content

Commit 3ef58ed

Browse files
committed
refactor: generate layer4 types from cue schema
This change updates layer4 to rely on generated types. This change also removes the Changes and CorruptedState types. Signed-off-by: Travis Truman <[email protected]>
1 parent 8e7bb00 commit 3ef58ed

File tree

14 files changed

+174
-748
lines changed

14 files changed

+174
-748
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ jobs:
4444
4545
- name: Quality Gate - Test coverage shall be above threshold
4646
env:
47-
TESTCOVERAGE_THRESHOLD: 72
47+
TESTCOVERAGE_THRESHOLD: 71
4848
run: |
4949
echo "Quality Gate: checking test coverage is above threshold ..."
5050
echo "Threshold : $TESTCOVERAGE_THRESHOLD %"

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,13 @@ cuegen:
3939
@mv cue_types_gen.go layer2/generated_types.go
4040
@cue exp gengotypes ./schemas/layer-3.cue
4141
@mv cue_types_gen.go layer3/generated_types.go
42+
@cue exp gengotypes ./schemas/layer-4.cue
43+
@mv cue_types_gen.go layer4/generated_types.go
4244
@go build -o utils/types_tagger utils/types_tagger.go
4345
@utils/types_tagger layer1/generated_types.go
4446
@utils/types_tagger layer2/generated_types.go
4547
@utils/types_tagger layer3/generated_types.go
48+
@utils/types_tagger layer4/generated_types.go
4649
@rm utils/types_tagger
4750

4851
dirtycheck:

layer4/assessment_log.go

Lines changed: 6 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,9 @@ import (
99
"time"
1010
)
1111

12-
// AssessmentLog is a struct that contains the results of a single step within a ControlEvaluation.
13-
type AssessmentLog struct {
14-
// RequirementID is the unique identifier for the requirement being tested
15-
RequirementId string `yaml:"requirement-id"`
16-
// ProcedureId uniquely identifies the assessment procedure associated with the log
17-
ProcedureId string `json:"procedure-id,omitempty"`
18-
// Applicability is a slice of identifier strings to determine when this test is applicable
19-
Applicability []string `yaml:"applicability"`
20-
// Description is a human-readable description of the test
21-
Description string `yaml:"description"`
22-
// Result is true if the test passed
23-
Result Result `yaml:"result"`
24-
// Message is the human-readable result of the test
25-
Message string `yaml:"message"`
26-
// Steps is a slice of steps that were executed during the test
27-
Steps []AssessmentStep `yaml:"steps"`
28-
// StepsExecuted is the number of steps that were executed during the test
29-
StepsExecuted int `yaml:"steps-executed,omitempty"`
30-
// Start is the time the assessment run began.
31-
Start string `yaml:"start"`
32-
// End is the time the assessment run finished.
33-
// This is omitted if the assessment was interrupted or did not complete.
34-
End string `yaml:"end,omitempty"`
35-
// Value is the object that was returned during the test
36-
Value interface{} `yaml:"value,omitempty"`
37-
// Changes is a slice of changes that were made during the test
38-
Changes map[string]*Change `yaml:"changes,omitempty"`
39-
// Recommendation is a string to aid users in remediation, such as the text from a layer 2 assessment requirement
40-
Recommendation string `yaml:"recommendation,omitempty"`
41-
}
42-
4312
// AssessmentStep is a function type that inspects the provided targetData and returns a Result with a message.
4413
// The message may be an error string or other descriptive text.
45-
type AssessmentStep func(payload interface{}, c map[string]*Change) (Result, string)
14+
type AssessmentStep func(payload interface{}) (Result, string)
4615

4716
func (as AssessmentStep) String() string {
4817
// Get the function pointer correctly
@@ -81,71 +50,34 @@ func (a *AssessmentLog) AddStep(step AssessmentStep) {
8150

8251
func (a *AssessmentLog) runStep(targetData interface{}, step AssessmentStep) Result {
8352
a.StepsExecuted++
84-
result, message := step(targetData, a.Changes)
53+
result, message := step(targetData)
8554
a.Result = UpdateAggregateResult(a.Result, result)
8655
a.Message = message
8756
return result
8857
}
8958

9059
// Run will execute all steps, halting if any step does not return layer4.Passed.
91-
func (a *AssessmentLog) Run(targetData interface{}, changesAllowed bool) Result {
60+
func (a *AssessmentLog) Run(targetData interface{}) Result {
61+
a.Result = NotRun
9262
if a.Result != NotRun {
9363
return a.Result
9464
}
9565

96-
a.Start = time.Now().Format(time.RFC3339)
66+
a.Start = Datetime(time.Now().Format(time.RFC3339))
9767
err := a.precheck()
9868
if err != nil {
9969
a.Result = Unknown
10070
return a.Result
10171
}
102-
for _, change := range a.Changes {
103-
if changesAllowed {
104-
change.Allow()
105-
}
106-
}
10772
for _, step := range a.Steps {
10873
if a.runStep(targetData, step) == Failed {
10974
return Failed
11075
}
11176
}
112-
a.End = time.Now().Format(time.RFC3339)
77+
a.End = Datetime(time.Now().Format(time.RFC3339))
11378
return a.Result
11479
}
11580

116-
// NewChange creates a new Change object and adds it to the AssessmentLog.
117-
func (a *AssessmentLog) NewChange(
118-
changeName,
119-
targetName,
120-
description string,
121-
targetObject interface{},
122-
applyFunc ApplyFunc,
123-
revertFunc RevertFunc,
124-
) *Change {
125-
change := NewChange(targetName, description, targetObject, applyFunc, revertFunc)
126-
if a.Changes == nil {
127-
a.Changes = make(map[string]*Change)
128-
}
129-
a.Changes[changeName] = &change
130-
return &change
131-
}
132-
133-
// RevertChanges reverts all changes made by the assessment.
134-
// It will not revert changes that have not been applied.
135-
func (a *AssessmentLog) RevertChanges() (corrupted bool) {
136-
for _, change := range a.Changes {
137-
if !corrupted && (change.Applied || change.Error != nil) {
138-
if !change.Reverted {
139-
change.Revert(nil)
140-
}
141-
if change.Error != nil || !change.Reverted {
142-
corrupted = true // do not break loop here; continue attempting to revert all changes
143-
}
144-
}
145-
}
146-
return
147-
}
148-
14981
// precheck verifies that the assessment has all the required fields.
15082
// It returns an error if the assessment is not valid.
15183
func (a *AssessmentLog) precheck() error {

layer4/assessment_log_test.go

Lines changed: 2 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -115,119 +115,17 @@ func TestRun(t *testing.T) {
115115
for _, data := range getAssessmentsTestData() {
116116
t.Run(data.testName, func(t *testing.T) {
117117
a := data.assessment // copy the assessment to prevent duplicate executions in the next test
118-
result := a.Run(nil, true)
118+
result := a.Run(nil)
119119
if result != a.Result {
120120
t.Errorf("expected match between Run return value (%s) and assessment Result value (%s)", result, data.expectedResult)
121121
}
122-
if a.StepsExecuted != data.numberOfStepsToRun {
122+
if a.StepsExecuted != int64(data.numberOfStepsToRun) {
123123
t.Errorf("expected to run %d tests, got %d", data.numberOfStepsToRun, a.StepsExecuted)
124124
}
125125
})
126126
}
127127
}
128128

129-
func TestRunB(t *testing.T) {
130-
for _, data := range getAssessmentsTestData() {
131-
t.Run(data.testName+"-no-changes", func(t *testing.T) {
132-
data.assessment.Run(nil, false)
133-
if data.assessment.StepsExecuted != data.numberOfStepsToRun {
134-
t.Errorf("expected to run %d tests, got %d", data.numberOfStepsToRun, data.assessment.StepsExecuted)
135-
}
136-
for _, change := range data.assessment.Changes {
137-
if change.Allowed {
138-
t.Errorf("expected all changes to be disallowed, but found an allowed change")
139-
return
140-
}
141-
if change.Applied || change.Reverted {
142-
t.Errorf("expected no changes to be applied, but found applied=%t, reverted=%t", change.Applied, change.Reverted)
143-
return
144-
}
145-
}
146-
})
147-
}
148-
}
149-
150-
// TestNewChange ensures that NewChange creates a new Change object and adds it to the AssessmentLog
151-
func TestNewChange(t *testing.T) {
152-
anyOldAssessment := AssessmentLog{}
153-
testName := "Add-a-new-change"
154-
t.Run(testName, func(t *testing.T) {
155-
if len(anyOldAssessment.Changes) != 0 {
156-
t.Errorf("Expected empty assessment object to start with 0 Change objects, got %d", len(anyOldAssessment.Changes))
157-
}
158-
change := anyOldAssessment.NewChange(testName, "targetName", "description", nil, goodApplyFunc, goodRevertFunc)
159-
if len(anyOldAssessment.Changes) != 1 {
160-
t.Errorf("Expected assessment object to have 1 Change object, got %d", len(anyOldAssessment.Changes))
161-
}
162-
if change == nil {
163-
t.Error("expected a change object to be returned by NewChange, got nil")
164-
}
165-
if change != anyOldAssessment.Changes[testName] {
166-
t.Errorf("Found different change object in assessment object than the one returned by NewChange")
167-
}
168-
169-
})
170-
}
171-
172-
// TestRevertChanges ensures that RevertChanges attempts to revert all changes in the AssessmentLog
173-
func TestRevertChanges(t *testing.T) {
174-
revertChangesTestData := []struct {
175-
testName string
176-
assessment AssessmentLog
177-
corrupted bool
178-
}{
179-
{
180-
testName: "No changes",
181-
assessment: AssessmentLog{},
182-
corrupted: false,
183-
},
184-
{
185-
testName: "Change already applied and reverted",
186-
assessment: AssessmentLog{Changes: map[string]*Change{"test": goodRevertedChangePtr()}},
187-
corrupted: false,
188-
},
189-
{
190-
testName: "Change without apply function",
191-
assessment: AssessmentLog{Changes: map[string]*Change{"test": noApplyChangePtr()}},
192-
corrupted: true,
193-
},
194-
{
195-
testName: "Change with error from apply function",
196-
assessment: AssessmentLog{Changes: map[string]*Change{"test": badApplyChangePtr()}},
197-
corrupted: true,
198-
},
199-
{
200-
testName: "Change with error from revert function",
201-
assessment: AssessmentLog{Changes: map[string]*Change{"test": badRevertChangePtr()}},
202-
corrupted: true,
203-
},
204-
{
205-
testName: "Change previously applied and needs reverted",
206-
assessment: AssessmentLog{Changes: map[string]*Change{"test": goodNotRevertedChangePtr()}},
207-
corrupted: false,
208-
},
209-
{
210-
testName: "Two changes already applied, with one already reverted",
211-
assessment: passingAssessment(),
212-
corrupted: false,
213-
},
214-
}
215-
for _, data := range revertChangesTestData {
216-
t.Run(data.testName, func(t *testing.T) {
217-
for _, change := range data.assessment.Changes {
218-
if !change.Allowed {
219-
return
220-
}
221-
change.Apply("target_name", "target_object", "change_input")
222-
}
223-
corrupted := data.assessment.RevertChanges()
224-
if corrupted != data.corrupted {
225-
t.Errorf("expected corruption to be %t, got %t", data.corrupted, corrupted)
226-
}
227-
})
228-
}
229-
}
230-
231129
func TestNewAssessment(t *testing.T) {
232130
newAssessmentsTestData := []struct {
233131
testName string

layer4/change.go

Lines changed: 0 additions & 110 deletions
This file was deleted.

0 commit comments

Comments
 (0)