Skip to content

Commit 0a0897d

Browse files
committed
Add support for spec.hubTemplateOptions when generating a policy
Relates: https://issues.redhat.com/browse/ACM-13608 Signed-off-by: mprahl <[email protected]>
1 parent 243dd4a commit 0a0897d

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed

docs/policygenerator-reference.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ policyDefaults:
7676
kind: "Policy"
7777
# Optional. (See policyDefaults.dependencies.apiVersion for description.)
7878
apiVersion: "policy.open-cluster-management.io/v1"
79+
# Optional. Changes the default behavior of hub templates.
80+
hubTemplateOptions:
81+
# Optional. serviceAccountName is the name of a service account in the same namespace as the policy to use for all hub
82+
# template lookups. The service account must have list and watch permissions on any object the hub templates
83+
# look up. If not specified, lookups are restricted to namespaced objects in the same namespace as the policy and
84+
# to the `ManagedCluster` object associated with the propagated policy.
85+
serviceAccountName: ""
7986
# Optional. Determines whether objects created or monitored by the policy should be deleted when the policy is
8087
# deleted. Pruning only takes place if the remediation action of the policy has been set to "enforce". Example values
8188
# are "DeleteIfCreated", "DeleteAll", or "None". This defaults to unset, which is equivalent to "None".
@@ -252,6 +259,9 @@ policies:
252259
# Optional. (See policyDefaults.extraDependencies for description)
253260
# Cannot be specified when policyDefaults.consolidateManifests is set to true.
254261
extraDependencies: []
262+
# Optional. (See policyDefaults.hubTemplateOptions for description.)
263+
hubTemplateOptions:
264+
serviceAccountName: ""
255265
# Optional. (See policyDefaults.pruneObjectBehavior for description.)
256266
# Cannot be specified when policyDefaults.consolidateManifests is set to true.
257267
pruneObjectBehavior: ""

internal/plugin.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,10 @@ func (p *Plugin) applyDefaults(unmarshaledConfig map[string]interface{}) {
696696
policy.Severity = p.PolicyDefaults.Severity
697697
}
698698

