Skip to content

Commit

Permalink
contrast: use kata-specific genpolicy binary for bare-metal platforms
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Meyer <[email protected]>
  • Loading branch information
katexochen committed Sep 5, 2024
1 parent b19826e commit db98d6b
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cli/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func generatePolicies(ctx context.Context, flags *generateFlags, yamlPaths []str
return fmt.Errorf("creating default policy.rego file: %w", err)
}

runner, err := genpolicy.New(flags.policyPath, flags.settingsPath, flags.genpolicyCachePath)
runner, err := genpolicy.New(flags.policyPath, flags.settingsPath, flags.genpolicyCachePath, cfg.Bin)
if err != nil {
return fmt.Errorf("preparing genpolicy: %w", err)
}
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions cli/genpolicy/assets/genpolicy-microsoft
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# THIS FILE IS REPLACED DURING BUILD AND ONLY HERE TO SATISFY GO TOOLING
10 changes: 8 additions & 2 deletions cli/genpolicy/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import (
)

var (
//go:embed assets/genpolicy
genpolicyBin []byte
//go:embed assets/genpolicy-microsoft
aksGenpolicyBin []byte
//go:embed assets/genpolicy-kata
kataGenpolicyBin []byte
//go:embed assets/genpolicy-settings-microsoft.json
aksSettings []byte
//go:embed assets/genpolicy-settings-kata.json
Expand All @@ -28,6 +30,8 @@ type Config struct {
Rules []byte
// Settings is a json config file that holds platform-specific configuration.
Settings []byte
// Bin is the genpolicy binary.
Bin []byte
}

// NewConfig selects the appropriate genpolicy configuration for the target platform.
Expand All @@ -37,11 +41,13 @@ func NewConfig(platform platforms.Platform) *Config {
return &Config{
Rules: aksRules,
Settings: aksSettings,
Bin: aksGenpolicyBin,
}
case platforms.K3sQEMUSNP, platforms.K3sQEMUTDX, platforms.RKE2QEMUTDX:
return &Config{
Rules: kataRules,
Settings: kataSettings,
Bin: kataGenpolicyBin,
}
default:
return nil
Expand Down
4 changes: 2 additions & 2 deletions cli/genpolicy/genpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ type Runner struct {
}

// New creates a new Runner for the given configuration.
func New(rulesPath, settingsPath, cachePath string) (*Runner, error) {
func New(rulesPath, settingsPath, cachePath string, bin []byte) (*Runner, error) {
e := embedbin.New()
genpolicy, err := e.Install("", genpolicyBin)
genpolicy, err := e.Install("", bin)
if err != nil {
return nil, fmt.Errorf("installing genpolicy: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cli/genpolicy/genpolicy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestRunner(t *testing.T) {
logger := slog.Default()

d := t.TempDir()
genpolicyBin = []byte(fmt.Sprintf(scriptTemplate, d))
genpolicyBin := []byte(fmt.Sprintf(scriptTemplate, d))

expectedRulesPath := "/rules.rego"
rulesPathFile := filepath.Join(d, "rules_path")
Expand All @@ -58,7 +58,7 @@ func TestRunner(t *testing.T) {
expectedYAMLPath := filepath.Join(d, "test.yaml")
yamlPathFile := filepath.Join(d, "yaml_path")

r, err := New(expectedRulesPath, expectedSettingsPath, cachePath)
r, err := New(expectedRulesPath, expectedSettingsPath, cachePath, genpolicyBin)
require.NoError(err)

require.NoError(r.Run(ctx, expectedYAMLPath, logger))
Expand Down
4 changes: 2 additions & 2 deletions packages/by-name/contrast/package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
buildGoTest,
microsoft,
kata,
genpolicy ? microsoft.genpolicy,
contrast,
installShellFiles,
}:
Expand Down Expand Up @@ -174,7 +173,8 @@ buildGoModule rec {
subPackages = packageOutputs ++ [ "internal/kuberesource/resourcegen" ];

prePatch = ''
install -D ${lib.getExe genpolicy} cli/genpolicy/assets/genpolicy
install -D ${lib.getExe microsoft.genpolicy} cli/genpolicy/assets/genpolicy-microsoft
install -D ${lib.getExe kata.genpolicy} cli/genpolicy/assets/genpolicy-kata
install -D ${microsoft.genpolicy.rules}/genpolicy-rules.rego cli/genpolicy/assets/genpolicy-rules-microsoft.rego
install -D ${kata.genpolicy.rules}/genpolicy-rules.rego cli/genpolicy/assets/genpolicy-rules-kata.rego
install -D ${embeddedReferenceValues} internal/manifest/assets/reference-values.json
Expand Down

0 comments on commit db98d6b

Please sign in to comment.