Skip to content

Commit

Permalink
config: Generate C:\-style capabilities paths (#1209)
Browse files Browse the repository at this point in the history
This change ensures that config files with relative capabilities
references work correctly.

Fixes #1202

Signed-off-by: Charlie Egan <charlie@styra.com>
  • Loading branch information
charlieegan3 authored Oct 16, 2024
1 parent d39da24 commit 173a992
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
14 changes: 12 additions & 2 deletions internal/capabilities/capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/url"
"os"
"path"
"regexp"
"slices"
"sort"
"strings"
Expand Down Expand Up @@ -190,9 +191,18 @@ func lookupEmbeddedURL(parsedURL *url.URL) (*ast.Capabilities, error) {
}

func lookupFileURL(parsedURL *url.URL) (*ast.Capabilities, error) {
fd, err := os.Open(parsedURL.Path)
// the provided URL's path could be either a windows path or a unix one
// we must account for both cases by stripping the leading / if found
driveLetterPattern := regexp.MustCompile(`^\/[a-zA-Z]:`)

path := parsedURL.Path
if driveLetterPattern.MatchString(path) {
path = path[1:]
}

fd, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("error opening file '%s': %w", parsedURL.Path, err)
return nil, fmt.Errorf("error opening file '%s': %w", path, err)
}

caps, err := ast.LoadCapabilitiesJSON(fd)
Expand Down
6 changes: 6 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,12 @@ func (config *Config) UnmarshalYAML(value *yaml.Node) error {
)
}

// prepending a / is done here to ensure that windows drive letter paths
// are parsed as paths and not host:ports in URLs.
if !strings.HasPrefix(absfp, "/") {
absfp = "/" + absfp
}

capabilitiesURL = "file://" + absfp
}

Expand Down

0 comments on commit 173a992

Please # to comment.