|
| 1 | +/* |
| 2 | + * Copyright 2025 The gRPC Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.grpc.xds.internal.extauthz; |
| 18 | + |
| 19 | +import com.google.common.collect.ImmutableList; |
| 20 | +import io.envoyproxy.envoy.service.auth.v3.CheckResponse; |
| 21 | +import io.envoyproxy.envoy.service.auth.v3.DeniedHttpResponse; |
| 22 | +import io.envoyproxy.envoy.service.auth.v3.OkHttpResponse; |
| 23 | +import io.grpc.Metadata; |
| 24 | +import io.grpc.Status; |
| 25 | +import io.grpc.internal.GrpcUtil; |
| 26 | +import io.grpc.xds.internal.headermutations.HeaderMutationDisallowedException; |
| 27 | +import io.grpc.xds.internal.headermutations.HeaderMutationFilter; |
| 28 | +import io.grpc.xds.internal.headermutations.HeaderMutations; |
| 29 | +import io.grpc.xds.internal.headermutations.HeaderMutator; |
| 30 | + |
| 31 | +/** |
| 32 | + * Handles the response from the external authorization service, processing it to determine the |
| 33 | + * authorization decision and applying any necessary header mutations. |
| 34 | + */ |
| 35 | +public interface CheckResponseHandler { |
| 36 | + |
| 37 | + /** |
| 38 | + * A factory for creating {@link CheckResponseHandler} instances. |
| 39 | + */ |
| 40 | + @FunctionalInterface |
| 41 | + interface Factory { |
| 42 | + /** |
| 43 | + * Creates a new ResponseHandler. |
| 44 | + * |
| 45 | + * @param headerMutator Utility to apply header mutations. |
| 46 | + * @param headerMutationFilter Filter to apply to header mutations. |
| 47 | + * @param config The external authorization configuration. |
| 48 | + */ |
| 49 | + CheckResponseHandler create(HeaderMutator headerMutator, |
| 50 | + HeaderMutationFilter headerMutationFilter, ExtAuthzConfig config); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * The default factory for creating {@link CheckResponseHandler} instances. |
| 55 | + */ |
| 56 | + Factory INSTANCE = ResponseHandlerImpl::new; |
| 57 | + |
| 58 | + /** |
| 59 | + * Processes the CheckResponse from the external authorization service. |
| 60 | + * |
| 61 | + * @param response The response from the authorization service. |
| 62 | + * @param headers The request headers, which may be mutated as part of handling the response. |
| 63 | + * @return An {@link AuthzResponse} indicating the outcome of the authorization check. |
| 64 | + */ |
| 65 | + AuthzResponse handleResponse(final CheckResponse response, Metadata headers); |
| 66 | + |
| 67 | + /** Default implementation of {@link CheckResponseHandler}. */ |
| 68 | + static final class ResponseHandlerImpl implements CheckResponseHandler { |
| 69 | + private final HeaderMutator headerMutator; |
| 70 | + private final HeaderMutationFilter headerMutationFilter; |
| 71 | + private final ExtAuthzConfig config; |
| 72 | + |
| 73 | + ResponseHandlerImpl(HeaderMutator headerMutator, // NOPMD |
| 74 | + HeaderMutationFilter headerMutationFilter, ExtAuthzConfig config) { |
| 75 | + this.headerMutator = headerMutator; |
| 76 | + this.headerMutationFilter = headerMutationFilter; |
| 77 | + this.config = config; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public AuthzResponse handleResponse(final CheckResponse response, Metadata headers) { |
| 82 | + try { |
| 83 | + if (response.getStatus().getCode() == Status.Code.OK.value()) { |
| 84 | + return handleOkResponse(response, headers); |
| 85 | + } else { |
| 86 | + return handleNotOkResponse(response); |
| 87 | + } |
| 88 | + } catch (HeaderMutationDisallowedException e) { |
| 89 | + return AuthzResponse.deny(e.getStatus()).build(); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private AuthzResponse handleOkResponse(final CheckResponse response, Metadata headers) |
| 94 | + throws HeaderMutationDisallowedException { |
| 95 | + if (!response.hasOkResponse()) { |
| 96 | + return AuthzResponse.allow(headers).build(); |
| 97 | + } |
| 98 | + OkHttpResponse okResponse = response.getOkResponse(); |
| 99 | + HeaderMutations requestedMutations = buildHeaderMutationsFromOkResponse(okResponse); |
| 100 | + HeaderMutations allowedMutations = headerMutationFilter.filter(requestedMutations); |
| 101 | + |
| 102 | + applyMutations(allowedMutations, headers); |
| 103 | + return AuthzResponse.allow(headers) |
| 104 | + .setResponseHeaderMutations(allowedMutations.responseMutations()).build(); |
| 105 | + } |
| 106 | + |
| 107 | + private HeaderMutations buildHeaderMutationsFromOkResponse(OkHttpResponse okResponse) { |
| 108 | + return HeaderMutations.create( |
| 109 | + HeaderMutations.RequestHeaderMutations.create( |
| 110 | + ImmutableList.copyOf(okResponse.getHeadersList()), |
| 111 | + ImmutableList.copyOf(okResponse.getHeadersToRemoveList())), |
| 112 | + HeaderMutations.ResponseHeaderMutations |
| 113 | + .create(ImmutableList.copyOf(okResponse.getResponseHeadersToAddList()))); |
| 114 | + } |
| 115 | + |
| 116 | + private AuthzResponse handleNotOkResponse(CheckResponse response) |
| 117 | + throws HeaderMutationDisallowedException { |
| 118 | + Status statusToReturn = config.statusOnError(); |
| 119 | + if (!response.hasDeniedResponse()) { |
| 120 | + return AuthzResponse.deny(statusToReturn).build(); |
| 121 | + } |
| 122 | + DeniedHttpResponse deniedResponse = response.getDeniedResponse(); |
| 123 | + HeaderMutations requestedMutations = buildHeaderMutationsFromDeniedResponse(deniedResponse); |
| 124 | + HeaderMutations allowedMutations = headerMutationFilter.filter(requestedMutations); |
| 125 | + |
| 126 | + Status status = statusToReturn; |
| 127 | + if (deniedResponse.hasStatus()) { |
| 128 | + status = GrpcUtil.httpStatusToGrpcStatus(deniedResponse.getStatus().getCodeValue()) |
| 129 | + .withDescription(deniedResponse.getBody()); |
| 130 | + } |
| 131 | + return AuthzResponse.deny(status) |
| 132 | + .setResponseHeaderMutations(allowedMutations.responseMutations()).build(); |
| 133 | + } |
| 134 | + |
| 135 | + private HeaderMutations buildHeaderMutationsFromDeniedResponse( |
| 136 | + DeniedHttpResponse deniedResponse) { |
| 137 | + return HeaderMutations.create( |
| 138 | + HeaderMutations.RequestHeaderMutations.create(ImmutableList.of(), ImmutableList.of()), |
| 139 | + HeaderMutations.ResponseHeaderMutations |
| 140 | + .create(ImmutableList.copyOf(deniedResponse.getHeadersList()))); |
| 141 | + } |
| 142 | + |
| 143 | + |
| 144 | + private void applyMutations(final HeaderMutations mutations, Metadata headers) { |
| 145 | + headerMutator.applyRequestMutations(mutations.requestMutations(), headers); |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments