Skip to content
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

spoc merge: add --check #2240

Merged
merged 1 commit into from
May 14, 2024
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
14 changes: 11 additions & 3 deletions cmd/spoc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,11 @@ func main() {
},
},
&cli.Command{
Name: "merge",
Aliases: []string{"m"},
Usage: "merge multiple security profiles",
Name: "merge",
Aliases: []string{"m"},
Usage: "merge multiple security profiles",
Description: "Merge multiple security profiles into a combined profile. " +
"Permissions are additive. For AppArmor, the first profile may additionally contain glob paths.",
Action: merge,
ArgsUsage: "INFILE...",
Flags: []cli.Flag{
Expand All @@ -98,6 +100,12 @@ func main() {
DefaultText: merger.DefaultOutputFile,
TakesFile: true,
},
&cli.BoolFlag{
Name: merger.FlagCheck,
Aliases: []string{"c"},
Usage: "do not write an output file, " +
"but exit with an error if the first profile is not a superset of all others.",
},
},
},
&cli.Command{
Expand Down
2 changes: 2 additions & 0 deletions internal/pkg/cli/merger/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ var DefaultOutputFile = cli.DefaultFile
const (
// FlagOutputFile is the flag for defining the output file location.
FlagOutputFile string = cli.FlagOutputFile

FlagCheck string = "check"
)
20 changes: 19 additions & 1 deletion internal/pkg/cli/merger/merger.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"bytes"
"fmt"
"log"
"os"
"reflect"

"k8s.io/cli-runtime/pkg/printers"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -60,13 +62,29 @@ func (p *Merger) Run() error {
}
}

baseProfile := contents[0].DeepCopyObject()

merged, err := recordingmerger.MergeProfiles(contents)
if err != nil {
return fmt.Errorf("merge profiles: %w", err)
}

var buffer bytes.Buffer
printer := printers.YAMLPrinter{}

if p.options.check {
if reflect.DeepEqual(baseProfile, merged) {
log.Println("Base profile is up-to-date.")
os.Exit(0)
} else {
log.Println("Base profile needs an update.")
if err := printer.PrintObj(merged, os.Stderr); err != nil {
return fmt.Errorf("print YAML: %w", err)
}
os.Exit(1)
mhils marked this conversation as resolved.
Show resolved Hide resolved
}
}

var buffer bytes.Buffer
if err := printer.PrintObj(merged, &buffer); err != nil {
return fmt.Errorf("print YAML: %w", err)
}
Expand Down
3 changes: 3 additions & 0 deletions internal/pkg/cli/merger/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
type Options struct {
inputFiles []string
outputFile string
check bool
}

// Default returns a default options instance.
Expand All @@ -45,6 +46,8 @@ func FromContext(ctx *ucli.Context) (*Options, error) {
}
options.inputFiles = args

options.check = ctx.IsSet(FlagCheck)

if ctx.IsSet(FlagOutputFile) {
options.outputFile = ctx.String(FlagOutputFile)
}
Expand Down
Loading