From a6da1480b5197ad0d2dc7194b117d8dfe615df44 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Thu, 1 Jun 2023 14:09:50 +0200 Subject: [PATCH] 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 --- dockerversion/useragent.go | 4 ++-- dockerversion/useragent_test.go | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/dockerversion/useragent.go b/dockerversion/useragent.go index 232a28acc9..7013a4543d 100644 --- a/dockerversion/useragent.go +++ b/dockerversion/useragent.go @@ -17,8 +17,8 @@ type UAStringKey struct{} // In accordance with RFC 7231 (5.5.3) is of the form: // // [docker client's UA] UpstreamClient([upstream client's UA]) -func DockerUserAgent(ctx context.Context) string { - ua := getDaemonUserAgent() +func DockerUserAgent(ctx context.Context, extraVersions ...useragent.VersionInfo) string { + ua := useragent.AppendVersions(getDaemonUserAgent(), extraVersions...) if upstreamUA := getUpstreamUserAgent(ctx); upstreamUA != "" { ua += " " + upstreamUA } diff --git a/dockerversion/useragent_test.go b/dockerversion/useragent_test.go index 86d5004fb7..b9fe3d2dfa 100644 --- a/dockerversion/useragent_test.go +++ b/dockerversion/useragent_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/docker/docker/pkg/useragent" "gotest.tools/v3/assert" is "gotest.tools/v3/assert/cmp" ) @@ -15,10 +16,23 @@ func TestDockerUserAgent(t *testing.T) { 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) { ctx := context.WithValue(context.TODO(), UAStringKey{}, "Magic-Client/1.2.3 (linux)") ua := DockerUserAgent(ctx) expected := getDaemonUserAgent() + ` UpstreamClient(Magic-Client/1.2.3 \(linux\))` 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)) + }) }