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

added OCI hook configuration command #466

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
75 changes: 75 additions & 0 deletions cmd/injecthook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package cmd

import (
"encoding/json"
"os"
"path/filepath"

"github.com/opencontainers/runtime-spec/specs-go"
"github.com/spf13/cobra"
hooks "github.com/containers/common/pkg/hooks/1.0.0"
)

var hookDir string
var criSocket string

// injectCmd represents the inject command to place the OCI hook configuration
var injectCmd = &cobra.Command{
Use: "inject",
Short: "Inject OCI hooks",
Long: `Injects an OCI hook configuration for KubeArmor into the specified directory.`,
RunE: func(cmd *cobra.Command, args []string) error {

if err := os.MkdirAll(hookDir, 0750); err != nil {
return err
}

dst, err := os.OpenFile(filepath.Join(hookDir, "ka.json"), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer dst.Close()

always := true
hook := hooks.Hook{
Version: "1.0.0",
Hook: specs.Hook{
Path: "/usr/share/kubearmor/hook",
Args: []string{
"/usr/share/kubearmor/hook",
"--runtime-socket",
criSocket,
},
Env: []string{
"XDG_CONFIG_HOME=/tmp/",
},
},
When: hooks.When{Always: &always},
Stages: []string{
"poststart",
"poststop",
},
}

hookBytes, err := json.Marshal(hook)
if err != nil {
return err
}

_, err = dst.Write(hookBytes)
if err != nil {
return err
}

return nil
},
}

func init() {
rootCmd.AddCommand(injectCmd)

injectCmd.Flags().StringVar(&hookDir, "hook-dir", "", "Path to the hook config directory (e.g., /etc/containers/oci/hooks.d)")
injectCmd.Flags().StringVar(&criSocket, "cri-socket", "", "Path to the runtime socket (e.g., /run/podman/podman.sock)")
injectCmd.MarkFlagRequired("hook-dir")
injectCmd.MarkFlagRequired("cri-socket")
}