|
| 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.grpcservice.HeaderValue; |
| 27 | +import io.grpc.xds.internal.grpcservice.HeaderValueValidationUtils; |
| 28 | +import io.grpc.xds.internal.headermutations.HeaderMutationDisallowedException; |
| 29 | +import io.grpc.xds.internal.headermutations.HeaderMutationFilter; |
| 30 | +import io.grpc.xds.internal.headermutations.HeaderMutations; |
| 31 | +import io.grpc.xds.internal.headermutations.HeaderValueOption; |
| 32 | +import javax.annotation.concurrent.ThreadSafe; |
| 33 | + |
| 34 | +/** |
| 35 | + * Handles the response from the external authorization service, processing it to determine the |
| 36 | + * authorization decision and applying any necessary header mutations. |
| 37 | + */ |
| 38 | +@ThreadSafe |
| 39 | +public class CheckResponseHandler { |
| 40 | + private final HeaderMutationFilter headerMutationFilter; |
| 41 | + |
| 42 | + public CheckResponseHandler(HeaderMutationFilter headerMutationFilter) { |
| 43 | + this.headerMutationFilter = headerMutationFilter; |
| 44 | + } |
| 45 | + |
| 46 | + AuthzResponse handleResponse(final CheckResponse response) { |
| 47 | + try { |
| 48 | + if (response.getStatus().getCode() == Status.Code.OK.value()) { |
| 49 | + return handleOkResponse(response); |
| 50 | + } else { |
| 51 | + return handleNotOkResponse(response); |
| 52 | + } |
| 53 | + } catch (HeaderMutationDisallowedException e) { |
| 54 | + return AuthzResponse.deny(e.getStatus()).build(); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + private AuthzResponse handleOkResponse(final CheckResponse response) |
| 59 | + throws HeaderMutationDisallowedException { |
| 60 | + if (!response.hasOkResponse()) { |
| 61 | + return AuthzResponse.allow( |
| 62 | + HeaderMutations.create(ImmutableList.of(), ImmutableList.of())).build(); |
| 63 | + } |
| 64 | + OkHttpResponse okResponse = response.getOkResponse(); |
| 65 | + CheckResponseMutations allowedMutations = buildHeaderMutationsFromOkResponse(okResponse); |
| 66 | + |
| 67 | + return AuthzResponse.allow(allowedMutations.requestMutations()) |
| 68 | + .setResponseHeaderMutations(allowedMutations.responseMutations()).build(); |
| 69 | + } |
| 70 | + |
| 71 | + private CheckResponseMutations buildHeaderMutationsFromOkResponse(OkHttpResponse okResponse) |
| 72 | + throws HeaderMutationDisallowedException { |
| 73 | + HeaderMutations requestMutations = HeaderMutations.create( |
| 74 | + convertHeaders(okResponse.getHeadersList()), |
| 75 | + ImmutableList.copyOf(okResponse.getHeadersToRemoveList())); |
| 76 | + HeaderMutations responseMutations = HeaderMutations.create( |
| 77 | + convertHeaders(okResponse.getResponseHeadersToAddList()), |
| 78 | + ImmutableList.of()); |
| 79 | + return CheckResponseMutations.create( |
| 80 | + headerMutationFilter.filter(requestMutations), |
| 81 | + headerMutationFilter.filter(responseMutations)); |
| 82 | + } |
| 83 | + |
| 84 | + private AuthzResponse handleNotOkResponse(CheckResponse response) |
| 85 | + throws HeaderMutationDisallowedException { |
| 86 | + String baseMsg = "RPC denied by external authorization server"; |
| 87 | + String outerMsg = response.getStatus().getMessage(); |
| 88 | + String description = outerMsg.isEmpty() ? baseMsg : baseMsg + ": " + outerMsg; |
| 89 | + |
| 90 | + if (!response.hasDeniedResponse()) { |
| 91 | + return AuthzResponse.deny(Status.PERMISSION_DENIED.withDescription(description)).build(); |
| 92 | + } |
| 93 | + DeniedHttpResponse deniedResponse = response.getDeniedResponse(); |
| 94 | + CheckResponseMutations allowedMutations = |
| 95 | + buildHeaderMutationsFromDeniedResponse(deniedResponse); |
| 96 | + |
| 97 | + Status status = Status.PERMISSION_DENIED; |
| 98 | + if (deniedResponse.hasStatus()) { |
| 99 | + status = GrpcUtil.httpStatusToGrpcStatus(deniedResponse.getStatus().getCodeValue()); |
| 100 | + } |
| 101 | + // Per gRFC A92: deniedResponse.body is ignored for gRPC (doesn't apply to gRPC). |
| 102 | + return AuthzResponse.deny(status.withDescription(description)) |
| 103 | + .setResponseHeaderMutations(allowedMutations.responseMutations()).build(); |
| 104 | + } |
| 105 | + |
| 106 | + private CheckResponseMutations buildHeaderMutationsFromDeniedResponse( |
| 107 | + DeniedHttpResponse deniedResponse) throws HeaderMutationDisallowedException { |
| 108 | + HeaderMutations requestMutations = |
| 109 | + HeaderMutations.create(ImmutableList.of(), ImmutableList.of()); |
| 110 | + HeaderMutations responseMutations = HeaderMutations.create( |
| 111 | + convertHeaders(deniedResponse.getHeadersList()), |
| 112 | + ImmutableList.of()); |
| 113 | + return CheckResponseMutations.create( |
| 114 | + headerMutationFilter.filter(requestMutations), |
| 115 | + headerMutationFilter.filter(responseMutations)); |
| 116 | + } |
| 117 | + |
| 118 | + private ImmutableList<HeaderValueOption> convertHeaders( |
| 119 | + java.util.List<io.envoyproxy.envoy.config.core.v3.HeaderValueOption> headersList) |
| 120 | + throws HeaderMutationDisallowedException { |
| 121 | + ImmutableList.Builder<HeaderValueOption> builder = ImmutableList.builder(); |
| 122 | + for (io.envoyproxy.envoy.config.core.v3.HeaderValueOption optionProto : headersList) { |
| 123 | + io.envoyproxy.envoy.config.core.v3.HeaderValue header = optionProto.getHeader(); |
| 124 | + String key = header.getKey(); |
| 125 | + HeaderValue internalHeader; |
| 126 | + if (key.endsWith(Metadata.BINARY_HEADER_SUFFIX)) { |
| 127 | + internalHeader = HeaderValue.create(key, header.getRawValue()); |
| 128 | + } else { |
| 129 | + internalHeader = HeaderValue.create(key, header.getValue()); |
| 130 | + } |
| 131 | + if (HeaderValueValidationUtils.isDisallowed(internalHeader)) { |
| 132 | + continue; |
| 133 | + } |
| 134 | + HeaderValueOption.HeaderAppendAction action; |
| 135 | + switch (optionProto.getAppendAction()) { |
| 136 | + case APPEND_IF_EXISTS_OR_ADD: |
| 137 | + action = HeaderValueOption.HeaderAppendAction.APPEND_IF_EXISTS_OR_ADD; |
| 138 | + break; |
| 139 | + case ADD_IF_ABSENT: |
| 140 | + action = HeaderValueOption.HeaderAppendAction.ADD_IF_ABSENT; |
| 141 | + break; |
| 142 | + case OVERWRITE_IF_EXISTS_OR_ADD: |
| 143 | + action = HeaderValueOption.HeaderAppendAction.OVERWRITE_IF_EXISTS_OR_ADD; |
| 144 | + break; |
| 145 | + case OVERWRITE_IF_EXISTS: |
| 146 | + action = HeaderValueOption.HeaderAppendAction.OVERWRITE_IF_EXISTS; |
| 147 | + break; |
| 148 | + case UNRECOGNIZED: |
| 149 | + default: |
| 150 | + // Envoy Parity / Spec Parity: Unconditionally reject invalid/unrecognized append actions |
| 151 | + throw new HeaderMutationDisallowedException( |
| 152 | + "Unrecognized HeaderAppendAction: " + optionProto.getAppendAction()); |
| 153 | + } |
| 154 | + builder |
| 155 | + .add(HeaderValueOption.create(internalHeader, action)); |
| 156 | + } |
| 157 | + return builder.build(); |
| 158 | + } |
| 159 | +} |
| 160 | + |
0 commit comments