From 29c8fc383d387bbb05f8448d46910ceb05bf3a87 Mon Sep 17 00:00:00 2001 From: Pascal Zimmermann Date: Tue, 24 Nov 2026 16:26:38 +0100 Subject: [PATCH 1/2] test: Cover the docker credential stripping in pushManifestV3 Follow-up to #1410: - Add a DefaultApplicationsTest case for pushManifestV3 that checks the apply_manifest body has no docker credentials while the package request still gets them. - Move the stripDockerCredentials test to DefaultApplicationsTest and cover several apps, untouched apps and kept fields. - Only skip stripping when both username and password are missing. - Delete the temp manifest files written by ApplicationManifestUtilsV3Test. --- .../applications/DefaultApplications.java | 3 +- .../ApplicationManifestUtilsV3Test.java | 40 ++---- .../applications/DefaultApplicationsTest.java | 125 ++++++++++++++++++ 3 files changed, 135 insertions(+), 33 deletions(-) diff --git a/cloudfoundry-operations/src/main/java/org/cloudfoundry/operations/applications/DefaultApplications.java b/cloudfoundry-operations/src/main/java/org/cloudfoundry/operations/applications/DefaultApplications.java index 8aab3b7b5b..4b4eef6ddf 100644 --- a/cloudfoundry-operations/src/main/java/org/cloudfoundry/operations/applications/DefaultApplications.java +++ b/cloudfoundry-operations/src/main/java/org/cloudfoundry/operations/applications/DefaultApplications.java @@ -1236,7 +1236,8 @@ static ManifestV3 stripDockerCredentials(ManifestV3 manifest) { .map( app -> { if (app.getDocker() == null - || app.getDocker().getPassword() == null) { + || (app.getDocker().getUsername() == null + && app.getDocker().getPassword() == null)) { return app; } Docker docker = diff --git a/cloudfoundry-operations/src/test/java/org/cloudfoundry/operations/applications/ApplicationManifestUtilsV3Test.java b/cloudfoundry-operations/src/test/java/org/cloudfoundry/operations/applications/ApplicationManifestUtilsV3Test.java index f1c2681841..96483de51b 100644 --- a/cloudfoundry-operations/src/test/java/org/cloudfoundry/operations/applications/ApplicationManifestUtilsV3Test.java +++ b/cloudfoundry-operations/src/test/java/org/cloudfoundry/operations/applications/ApplicationManifestUtilsV3Test.java @@ -1,7 +1,6 @@ package org.cloudfoundry.operations.applications; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; import java.io.IOException; import java.nio.file.Files; @@ -168,38 +167,15 @@ void testDockerPasswordRoundTrips() throws IOException { assertSerializeDeserialize(manifest); } - @Test - void stripDockerCredentials() { - ManifestV3 manifest = - ManifestV3.builder() - .application( - ManifestV3Application.builder() - .name("test-app") - .docker( - Docker.builder() - .image("my-registry/my-image:latest") - .username("user") - .password("secret") - .build()) - .build()) - .build(); - - Docker docker = - DefaultApplications.stripDockerCredentials(manifest) - .getApplications() - .get(0) - .getDocker(); - - assertEquals("my-registry/my-image:latest", docker.getImage()); - assertNull(docker.getUsername()); - assertNull(docker.getPassword()); - } - private void assertSerializeDeserialize(ManifestV3 manifest) throws IOException { Path file = Files.createTempFile("test-manifest-", ".yml"); - ApplicationManifestUtilsV3.write(file, manifest); - ManifestV3 read = ApplicationManifestUtilsV3.read(file); - - assertEquals(manifest, read); + try { + ApplicationManifestUtilsV3.write(file, manifest); + ManifestV3 read = ApplicationManifestUtilsV3.read(file); + + assertEquals(manifest, read); + } finally { + Files.deleteIfExists(file); + } } } diff --git a/cloudfoundry-operations/src/test/java/org/cloudfoundry/operations/applications/DefaultApplicationsTest.java b/cloudfoundry-operations/src/test/java/org/cloudfoundry/operations/applications/DefaultApplicationsTest.java index 3fbffef118..93647467af 100644 --- a/cloudfoundry-operations/src/test/java/org/cloudfoundry/operations/applications/DefaultApplicationsTest.java +++ b/cloudfoundry-operations/src/test/java/org/cloudfoundry/operations/applications/DefaultApplicationsTest.java @@ -20,13 +20,16 @@ import static org.cloudfoundry.client.v3.LifecycleType.BUILDPACK; import static org.cloudfoundry.client.v3.LifecycleType.DOCKER; import static org.cloudfoundry.operations.TestObjects.fill; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.RETURNS_SMART_NULLS; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Path; +import java.nio.file.Paths; import java.time.Duration; import java.util.Arrays; import java.util.Base64; @@ -135,6 +138,11 @@ import org.cloudfoundry.client.v3.applications.ListApplicationsResponse; import org.cloudfoundry.client.v3.applications.UpdateApplicationFeatureRequest; import org.cloudfoundry.client.v3.applications.UpdateApplicationFeatureResponse; +import org.cloudfoundry.client.v3.jobs.JobState; +import org.cloudfoundry.client.v3.packages.CreatePackageRequest; +import org.cloudfoundry.client.v3.packages.Packages; +import org.cloudfoundry.client.v3.spaces.ApplyManifestRequest; +import org.cloudfoundry.client.v3.spaces.ApplyManifestResponse; import org.cloudfoundry.client.v3.tasks.CancelTaskRequest; import org.cloudfoundry.client.v3.tasks.CancelTaskResponse; import org.cloudfoundry.client.v3.tasks.CreateTaskRequest; @@ -159,6 +167,7 @@ import org.cloudfoundry.util.FluentMap; import org.cloudfoundry.util.ResourceMatchingUtils; import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; import org.springframework.core.io.ClassPathResource; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @@ -3320,6 +3329,122 @@ void pushUploadFails() throws IOException { .verify(Duration.ofSeconds(5)); } + @Test + void pushManifestV3StripsDockerCredentialsOnlyFromApplyManifest() { + Packages packages = mock(Packages.class, RETURNS_SMART_NULLS); + when(this.cloudFoundryClient.packages()).thenReturn(packages); + when(this.cloudFoundryClient.spacesV3().applyManifest(any(ApplyManifestRequest.class))) + .thenReturn( + Mono.just(ApplyManifestResponse.builder().jobId("test-job-id").build())); + when(this.cloudFoundryClient + .jobsV3() + .get( + org.cloudfoundry.client.v3.jobs.GetJobRequest.builder() + .jobId("test-job-id") + .build())) + .thenReturn( + Mono.just( + fill(org.cloudfoundry.client.v3.jobs.GetJobResponse.builder()) + .state(JobState.COMPLETE) + .build())); + requestApplicationsV3( + this.cloudFoundryClient, "test-app", TEST_SPACE_ID, "test-application-id"); + // Stop the push after the package request, the rest of the flow isn't relevant here. + when(packages.create(any(CreatePackageRequest.class))) + .thenReturn(Mono.error(new IllegalStateException("stop after create package"))); + + this.applications + .pushManifestV3( + PushManifestV3Request.builder() + .manifest( + ManifestV3.builder() + .application( + ManifestV3Application.builder() + .name("test-app") + .docker( + Docker.builder() + .image("test-image") + .username( + "test-username") + .password( + "test-password") + .build()) + .build()) + .build()) + .build()) + .as(StepVerifier::create) + .expectErrorMessage("stop after create package") + .verify(Duration.ofSeconds(5)); + + ArgumentCaptor applyManifest = + ArgumentCaptor.forClass(ApplyManifestRequest.class); + verify(this.cloudFoundryClient.spacesV3()).applyManifest(applyManifest.capture()); + String appliedManifest = + new String(applyManifest.getValue().getManifest(), StandardCharsets.UTF_8); + assertThat(appliedManifest) + .contains("test-image") + .doesNotContain("test-username") + .doesNotContain("test-password"); + + ArgumentCaptor createPackage = + ArgumentCaptor.forClass(CreatePackageRequest.class); + verify(packages).create(createPackage.capture()); + org.cloudfoundry.client.v3.packages.DockerData dockerData = + (org.cloudfoundry.client.v3.packages.DockerData) createPackage.getValue().getData(); + assertThat(dockerData.getImage()).isEqualTo("test-image"); + assertThat(dockerData.getUsername()).isEqualTo("test-username"); + assertThat(dockerData.getPassword()).isEqualTo("test-password"); + } + + @Test + void stripDockerCredentials() { + ManifestV3Application privateDocker = + ManifestV3Application.builder() + .name("private-docker-app") + .docker( + Docker.builder() + .image("my-registry/my-image:latest") + .username("user") + .password("secret") + .build()) + .environmentVariable("TEST_KEY", "test-value") + .instances(2) + .build(); + ManifestV3Application publicDocker = + ManifestV3Application.builder() + .name("public-docker-app") + .docker(Docker.builder().image("public-image").build()) + .build(); + ManifestV3Application buildpackApp = + ManifestV3Application.builder() + .name("buildpack-app") + .path(Paths.get("/tmp")) + .build(); + ManifestV3 manifest = + ManifestV3.builder() + .application(privateDocker) + .application(publicDocker) + .application(buildpackApp) + .build(); + + ManifestV3 stripped = DefaultApplications.stripDockerCredentials(manifest); + + assertThat(stripped.getApplications()).hasSize(3); + ManifestV3Application strippedPrivate = stripped.getApplications().get(0); + assertThat(strippedPrivate.getName()).isEqualTo("private-docker-app"); + assertThat(strippedPrivate.getDocker().getImage()).isEqualTo("my-registry/my-image:latest"); + assertThat(strippedPrivate.getDocker().getUsername()).isNull(); + assertThat(strippedPrivate.getDocker().getPassword()).isNull(); + assertThat(strippedPrivate.getEnvironmentVariables()) + .containsEntry("TEST_KEY", "test-value"); + assertThat(strippedPrivate.getInstances()).isEqualTo(2); + assertThat(stripped.getApplications().get(1)).isSameAs(publicDocker); + assertThat(stripped.getApplications().get(2)).isSameAs(buildpackApp); + + // createPackage still reads the credentials from the original manifest + assertThat(manifest.getApplications().get(0).getDocker().getPassword()).isEqualTo("secret"); + } + @Test void rename() { requestApplications( From f6d72f132fdee1929515773d156159cad0ff37e0 Mon Sep 17 00:00:00 2001 From: Pascal Zimmermann Date: Tue, 24 Nov 2026 18:34:07 +0100 Subject: [PATCH 2/2] test: Drop a no-op assertion and sort the new tests - Remove the stripDockerCredentials assertion on the original manifest. ManifestV3 is immutable, so it could never fail, and the push test already checks that createPackage gets the credentials. - Move the two new tests to their alphabetical place in DefaultApplicationsTest. --- .../applications/DefaultApplicationsTest.java | 229 +++++++++--------- 1 file changed, 113 insertions(+), 116 deletions(-) diff --git a/cloudfoundry-operations/src/test/java/org/cloudfoundry/operations/applications/DefaultApplicationsTest.java b/cloudfoundry-operations/src/test/java/org/cloudfoundry/operations/applications/DefaultApplicationsTest.java index 93647467af..83500e8bcf 100644 --- a/cloudfoundry-operations/src/test/java/org/cloudfoundry/operations/applications/DefaultApplicationsTest.java +++ b/cloudfoundry-operations/src/test/java/org/cloudfoundry/operations/applications/DefaultApplicationsTest.java @@ -2045,6 +2045,73 @@ void pushInvalidStack() throws IOException { .verify(Duration.ofSeconds(5)); } + @Test + void pushManifestV3StripsDockerCredentialsOnlyFromApplyManifest() { + Packages packages = mock(Packages.class, RETURNS_SMART_NULLS); + when(this.cloudFoundryClient.packages()).thenReturn(packages); + when(this.cloudFoundryClient.spacesV3().applyManifest(any(ApplyManifestRequest.class))) + .thenReturn( + Mono.just(ApplyManifestResponse.builder().jobId("test-job-id").build())); + when(this.cloudFoundryClient + .jobsV3() + .get( + org.cloudfoundry.client.v3.jobs.GetJobRequest.builder() + .jobId("test-job-id") + .build())) + .thenReturn( + Mono.just( + fill(org.cloudfoundry.client.v3.jobs.GetJobResponse.builder()) + .state(JobState.COMPLETE) + .build())); + requestApplicationsV3( + this.cloudFoundryClient, "test-app", TEST_SPACE_ID, "test-application-id"); + // Stop the push after the package request, the rest of the flow isn't relevant here. + when(packages.create(any(CreatePackageRequest.class))) + .thenReturn(Mono.error(new IllegalStateException("stop after create package"))); + + this.applications + .pushManifestV3( + PushManifestV3Request.builder() + .manifest( + ManifestV3.builder() + .application( + ManifestV3Application.builder() + .name("test-app") + .docker( + Docker.builder() + .image("test-image") + .username( + "test-username") + .password( + "test-password") + .build()) + .build()) + .build()) + .build()) + .as(StepVerifier::create) + .expectErrorMessage("stop after create package") + .verify(Duration.ofSeconds(5)); + + ArgumentCaptor applyManifest = + ArgumentCaptor.forClass(ApplyManifestRequest.class); + verify(this.cloudFoundryClient.spacesV3()).applyManifest(applyManifest.capture()); + String appliedManifest = + new String(applyManifest.getValue().getManifest(), StandardCharsets.UTF_8); + assertThat(appliedManifest) + .contains("test-image") + .doesNotContain("test-username") + .doesNotContain("test-password"); + + ArgumentCaptor createPackage = + ArgumentCaptor.forClass(CreatePackageRequest.class); + verify(packages).create(createPackage.capture()); + org.cloudfoundry.client.v3.packages.DockerData dockerData = + (org.cloudfoundry.client.v3.packages.DockerData) createPackage.getValue().getData(); + assertThat(dockerData.getImage()).isEqualTo("test-image"); + assertThat(dockerData.getUsername()).isEqualTo("test-username"); + assertThat(dockerData.getPassword()).isEqualTo("test-password"); + } + @Test void pushMultipleRoutes() throws IOException { Path testApplication = new ClassPathResource("test-application.zip").getFile().toPath(); @@ -3329,122 +3396,6 @@ void pushUploadFails() throws IOException { .verify(Duration.ofSeconds(5)); } - @Test - void pushManifestV3StripsDockerCredentialsOnlyFromApplyManifest() { - Packages packages = mock(Packages.class, RETURNS_SMART_NULLS); - when(this.cloudFoundryClient.packages()).thenReturn(packages); - when(this.cloudFoundryClient.spacesV3().applyManifest(any(ApplyManifestRequest.class))) - .thenReturn( - Mono.just(ApplyManifestResponse.builder().jobId("test-job-id").build())); - when(this.cloudFoundryClient - .jobsV3() - .get( - org.cloudfoundry.client.v3.jobs.GetJobRequest.builder() - .jobId("test-job-id") - .build())) - .thenReturn( - Mono.just( - fill(org.cloudfoundry.client.v3.jobs.GetJobResponse.builder()) - .state(JobState.COMPLETE) - .build())); - requestApplicationsV3( - this.cloudFoundryClient, "test-app", TEST_SPACE_ID, "test-application-id"); - // Stop the push after the package request, the rest of the flow isn't relevant here. - when(packages.create(any(CreatePackageRequest.class))) - .thenReturn(Mono.error(new IllegalStateException("stop after create package"))); - - this.applications - .pushManifestV3( - PushManifestV3Request.builder() - .manifest( - ManifestV3.builder() - .application( - ManifestV3Application.builder() - .name("test-app") - .docker( - Docker.builder() - .image("test-image") - .username( - "test-username") - .password( - "test-password") - .build()) - .build()) - .build()) - .build()) - .as(StepVerifier::create) - .expectErrorMessage("stop after create package") - .verify(Duration.ofSeconds(5)); - - ArgumentCaptor applyManifest = - ArgumentCaptor.forClass(ApplyManifestRequest.class); - verify(this.cloudFoundryClient.spacesV3()).applyManifest(applyManifest.capture()); - String appliedManifest = - new String(applyManifest.getValue().getManifest(), StandardCharsets.UTF_8); - assertThat(appliedManifest) - .contains("test-image") - .doesNotContain("test-username") - .doesNotContain("test-password"); - - ArgumentCaptor createPackage = - ArgumentCaptor.forClass(CreatePackageRequest.class); - verify(packages).create(createPackage.capture()); - org.cloudfoundry.client.v3.packages.DockerData dockerData = - (org.cloudfoundry.client.v3.packages.DockerData) createPackage.getValue().getData(); - assertThat(dockerData.getImage()).isEqualTo("test-image"); - assertThat(dockerData.getUsername()).isEqualTo("test-username"); - assertThat(dockerData.getPassword()).isEqualTo("test-password"); - } - - @Test - void stripDockerCredentials() { - ManifestV3Application privateDocker = - ManifestV3Application.builder() - .name("private-docker-app") - .docker( - Docker.builder() - .image("my-registry/my-image:latest") - .username("user") - .password("secret") - .build()) - .environmentVariable("TEST_KEY", "test-value") - .instances(2) - .build(); - ManifestV3Application publicDocker = - ManifestV3Application.builder() - .name("public-docker-app") - .docker(Docker.builder().image("public-image").build()) - .build(); - ManifestV3Application buildpackApp = - ManifestV3Application.builder() - .name("buildpack-app") - .path(Paths.get("/tmp")) - .build(); - ManifestV3 manifest = - ManifestV3.builder() - .application(privateDocker) - .application(publicDocker) - .application(buildpackApp) - .build(); - - ManifestV3 stripped = DefaultApplications.stripDockerCredentials(manifest); - - assertThat(stripped.getApplications()).hasSize(3); - ManifestV3Application strippedPrivate = stripped.getApplications().get(0); - assertThat(strippedPrivate.getName()).isEqualTo("private-docker-app"); - assertThat(strippedPrivate.getDocker().getImage()).isEqualTo("my-registry/my-image:latest"); - assertThat(strippedPrivate.getDocker().getUsername()).isNull(); - assertThat(strippedPrivate.getDocker().getPassword()).isNull(); - assertThat(strippedPrivate.getEnvironmentVariables()) - .containsEntry("TEST_KEY", "test-value"); - assertThat(strippedPrivate.getInstances()).isEqualTo(2); - assertThat(stripped.getApplications().get(1)).isSameAs(publicDocker); - assertThat(stripped.getApplications().get(2)).isSameAs(buildpackApp); - - // createPackage still reads the credentials from the original manifest - assertThat(manifest.getApplications().get(0).getDocker().getPassword()).isEqualTo("secret"); - } - @Test void rename() { requestApplications( @@ -4135,6 +4086,52 @@ void stopStoppedApplication() { .verify(Duration.ofSeconds(5)); } + @Test + void stripDockerCredentials() { + ManifestV3Application privateDocker = + ManifestV3Application.builder() + .name("private-docker-app") + .docker( + Docker.builder() + .image("my-registry/my-image:latest") + .username("user") + .password("secret") + .build()) + .environmentVariable("TEST_KEY", "test-value") + .instances(2) + .build(); + ManifestV3Application publicDocker = + ManifestV3Application.builder() + .name("public-docker-app") + .docker(Docker.builder().image("public-image").build()) + .build(); + ManifestV3Application buildpackApp = + ManifestV3Application.builder() + .name("buildpack-app") + .path(Paths.get("/tmp")) + .build(); + ManifestV3 manifest = + ManifestV3.builder() + .application(privateDocker) + .application(publicDocker) + .application(buildpackApp) + .build(); + + ManifestV3 stripped = DefaultApplications.stripDockerCredentials(manifest); + + assertThat(stripped.getApplications()).hasSize(3); + ManifestV3Application strippedPrivate = stripped.getApplications().get(0); + assertThat(strippedPrivate.getName()).isEqualTo("private-docker-app"); + assertThat(strippedPrivate.getDocker().getImage()).isEqualTo("my-registry/my-image:latest"); + assertThat(strippedPrivate.getDocker().getUsername()).isNull(); + assertThat(strippedPrivate.getDocker().getPassword()).isNull(); + assertThat(strippedPrivate.getEnvironmentVariables()) + .containsEntry("TEST_KEY", "test-value"); + assertThat(strippedPrivate.getInstances()).isEqualTo(2); + assertThat(stripped.getApplications().get(1)).isSameAs(publicDocker); + assertThat(stripped.getApplications().get(2)).isSameAs(buildpackApp); + } + @Test void terminateTask() { requestApplicationsV3(