feature!: support for setting HTTP User-Agent header (#157)
* feature!: support for setting HTTP User-Agent header * fix lint * fix e2e * refactor: move http.go to internal/util/useragent package and rename functions to Get and Set * Move packages and use attest version
This commit is contained in:
@@ -7,14 +7,19 @@ import (
|
||||
_ "embed"
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/docker/attest/attestation"
|
||||
"github.com/docker/attest/internal/useragent"
|
||||
"github.com/docker/attest/signerverifier"
|
||||
"github.com/docker/attest/tlog"
|
||||
"github.com/google/go-containerregistry/pkg/registry"
|
||||
"github.com/secure-systems-lab/go-securesystemslib/dsse"
|
||||
)
|
||||
|
||||
@@ -81,6 +86,19 @@ func Setup(t *testing.T) (context.Context, dsse.SignerVerifier) {
|
||||
return ctx, signer
|
||||
}
|
||||
|
||||
func NewLocalRegistry(ctx context.Context, options ...registry.Option) *httptest.Server {
|
||||
regHandler := registry.New(options...)
|
||||
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// Check the user agent
|
||||
ua := r.Header.Get("User-Agent")
|
||||
userAgent := useragent.Get(ctx)
|
||||
if !strings.HasPrefix(ua, userAgent) {
|
||||
http.Error(w, fmt.Sprintf("expected user agent to contain %q, got %q", userAgent, ua), http.StatusForbidden)
|
||||
}
|
||||
regHandler.ServeHTTP(w, r)
|
||||
}))
|
||||
}
|
||||
|
||||
func publicKeyToPEM(pubKey crypto.PublicKey) (string, error) {
|
||||
derBytes, err := x509.MarshalPKIXPublicKey(pubKey)
|
||||
if err != nil {
|
||||
|
||||
31
internal/useragent/useragent.go
Normal file
31
internal/useragent/useragent.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package useragent
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/docker/attest/internal/version"
|
||||
)
|
||||
|
||||
type userAgentKeyType string
|
||||
|
||||
const (
|
||||
userAgentKey userAgentKeyType = "attest-user-agent"
|
||||
defaultUserAgent string = "attest/unknown (docker)"
|
||||
)
|
||||
|
||||
func Set(ctx context.Context, userAgent string) context.Context {
|
||||
return context.WithValue(ctx, userAgentKey, userAgent)
|
||||
}
|
||||
|
||||
// Get retrieves the HTTP user agent from the context.
|
||||
func Get(ctx context.Context) string {
|
||||
if ua, ok := ctx.Value(userAgentKey).(string); ok {
|
||||
return ua
|
||||
}
|
||||
version, err := version.Get()
|
||||
if err != nil || version == nil {
|
||||
return defaultUserAgent
|
||||
}
|
||||
|
||||
return "attest/" + version.String() + " (docker)"
|
||||
}
|
||||
19
internal/useragent/useragent_test.go
Normal file
19
internal/useragent/useragent_test.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package useragent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// test the user agent setting and getting.
|
||||
func TestSetUserAgent(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
if Get(ctx) != defaultUserAgent {
|
||||
t.Errorf("expected user agent to be '%s', got %q", defaultUserAgent, Get(ctx))
|
||||
}
|
||||
|
||||
ctx = Set(ctx, "test-agent")
|
||||
if Get(ctx) != "test-agent" {
|
||||
t.Errorf("expected user agent to be 'test-agent', got %q", Get(ctx))
|
||||
}
|
||||
}
|
||||
39
internal/version/version.go
Normal file
39
internal/version/version.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package version
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime/debug"
|
||||
|
||||
"github.com/Masterminds/semver/v3"
|
||||
)
|
||||
|
||||
const ThisModulePath = "github.com/docker/attest"
|
||||
|
||||
// Get returns the version of the attest module.
|
||||
// this can return nil if the version can't be determined (without an error).
|
||||
func Get() (*semver.Version, error) {
|
||||
var attestMod *debug.Module
|
||||
bi, ok := debug.ReadBuildInfo()
|
||||
if !ok {
|
||||
return nil, nil
|
||||
}
|
||||
if bi.Main.Path == ThisModulePath {
|
||||
attestMod = &bi.Main
|
||||
} else {
|
||||
for _, dep := range bi.Deps {
|
||||
if dep.Path == ThisModulePath {
|
||||
attestMod = dep
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if attestMod == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
attestVersion, err := semver.NewVersion(attestMod.Version)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse version %s: %w", attestMod.Version, err)
|
||||
}
|
||||
return attestVersion, nil
|
||||
}
|
||||
Reference in New Issue
Block a user