dockerversion: DockerUserAgent(): allow custom versions to be passed

Allow additional metadata to be passed as part of the generated User-Agent.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2023-06-01 14:09:50 +02:00
parent 9a1f2e6d7c
commit a6da1480b5
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
2 changed files with 16 additions and 2 deletions

View file

@ -17,8 +17,8 @@ type UAStringKey struct{}
// In accordance with RFC 7231 (5.5.3) is of the form: // In accordance with RFC 7231 (5.5.3) is of the form:
// //
// [docker client's UA] UpstreamClient([upstream client's UA]) // [docker client's UA] UpstreamClient([upstream client's UA])
func DockerUserAgent(ctx context.Context) string { func DockerUserAgent(ctx context.Context, extraVersions ...useragent.VersionInfo) string {
ua := getDaemonUserAgent() ua := useragent.AppendVersions(getDaemonUserAgent(), extraVersions...)
if upstreamUA := getUpstreamUserAgent(ctx); upstreamUA != "" { if upstreamUA := getUpstreamUserAgent(ctx); upstreamUA != "" {
ua += " " + upstreamUA ua += " " + upstreamUA
} }

View file

@ -4,6 +4,7 @@ import (
"context" "context"
"testing" "testing"
"github.com/docker/docker/pkg/useragent"
"gotest.tools/v3/assert" "gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp" is "gotest.tools/v3/assert/cmp"
) )
@ -15,10 +16,23 @@ func TestDockerUserAgent(t *testing.T) {
assert.Check(t, is.Equal(ua, expected)) assert.Check(t, is.Equal(ua, expected))
}) })
t.Run("daemon user-agent custom metadata", func(t *testing.T) {
ua := DockerUserAgent(context.TODO(), useragent.VersionInfo{Name: "hello", Version: "world"}, useragent.VersionInfo{Name: "foo", Version: "bar"})
expected := getDaemonUserAgent() + ` hello/world foo/bar`
assert.Check(t, is.Equal(ua, expected))
})
t.Run("daemon user-agent with upstream", func(t *testing.T) { t.Run("daemon user-agent with upstream", func(t *testing.T) {
ctx := context.WithValue(context.TODO(), UAStringKey{}, "Magic-Client/1.2.3 (linux)") ctx := context.WithValue(context.TODO(), UAStringKey{}, "Magic-Client/1.2.3 (linux)")
ua := DockerUserAgent(ctx) ua := DockerUserAgent(ctx)
expected := getDaemonUserAgent() + ` UpstreamClient(Magic-Client/1.2.3 \(linux\))` expected := getDaemonUserAgent() + ` UpstreamClient(Magic-Client/1.2.3 \(linux\))`
assert.Check(t, is.Equal(ua, expected)) assert.Check(t, is.Equal(ua, expected))
}) })
t.Run("daemon user-agent with upstream and custom metadata", func(t *testing.T) {
ctx := context.WithValue(context.TODO(), UAStringKey{}, "Magic-Client/1.2.3 (linux)")
ua := DockerUserAgent(ctx, useragent.VersionInfo{Name: "hello", Version: "world"}, useragent.VersionInfo{Name: "foo", Version: "bar"})
expected := getDaemonUserAgent() + ` hello/world foo/bar UpstreamClient(Magic-Client/1.2.3 \(linux\))`
assert.Check(t, is.Equal(ua, expected))
})
} }