Skip to content

Commit

Permalink
allow to use PolicyGenerator with empty namespace in PolicyDefaults
Browse files Browse the repository at this point in the history
  • Loading branch information
yana1205 committed Jul 20, 2023
1 parent 9334954 commit 0390efc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
4 changes: 3 additions & 1 deletion cmd/compose/c2pcr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ compliance:
url: ./pkg/composer/testdata/oscal/component-definition.json
policyResources:
url: ./pkg/composer/testdata/policies
policyResults:
url: a/b/c
clusterGroups:
- name: cluster-nist-high # name of clusterGroup
matchLabels:
Expand All @@ -17,4 +19,4 @@ binding:
clusterGroups:
- cluster-nist-high # clusterGroup name
target:
namespace: compliance-high # namespace to which C2P deploys generated resources
namespace: "" # namespace to which C2P deploys generated resources
13 changes: 13 additions & 0 deletions pkg/composer/composer.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import (

var logger *zap.Logger = pkg.GetLogger("composer")

var DummyNamespace string = "dummy-namespace-c2p"

type Composer struct {
policiesDir string
tempDir pkg.TempDirectory
Expand Down Expand Up @@ -176,6 +178,9 @@ func (c *Composer) Compose(namespace string, componentObjects []oscal.ComponentO
},
}

if policySetGeneratorManifest.PolicyDefaults.Namespace == "" {
policySetGeneratorManifest.PolicyDefaults.Namespace = DummyNamespace
}
if err := pkg.WriteObjToYamlFileByGoYaml(c.tempDir.GetTempDir()+"/policy-generator.yaml", policySetGeneratorManifest); err != nil {
return err
}
Expand Down Expand Up @@ -225,6 +230,14 @@ func (c *Composer) GeneratePolicySet() (*resmap.ResMap, error) {
logger.Sugar().Error(err, "failed to run kustomize")
return nil, err
}
// TODO: Workaround to allow to run PolicyGenerator with empty namespace.
for _, resource := range generatedManifests.Resources() {
if resource.GetNamespace() == DummyNamespace {
if err := resource.SetNamespace(""); err != nil {
return nil, err
}
}
}
return &generatedManifests, nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/composer/composer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestComposer(t *testing.T) {
ClusterGroups: []string{"test-group"},
},
Target: typec2pcr.Target{
Namespace: "test",
Namespace: "",
},
}
c2pcrParser := c2pcr.NewParser(gitUtils)
Expand Down

0 comments on commit 0390efc

Please sign in to comment.