Skip to content

Commit

Permalink
accomidate windows file paths
Browse files Browse the repository at this point in the history
  • Loading branch information
c-kruse committed Nov 12, 2021
1 parent b915d7c commit b54ef06
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions internal/cert/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,27 +65,34 @@ func CollectMetrics(ctx context.Context, path string, cfg Config) (Metrics, erro
}

func parse(cert string) (certificateLoader, error) {
if strings.HasPrefix(cert, "file://") ||
strings.HasPrefix(cert, "/") ||
strings.Index(cert, ":\\") == 1 {

path := strings.TrimPrefix(cert, "file://")
info, err := os.Stat(path)
if err != nil {
return nil, fmt.Errorf("file not found: %s", path)
}
if info.IsDir() {
return nil, fmt.Errorf("cannot use directory: %s", path)
}
return fromFile(path), nil
}

// Parse as network URL
certURL, err := url.Parse(cert)
if err != nil {
return nil, fmt.Errorf("error parsing URL %v", err)
}
switch certURL.Scheme {
case "", "file":
info, err := os.Stat(certURL.Path)
if err != nil {
return nil, fmt.Errorf("file not found: %s", certURL.Path)
}
if info.IsDir() {
return nil, fmt.Errorf("cannot use directory: %s", certURL.Path)
}
return fromFile(certURL.Path), nil
case "https", "tcp", "tcp4", "tcp6":
if certURL.Scheme == "https" {
certURL.Scheme = "tcp"
if certURL.Port() == "" {
certURL.Host = fmt.Sprintf("%s:443", certURL.Host)
}
case "https":
certURL.Scheme = "tcp"
if certURL.Port() == "" {
certURL.Host = fmt.Sprintf("%s:443", certURL.Host)
}
fallthrough
case "tcp", "tcp4", "tcp6":
return fromTLSHandshake(certURL), nil
default:
return nil, fmt.Errorf("unsupported scheme %s", certURL.Scheme)
Expand Down

0 comments on commit b54ef06

Please # to comment.