|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package com.azure.containers.containerregistry.implementation; |
| 5 | + |
| 6 | +import com.azure.core.http.HttpHeaders; |
| 7 | +import com.azure.core.http.HttpMethod; |
| 8 | +import com.azure.core.http.HttpPipelineCallContext; |
| 9 | +import com.azure.core.http.HttpPipelineNextPolicy; |
| 10 | +import com.azure.core.http.HttpRequest; |
| 11 | +import com.azure.core.http.HttpResponse; |
| 12 | +import com.azure.core.http.policy.HttpPipelinePolicy; |
| 13 | +import com.azure.core.util.CoreUtils; |
| 14 | +import com.azure.core.util.logging.ClientLogger; |
| 15 | +import reactor.core.publisher.Mono; |
| 16 | + |
| 17 | +import java.util.Arrays; |
| 18 | +import java.util.HashSet; |
| 19 | +import java.util.Set; |
| 20 | + |
| 21 | +import static com.azure.containers.containerregistry.implementation.UtilsImpl.DOCKER_DIGEST_HEADER_NAME; |
| 22 | + |
| 23 | +/** |
| 24 | + * <p> Redirect policy for the container registry.</p> |
| 25 | + * |
| 26 | + * <p> This reads some of the headers that are returned from the redirect call that core redirect policy does not handle.</p> |
| 27 | + */ |
| 28 | +public final class ContainerRegistryRedirectPolicy implements HttpPipelinePolicy { |
| 29 | + private static final ClientLogger LOGGER = new ClientLogger(com.azure.core.http.policy.DefaultRedirectStrategy.class); |
| 30 | + private static final int MAX_REDIRECT_ATTEMPTS; |
| 31 | + private static final String REDIRECT_LOCATION_HEADER_NAME; |
| 32 | + private static final int PERMANENT_REDIRECT_STATUS_CODE; |
| 33 | + private static final int TEMPORARY_REDIRECT_STATUS_CODE; |
| 34 | + private static final Set<HttpMethod> REDIRECT_ALLOWED_METHODS; |
| 35 | + private static final String AUTHORIZATION; |
| 36 | + |
| 37 | + static { |
| 38 | + REDIRECT_ALLOWED_METHODS = new HashSet<>(Arrays.asList(HttpMethod.GET, HttpMethod.HEAD)); |
| 39 | + PERMANENT_REDIRECT_STATUS_CODE = 308; |
| 40 | + TEMPORARY_REDIRECT_STATUS_CODE = 307; |
| 41 | + REDIRECT_LOCATION_HEADER_NAME = "Location"; |
| 42 | + MAX_REDIRECT_ATTEMPTS = 3; |
| 43 | + AUTHORIZATION = "Authorization"; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public Mono<HttpResponse> process(HttpPipelineCallContext context, HttpPipelineNextPolicy next) { |
| 48 | + return this.attemptRedirect(context, next, context.getHttpRequest(), 1, new HashSet<>()); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Function to process through the HTTP Response received in the pipeline |
| 53 | + * and redirect sending the request with new redirect url. |
| 54 | + */ |
| 55 | + private Mono<HttpResponse> attemptRedirect(HttpPipelineCallContext context, HttpPipelineNextPolicy next, HttpRequest originalHttpRequest, int redirectAttempt, Set<String> attemptedRedirectUrls) { |
| 56 | + context.setHttpRequest(originalHttpRequest.copy()); |
| 57 | + return next.clone().process().flatMap((httpResponse) -> { |
| 58 | + if (this.shouldAttemptRedirect(context, httpResponse, redirectAttempt, attemptedRedirectUrls)) { |
| 59 | + HttpRequest redirectRequestCopy = this.createRedirectRequest(httpResponse); |
| 60 | + return httpResponse.getBody().ignoreElements() |
| 61 | + .then(this.attemptRedirect(context, next, redirectRequestCopy, redirectAttempt + 1, attemptedRedirectUrls)) |
| 62 | + .flatMap(newResponse -> { |
| 63 | + String digest = httpResponse.getHeaders().getValue(DOCKER_DIGEST_HEADER_NAME); |
| 64 | + if (digest != null) { |
| 65 | + newResponse.getHeaders().add(DOCKER_DIGEST_HEADER_NAME, digest); |
| 66 | + } |
| 67 | + return Mono.just(newResponse); |
| 68 | + }); |
| 69 | + } else { |
| 70 | + return Mono.just(httpResponse); |
| 71 | + } |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + public boolean shouldAttemptRedirect(HttpPipelineCallContext context, HttpResponse httpResponse, int tryCount, Set<String> attemptedRedirectUrls) { |
| 76 | + if (this.isValidRedirectStatusCode(httpResponse.getStatusCode()) && this.isValidRedirectCount(tryCount) && this.isAllowedRedirectMethod(httpResponse.getRequest().getHttpMethod())) { |
| 77 | + String redirectUrl = this.tryGetRedirectHeader(httpResponse.getHeaders(), REDIRECT_LOCATION_HEADER_NAME); |
| 78 | + if (redirectUrl != null && !this.alreadyAttemptedRedirectUrl(redirectUrl, attemptedRedirectUrls)) { |
| 79 | + LOGGER.verbose("[Redirecting] Try count: {}, Attempted Redirect URLs: {}", tryCount, String.join(",", attemptedRedirectUrls)); |
| 80 | + attemptedRedirectUrls.add(redirectUrl); |
| 81 | + return true; |
| 82 | + } else { |
| 83 | + return false; |
| 84 | + } |
| 85 | + } else { |
| 86 | + return false; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private HttpRequest createRedirectRequest(HttpResponse httpResponse) { |
| 91 | + String responseLocation = this.tryGetRedirectHeader(httpResponse.getHeaders(), REDIRECT_LOCATION_HEADER_NAME); |
| 92 | + HttpRequest request = httpResponse.getRequest(); |
| 93 | + request.setUrl(responseLocation); |
| 94 | + request.getHeaders().remove(AUTHORIZATION); |
| 95 | + return httpResponse.getRequest().setUrl(responseLocation); |
| 96 | + } |
| 97 | + |
| 98 | + private boolean alreadyAttemptedRedirectUrl(String redirectUrl, Set<String> attemptedRedirectUrls) { |
| 99 | + if (attemptedRedirectUrls.contains(redirectUrl)) { |
| 100 | + LOGGER.error("Request was redirected more than once to: {}", new Object[]{redirectUrl}); |
| 101 | + return true; |
| 102 | + } else { |
| 103 | + return false; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private boolean isValidRedirectCount(int tryCount) { |
| 108 | + if (tryCount >= MAX_REDIRECT_ATTEMPTS) { |
| 109 | + LOGGER.error("Request has been redirected more than {} times.", new Object[]{MAX_REDIRECT_ATTEMPTS}); |
| 110 | + return false; |
| 111 | + } else { |
| 112 | + return true; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private boolean isAllowedRedirectMethod(HttpMethod httpMethod) { |
| 117 | + if (REDIRECT_ALLOWED_METHODS.contains(httpMethod)) { |
| 118 | + return true; |
| 119 | + } else { |
| 120 | + LOGGER.error("Request was redirected from an invalid redirect allowed method: {}", new Object[]{httpMethod}); |
| 121 | + return false; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + private boolean isValidRedirectStatusCode(int statusCode) { |
| 126 | + return statusCode == PERMANENT_REDIRECT_STATUS_CODE || statusCode == TEMPORARY_REDIRECT_STATUS_CODE; |
| 127 | + } |
| 128 | + |
| 129 | + String tryGetRedirectHeader(HttpHeaders headers, String headerName) { |
| 130 | + String headerValue = headers.getValue(headerName); |
| 131 | + if (CoreUtils.isNullOrEmpty(headerValue)) { |
| 132 | + LOGGER.error("Redirect url was null for header name: {}, request redirect was terminated.", headerName); |
| 133 | + return null; |
| 134 | + } else { |
| 135 | + return headerValue; |
| 136 | + } |
| 137 | + } |
| 138 | +} |
| 139 | + |
0 commit comments