Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image building does not validate that image registry host matches publish registry URL #29281

Open
liyuan-rey opened this issue Jan 6, 2022 · 7 comments
Labels
type: bug A general bug
Milestone

Comments

@liyuan-rey
Copy link

liyuan-rey commented Jan 6, 2022

hello,

Envronment:
System: Windows 10 Home x64 21H2
JDK: 11.0.13
Spring Boot: 2.6.2
Docker Desktop 4.3.2 (WSL 2 based)

We built a private docker repositories with Sonatype Nexus at http://172.2.3.5:9000/.

Try build image for a simple Spring REST WebApp.

build.gradle

plugins {
    id 'java'
    id 'org.springframework.boot' version '2.6.2'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
}

...

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
}

bootBuildImage {
    imageName = "mycompany/mygroup/myproject:1.0.1"
    publish = true
    docker {
        publishRegistry {
            url = "http://172.2.3.5:9000/"
            username = "user"
            password = "pass"
        }
    }
}

Run .\gradlew.bat bootBuildImage, got outputs like:

...
> Task :bootBuildImage
- Building image 'docker.io/mycompany/mygroup/myproject:1.0.1'

 > Pulling builder image 'docker.io/paketobuildpacks/builder:base' ......
...
 > Pulling run image 'docker.io/paketobuildpacks/run:base-cnb' ......
...
 > Running creator
...

- Successfully built image 'docker.io/mycompany/mygroup/myproject:1.0.1'

- > Pushing image 'docker.io/mycompany/mygroup/myproject:1.0.1' .......

...

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':bootBuildImage'.
> Error response received when pushing image: unauthorized: incorrect username or password

...

Notice that the red lines always output 'docker.io/' even if 'docker.publishRegistry.url' is specified, so I guess the task might try to push the image to ‘docker.io’ instead of '172.2.3.5:9000', and it fails.

Please let me know if there is any misunderstanding, thank you.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 6, 2022
@liyuan-rey liyuan-rey changed the title task 'bootBuildImage' (of spring-boot-gradle-plugin) push jar to docker.io even when specified private repo with docker.publishRegistry.url task 'bootBuildImage' (of spring-boot-gradle-plugin) push image to docker.io even when specified private repo with docker.publishRegistry.url Jan 6, 2022
@wilkinsona
Copy link
Member

You haven't provided a domain when specifying the image name so it defaults to docker.io. If you set imageName to 172.2.3.5:9000/mycompany/mygroup/myproject:1.0.1 it should work.

I wonder if we can make this easier. It'd be nice to not have to duplicate the domain in the image name and in the publish registry URL.

@wilkinsona wilkinsona added the for: team-attention An issue we'd like other members of the team to review label Jan 6, 2022
@liyuan-rey
Copy link
Author

@wilkinsona
Yes, it works when I add host:port to imageName.
Thank you for your help! 😄

I'm not sure if there are additional jobs to track, so I leave this issue as "open". It can be closed if necessary.

@scottfrederick
Copy link
Contributor

It'd be nice to not have to duplicate the domain in the image name and in the publish registry URL.

The docker.publishRegistry.url must contain the full URL to the registry, including the scheme part, whereas the image name just contains the registry host. I'm not sure how we'd keep from duplicating the registry host without assuming things about the rest of the URL.

Another option would be to do more validation to ensure that the registry part of the built image and the host part of the registry URL match before attempting to push an image. If additional tags are applied to the built image, and publishing is requested in the build configuration, it is also required that all image tags have the same registry part. We should probably validate that this is the case before publishing also.

@scottfrederick
Copy link
Contributor

scottfrederick commented Jul 20, 2022

The documentation should also clarify that the value of docker.publishRegistry.url has no effect on where a built image gets pushed. It is optional, which is currently mentioned in the documentation.

#31820 has been created to cover the documentation change.

@scottfrederick scottfrederick added type: bug A general bug and removed for: team-attention An issue we'd like other members of the team to review status: waiting-for-triage An issue we've not yet triaged labels Jul 20, 2022
@scottfrederick scottfrederick added this to the 2.6.x milestone Jul 20, 2022
@scottfrederick scottfrederick changed the title task 'bootBuildImage' (of spring-boot-gradle-plugin) push image to docker.io even when specified private repo with docker.publishRegistry.url Image building does not validate that image registry host matches publish registry URL Jul 20, 2022
@wilkinsona wilkinsona modified the milestones: 2.6.x, 2.7.x Nov 24, 2022
@philwebb philwebb modified the milestones: 2.7.x, 3.1.x Nov 8, 2023
@wilkinsona wilkinsona modified the milestones: 3.1.x, 3.2.x May 20, 2024
@mhalbritter
Copy link
Contributor

mhalbritter commented Oct 28, 2024

The docker.publishRegistry.url must contain the full URL to the registry, including the scheme part, whereas the image name just contains the registry host. I'm not sure how we'd keep from duplicating the registry host without assuming things about the rest of the URL.

If I read the code correctly, docker.publishRegistry.url sets org.springframework.boot.buildpack.platform.docker.configuration.DockerRegistryUserAuthentication#url, which is sent to the Docker Engine in the serveraddress field.

Looking at the docs, it says:

The serveraddress is a domain/IP without a protocol.

I find the docker.publishRegistry.url confusing: When do I need to set it? It appears to be working even when I don't set it, the credentials get sent to the registry.

When setting this to some bogus value, the image is pushed to the registry with the credentials from username and password nonetheless. I first thought this setting exist to select to which registry the credentials are sent, but it doesn't look like it.

@scottfrederick
Copy link
Contributor

When setting this to some bogus value, the image is pushed to the registry with the credentials from username and password nonetheless.

What registry did you test with? It's been a while since I've looked at this, but IIRC the value is inspected by the registry server, and it's possible that different registries handle it differently. If that's the case, we might want to clarify that in the documentation.

@mhalbritter
Copy link
Contributor

mhalbritter commented Oct 28, 2024

Hi Scott! I've tested with https://hub.docker.com/_/registry.

I've thought the Docker Engine inspect that header and then either pass the credentials to the registry or it doesn't if it doesn't match. But i haven't checked the source code.

@philwebb philwebb modified the milestones: 3.2.x, 3.3.x Nov 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

6 participants