Skip to content

Commit 478b898

Browse files
authored
Add validate policyNS + policyName must be not more than 63 chars (#26)
Signed-off-by: melserngawy <[email protected]>
1 parent e6e1081 commit 478b898

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

internal/plugin.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const (
2222
placementBindingKind = "PlacementBinding"
2323
placementRuleAPIVersion = "apps.open-cluster-management.io/v1"
2424
placementRuleKind = "PlacementRule"
25+
maxObjectNameLength = 63
2526
)
2627

2728
// Plugin is used to store the PolicyGenerator configuration and the methods to generate the
@@ -358,6 +359,11 @@ func (p *Plugin) assertValidConfig() error {
358359
return errors.New("each policy must have a name set")
359360
}
360361

362+
if len(p.PolicyDefaults.Namespace+"."+policy.Name) > maxObjectNameLength {
363+
return fmt.Errorf("the policy namespace and name cannot be more than 63 characters %s.%s",
364+
p.PolicyDefaults.Namespace, policy.Name)
365+
}
366+
361367
if seen[policy.Name] {
362368
return fmt.Errorf("each policy must have a unique name set: %s", policy.Name)
363369
}

internal/plugin_config_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,39 @@ policies:
223223
assertEqual(t, err.Error(), expected)
224224
}
225225

226+
func TestCreateInvalidPolicyName(t *testing.T) {
227+
t.Parallel()
228+
tmpDir := t.TempDir()
229+
createConfigMap(t, tmpDir, "configmap.yaml")
230+
configMapPath := path.Join(tmpDir, "configmap.yaml")
231+
policyNS := "my-policies-my-policies-my-policies"
232+
policyName := "policy-app-config-policy-app-config-policy-app-config"
233+
defaultsConfig := fmt.Sprintf(
234+
`
235+
apiVersion: policy.open-cluster-management.io/v1
236+
kind: PolicyGenerator
237+
metadata:
238+
name: policy-generator-name
239+
policyDefaults:
240+
namespace: %s
241+
policies:
242+
- name: %s
243+
manifests:
244+
- path: %s
245+
`,
246+
policyNS, policyName, configMapPath,
247+
)
248+
249+
p := Plugin{}
250+
err := p.Config([]byte(defaultsConfig))
251+
if err == nil {
252+
t.Fatal("Expected an error but did not get one")
253+
}
254+
255+
expected := fmt.Sprintf("the policy namespace and name cannot be more than 63 characters %s.%s", policyNS, policyName)
256+
assertEqual(t, err.Error(), expected)
257+
}
258+
226259
func TestConfigNoPolicies(t *testing.T) {
227260
t.Parallel()
228261
const config = `

0 commit comments

Comments
 (0)