Lean on built-in URL parsing to enable path prefix

Signed-off-by: Dan Luhring <dan.luhring@anchore.com>
This commit is contained in:
Dan Luhring 2021-02-01 10:26:48 -05:00
parent b22fd987db
commit 6d730d24dd
No known key found for this signature in database
GPG Key ID: 9CEE23D079426CEF
3 changed files with 23 additions and 18 deletions

View File

@ -158,19 +158,10 @@ func doImport(src source.Source, s source.Metadata, catalog *pkg.Catalog, d *dis
} }
} }
var scheme string
var hostname = appConfig.Anchore.Host
urlFields := strings.Split(hostname, "://")
if len(urlFields) > 1 {
scheme = urlFields[0]
hostname = urlFields[1]
}
c, err := anchore.NewClient(anchore.Configuration{ c, err := anchore.NewClient(anchore.Configuration{
Hostname: hostname, BasePath: appConfig.Anchore.Host,
Username: appConfig.Anchore.Username, Username: appConfig.Anchore.Username,
Password: appConfig.Anchore.Password, Password: appConfig.Anchore.Password,
Scheme: scheme,
}) })
if err != nil { if err != nil {
return fmt.Errorf("failed to create anchore client: %+v", err) return fmt.Errorf("failed to create anchore client: %+v", err)

View File

@ -3,6 +3,7 @@ package anchore
import ( import (
"context" "context"
"fmt" "fmt"
"strings"
"github.com/anchore/client-go/pkg/external" "github.com/anchore/client-go/pkg/external"
"github.com/anchore/syft/internal" "github.com/anchore/syft/internal"
@ -10,11 +11,10 @@ import (
) )
type Configuration struct { type Configuration struct {
Hostname string BasePath string
Username string Username string
Password string Password string
UserAgent string UserAgent string
Scheme string
} }
type Client struct { type Client struct {
@ -29,16 +29,14 @@ func NewClient(cfg Configuration) (*Client, error) {
cfg.UserAgent = fmt.Sprintf("%s / %s %s", internal.ApplicationName, versionInfo.Version, versionInfo.Platform) cfg.UserAgent = fmt.Sprintf("%s / %s %s", internal.ApplicationName, versionInfo.Version, versionInfo.Platform)
} }
if cfg.Scheme == "" { basePath := ensureURLHasScheme(cfg.BasePath) // we can rely on the built-in URL parsing for the scheme, host,
cfg.Scheme = "https" // port, and path prefix, as long as a scheme is present
}
return &Client{ return &Client{
config: cfg, config: cfg,
client: external.NewAPIClient(&external.Configuration{ client: external.NewAPIClient(&external.Configuration{
Host: cfg.Hostname, BasePath: basePath,
UserAgent: cfg.UserAgent, UserAgent: cfg.UserAgent,
Scheme: cfg.Scheme,
}), }),
}, nil }, nil
} }
@ -56,3 +54,19 @@ func (c *Client) newRequestContext(parentContext context.Context) context.Contex
}, },
) )
} }
func hasScheme(url string) bool {
parts := strings.Split(url, "://")
return len(parts) > 1
}
func ensureURLHasScheme(url string) string {
const defaultScheme = "http"
if !hasScheme(url) {
return fmt.Sprintf("%s://%s", defaultScheme, url)
}
return url
}

View File

@ -51,7 +51,7 @@ func importProgress(source string) (*progress.Stage, *progress.Manual) {
// nolint:funlen // nolint:funlen
func (c *Client) Import(ctx context.Context, cfg ImportConfig) error { func (c *Client) Import(ctx context.Context, cfg ImportConfig) error {
stage, prog := importProgress(c.config.Hostname) stage, prog := importProgress(c.config.BasePath)
ctxWithTimeout, cancel := context.WithTimeout(ctx, time.Second*30) ctxWithTimeout, cancel := context.WithTimeout(ctx, time.Second*30)
defer cancel() defer cancel()