-
Notifications
You must be signed in to change notification settings - Fork 1k
fix(docker): fix retrofit2 docker client which is replacing ?
with %3F
causing api failure
#6354
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
0f7e209
test(docker): add two test classes, one to demonstrate how real docke…
kirangodishala 5e32a8a
fix(docker): fix the issue of retrofit2 replacing `?` with `%3F` caus…
kirangodishala 9240756
fix(docker): address review comments on the fix to the issue of retro…
kirangodishala File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
...netflix/spinnaker/clouddriver/docker/registry/api/v2/client/DockerRegistryClientTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/* | ||
* Copyright 2025 OpsMx, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.spinnaker.clouddriver.docker.registry.api.v2.client; | ||
|
||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching; | ||
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; | ||
import static org.junit.jupiter.api.Assertions.assertIterableEquals; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.github.tomakehurst.wiremock.client.WireMock; | ||
import com.github.tomakehurst.wiremock.junit5.WireMockExtension; | ||
import com.netflix.spinnaker.clouddriver.docker.registry.api.v2.auth.DockerBearerToken; | ||
import com.netflix.spinnaker.clouddriver.docker.registry.api.v2.auth.DockerBearerTokenService; | ||
import com.netflix.spinnaker.config.DefaultServiceClientProvider; | ||
import com.netflix.spinnaker.config.okhttp3.DefaultOkHttpClientBuilderProvider; | ||
import com.netflix.spinnaker.config.okhttp3.OkHttpClientProvider; | ||
import com.netflix.spinnaker.kork.client.ServiceClientProvider; | ||
import com.netflix.spinnaker.kork.retrofit.ErrorHandlingExecutorCallAdapterFactory; | ||
import com.netflix.spinnaker.kork.retrofit.Retrofit2ServiceFactory; | ||
import com.netflix.spinnaker.kork.retrofit.Retrofit2ServiceFactoryAutoConfiguration; | ||
import com.netflix.spinnaker.kork.retrofit.exceptions.SpinnakerHttpException; | ||
import com.netflix.spinnaker.okhttp.OkHttpClientConfigurationProperties; | ||
import java.util.Arrays; | ||
import java.util.Map; | ||
import okhttp3.OkHttpClient; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
import org.mockito.Mockito; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.http.HttpStatus; | ||
import retrofit2.Retrofit; | ||
import retrofit2.converter.jackson.JacksonConverterFactory; | ||
|
||
@SpringBootTest( | ||
classes = { | ||
OkHttpClientConfigurationProperties.class, | ||
Retrofit2ServiceFactory.class, | ||
ServiceClientProvider.class, | ||
OkHttpClientProvider.class, | ||
OkHttpClient.class, | ||
DefaultServiceClientProvider.class, | ||
DefaultOkHttpClientBuilderProvider.class, | ||
Retrofit2ServiceFactoryAutoConfiguration.class, | ||
ObjectMapper.class | ||
}, | ||
webEnvironment = SpringBootTest.WebEnvironment.NONE) | ||
public class DockerRegistryClientTest { | ||
|
||
@RegisterExtension | ||
static WireMockExtension wmDockerRegistry = | ||
WireMockExtension.newInstance().options(wireMockConfig().dynamicPort()).build(); | ||
|
||
static DockerRegistryClient.DockerRegistryService dockerRegistryService; | ||
@MockBean DockerBearerTokenService dockerBearerTokenService; | ||
static DockerRegistryClient dockerRegistryClient; | ||
@Autowired ServiceClientProvider serviceClientProvider; | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
Map<String, Object> tagsResponse; | ||
String tagsResponseString; | ||
String nextLink = "</v2/library/nginx/tags/list?last=1-alpine-slim&n=5>; rel=\"next\""; | ||
|
||
@BeforeEach | ||
public void init() throws JsonProcessingException { | ||
tagsResponse = | ||
Map.of( | ||
"name", | ||
"library/nginx", | ||
"tags", | ||
new String[] {"1", "1-alpine", "1-alpine-otel", "1-alpine-perl", "1-alpine-slim"}); | ||
tagsResponseString = objectMapper.writeValueAsString(tagsResponse); | ||
|
||
DockerBearerToken bearerToken = new DockerBearerToken(); | ||
bearerToken.setToken("someToken"); | ||
bearerToken.setAccess_token("someToken"); | ||
Mockito.when(dockerBearerTokenService.getToken(anyString())).thenReturn(bearerToken); | ||
dockerRegistryService = | ||
buildService(DockerRegistryClient.DockerRegistryService.class, wmDockerRegistry.baseUrl()); | ||
dockerRegistryClient = | ||
new DockerRegistryClient( | ||
wmDockerRegistry.baseUrl(), 5, "", "", dockerRegistryService, dockerBearerTokenService); | ||
} | ||
|
||
private static <T> T buildService(Class<T> type, String baseUrl) { | ||
return new Retrofit.Builder() | ||
.baseUrl(baseUrl) | ||
.client(new OkHttpClient()) | ||
.addCallAdapterFactory(ErrorHandlingExecutorCallAdapterFactory.getInstance()) | ||
.addConverterFactory(JacksonConverterFactory.create()) | ||
.build() | ||
.create(type); | ||
} | ||
|
||
@Test | ||
public void getTagsWithoutNextLink() { | ||
wmDockerRegistry.stubFor( | ||
WireMock.get(urlMatching("/v2/library/nginx/tags/list")) | ||
.willReturn( | ||
aResponse().withStatus(HttpStatus.OK.value()).withBody(tagsResponseString))); | ||
|
||
DockerRegistryTags dockerRegistryTags = dockerRegistryClient.getTags("library/nginx"); | ||
String[] tags = (String[]) tagsResponse.get("tags"); | ||
assertIterableEquals(Arrays.asList(tags), dockerRegistryTags.getTags()); | ||
} | ||
|
||
@Test | ||
public void getTagsWithNextLink() { | ||
wmDockerRegistry.stubFor( | ||
WireMock.get(urlMatching("/v2/library/nginx/tags/list")) | ||
.willReturn( | ||
aResponse() | ||
.withStatus(HttpStatus.OK.value()) | ||
.withHeader("link", nextLink) | ||
.withBody(tagsResponseString))); | ||
// TODO: Fix the below error occurring due to retrofit2 replacing `?` with `%3F` | ||
Assertions.assertThrows( | ||
SpinnakerHttpException.class, | ||
() -> dockerRegistryClient.getTags("library/nginx"), | ||
"Status: 404, Method: GET, URL: http://<baseUrl>/v2/library/nginx/tags/list%3Flast=1-alpine-slim&n=5, Message: Not Found"); | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
...etflix/spinnaker/clouddriver/docker/registry/api/v2/client/DockerRegistryServiceTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* Copyright 2025 OpsMx, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.spinnaker.clouddriver.docker.registry.api.v2.client; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
||
import com.netflix.spinnaker.clouddriver.docker.registry.api.v2.auth.DockerBearerToken; | ||
import com.netflix.spinnaker.kork.retrofit.ErrorHandlingExecutorCallAdapterFactory; | ||
import com.netflix.spinnaker.kork.retrofit.Retrofit2SyncCall; | ||
import java.io.IOException; | ||
import java.time.Instant; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.ResponseBody; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import retrofit2.Call; | ||
import retrofit2.Retrofit; | ||
import retrofit2.converter.jackson.JacksonConverterFactory; | ||
import retrofit2.http.GET; | ||
import retrofit2.http.Header; | ||
import retrofit2.http.Headers; | ||
import retrofit2.http.Path; | ||
import retrofit2.http.Query; | ||
|
||
public class DockerRegistryServiceTest { | ||
|
||
static DockerRegistryClient.DockerRegistryService dockerRegistryService; | ||
static TokenService tokenService; | ||
String service = "registry.docker.io"; | ||
String scope = "repository:library/nginx:pull"; | ||
String repository = "library/nginx"; | ||
String tagsPath = "v2/library/nginx/tags/list"; | ||
private Instant expiryTime = Instant.now(); | ||
static String bearerToken; | ||
|
||
@BeforeAll | ||
public static void setup() { | ||
tokenService = buildService(TokenService.class, "https://auth.docker.io"); | ||
dockerRegistryService = | ||
buildService( | ||
DockerRegistryClient.DockerRegistryService.class, "https://registry-1.docker.io"); | ||
} | ||
|
||
@Test | ||
void getToken() { | ||
DockerBearerToken token = | ||
Retrofit2SyncCall.execute(tokenService.getToken("token", service, scope, "spinnaker")); | ||
assertNotNull(token.getAccess_token()); | ||
} | ||
|
||
@Test | ||
void getTagsWithToken() throws IOException { | ||
try (ResponseBody response = | ||
Retrofit2SyncCall.execute( | ||
dockerRegistryService.getTags(repository, getBearerToken(), "Spinnaker"))) { | ||
assertNotNull(response.string()); | ||
} | ||
} | ||
|
||
@Test | ||
void getTagsWithPathSupplied() throws IOException { | ||
try (ResponseBody response = | ||
Retrofit2SyncCall.execute( | ||
dockerRegistryService.get(tagsPath, getBearerToken(), "Spinnaker"))) { | ||
assertNotNull(response.string()); | ||
} | ||
} | ||
|
||
private String getBearerToken() { | ||
if (bearerToken == null || Instant.now().isAfter(expiryTime)) { | ||
DockerBearerToken token = | ||
Retrofit2SyncCall.execute(tokenService.getToken("token", service, scope, "spinnaker")); | ||
bearerToken = "Bearer " + token.getAccess_token(); | ||
this.expiryTime = Instant.now().plusSeconds(4 * 60); // 4 minutes | ||
} | ||
return bearerToken; | ||
} | ||
|
||
private static <T> T buildService(Class<T> type, String baseUrl) { | ||
return new Retrofit.Builder() | ||
.baseUrl(baseUrl) | ||
.client(new OkHttpClient()) | ||
.addCallAdapterFactory(ErrorHandlingExecutorCallAdapterFactory.getInstance()) | ||
.addConverterFactory(JacksonConverterFactory.create()) | ||
.build() | ||
.create(type); | ||
} | ||
|
||
private interface TokenService { | ||
dbyron-sf marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
@GET("/{path}") | ||
@Headers({"Docker-Distribution-API-Version: registry/2.0"}) | ||
Call<DockerBearerToken> getToken( | ||
@Path(value = "path", encoded = true) String path, | ||
@Query(value = "service") String service, | ||
@Query(value = "scope") String scope, | ||
@Header("User-Agent") String agent); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.