Skip to content

Commit

Permalink
fix: don't try to use authenticators if none are configured
Browse files Browse the repository at this point in the history
  • Loading branch information
alecthomas committed Oct 11, 2023
1 parent a9ea47b commit 3f5e846
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
19 changes: 11 additions & 8 deletions authn/authn.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ import (
func GetAuthenticationHeaders(ctx context.Context, endpoint *url.URL, authenticators map[string]string) (http.Header, error) {
logger := log.FromContext(ctx).Scope(endpoint.Hostname())

// Next, try the authenticator.
logger.Debugf("Trying authenticator")
authenticator, ok := authenticators[endpoint.Hostname()]
if !ok {
logger.Tracef("No authenticator found for %s in %s", endpoint, authenticators)
return nil, nil
}

endpoint = &url.URL{
Scheme: endpoint.Scheme,
Host: endpoint.Host,
Expand Down Expand Up @@ -60,14 +68,6 @@ func GetAuthenticationHeaders(ctx context.Context, endpoint *url.URL, authentica
}
}

// Next, try the authenticator.
logger.Debugf("Trying authenticator")
authenticator, ok := authenticators[endpoint.Hostname()]
if !ok {
logger.Tracef("No authenticator found in %s", authenticators)
return nil, nil
}

cmd := exec.Command(ctx, log.Error, ".", authenticator, endpoint.String())
out := &strings.Builder{}
cmd.Stdout = out
Expand Down Expand Up @@ -152,6 +152,9 @@ func checkAuth(ctx context.Context, logger *log.Logger, endpoint *url.URL, creds

// Transport returns a transport that will authenticate requests to the given endpoints.
func Transport(next http.RoundTripper, authenticators map[string]string) http.RoundTripper {
if len(authenticators) == 0 {
return next
}
return &authnTransport{
authenticators: authenticators,
credentials: map[string]http.Header{},
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.21
require (
connectrpc.com/connect v1.11.1
github.com/BurntSushi/toml v1.3.2
github.com/alecthomas/kong v0.8.0
github.com/alecthomas/kong v0.8.1
github.com/alecthomas/kong-toml v0.0.0-20230922031405-31cfb1264a3b
github.com/go-logr/logr v1.2.4
github.com/golang/protobuf v1.5.3
Expand Down
2 changes: 2 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3f5e846

Please sign in to comment.