Skip to content

Commit

Permalink
internal/rest/resources: Reserve the /core namespace
Browse files Browse the repository at this point in the history
...for CoreAPI servers

Fixes canonical#227

Signed-off-by: Wesley Hershberger <[email protected]>
  • Loading branch information
MggMuggins committed Aug 9, 2024
1 parent a2624ea commit bde792b
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions internal/rest/resources/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ func ValidateEndpoints(extensionServers map[string]rest.Server, coreAddress stri
serverAddresses[server.Address.String()] = true
}

// Reserve the /core namespace for Microcluster
if server.CoreAPI {
for _, resource := range server.Resources {
if firstPathElement(string(resource.PathPrefix)) == endpoints.EndpointsCore {
return fmt.Errorf("Resource path prefix from server %q conflicts with reserved namespace /%s", serverName, endpoints.EndpointsCore)
}
}
}

// Ensure no endpoint path conflicts with another endpoint on the same server.
// If a server is an extension to the core API, it will be compared against all core API paths.
for _, resource := range server.Resources {
Expand Down Expand Up @@ -124,3 +133,18 @@ func ValidateEndpoints(extensionServers map[string]rest.Server, coreAddress stri

return nil
}

// "/core" -> "core"
// "/core/internal" -> "core"
// "/" -> ""
// Returns the first non-root path element in `path`.
func firstPathElement(path string) string {
dir, file := filepath.Split(path)
if dir == "" || dir == "/" {
return file
}
if file == "" {
return filepath.Base(dir)
}
return firstPathElement(dir)
}

0 comments on commit bde792b

Please sign in to comment.