Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions pkg/object/objmetadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,20 @@ func ParseObjMetadata(s string) (ObjMetadata, error) {
// Finally, second field name. Name may contain colon transcoded as double underscore.
name := s[:index]
name = strings.ReplaceAll(name, colonTranscoded, ":")
// Check that there are no extra fields by search for fieldSeparator.
if strings.Contains(name, fieldSeparator) {
return NilObjMetadata, fmt.Errorf("too many fields within: %s", s)
gk := schema.GroupKind{Group: group, Kind: kind}
// RBAC resources may contain a literal underscore in their name, so the
// extra-fields check below would reject otherwise valid identifiers.
if _, isRBAC := RBACGroupKind[gk]; !isRBAC {
// Check that there are no extra fields by search for fieldSeparator.
if strings.Contains(name, fieldSeparator) {
return NilObjMetadata, fmt.Errorf("too many fields within: %s", s)
}
}
// Create the ObjMetadata object from the four parsed fields.
id := ObjMetadata{
Namespace: namespace,
Name: name,
GroupKind: schema.GroupKind{
Group: group,
Kind: kind,
},
GroupKind: gk,
}
return id, nil
}
Expand Down
12 changes: 12 additions & 0 deletions pkg/object/objmetadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ func TestParseObjMetadata(t *testing.T) {
},
isError: false,
},
"RBAC resources can have a literal underscore in their name": {
invStr: "test-namespace_roles_ops_rbac.authorization.k8s.io_ClusterRole",
inventory: &ObjMetadata{
Namespace: "test-namespace",
Name: "roles_ops",
GroupKind: schema.GroupKind{
Group: rbacv1.GroupName,
Kind: "ClusterRole",
},
},
isError: false,
},
"Test double underscore (colon) at end of name": {
invStr: "test-namespace_leader-locking-kube-scheduler___rbac.authorization.k8s.io_RoleBinding",
inventory: &ObjMetadata{
Expand Down