699+
if policy.HubTemplateOptions.ServiceAccountName == "" {
700+
policy.HubTemplateOptions.ServiceAccountName = p.PolicyDefaults.HubTemplateOptions.ServiceAccountName
701+
}
702+
699703
for j := range policy.Manifests {
700704
manifest := &policy.Manifests[j]
701705

@@ -1364,6 +1368,10 @@ func (p *Plugin) createPolicy(policyConf *types.PolicyConfig) error {
13641368
"policy-templates": policyTemplates,
13651369
}
13661370

1371+
if policyConf.HubTemplateOptions.ServiceAccountName != "" {
1372+
spec["hubTemplateOptions"] = policyConf.HubTemplateOptions
1373+
}
1374+
13671375
if p.PolicyDefaults.OrderPolicies && p.previousPolicyName != "" {
13681376
policyConf.Dependencies = []types.PolicyDependency{{
13691377
Name: p.previousPolicyName,

internal/plugin_test.go

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,6 +1496,123 @@ spec:
14961496
assertEqual(t, output, expected)
14971497
}
14981498

1499+
func TestCreatePolicyHubTemplateOptions(t *testing.T) {
1500+
t.Parallel()
1501+
tmpDir := t.TempDir()
1502+
createConfigMap(t, tmpDir, "configmap.yaml")
1503+
1504+
p := Plugin{}
1505+
p.PolicyDefaults.Namespace = "my-policies"
1506+
p.PolicyDefaults.HubTemplateOptions = types.HubTemplateOptions{ServiceAccountName: "default-sa"}
1507+
1508+
policyConf := types.PolicyConfig{
1509+
Name: "policy-app-config",
1510+
Manifests: []types.Manifest{
1511+
{Path: path.Join(tmpDir, "configmap.yaml")},
1512+
},
1513+
}
1514+
p.Policies = append(p.Policies, policyConf)
1515+
1516+
p.applyDefaults(map[string]interface{}{})
1517+
1518+
err := p.createPolicy(&p.Policies[0])
1519+
if err != nil {
1520+
t.Fatal(err.Error())
1521+
}
1522+
1523+
output := p.outputBuffer.String()
1524+
expected := `
1525+
---
1526+
apiVersion: policy.open-cluster-management.io/v1
1527+
kind: Policy
1528+
metadata:
1529+
annotations:
1530+
policy.open-cluster-management.io/categories: CM Configuration Management
1531+
policy.open-cluster-management.io/controls: CM-2 Baseline Configuration
1532+
policy.open-cluster-management.io/description: ""
1533+
policy.open-cluster-management.io/standards: NIST SP 800-53
1534+
name: policy-app-config
1535+
namespace: my-policies
1536+
spec:
1537+
disabled: false
1538+
hubTemplateOptions:
1539+
serviceAccountName: default-sa
1540+
policy-templates:
1541+
- objectDefinition:
1542+
apiVersion: policy.open-cluster-management.io/v1
1543+
kind: ConfigurationPolicy
1544+
metadata:
1545+
name: policy-app-config
1546+
spec:
1547+
object-templates:
1548+
- complianceType: musthave
1549+
objectDefinition:
1550+
apiVersion: v1
1551+
data:
1552+
game.properties: enemies=potato
1553+
kind: ConfigMap
1554+
metadata:
1555+
name: my-configmap
1556+
remediationAction: inform
1557+
severity: low
1558+
remediationAction: inform
1559+
`
1560+
expected = strings.TrimPrefix(expected, "\n")
1561+
assertEqual(t, output, expected)
1562+
1563+
// Override the value on the policy
1564+
p.outputBuffer.Reset()
1565+
p.Policies[0].PolicyOptions = types.PolicyOptions{
1566+
HubTemplateOptions: types.HubTemplateOptions{ServiceAccountName: "override-sa"},
1567+
}
1568+
p.applyDefaults(map[string]interface{}{})
1569+
1570+
err = p.createPolicy(&p.Policies[0])
1571+
if err != nil {
1572+
t.Fatal(err.Error())
1573+
}
1574+
1575+
output = p.outputBuffer.String()
1576+
expected = `
1577+
---
1578+
apiVersion: policy.open-cluster-management.io/v1
1579+
kind: Policy
1580+
metadata:
1581+
annotations:
1582+
policy.open-cluster-management.io/categories: CM Configuration Management
1583+
policy.open-cluster-management.io/controls: CM-2 Baseline Configuration
1584+
policy.open-cluster-management.io/description: ""
1585+
policy.open-cluster-management.io/standards: NIST SP 800-53
1586+
name: policy-app-config
1587+
namespace: my-policies
1588+
spec:
1589+
disabled: false
1590+
hubTemplateOptions:
1591+
serviceAccountName: override-sa
1592+
policy-templates:
1593+
- objectDefinition:
1594+
apiVersion: policy.open-cluster-management.io/v1
1595+
kind: ConfigurationPolicy
1596+
metadata:
1597+
name: policy-app-config
1598+
spec:
1599+
object-templates:
1600+
- complianceType: musthave
1601+
objectDefinition:
1602+
apiVersion: v1
1603+
data:
1604+
game.properties: enemies=potato
1605+
kind: ConfigMap
1606+
metadata:
1607+
name: my-configmap
1608+
remediationAction: inform
1609+
severity: low
1610+
remediationAction: inform
1611+
`
1612+
expected = strings.TrimPrefix(expected, "\n")
1613+
assertEqual(t, output, expected)
1614+
}
1615+
14991616
func TestCreatePolicyFromCertificatePolicyTypeManifest(t *testing.T) {
15001617
t.Parallel()
15011618
tmpDir := t.TempDir()

internal/types/types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import (
77
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
88
)
99

10+
type HubTemplateOptions struct {
11+
ServiceAccountName string `json:"serviceAccountName,omitempty" yaml:"serviceAccountName,omitempty"`
12+
}
13+
1014
type PolicyOptions struct {
1115
Categories []string `json:"categories,omitempty" yaml:"categories,omitempty"`
1216
Controls []string `json:"controls,omitempty" yaml:"controls,omitempty"`
@@ -28,6 +32,7 @@ type PolicyOptions struct {
2832
PolicyAnnotations map[string]string `json:"policyAnnotations,omitempty" yaml:"policyAnnotations,omitempty"`
2933
PolicyLabels map[string]string `json:"policyLabels,omitempty" yaml:"policyLabels,omitempty"`
3034
ConfigurationPolicyAnnotations map[string]string `json:"configurationPolicyAnnotations,omitempty" yaml:"configurationPolicyAnnotations,omitempty"`
35+
HubTemplateOptions HubTemplateOptions `json:"hubTemplateOptions,omitempty" yaml:"hubTemplateOptions,omitempty"`
3136
}
3237

3338
type PolicySetOptions struct {

0 commit comments

Comments
 (0)