Skip to content

Commit dc4af5e

Browse files
committed
genpolicy: support insecure registries
1 parent 66da417 commit dc4af5e

3 files changed

Lines changed: 36 additions & 10 deletions

File tree

cli/cmd/generate.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ subcommands.`,
9393
cmd.Flags().Bool("insecure-enable-debug-shell-access", false, "enable the debug shell service in the pod CVM to get access from container to guest VM")
9494
cmd.Flags().Bool("calculate-pod-memory", false, "calculate pod memory based on image layer sizes and container resource limits")
9595
cmd.Flags().StringP("output", "o", "", "output file for generated YAML")
96+
cmd.Flags().StringArray("insecure-registry", []string{}, "registries to access via plain HTTP instead of HTTPS (can be passed more than once)")
9697
must(cmd.MarkFlagFilename("policy", "rego"))
9798
must(cmd.MarkFlagFilename("settings", "json"))
9899
must(cmd.MarkFlagFilename("manifest", "json"))
@@ -206,6 +207,10 @@ func runGenerate(cmd *cobra.Command, args []string) error {
206207
}
207208
fmt.Fprintln(cmd.OutOrStdout(), "✔️ Patched targets")
208209

210+
if len(flags.insecureRegistries) > 0 {
211+
fmt.Fprintln(cmd.OutOrStdout(), "⚠️ Using insecure registries for policy generation!")
212+
}
213+
209214
if err := generatePolicies(cmd.Context(), flags, fileMap, extraFile.Name(), log); err != nil {
210215
return fmt.Errorf("generate policies: %w", err)
211216
}
@@ -450,7 +455,7 @@ func generatePolicies(ctx context.Context, flags *generateFlags, fileMap map[str
450455
return fmt.Errorf("creating default policy.rego file: %w", err)
451456
}
452457

453-
runner, err := genpolicy.New(flags.policyPath, flags.settingsPath, flags.genpolicyCachePath, cfg.Bin)
458+
runner, err := genpolicy.New(flags.policyPath, flags.settingsPath, flags.genpolicyCachePath, flags.insecureRegistries, cfg.Bin)
454459
if err != nil {
455460
return fmt.Errorf("preparing genpolicy: %w", err)
456461
}
@@ -960,6 +965,7 @@ type generateFlags struct {
960965
injectImageStore bool
961966
insecureEnableDebugShell bool
962967
calculatePodMemory bool
968+
insecureRegistries []string
963969
outputFile string
964970
}
965971

@@ -1065,6 +1071,10 @@ func parseGenerateFlags(cmd *cobra.Command) (*generateFlags, error) {
10651071
if err != nil {
10661072
return nil, err
10671073
}
1074+
insecureRegistries, err := cmd.Flags().GetStringArray("insecure-registry")
1075+
if err != nil {
1076+
return nil, err
1077+
}
10681078
outputFile, err := cmd.Flags().GetString("output")
10691079
if err != nil {
10701080
return nil, err
@@ -1092,6 +1102,7 @@ func parseGenerateFlags(cmd *cobra.Command) (*generateFlags, error) {
10921102
injectImageStore: injectImageStore,
10931103
insecureEnableDebugShell: insecureEnableDebugShell,
10941104
calculatePodMemory: calculatePodMemory,
1105+
insecureRegistries: insecureRegistries,
10951106
outputFile: outputFile,
10961107
}, nil
10971108
}

cli/genpolicy/genpolicy.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@ import (
2424
type Runner struct {
2525
genpolicy embedbin.Installed
2626

27-
rulesPath string
28-
settingsPath string
29-
cachePath string
27+
rulesPath string
28+
settingsPath string
29+
cachePath string
30+
insecureRegistries []string
3031
}
3132

3233
// New creates a new Runner for the given configuration.
33-
func New(rulesPath, settingsPath, cachePath string, bin []byte) (*Runner, error) {
34+
func New(rulesPath, settingsPath, cachePath string, insecureRegistries []string, bin []byte) (*Runner, error) {
3435
e := embedbin.New()
3536
genpolicy, err := e.Install("", bin)
3637
if err != nil {
@@ -41,10 +42,11 @@ func New(rulesPath, settingsPath, cachePath string, bin []byte) (*Runner, error)
4142
}
4243

4344
runner := &Runner{
44-
genpolicy: genpolicy,
45-
rulesPath: rulesPath,
46-
settingsPath: settingsPath,
47-
cachePath: cachePath,
45+
genpolicy: genpolicy,
46+
rulesPath: rulesPath,
47+
settingsPath: settingsPath,
48+
cachePath: cachePath,
49+
insecureRegistries: insecureRegistries,
4850
}
4951

5052
return runner, nil
@@ -62,6 +64,9 @@ func (r *Runner) Run(ctx context.Context, res any, extraPath string, needLayersC
6264
"--config-file=" + extraPath,
6365
"--base64-out",
6466
}
67+
for _, registry := range r.insecureRegistries {
68+
args = append(args, "--insecure-registry="+registry)
69+
}
6570
genpolicy := exec.CommandContext(ctx, r.genpolicy.Path(), args...)
6671
genpolicy.Cancel = func() error {
6772
return genpolicy.Process.Signal(os.Interrupt)

cli/genpolicy/genpolicy_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"log/slog"
99
"os"
1010
"path/filepath"
11+
"strings"
1112
"testing"
1213

1314
"github.com/edgelesssys/contrast/internal/kuberesource"
@@ -39,6 +40,9 @@ while [ $# -gt 0 ]; do
3940
--layers-cache-file-path=*)
4041
printf "%%s" "[]" >${1#--layers-cache-file-path=}
4142
;;
43+
--insecure-registry=*)
44+
printf "%%s" "${1#--insecure-registry=}" >>insecure_registries
45+
;;
4246
--runtime-class-names*|--yaml-file*|--base64-out*)
4347
;;
4448
*)
@@ -74,10 +78,12 @@ func TestRunner(t *testing.T) {
7478
settingsPathFile := filepath.Join(d, "settings_path")
7579
expectedExtraPath := "/extra.yml"
7680
extraPathFile := filepath.Join(d, "extra_path")
81+
expectedInsecureRegistries := []string{"registry1", "registry2"}
82+
insecureRegistriesFile := filepath.Join(d, "insecure_registries")
7783
cachePath := filepath.Join(d, "cache", "cache.json")
7884
envFile := filepath.Join(d, "env_path")
7985

80-
r, err := New(expectedRulesPath, expectedSettingsPath, cachePath, genpolicyBin)
86+
r, err := New(expectedRulesPath, expectedSettingsPath, cachePath, expectedInsecureRegistries, genpolicyBin)
8187
require.NoError(err)
8288

8389
applyConfig, err := kuberesource.UnmarshalApplyConfigurations([]byte(podYAML))
@@ -98,6 +104,10 @@ func TestRunner(t *testing.T) {
98104
require.NoError(err)
99105
assert.Equal(expectedExtraPath, string(extraPath))
100106

107+
insecureRegistries, err := os.ReadFile(insecureRegistriesFile)
108+
require.NoError(err)
109+
assert.Equal(strings.Join(expectedInsecureRegistries, ""), string(insecureRegistries))
110+
101111
yamlString, err := os.ReadFile(filepath.Join(d, "stdout.yaml"))
102112
require.NoError(err)
103113
assert.YAMLEq(podYAML, string(yamlString))

0 commit comments

Comments
 (0)