mirror of
https://github.com/opentffoundation/opentf.git
synced 2025-12-21 10:47:34 -05:00
Previously we were using a third-party library, but that doesn't have any
support for passing context.Context through its API and so isn't suitable
for our goals of adding OpenTelemetry tracing for all outgoing network
requests.
We now have our own fork that is updated to use context.Context. It also
has a slightly reduced scope no longer including various details that
are tightly-coupled to our cliconfig mechanism and so better placed in the
main OpenTofu codebase so we can evolve it in future without making
lockstep library releases.
The "registry-address" library also uses svchost and uses some of its types
in its public API, so this also incorporates v2 of that library that is
updated to use our own svchost module.
Unfortunately this commit is a mix of mechanical updates to the new
libraries and some new code dealing with the functionality that is removed
in our fork of svchost. The new code is primarily in the "svcauthconfig"
package, which is similar in purpose "ociauthconfig" but for OpenTofu's
own auth mechanism instead of the OCI Distribution protocol's auth
mechanism.
This includes some additional plumbing of context.Context where it was
possible to do so without broad changes to files that would not otherwise
have been included in this commit, but there are a few leftover spots that
are context.TODO() which we'll address separately in later commits.
This removes the temporary workaround from d079da6e9e, since we are now
able to plumb the OpenTelemetry span tree all the way to the service
discovery requests.
Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
58 lines
2.1 KiB
Go
58 lines
2.1 KiB
Go
// Copyright (c) The OpenTofu Authors
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
// Copyright (c) 2023 HashiCorp, Inc.
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
package svcauthconfig
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/opentofu/svchost/svcauth"
|
|
)
|
|
|
|
// The symbols in this file are intended for use in tests only, and are
|
|
// not appropriate for use in normal code.
|
|
|
|
// HostCredentialsBearerToken is a testing helper implementing a little
|
|
// abstraction inversion to get the bearer token used by a credentials source
|
|
// even though the [svcauth.HostCredentials] API is designed to be generic over
|
|
// what kind of credentials it encloses.
|
|
//
|
|
// This only works for a [svcauth.HostCredentials] implementation whose
|
|
// behavior is to add an Authorization header field to the request using
|
|
// the "Bearer" scheme, in which case it returns whatever content appears
|
|
// after that scheme. HostCredentials implementations that don't match that
|
|
// pattern must be tested in a different way.
|
|
//
|
|
// This helper should not be used in non-test code. The svcauth API
|
|
// intentionally encapsulates the details of how credentials are applied to
|
|
// a request so that it can potentially be extended in future to support
|
|
// other authentication schemes such as HTTP Basic authentication.
|
|
func HostCredentialsBearerToken(t testing.TB, creds svcauth.HostCredentials) string {
|
|
t.Helper()
|
|
|
|
fakeReq, err := http.NewRequest("GET", "http://example.com/", nil)
|
|
if err != nil {
|
|
t.Fatalf("failed to create fake request: %s", err) // should be impossible, since we control all the inputs
|
|
}
|
|
creds.PrepareRequest(fakeReq)
|
|
|
|
header := fakeReq.Header
|
|
authz := header.Values("authorization")
|
|
if len(authz) == 0 {
|
|
t.Fatal("the svcauth.HostCredentials implementation did not add an Authorization header field")
|
|
}
|
|
if len(authz) > 1 {
|
|
t.Fatalf("the svcauth.HostCredentials implementation added %d Authorization header fields; want exactly one", len(authz))
|
|
}
|
|
|
|
raw := strings.TrimPrefix(strings.ToLower(authz[0]), "bearer ")
|
|
if len(raw) == len(authz[0]) {
|
|
t.Fatal("the svchost.HostCredentials implemented added an Authorization header that does not use the Bearer scheme")
|
|
}
|
|
return strings.TrimSpace(raw)
|
|
}
|