Skip to content

Commit

Permalink
Add spoc, the Security Profiles Operator CLI
Browse files Browse the repository at this point in the history
We discussed in our last sync meeting that it would be helpful to have a
slimmer SPO CLI for faster execution in edge scenarios. For that we
would need a new binary, which can be used to directly interact with
features like the ebpf recorder.

Refers to #1482

Signed-off-by: Sascha Grunert <sgrunert@redhat.com>
  • Loading branch information
saschagrunert committed Feb 20, 2023
1 parent c26a139 commit 089652b
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 37 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ LABEL name="Security Profiles Operator" \

COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=make /work/result/security-profiles-operator /
COPY --from=make /work/result/spoc /

USER 65535:65535

Expand Down
1 change: 1 addition & 0 deletions Dockerfile.ubi
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ LABEL name="Security Profiles Operator" \
description="The Security Profiles Operator makes it easier for cluster admins to manage their seccomp, SELinux or AppArmor profiles and apply them to Kubernetes' workloads."

COPY --from=build /work/build/security-profiles-operator /usr/bin/
COPY --from=build /work/build/spoc /usr/bin/

ENTRYPOINT ["/usr/bin/security-profiles-operator"]

Expand Down
12 changes: 10 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ CI_IMAGE ?= golang:1.20
CONTROLLER_GEN_CMD := CGO_LDFLAGS= $(GO) run -tags generate sigs.k8s.io/controller-tools/cmd/controller-gen

PROJECT := security-profiles-operator
CLI_BINARY := spoc
BUILD_DIR := build

APPARMOR_ENABLED ?= 1
Expand Down Expand Up @@ -125,7 +126,7 @@ DOCKERFILE ?= Dockerfile

# Utility targets

all: $(BUILD_DIR)/$(PROJECT) ## Build the security-profiles-operator binary
all: $(BUILD_DIR)/$(PROJECT) $(BUILD_DIR)/$(CLI_BINARY) ## Build the project binaries

.PHONY: help
help: ## Display this help
Expand All @@ -147,8 +148,15 @@ help: ## Display this help
$(BUILD_DIR):
mkdir -p $(BUILD_DIR)

define go-build-spo
$(GO) build -trimpath -ldflags '$(LDFLAGS)' -tags '$(BUILDTAGS)' -o $@ ./cmd/$(1)
endef

$(BUILD_DIR)/$(PROJECT): $(BUILD_DIR) $(BUILD_FILES)
$(GO) build -trimpath -ldflags '$(LDFLAGS)' -tags '$(BUILDTAGS)' -o $@ ./cmd/security-profiles-operator
$(call go-build-spo,$(PROJECT))

$(BUILD_DIR)/$(CLI_BINARY): $(BUILD_DIR) $(BUILD_FILES)
$(call go-build-spo,$(CLI_BINARY))

.PHONY: clean
clean: ## Clean the build directory
Expand Down
67 changes: 67 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
Copyright 2023 The Kubernetes Authors.
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 cmd

import (
"fmt"
"log"

"github.com/urfave/cli/v2"

"sigs.k8s.io/security-profiles-operator/internal/pkg/version"
)

const jsonFlag string = "json"

func DefaultApp() (*cli.App, *version.Info) {
app := cli.NewApp()

info, err := version.Get()
if err != nil {
log.Fatal(err)
}
app.Version = info.Version

app.Commands = cli.Commands{
&cli.Command{
Name: "version",
Aliases: []string{"v"},
Usage: "display detailed version information",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: jsonFlag,
Aliases: []string{"j"},
Usage: "print JSON instead of text",
},
},
Action: func(c *cli.Context) error {
res := info.String()
if c.Bool(jsonFlag) {
j, err := info.JSONString()
if err != nil {
return fmt.Errorf("unable to generate JSON from version info: %w", err)
}
res = j
}
print(res)
return nil
},
},
}

return app, info
}
39 changes: 5 additions & 34 deletions cmd/security-profiles-operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"errors"
"flag"
"fmt"
"log"
"net/http"
_ "net/http/pprof" //nolint:gosec // required for profiling
"os"
Expand All @@ -46,6 +45,7 @@ import (
secprofnodestatusv1alpha1 "sigs.k8s.io/security-profiles-operator/api/secprofnodestatus/v1alpha1"
selxv1alpha2 "sigs.k8s.io/security-profiles-operator/api/selinuxprofile/v1alpha2"
spodv1alpha1 "sigs.k8s.io/security-profiles-operator/api/spod/v1alpha1"
"sigs.k8s.io/security-profiles-operator/cmd"
"sigs.k8s.io/security-profiles-operator/internal/pkg/config"
"sigs.k8s.io/security-profiles-operator/internal/pkg/controller"
"sigs.k8s.io/security-profiles-operator/internal/pkg/daemon/apparmorprofile"
Expand Down Expand Up @@ -83,43 +83,13 @@ var (
)

func main() {
app := cli.NewApp()
app, info := cmd.DefaultApp()
app.Name = config.OperatorName
app.Usage = "Kubernetes Security Profiles Operator"
app.Description = "The Security Profiles Operator makes it easier for cluster admins " +
"to manage their seccomp or AppArmor profiles and apply them to Kubernetes' workloads."

info, err := version.Get()
if err != nil {
log.Fatal(err)
}
app.Version = info.Version

app.Commands = cli.Commands{
&cli.Command{
Name: "version",
Aliases: []string{"v"},
Usage: "display detailed version information",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: jsonFlag,
Aliases: []string{"j"},
Usage: "print JSON instead of text",
},
},
Action: func(c *cli.Context) error {
res := info.String()
if c.Bool(jsonFlag) {
j, err := info.JSONString()
if err != nil {
return fmt.Errorf("unable to generate JSON from version info: %w", err)
}
res = j
}
print(res)
return nil
},
},
app.Commands = append(app.Commands,
&cli.Command{
Before: initialize,
Name: "manager",
Expand Down Expand Up @@ -226,7 +196,8 @@ func main() {
return runBPFRecorder(ctx, info)
},
},
}
)

app.Flags = []cli.Flag{
&cli.UintFlag{
Name: "verbosity",
Expand Down
44 changes: 44 additions & 0 deletions cmd/spoc/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Copyright 2023 The Kubernetes Authors.
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 main

import (
"log"
"os"

"github.com/urfave/cli/v2"

"sigs.k8s.io/security-profiles-operator/cmd"
)

func main() {
app, _ := cmd.DefaultApp()
app.Usage = "Security Profiles Operator CLI"

app.Commands = append(app.Commands,
&cli.Command{
Name: "record",
Aliases: []string{"r"},
Usage: "run the recorder",
Action: record,
},
)

if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
24 changes: 24 additions & 0 deletions cmd/spoc/record.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
Copyright 2023 The Kubernetes Authors.
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 main

import "github.com/urfave/cli/v2"

// record runs the `spoc record` subcommand.
func record(ctx *cli.Context) error {
return nil
}
2 changes: 1 addition & 1 deletion nix/derivation.nix
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ with pkgs; buildGo119Module rec {
make WITH_BPF=1
'';
installPhase = ''
install -Dm755 -t $out build/security-profiles-operator
install -Dm755 -t $out build/security-profiles-operator build/spoc
'';
}

0 comments on commit 089652b

Please sign in to comment.