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

Add spoc convert Command #2201

Merged
merged 6 commits into from
Apr 10, 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
36 changes: 36 additions & 0 deletions cmd/spoc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"sigs.k8s.io/security-profiles-operator/cmd"
spocli "sigs.k8s.io/security-profiles-operator/internal/pkg/cli"
"sigs.k8s.io/security-profiles-operator/internal/pkg/cli/converter"
"sigs.k8s.io/security-profiles-operator/internal/pkg/cli/merger"
"sigs.k8s.io/security-profiles-operator/internal/pkg/cli/puller"
"sigs.k8s.io/security-profiles-operator/internal/pkg/cli/pusher"
Expand Down Expand Up @@ -99,6 +100,27 @@ func main() {
},
},
},
&cli.Command{
Name: "convert",
Aliases: []string{"c"},
Usage: "convert a security profile to its raw format",
Action: convert,
ArgsUsage: "PROFILE",
Flags: []cli.Flag{
&cli.StringFlag{
Name: converter.FlagOutputFile,
Aliases: []string{"o"},
Usage: "the output file path for the raw profile",
DefaultText: converter.DefaultOutputFile,
TakesFile: true,
},
&cli.StringFlag{
Name: converter.FlagProgramName,
Aliases: []string{"p"},
Usage: "AppArmor only: the path to the program that is confined.",
},
},
},
&cli.Command{
Name: "run",
Aliases: []string{"x"},
Expand Down Expand Up @@ -227,6 +249,20 @@ func merge(ctx *cli.Context) error {
return nil
}

// convert runs the `spoc convert` subcommand.
func convert(ctx *cli.Context) error {
options, err := converter.FromContext(ctx)
if err != nil {
return fmt.Errorf("build options: %w", err)
}

if err := converter.New(options).Run(); err != nil {
return fmt.Errorf("launch converter: %w", err)
}

return nil
}

// run runs the `spoc run` subcommand.
func run(ctx *cli.Context) error {
options, err := runner.FromContext(ctx)
Expand Down
37 changes: 15 additions & 22 deletions internal/pkg/artifact/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package artifact

import (
"context"
"errors"
"fmt"
"path/filepath"
"strings"
Expand Down Expand Up @@ -337,41 +338,33 @@ func (a *Artifact) Pull(
return nil, fmt.Errorf("read profile: %w", err)
}

a.logger.Info("Trying to unmarshal seccomp profile")
seccompProfile := &seccompprofileapi.SeccompProfile{}
err = a.YamlUnmarshal(content, seccompProfile)
if err == nil {
profile, err := a.ReadProfile(content)
if err != nil {
return nil, errors.Join(ErrDecodeYAML, err)
}

switch obj := profile.(type) {
case *seccompprofileapi.SeccompProfile:
return &PullResult{
typ: PullResultTypeSeccompProfile,
seccompProfile: seccompProfile,
seccompProfile: obj,
content: content,
}, nil
}

a.logger.Info("Trying to unmarshal selinux profile")
selinuxProfile := &selinuxprofileapi.SelinuxProfile{}
err = a.YamlUnmarshal(content, selinuxProfile)
if err == nil {
case *selinuxprofileapi.SelinuxProfile:
return &PullResult{
typ: PullResultTypeSelinuxProfile,
selinuxProfile: selinuxProfile,
selinuxProfile: obj,
content: content,
}, nil
}

a.logger.Info("Trying to unmarshal apparmor profile")
apparmorProfile := &apparmorprofileapi.AppArmorProfile{}
err = a.YamlUnmarshal(content, apparmorProfile)
if err == nil {
case *apparmorprofileapi.AppArmorProfile:
return &PullResult{
typ: PullResultTypeApparmorProfile,
apparmorProfile: apparmorProfile,
apparmorProfile: obj,
content: content,
}, nil
default:
return nil, fmt.Errorf("cannot process %T to PullResult", obj)
}

a.logger.Info("No profile found")
return nil, fmt.Errorf("%w: last err: %w", ErrDecodeYAML, err)
}

// profileName returns the name for the profile based on the platform.
Expand Down
12 changes: 8 additions & 4 deletions internal/pkg/artifact/artifact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ import (
"github.com/stretchr/testify/require"
"oras.land/oras-go/v2/registry/remote"

apparmorprofileapi "sigs.k8s.io/security-profiles-operator/api/apparmorprofile/v1alpha1"
seccompprofileapi "sigs.k8s.io/security-profiles-operator/api/seccompprofile/v1beta1"
selinuxprofileapi "sigs.k8s.io/security-profiles-operator/api/selinuxprofile/v1alpha2"
"sigs.k8s.io/security-profiles-operator/internal/pkg/artifact/artifactfakes"
)

Expand Down Expand Up @@ -233,6 +236,7 @@ func TestPull(t *testing.T) {
mock.NewRepositoryReturns(&remote.Repository{}, nil)
mock.ParseReferenceReturns(testRef, nil)
mock.ReadFileReturns([]byte{}, nil)
mock.ReadProfileReturns(&seccompprofileapi.SeccompProfile{}, nil)
mock.RemoveAllReturns(errTest)
mock.FileCloseReturns(errTest)
},
Expand All @@ -250,6 +254,7 @@ func TestPull(t *testing.T) {
mock.NewRepositoryReturns(&remote.Repository{}, nil)
mock.ParseReferenceReturns(testRef, nil)
mock.ReadFileReturns([]byte{}, nil)
mock.ReadProfileReturns(&seccompprofileapi.SeccompProfile{}, nil)
},
assert: func(res *PullResult, err error) {
require.NoError(t, err)
Expand All @@ -265,7 +270,7 @@ func TestPull(t *testing.T) {
mock.NewRepositoryReturns(&remote.Repository{}, nil)
mock.ParseReferenceReturns(testRef, nil)
mock.ReadFileReturns([]byte{}, nil)
mock.YamlUnmarshalReturnsOnCall(0, errTest)
mock.ReadProfileReturns(&selinuxprofileapi.SelinuxProfile{}, nil)
},
assert: func(res *PullResult, err error) {
require.NoError(t, err)
Expand All @@ -281,8 +286,7 @@ func TestPull(t *testing.T) {
mock.NewRepositoryReturns(&remote.Repository{}, nil)
mock.ParseReferenceReturns(testRef, nil)
mock.ReadFileReturns([]byte{}, nil)
mock.YamlUnmarshalReturnsOnCall(0, errTest)
mock.YamlUnmarshalReturnsOnCall(1, errTest)
mock.ReadProfileReturns(&apparmorprofileapi.AppArmorProfile{}, nil)
},
assert: func(res *PullResult, err error) {
require.NoError(t, err)
Expand All @@ -298,7 +302,7 @@ func TestPull(t *testing.T) {
mock.NewRepositoryReturns(&remote.Repository{}, nil)
mock.ParseReferenceReturns(testRef, nil)
mock.ReadFileReturns([]byte{}, nil)
mock.YamlUnmarshalReturns(errTest)
mock.ReadProfileReturns(nil, errTest)
},
assert: func(res *PullResult, err error) {
require.ErrorIs(t, err, ErrDecodeYAML)
Expand Down
166 changes: 85 additions & 81 deletions internal/pkg/artifact/artifactfakes/fake_impl.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading