Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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
87 changes: 62 additions & 25 deletions lib/identifier/registered_resource_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,88 @@ import (
)

type FullyQualifiedRegisteredResourceValue struct {
Name string
Value string
Namespace string
Name string
Value string
}

// protovalidate already validates the FQN format in the service request
// for parsing purposes, we can just look for any non-whitespace characters
// e.g. should be in format of "https://reg_res/<name>/value/<value>"
// New FQN format: https://<namespace>/rr/<name>/value/<value>
var registeredResourceValueFqnRegex = regexp.MustCompile(
`^https:\/\/reg_res\/(?<name>[^\/]+)\/value\/(?<value>[^\/]+)$`,
`^https:\/\/(?P<namespace>[^\/]+)\/rr\/(?P<name>[^\/]+)\/value\/(?P<value>[^\/]+)$`,
)

// Legacy FQN format: https://reg_res/<name>/value/<value>
var legacyRegisteredResourceValueFqnRegex = regexp.MustCompile(
`^https:\/\/reg_res\/(?P<name>[^\/]+)\/value\/(?P<value>[^\/]+)$`,
)

// parseRegisteredResourceValueFqn parses a registered resource value FQN string into a FullyQualifiedRegisteredResourceValue struct.
// The FQN must be in the format: https://reg_res/<name>/value/<value>
// Supports both the new format: https://<namespace>/rr/<name>/value/<value>
// and the legacy format: https://reg_res/<name>/value/<value>
func parseRegisteredResourceValueFqn(fqn string) (*FullyQualifiedRegisteredResourceValue, error) {
// Try new format first
matches := registeredResourceValueFqnRegex.FindStringSubmatch(fqn)
if len(matches) > 0 {
namespaceIdx := registeredResourceValueFqnRegex.SubexpIndex("namespace")
nameIdx := registeredResourceValueFqnRegex.SubexpIndex("name")
valueIdx := registeredResourceValueFqnRegex.SubexpIndex("value")

// Check if we have matches first
if len(matches) == 0 {
return nil, fmt.Errorf("%w: FQN must be in format https://reg_res/<name>/value/<value>", ErrInvalidFQNFormat)
}
if namespaceIdx == -1 || nameIdx == -1 || valueIdx == -1 || len(matches) <= namespaceIdx || len(matches) <= nameIdx || len(matches) <= valueIdx {
return nil, fmt.Errorf("%w: valid FQN format of https://<namespace>/rr/<name>/value/<value> must be provided", ErrInvalidFQNFormat)
}

nameIdx := registeredResourceValueFqnRegex.SubexpIndex("name")
valueIdx := registeredResourceValueFqnRegex.SubexpIndex("value")
namespace := strings.ToLower(matches[namespaceIdx])
name := strings.ToLower(matches[nameIdx])
value := strings.ToLower(matches[valueIdx])

if nameIdx == -1 || valueIdx == -1 || len(matches) <= nameIdx || len(matches) <= valueIdx {
return nil, fmt.Errorf("%w: valid FQN format of https://reg_res/<name>/value/<value> must be provided", ErrInvalidFQNFormat)
if !validObjectNameRegex.MatchString(name) || !validObjectNameRegex.MatchString(value) {
return nil, fmt.Errorf("%w: found name %s with value %s", ErrInvalidFQNFormat, name, value)
}

return &FullyQualifiedRegisteredResourceValue{
Namespace: namespace,
Name: name,
Value: value,
}, nil
}

name := strings.ToLower(matches[nameIdx])
value := strings.ToLower(matches[valueIdx])
isValid := validObjectNameRegex.MatchString(name) && validObjectNameRegex.MatchString(value)
if !isValid {
return nil, fmt.Errorf("%w: found name %s with value %s", ErrInvalidFQNFormat, name, value)
// Try legacy format
matches = legacyRegisteredResourceValueFqnRegex.FindStringSubmatch(fqn)
if len(matches) > 0 {
nameIdx := legacyRegisteredResourceValueFqnRegex.SubexpIndex("name")
valueIdx := legacyRegisteredResourceValueFqnRegex.SubexpIndex("value")

if nameIdx == -1 || valueIdx == -1 || len(matches) <= nameIdx || len(matches) <= valueIdx {
return nil, fmt.Errorf("%w: valid FQN format of https://reg_res/<name>/value/<value> must be provided", ErrInvalidFQNFormat)
}

name := strings.ToLower(matches[nameIdx])
value := strings.ToLower(matches[valueIdx])

if !validObjectNameRegex.MatchString(name) || !validObjectNameRegex.MatchString(value) {
return nil, fmt.Errorf("%w: found name %s with value %s", ErrInvalidFQNFormat, name, value)
}

return &FullyQualifiedRegisteredResourceValue{
Name: name,
Value: value,
}, nil
}

return &FullyQualifiedRegisteredResourceValue{
Name: name,
Value: value,
}, nil
return nil, fmt.Errorf("%w: FQN must be in format https://<namespace>/rr/<name>/value/<value>", ErrInvalidFQNFormat)
}
Comment thread
alkalescent marked this conversation as resolved.
Resolved

// Implementing FullyQualified interface for FullyQualifiedRegisteredResourceValue
func (rrv *FullyQualifiedRegisteredResourceValue) FQN() string {
builder := strings.Builder{}
builder.WriteString("https://reg_res/")
if rrv.Namespace != "" {
builder.WriteString("https://")
builder.WriteString(rrv.Namespace)
builder.WriteString("/rr/")
} else {
// Legacy format for backward compatibility
builder.WriteString("https://reg_res/")
}
builder.WriteString(rrv.Name)
builder.WriteString("/value/")
builder.WriteString(rrv.Value)
Expand Down
Loading
Loading