Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions mantle/cmd/kola/qemuexec.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,15 +317,15 @@ func runQemuExec(cmd *cobra.Command, args []string) error {
builder.Hostname = hostname
// for historical reasons, both --memory and --qemu-memory are supported
if memory != 0 {
builder.Memory = memory
builder.MemoryMiB = memory
} else if kola.QEMUOptions.Memory != "" {
parsedMem, err := strconv.ParseInt(kola.QEMUOptions.Memory, 10, 32)
if err != nil {
return errors.Wrapf(err, "parsing memory option")
}
builder.Memory = int(parsedMem)
builder.MemoryMiB = int(parsedMem)
} else if kola.QEMUOptions.SecureExecution {
builder.Memory = 4096 // SE needs at least 4GB
builder.MemoryMiB = 4096 // SE needs at least 4GB
}
if err = builder.AddDisksFromSpecs(addDisks); err != nil {
return err
Expand Down
204 changes: 204 additions & 0 deletions mantle/kola/tests/fips/failure.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
// Copyright 2025 Red Hat, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package fips

import (
"context"
"fmt"
"os"
"regexp"
"time"

"github.com/pkg/errors"

coreosarch "github.com/coreos/stream-metadata-go/arch"

"github.com/coreos/coreos-assembler/mantle/kola"
"github.com/coreos/coreos-assembler/mantle/kola/cluster"
"github.com/coreos/coreos-assembler/mantle/kola/register"
"github.com/coreos/coreos-assembler/mantle/platform"
"github.com/coreos/coreos-assembler/mantle/platform/conf"
)

var failConfig = conf.Ignition(`{
"ignition": {
"version": "3.4.0"
},
"storage": {
"filesystems": [
{
"device": "/dev/mapper/root",
"format": "xfs",
"label": "root",
"wipeFilesystem": true
}
],
"luks": [
{
"clevis": {
"tpm2": true
},
"device": "/dev/disk/by-partlabel/root",
"label": "luks-root",
"name": "root",
"options": [
"--cipher",
"aes-cbc-essiv:sha256",
"--pbkdf",
"argon2i"
],
"wipeVolume": true
}
],
"files": [
{
"group": {
"name": "root"
},
"overwrite": true,
"path": "/etc/ignition-machine-config-encapsulated.json",
"user": {
"name": "root"
},
"contents": {
"source": "data:,%7B%22metadata%22%3A%7B%22name%22%3A%22rendered-worker-1cc576110e0cf8396831ce4016f63900%22%2C%22selfLink%22%3A%22%2Fapis%2Fmachineconfiguration.openshift.io%2Fv1%2Fmachineconfigs%2Frendered-worker-1cc576110e0cf8396831ce4016f63900%22%2C%22uid%22%3A%2248871c03-899d-4332-a5f5-bef94e54b23f%22%2C%22resourceVersion%22%3A%224168%22%2C%22generation%22%3A1%2C%22creationTimestamp%22%3A%222019-11-04T15%3A54%3A08Z%22%2C%22annotations%22%3A%7B%22machineconfiguration.openshift.io%2Fgenerated-by-controller-version%22%3A%22bd846958bc95d049547164046a962054fca093df%22%7D%2C%22ownerReferences%22%3A%5B%7B%22apiVersion%22%3A%22machineconfiguration.openshift.io%2Fv1%22%2C%22kind%22%3A%22MachineConfigPool%22%2C%22name%22%3A%22worker%22%2C%22uid%22%3A%223d0dee9e-c9d6-4656-a4a9-81785b9ab01a%22%2C%22controller%22%3Atrue%2C%22blockOwnerDeletion%22%3Atrue%7D%5D%7D%2C%22spec%22%3A%7B%22osImageURL%22%3A%22registry.svc.ci.openshift.org%2Focp%2F4.3-2019-11-04-125204%40sha256%3A8a344c5b157bd01c3ca1abfcef0004fc39f5d69cac1cdaad0fd8dd332ad8e272%22%2C%22config%22%3A%7B%22ignition%22%3A%7B%22config%22%3A%7B%7D%2C%22security%22%3A%7B%22tls%22%3A%7B%7D%7D%2C%22timeouts%22%3A%7B%7D%2C%22version%22%3A%223.0.0%22%7D%2C%22networkd%22%3A%7B%7D%2C%22passwd%22%3A%7B%7D%2C%22storage%22%3A%7B%7D%2C%22systemd%22%3A%7B%7D%7D%2C%22kernelArguments%22%3A%5B%5D%2C%22fips%22%3Atrue%7D%7D",
"verification": {}
},
"mode": 420
}
]
}
}`)

func init() {
register.RegisterTest(&register.Test{
Name: "fips.failure",
Description: "Verify cryptsetup lukscreate will fail with FIPS and incompatible crypto algorithms.",
Run: runFipsFailure,
ClusterSize: 0,
Platforms: []string{"qemu"},
Tags: []string{"ignition"},
Distros: []string{"rhcos"},
})
}

func runFipsFailure(c cluster.TestCluster) {
if err := ignitionFailure(c); err != nil {
c.Fatal(err.Error())
}
}

// Read file and verify if it contains a pattern
// 1. Read file, make sure it exists
// 2. regex for pattern
func fileContainsPattern(path string, searchPattern string) (bool, error) {
file, err := os.ReadFile(path)
if err != nil {
return false, err
}
// File has content, but the pattern is not present
match := regexp.MustCompile(searchPattern).Match(file)
if match {
// Pattern found
return true, nil
}
// Pattern not found
return false, nil
}

// Start the VM, take string and grep for it in the temporary console logs
func verifyError(builder *platform.QemuBuilder, searchPattern string) error {
inst, err := builder.Exec()
if err != nil {
return err
}
defer inst.Destroy()
ctx, cancel := context.WithTimeout(context.Background(), 4*time.Minute)

defer cancel()

errchan := make(chan error)
go func() {
resultingError := inst.WaitAll(ctx)
if resultingError == nil {
resultingError = fmt.Errorf("ignition unexpectedly succeeded")
} else if resultingError == platform.ErrInitramfsEmergency {
// Expected initramfs failure, checking the console file to ensure
// that it failed the expected way
found, err := fileContainsPattern(builder.ConsoleFile, searchPattern)
if err != nil {
resultingError = errors.Wrapf(err, "looking for pattern '%s' in file '%s' failed", searchPattern, builder.ConsoleFile)
} else if !found {
resultingError = fmt.Errorf("pattern '%s' in file '%s' not found", searchPattern, builder.ConsoleFile)
} else {
// The expected case
resultingError = nil
}
} else {
resultingError = errors.Wrapf(resultingError, "expected initramfs emergency.target error")
}
errchan <- resultingError
Comment on lines +134 to +152
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The error handling logic in this goroutine is a bit complex due to multiple reassignments of resultingError. Using a switch statement on the error from inst.WaitAll(ctx) would make the logic clearer and easier to maintain.

Suggested change
resultingError := inst.WaitAll(ctx)
if resultingError == nil {
resultingError = fmt.Errorf("ignition unexpectedly succeeded")
} else if resultingError == platform.ErrInitramfsEmergency {
// Expected initramfs failure, checking the console file to ensure
// that it failed the expected way
found, err := fileContainsPattern(builder.ConsoleFile, searchPattern)
if err != nil {
resultingError = errors.Wrapf(err, "looking for pattern '%s' in file '%s' failed", searchPattern, builder.ConsoleFile)
} else if !found {
resultingError = fmt.Errorf("pattern '%s' in file '%s' not found", searchPattern, builder.ConsoleFile)
} else {
// The expected case
resultingError = nil
}
} else {
resultingError = errors.Wrapf(resultingError, "expected initramfs emergency.target error")
}
errchan <- resultingError
err := inst.WaitAll(ctx)
switch err {
case nil:
errchan <- fmt.Errorf("ignition unexpectedly succeeded")
case platform.ErrInitramfsEmergency:
// Expected initramfs failure, checking the console file to ensure
// that it failed the expected way
found, patternErr := fileContainsPattern(builder.ConsoleFile, searchPattern)
if patternErr != nil {
errchan <- errors.Wrapf(patternErr, "looking for pattern '%s' in file '%s' failed", searchPattern, builder.ConsoleFile)
} else if !found {
errchan <- fmt.Errorf("pattern '%s' in file '%s' not found", searchPattern, builder.ConsoleFile)
} else {
// The expected case
errchan <- nil
}
default:
errchan <- errors.Wrap(err, "expected initramfs emergency.target error")
}

}()

select {
case <-ctx.Done():
if err := inst.Kill(); err != nil {
return errors.Wrapf(err, "failed to kill the vm instance")
}
return errors.Wrapf(ctx.Err(), "timed out waiting for initramfs error")
case err := <-errchan:
if err != nil {
return err
}
return nil
}
}

func ignitionFailure(c cluster.TestCluster) error {
builder := platform.NewQemuBuilder()
defer builder.Close()

// Prepare Ingnition config
failConfig, err := failConfig.Render(conf.FailWarnings)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The local variable failConfig shadows the global package-level variable of the same name. This can be confusing and lead to bugs. It's better to use a different name for the local variable, for example renderedConfig.

Suggested change
failConfig, err := failConfig.Render(conf.FailWarnings)
renderedConfig, err := failConfig.Render(conf.FailWarnings)

if err != nil {
return errors.Wrapf(err, "creating invalid FIPS config")
}

// Create a temporary log file
consoleFile := c.H.TempFile("console-")

// Instruct builder to use it
builder.ConsoleFile = consoleFile.Name()
builder.SetConfig(failConfig)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To fix the variable shadowing issue from line 174, this should use the newly named renderedConfig variable.

Suggested change
builder.SetConfig(failConfig)
builder.SetConfig(renderedConfig)

err = builder.AddBootDisk(&platform.Disk{
BackingFile: kola.QEMUOptions.DiskImage,
})
if err != nil {
return err
}

builder.MemoryMiB = 4096
switch coreosarch.CurrentRpmArch() {
case "ppc64le":
builder.MemoryMiB = 8192
}
builder.Firmware = kola.QEMUOptions.Firmware

searchPattern := "Only PBKDF2 is supported in FIPS mode"
if err := verifyError(builder, searchPattern); err != nil {
return err
}
return nil
}
7 changes: 1 addition & 6 deletions mantle/kola/tests/ignition/luks.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,7 @@ func setupTangMachine(c cluster.TestCluster) ut.TangServer {
c.Fatal(err)
}

// TODO: move container image to centralized namespace
// container source: https://github.com/mike-nguyen/tang-docker-container/
containerImage := "quay.io/mike_nguyen/tang"
if coreosarch.CurrentRpmArch() != "x86_64" {
containerImage = "quay.io/multi-arch/tang:" + coreosarch.CurrentRpmArch()
}
containerImage := "quay.io/coreos-assembler/tang:latest"

containerID, errMsg, err := m.SSH("sudo podman run -d -p 80:80 " + containerImage)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion mantle/kola/tests/ignition/qemufailure.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func ignitionFailure(c cluster.TestCluster) error {
if err != nil {
return err
}
builder.Memory = 1024
builder.MemoryMiB = 1024
builder.Firmware = kola.QEMUOptions.Firmware
inst, err := builder.Exec()
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions mantle/platform/machine/qemu/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,11 @@ func (qc *Cluster) NewMachineWithQemuOptions(userdata *conf.UserData, options pl
if err != nil {
return nil, errors.Wrapf(err, "parsing memory option")
}
builder.Memory = int(memory)
builder.MemoryMiB = int(memory)
} else if options.MinMemory != 0 {
builder.Memory = options.MinMemory
builder.MemoryMiB = options.MinMemory
} else if qc.flight.opts.SecureExecution {
builder.Memory = 4096 // SE needs at least 4GB
builder.MemoryMiB = 4096 // SE needs at least 4GB
}

channel := "virtio"
Expand Down
2 changes: 1 addition & 1 deletion mantle/platform/machine/qemuiso/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (qc *Cluster) NewMachineWithQemuOptions(userdata *conf.UserData, options pl
if err != nil {
return nil, errors.Wrapf(err, "parsing memory option")
}
builder.Memory = int(memory)
builder.MemoryMiB = int(memory)
}

if err := builder.AddIso(qc.flight.opts.IsoPath, "", qc.flight.opts.AsDisk); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion mantle/platform/metal.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func NewMetalQemuBuilderDefault() *QemuBuilder {
builder := NewQemuBuilder()
// https://github.com/coreos/fedora-coreos-tracker/issues/388
// https://github.com/coreos/fedora-coreos-docs/pull/46
builder.Memory = 4096
builder.MemoryMiB = 4096
return builder
}

Expand Down
10 changes: 5 additions & 5 deletions mantle/platform/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,8 @@ type QemuBuilder struct {

// If set, use QEMU full emulation for the target architecture
architecture string
// Memory defaults to 1024 on most architectures, others it may be 2048
Memory int
// MemoryMiB defaults to 1024 on most architectures, others it may be 2048
MemoryMiB int
// Processors < 0 means to use host count, unset means 1, values > 1 are directly used
Processors int
UUID string
Expand Down Expand Up @@ -1242,7 +1242,7 @@ func (builder *QemuBuilder) finalize() {
if builder.finalized {
return
}
if builder.Memory == 0 {
if builder.MemoryMiB == 0 {
// FIXME; Required memory should really be a property of the tests, and
// let's try to drop these arch-specific overrides. ARM was bumped via
// commit 09391907c0b25726374004669fa6c2b161e3892f
Expand All @@ -1260,7 +1260,7 @@ func (builder *QemuBuilder) finalize() {
case "aarch64", "s390x", "ppc64le":
memory = 2048
}
builder.Memory = memory
builder.MemoryMiB = memory
}
builder.finalized = true
}
Expand Down Expand Up @@ -1591,7 +1591,7 @@ func (builder *QemuBuilder) Exec() (*QemuInstance, error) {
if err != nil {
return nil, err
}
argv = append(argv, "-m", fmt.Sprintf("%d", builder.Memory))
argv = append(argv, "-m", fmt.Sprintf("%d", builder.MemoryMiB))

if builder.Processors < 0 {
nproc, err := system.GetProcessors()
Expand Down
Loading