-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
OpenSCAP tailoring: add key/value rule overrides #300
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
68fdf42
schutz: update `osbuild` commit
kingsleyzissou 2e725fc
pkg/distro: generalise default datastream
kingsleyzissou bd12442
pkg/distro: simplify directory creation
kingsleyzissou ee3e8c3
pkg/distro: simplify tailoring stage options
kingsleyzissou a03aaa8
pkg/distro: simplify remediation stage options
kingsleyzissou 770a6c7
pkg/blueprints: add tailoring overrides
kingsleyzissou fefcc4c
pkg/osbuild: enable OpenSCAP tailoring overrides
kingsleyzissou be317b5
pkg/customizations: enable tailor overrides
kingsleyzissou 3b6b310
test/configs: update OpenSCAP tests
kingsleyzissou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package blueprint | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
type OpenSCAPCustomization struct { | ||
Datastream string `json:"datastream,omitempty" toml:"datastream,omitempty"` | ||
ProfileID string `json:"profile_id,omitempty" toml:"profile_id,omitempty"` | ||
Tailoring *OpenSCAPTailoringCustomizations `json:"tailoring,omitempty" toml:"tailoring,omitempty"` | ||
} | ||
|
||
type OpenSCAPTailoringCustomizations struct { | ||
Selected []string `json:"selected,omitempty" toml:"selected,omitempty"` | ||
Unselected []string `json:"unselected,omitempty" toml:"unselected,omitempty"` | ||
Overrides []OpenSCAPTailoringOverride `json:"overrides,omitempty" toml:"overrides,omitempty"` | ||
} | ||
|
||
type OpenSCAPTailoringOverride struct { | ||
Var string `json:"var,omitempty" toml:"var,omitempty"` | ||
Value interface{} `json:"value,omitempty" toml:"value,omitempty"` | ||
} | ||
|
||
func (c *Customizations) GetOpenSCAP() *OpenSCAPCustomization { | ||
if c == nil { | ||
return nil | ||
} | ||
return c.OpenSCAP | ||
} | ||
|
||
func (ot *OpenSCAPTailoringOverride) UnmarshalTOML(data interface{}) error { | ||
d, _ := data.(map[string]interface{}) | ||
|
||
switch d["var"].(type) { | ||
case string: | ||
ot.Var = d["var"].(string) | ||
default: | ||
return fmt.Errorf("TOML unmarshal: override var must be string, got %v of type %T", d["var"], d["var"]) | ||
} | ||
|
||
switch d["value"].(type) { | ||
case int64: | ||
ot.Value = uint64(d["value"].(int64)) | ||
case string: | ||
ot.Value = d["value"].(string) | ||
default: | ||
return fmt.Errorf("TOML unmarshal: override value must be integer or string, got %v of type %T", d["value"], d["value"]) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (ot *OpenSCAPTailoringOverride) UnmarshalJSON(data []byte) error { | ||
var v interface{} | ||
if err := json.Unmarshal(data, &v); err != nil { | ||
return err | ||
} | ||
d, _ := v.(map[string]interface{}) | ||
|
||
switch d["var"].(type) { | ||
case string: | ||
ot.Var = d["var"].(string) | ||
default: | ||
return fmt.Errorf("JSON unmarshal: override var must be string, got %v of type %T", d["var"], d["var"]) | ||
} | ||
|
||
switch d["value"].(type) { | ||
case float64: | ||
ot.Value = uint64(d["value"].(float64)) | ||
case string: | ||
ot.Value = d["value"].(string) | ||
default: | ||
return fmt.Errorf("JSON unmarshal: override var must be float64 number or string, got %v of type %T", d["value"], d["value"]) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package blueprint | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/BurntSushi/toml" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetOpenSCAPConfig(t *testing.T) { | ||
|
||
expectedOscap := OpenSCAPCustomization{ | ||
Datastream: "test-data-stream.xml", | ||
ProfileID: "test_profile", | ||
Tailoring: &OpenSCAPTailoringCustomizations{ | ||
Selected: []string{"quick_rule"}, | ||
Unselected: []string{"very_slow_rule"}, | ||
Overrides: []OpenSCAPTailoringOverride{ | ||
OpenSCAPTailoringOverride{ | ||
Var: "rule_id", | ||
Value: 50, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
TestCustomizations := Customizations{ | ||
OpenSCAP: &expectedOscap, | ||
} | ||
|
||
retOpenSCAPCustomiztions := TestCustomizations.GetOpenSCAP() | ||
|
||
assert.EqualValues(t, expectedOscap, *retOpenSCAPCustomiztions) | ||
} | ||
|
||
func TestOpenSCAPOverrideTOMLUnmarshaler(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
TOML string | ||
want *OpenSCAPTailoringOverride | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "string based rule", | ||
TOML: ` | ||
var = "sshd_idle_timeout_value" | ||
value = "600" | ||
`, | ||
want: &OpenSCAPTailoringOverride{ | ||
Var: "sshd_idle_timeout_value", | ||
Value: "600", | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "integer based rule", | ||
TOML: ` | ||
var = "sshd_idle_timeout_value" | ||
value = 600 | ||
`, | ||
want: &OpenSCAPTailoringOverride{ | ||
Var: "sshd_idle_timeout_value", | ||
Value: uint64(600), | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "invalid rule", | ||
TOML: ` | ||
var = "sshd_idle_timeout_value" | ||
`, | ||
want: nil, | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
var override OpenSCAPTailoringOverride | ||
err := toml.Unmarshal([]byte(tt.TOML), &override) | ||
if tt.wantErr { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
assert.NotNil(t, override) | ||
assert.Equal(t, tt.want, &override) | ||
} | ||
} | ||
} | ||
|
||
func TestOpenSCAPOverrideJSONUnmarshaler(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
JSON string | ||
want *OpenSCAPTailoringOverride | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "string based rule", | ||
JSON: `{ | ||
"var": "sshd_idle_timeout_value", | ||
"value": "600" | ||
}`, | ||
want: &OpenSCAPTailoringOverride{ | ||
Var: "sshd_idle_timeout_value", | ||
Value: "600", | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "integer based rule", | ||
JSON: `{ | ||
"var": "sshd_idle_timeout_value", | ||
"value": 600 | ||
}`, | ||
want: &OpenSCAPTailoringOverride{ | ||
Var: "sshd_idle_timeout_value", | ||
Value: uint64(600), | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "invalid rule", | ||
JSON: `{ | ||
"var": "sshd_idle_timeout_value" | ||
}`, | ||
want: nil, | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
var override OpenSCAPTailoringOverride | ||
err := json.Unmarshal([]byte(tt.JSON), &override) | ||
if tt.wantErr { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
assert.NotNil(t, override) | ||
assert.Equal(t, tt.want, &override) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,9 @@ | ||
package oscap | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/osbuild/images/pkg/customizations/fsnode" | ||
"github.com/osbuild/images/pkg/distro" | ||
) | ||
|
||
type Profile string | ||
|
@@ -40,28 +38,42 @@ const ( | |
defaultRHEL8Datastream string = "/usr/share/xml/scap/ssg/content/ssg-rhel8-ds.xml" | ||
defaultRHEL9Datastream string = "/usr/share/xml/scap/ssg/content/ssg-rhel9-ds.xml" | ||
|
||
// tailoring directory path | ||
// directory paths | ||
dataDirPath string = "/oscap_data" | ||
tailoringDirPath string = "/usr/share/xml/osbuild-openscap-data" | ||
) | ||
|
||
func DefaultFedoraDatastream() string { | ||
return defaultFedoraDatastream | ||
} | ||
func getDatastream(datastream string, d distro.Distro) string { | ||
if datastream != "" { | ||
return datastream | ||
} | ||
|
||
func DefaultRHEL8Datastream(isRHEL bool) string { | ||
if isRHEL { | ||
return defaultRHEL8Datastream | ||
s := strings.ToLower(d.Name()) | ||
if strings.HasPrefix(s, "fedora") { | ||
return defaultFedoraDatastream | ||
} | ||
|
||
if strings.HasPrefix(s, "centos") { | ||
return defaultCentosDatastream(d.Releasever()) | ||
} | ||
return defaultCentos8Datastream | ||
|
||
return defaultRHELDatastream(d.Releasever()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a thought, not sure if of any value: Would it make sense to check also |
||
} | ||
|
||
func DefaultRHEL9Datastream(isRHEL bool) string { | ||
if isRHEL { | ||
return defaultRHEL9Datastream | ||
func defaultCentosDatastream(releaseVer string) string { | ||
if releaseVer == "8" { | ||
return defaultCentos8Datastream | ||
} | ||
return defaultCentos9Datastream | ||
} | ||
|
||
func defaultRHELDatastream(releaseVer string) string { | ||
if releaseVer == "8" { | ||
return defaultRHEL8Datastream | ||
} | ||
return defaultRHEL9Datastream | ||
} | ||
|
||
func IsProfileAllowed(profile string, allowlist []Profile) bool { | ||
for _, a := range allowlist { | ||
if a.String() == profile { | ||
|
@@ -77,15 +89,3 @@ func IsProfileAllowed(profile string, allowlist []Profile) bool { | |
|
||
return false | ||
} | ||
|
||
func GetTailoringFile(profile string) (string, string, *fsnode.Directory, error) { | ||
newProfile := fmt.Sprintf("%s_osbuild_tailoring", profile) | ||
path := filepath.Join(tailoringDirPath, "tailoring.xml") | ||
|
||
tailoringDir, err := fsnode.NewDirectory(tailoringDirPath, nil, nil, nil, true) | ||
if err != nil { | ||
return "", "", nil, err | ||
} | ||
|
||
return newProfile, path, tailoringDir, nil | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: you could use this instead of specifying the same value twice:
and basically in all similar cases in this file.