Skip to content

Commit fe47532

Browse files
authored
Adding support for enabling FIPS mode via an operating system boolean (suse-edge#545)
1 parent b184f7c commit fe47532

File tree

14 files changed

+171
-2
lines changed

14 files changed

+171
-2
lines changed

RELEASE_NOTES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
### Image Definition Changes
1818

19+
* Introduced a dedicated FIPS mode option, adding the required packages, kernel arguments, and crypto selection
20+
1921
### Image Configuration Directory Changes
2022

2123
## Bug Fixes

docs/building-images.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ operatingSystem:
7575
kernelArgs:
7676
- arg1
7777
- arg2
78+
enableFIPS: true
7879
groups:
7980
- name: group1
8081
- name: group2
@@ -153,6 +154,7 @@ The remainder of the operating system customizations may be applied regardless o
153154
parameter is omitted. If this option is set, the default entries will need to be manually added if they are
154155
still in use.
155156
* `kernelArgs` - Provides a list of flags that should be passed to the kernel on boot.
157+
* `enableFIPS` - Specifies whether to setup the operating system for FIPS mode (Default: false)
156158
* `groups` - Defines a list of operating system groups to create. This will not fail if the
157159
group already exists. Each entry is made up of the following fields:
158160
* `name` - Required; Name of the group to create.

pkg/combustion/combustion.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ func (c *Combustion) Configure(ctx *image.Context) error {
128128
name: systemdComponentName,
129129
runnable: configureSystemd,
130130
},
131+
{
132+
name: fipsComponentName,
133+
runnable: configureFips,
134+
},
131135
{
132136
name: elementalComponentName,
133137
runnable: configureElemental,

pkg/combustion/fips.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package combustion
2+
3+
import (
4+
_ "embed"
5+
"fmt"
6+
"os"
7+
"path/filepath"
8+
9+
"github.com/suse-edge/edge-image-builder/pkg/fileio"
10+
"github.com/suse-edge/edge-image-builder/pkg/image"
11+
"github.com/suse-edge/edge-image-builder/pkg/log"
12+
)
13+
14+
const (
15+
fipsComponentName = "fips"
16+
fipsScriptName = "15-fips-setup.sh"
17+
)
18+
19+
var (
20+
//go:embed templates/15-fips-setup.sh
21+
fipsScript string
22+
FipsPackages = []string{"patterns-base-fips"}
23+
FipsKernelArgs = []string{"fips=1"}
24+
)
25+
26+
func configureFips(ctx *image.Context) ([]string, error) {
27+
fips := ctx.ImageDefinition.OperatingSystem.EnableFips
28+
if !fips {
29+
log.AuditComponentSkipped(fipsComponentName)
30+
return nil, nil
31+
}
32+
33+
if err := writeFipsCombustionScript(ctx); err != nil {
34+
log.AuditComponentFailed(fipsComponentName)
35+
return nil, err
36+
}
37+
38+
log.AuditComponentSuccessful(fipsComponentName)
39+
return []string{fipsScriptName}, nil
40+
}
41+
42+
func writeFipsCombustionScript(ctx *image.Context) error {
43+
fipsScriptFilename := filepath.Join(ctx.CombustionDir, fipsScriptName)
44+
45+
if err := os.WriteFile(fipsScriptFilename, []byte(fipsScript), fileio.ExecutablePerms); err != nil {
46+
return fmt.Errorf("writing file %s: %w", fipsScriptFilename, err)
47+
}
48+
return nil
49+
}

pkg/combustion/fips_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package combustion
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
10+
"github.com/suse-edge/edge-image-builder/pkg/fileio"
11+
"github.com/suse-edge/edge-image-builder/pkg/image"
12+
)
13+
14+
func TestConfigureFips_NoConf(t *testing.T) {
15+
// Setup
16+
var ctx image.Context
17+
18+
ctx.ImageDefinition = &image.Definition{
19+
OperatingSystem: image.OperatingSystem{},
20+
}
21+
22+
// Test
23+
scripts, err := configureFips(&ctx)
24+
25+
// Verify
26+
require.NoError(t, err)
27+
assert.Nil(t, scripts)
28+
}
29+
30+
func TestConfigureFips_Enabled(t *testing.T) {
31+
// Setup
32+
ctx, teardown := setupContext(t)
33+
defer teardown()
34+
35+
ctx.ImageDefinition = &image.Definition{
36+
OperatingSystem: image.OperatingSystem{
37+
EnableFips: true,
38+
},
39+
}
40+
41+
// Test
42+
scripts, err := configureFips(ctx)
43+
44+
// Verify
45+
require.NoError(t, err)
46+
47+
require.Len(t, scripts, 1)
48+
assert.Equal(t, fipsScriptName, scripts[0])
49+
50+
expectedFilename := filepath.Join(ctx.CombustionDir, fipsScriptName)
51+
foundBytes, err := os.ReadFile(expectedFilename)
52+
require.NoError(t, err)
53+
54+
stats, err := os.Stat(expectedFilename)
55+
require.NoError(t, err)
56+
assert.Equal(t, fileio.ExecutablePerms, stats.Mode())
57+
58+
foundContents := string(foundBytes)
59+
60+
// - Ensure that we have the fips setup script defined
61+
assert.Contains(t, foundContents, "fips-mode-setup --enable", "fips setup script missing")
62+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
fips-mode-setup --enable

pkg/eib/eib.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func Run(ctx *image.Context, rootBuildDir string) error {
3030
}
3131

3232
appendElementalRPMs(ctx)
33+
appendFips(ctx)
3334
appendHelm(ctx)
3435

3536
c, err := buildCombustion(ctx, rootBuildDir)
@@ -107,6 +108,15 @@ func appendElementalRPMs(ctx *image.Context) {
107108
}, combustion.ElementalPackages...)
108109
}
109110

