diff --git a/internal/osbuild2/pwquality_conf_stage.go b/internal/osbuild2/pwquality_conf_stage.go new file mode 100644 index 000000000..0d00f0edd --- /dev/null +++ b/internal/osbuild2/pwquality_conf_stage.go @@ -0,0 +1,23 @@ +package osbuild2 + +type PwqualityConfConfig struct { + Minlen *int `json:"minlen,omitempty"` + Dcredit *int `json:"dcredit,omitempty"` + Ucredit *int `json:"ucredit,omitempty"` + Lcredit *int `json:"lcredit,omitempty"` + Ocredit *int `json:"ocredit,omitempty"` + Minclass *int `json:"minclass,omitempty"` +} + +type PwqualityConfStageOptions struct { + Config PwqualityConfConfig `json:"config"` +} + +func (PwqualityConfStageOptions) isStageOptions() {} + +func NewPwqualityConfStage(options *PwqualityConfStageOptions) *Stage { + return &Stage{ + Type: "org.osbuild.pwquality.conf", + Options: options, + } +} diff --git a/internal/osbuild2/pwquality_conf_stage_test.go b/internal/osbuild2/pwquality_conf_stage_test.go new file mode 100644 index 000000000..25d3eccce --- /dev/null +++ b/internal/osbuild2/pwquality_conf_stage_test.go @@ -0,0 +1,54 @@ +package osbuild2 + +import ( + "encoding/json" + "reflect" + "testing" + + "github.com/osbuild/osbuild-composer/internal/common" + "github.com/stretchr/testify/assert" +) + +func TestNewPwqualityConfStage(t *testing.T) { + expectedStage := &Stage{ + Type: "org.osbuild.pwquality.conf", + Options: &PwqualityConfStageOptions{}, + } + actualStage := NewPwqualityConfStage(&PwqualityConfStageOptions{}) + assert.Equal(t, expectedStage, actualStage) +} + +func TestJsonPwqualityConfStage(t *testing.T) { + // First test that the JSON can be parsed into the expected structure. + expectedOptions := PwqualityConfStageOptions{ + Config: PwqualityConfConfig{ + Minlen: common.IntToPtr(9), + Minclass: common.IntToPtr(0), + Dcredit: common.IntToPtr(1), + }, + } + inputString := `{ + "config": { + "minlen": 9, + "minclass": 0, + "dcredit": 1 + } + }` + var inputOptions PwqualityConfStageOptions + err := json.Unmarshal([]byte(inputString), &inputOptions) + assert.NoError(t, err, "failed to parse JSON yum config") + assert.True(t, reflect.DeepEqual(expectedOptions, inputOptions)) + + // Second try the other way around with stress on missing values + // for those parameters that the user didn't specify. + inputOptions = PwqualityConfStageOptions{ + Config: PwqualityConfConfig{ + Minlen: common.IntToPtr(9), + Minclass: common.IntToPtr(0), + }, + } + expectedString := `{"config":{"minlen":9,"minclass":0}}` + inputBytes, err := json.Marshal(inputOptions) + assert.NoError(t, err, "failed to marshal sshd config into JSON") + assert.Equal(t, expectedString, string(inputBytes)) +} diff --git a/internal/osbuild2/stage.go b/internal/osbuild2/stage.go index 81bc806d5..7dc10dbb6 100644 --- a/internal/osbuild2/stage.go +++ b/internal/osbuild2/stage.go @@ -150,6 +150,8 @@ func (stage *Stage) UnmarshalJSON(data []byte) error { options = new(SshdConfigStageOptions) case "org.osbuild.authconfig": options = new(AuthconfigStageOptions) + case "org.osbuild.pwquality.conf": + options = new(PwqualityConfStageOptions) default: return fmt.Errorf("unexpected stage type: %s", rawStage.Type) } diff --git a/internal/osbuild2/stage_test.go b/internal/osbuild2/stage_test.go index 7b518fee9..69410e5c2 100644 --- a/internal/osbuild2/stage_test.go +++ b/internal/osbuild2/stage_test.go @@ -657,6 +657,16 @@ func TestStage_UnmarshalJSON(t *testing.T) { data: []byte(`{"type":"org.osbuild.authconfig","options":{}}`), }, }, + { + name: "pwquality.conf", + fields: fields{ + Type: "org.osbuild.pwquality.conf", + Options: &PwqualityConfStageOptions{}, + }, + args: args{ + data: []byte(`{"type":"org.osbuild.pwquality.conf","options":{"config":{}}}`), + }, + }, } for idx, tt := range tests { t.Run(tt.name, func(t *testing.T) {