forked from osbuild/images
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show a warning message when building a FIPS enabled image in a non enabled FIPS host Signed-off-by: Miguel Martín <[email protected]>
- Loading branch information
Showing
5 changed files
with
92 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package common | ||
|
||
import ( | ||
"bufio" | ||
"os" | ||
"strings" | ||
) | ||
|
||
const ( | ||
FIPSEnabledImageWarning = `The host building this image is not ` + | ||
`running in FIPS mode. The image will still be FIPS compliant. ` + | ||
`If you have custom steps that generate keys or perform ` + | ||
`cryptographic operations, those must be considered non-compliant.` | ||
) | ||
|
||
var ( | ||
FIPSEnabledFilePath = "/proc/sys/crypto/fips_enabled" | ||
) | ||
|
||
func IsBuildHostFIPSEnabled() (enabled bool) { | ||
file, err := os.Open(FIPSEnabledFilePath) | ||
if err != nil { | ||
return | ||
} | ||
defer file.Close() | ||
buf := []byte{} | ||
_, err = file.Read(buf) | ||
if err != nil { | ||
return | ||
} | ||
scanner := bufio.NewScanner(file) | ||
scanner.Scan() | ||
if err := scanner.Err(); err != nil { | ||
return | ||
} | ||
return strings.TrimSpace(scanner.Text()) == "1" | ||
} |
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,38 @@ | ||
package common | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFIPSEnabledHost(t *testing.T) { | ||
file, err := os.CreateTemp("/tmp", "fips_enabled") | ||
assert.NoError(t, err, "unable to create tmp file") | ||
defer file.Close() | ||
defer os.Remove(file.Name()) | ||
FIPSEnabledFilePath = file.Name() | ||
|
||
fileContents := []string{ | ||
"", | ||
"0\n", | ||
"1\n", | ||
"xxxxxx\n", | ||
} | ||
|
||
for _, fileContent := range fileContents { | ||
err = file.Truncate(0) | ||
assert.NoError(t, err, "truncating file: %s", file.Name()) | ||
_, err = file.Seek(0, 0) | ||
assert.NoError(t, err, "seeking the begining of file: %s", file.Name()) | ||
_, err = file.Write([]byte(fileContent)) | ||
assert.NoError(t, err, "unable to write to file: %s", file.Name()) | ||
if strings.TrimSpace(fileContent) == "1" { | ||
assert.Equal(t, IsBuildHostFIPSEnabled(), true) | ||
} else { | ||
assert.Equal(t, IsBuildHostFIPSEnabled(), false) | ||
} | ||
} | ||
} |
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