111+
func appendFips(ctx *image.Context) {
112+
fips := ctx.ImageDefinition.OperatingSystem.EnableFips
113+
if fips {
114+
log.AuditInfo("FIPS mode is configured. The necessary RPM packages will be downloaded.")
115+
appendRPMs(ctx, nil, combustion.FipsPackages...)
116+
appendKernelArgs(ctx, combustion.FipsKernelArgs...)
117+
}
118+
}
119+
110120
func appendRPMs(ctx *image.Context, repos []image.AddRepo, packages ...string) {
111121
repositories := ctx.ImageDefinition.OperatingSystem.Packages.AdditionalRepos
112122
repositories = append(repositories, repos...)
@@ -125,6 +135,12 @@ func appendHelm(ctx *image.Context) {
125135
ctx.ImageDefinition.Kubernetes.Helm.Repositories = append(ctx.ImageDefinition.Kubernetes.Helm.Repositories, componentRepos...)
126136
}
127137

138+
func appendKernelArgs(ctx *image.Context, kernelArgs ...string) {
139+
kernelArgList := ctx.ImageDefinition.OperatingSystem.KernelArgs
140+
kernelArgList = append(kernelArgList, kernelArgs...)
141+
ctx.ImageDefinition.OperatingSystem.KernelArgs = kernelArgList
142+
}
143+
128144
func buildCombustion(ctx *image.Context, rootDir string) (*combustion.Combustion, error) {
129145
cacheDir := filepath.Join(rootDir, "cache")
130146
if err := os.MkdirAll(cacheDir, os.ModePerm); err != nil {

pkg/image/definition.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ type OperatingSystem struct {
7676
Time Time `yaml:"time"`
7777
Proxy Proxy `yaml:"proxy"`
7878
Keymap string `yaml:"keymap"`
79+
EnableFips bool `yaml:"enableFIPS"`
7980
}
8081

8182
type IsoConfiguration struct {

pkg/image/definition_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ func TestParse(t *testing.T) {
3737
}
3838
assert.Equal(t, expectedKernelArgs, definition.OperatingSystem.KernelArgs)
3939

40+
// Operating System -> FIPS
41+
enableFIPS := definition.OperatingSystem.EnableFips
42+
assert.Equal(t, true, enableFIPS)
43+
4044
// Operating System -> Groups
4145
groupConfigs := definition.OperatingSystem.Groups
4246
require.Len(t, groupConfigs, 2)

pkg/image/testdata/full-valid-example.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ operatingSystem:
2929
- alpha=foo
3030
- beta=bar
3131
- baz
32+
enableFIPS: true
3233
systemd:
3334
enable:
3435
- enable0

0 commit comments

Comments
 (0)