Skip to content

client: add WithUserAgent() option#45677

Merged
AkihiroSuda merged 1 commit intomoby:masterfrom
thaJeztah:client_useragent
Jun 10, 2023
Merged

client: add WithUserAgent() option#45677
AkihiroSuda merged 1 commit intomoby:masterfrom
thaJeztah:client_useragent

Conversation

@thaJeztah
Copy link
Copy Markdown
Member

When constructing the client, and setting the User-Agent, care must be taken to apply the header in the right location, as custom headers can be set in the CLI configuration, and merging these custom headers should not override the User-Agent header.

This patch adds a dedicated WithUserAgent() option, which stores the user-agent separate from other headers, centralizing the merging of other headers, so that other parts of the (CLI) code don't have to be concerned with merging them in the right order.

- What I did

- How I did it

- How to verify it

- Description for the changelog

- A picture of a cute animal (not mandatory but encouraged)

wholesome-cute-duck-pics-5da04713f2775__700

@thaJeztah thaJeztah added area/api API area/cli Client status/2-code-review kind/enhancement Enhancements are not bugs or new features but can improve usability or performance. labels Jun 1, 2023
@thaJeztah thaJeztah added this to the 25.0.0 milestone Jun 1, 2023
@thaJeztah
Copy link
Copy Markdown
Member Author

/cc @ndeloof

@ndeloof
Copy link
Copy Markdown
Contributor

ndeloof commented Jun 2, 2023

nice. Reading moby codebase I was convinced preventing user agent override was intentional

Comment thread client/request.go Outdated
}

if cli.userAgent != "" {
req.Header.Set("User-Agent", cli.userAgent)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think cli.userAgent should be *string and *cli.userAgent == "" should just unset the User-Agent header.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about that, but couldn't come-up with a case where we would first "set" and then "clear" the User-Agent; did you have a specific situation in mind?

Other things I was contemplating;

// NOTE: This is not intended to reflect the actual Go version being used.
// It was changed at the time of Go 1.1 release because the former User-Agent
// had ended up blocked by some intrusion detection systems.
// See https://codereview.appspot.com/7532043.
const defaultUserAgent = "Go-http-client/1.1"

@thaJeztah
Copy link
Copy Markdown
Member Author

Reading moby codebase I was convinced preventing user agent override was intentional

@ndeloof I "it depends". The code you linked to looks to be to prevent a user-configured custom header from overriding the user-agent;
https://github.com/docker/cli/blob/20923dfbc7fa7e338b6a2ee9df0dd4b713882186/cli/command/cli.go#L270-L274

But that code may also have been working around limitations of the code, or a working around a bug;

For the Docker CLI we probably still want to prevent the CLI config-file from overriding the user-agent, but the "library code" from both this (moby/moby) and docker/cli repositories should give the consumer of the library code full control over this. After all, you don't want to develop "my-awesome_cli" to be advertising itself as Docker-Client/unknown-version (linux) (which has no meaning at all, other than "we happened to be using some of docker/cli's library code). It's similar to Docker Engine now using the containerd client code (see #45669 and #45670)

For Docker Compose; we should discuss what user-agent we want to set (which could be a combination of both when run as a plugin).

Either way, this PR is just a building-block in that; this PR should make it easier (and non-ambiguous) to control the User-Agent without having to mess with setting/unsetting/overriding headers in other places.

But more work is still needed in the docker/cli code (the "docker context" code is overly complicated, so may still need to do some refactoring).

@thaJeztah
Copy link
Copy Markdown
Member Author

@rumpl @AkihiroSuda ptal 🤗

@thaJeztah
Copy link
Copy Markdown
Member Author

Oh! Nevermind; I still had to update this one with @AkihiroSuda's suggestion.. ignore me

When constructing the client, and setting the User-Agent, care must be
taken to apply the header in the right location, as custom headers can
be set in the CLI configuration, and merging these custom headers should
not override the User-Agent header.

This patch adds a dedicated `WithUserAgent()` option, which stores the
user-agent separate from other headers, centralizing the merging of
other headers, so that other parts of the (CLI) code don't have to be
concerned with merging them in the right order.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
@thaJeztah
Copy link
Copy Markdown
Member Author

Ah, LOL, mystery solved; looks like I was working on this, but had to clean up things

* 692fffda80 (HEAD -> client_useragent) WIP WIP WIP (7 days ago by <Sebastiaan van Stijn>)
* d8fbc2c1fe (origin/client_useragent) client: add WithUserAgent() option (7 days ago by <Sebastiaan van Stijn>)

Either way, this should be ready for review now 👍 PTAL

@AkihiroSuda AkihiroSuda merged commit 78cf115 into moby:master Jun 10, 2023
@thaJeztah thaJeztah deleted the client_useragent branch April 28, 2025 15:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/api API area/cli Client kind/enhancement Enhancements are not bugs or new features but can improve usability or performance. status/2-code-review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants