|
| 1 | +/* |
| 2 | + * Copyright 2012-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with |
| 5 | + * the License. A copy of the License is located at |
| 6 | + * |
| 7 | + * http://aws.amazon.com/apache2.0 |
| 8 | + * |
| 9 | + * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 10 | + * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions |
| 11 | + * and limitations under the License. |
| 12 | + */ |
| 13 | +package uk.org.llgc.annotation.store.adapters.elastic; |
| 14 | + |
| 15 | +import com.amazonaws.DefaultRequest; |
| 16 | +import com.amazonaws.auth.AWSCredentialsProvider; |
| 17 | +import com.amazonaws.auth.Signer; |
| 18 | +import com.amazonaws.http.HttpMethodName; |
| 19 | +import org.apache.http.Header; |
| 20 | +import org.apache.http.HttpEntityEnclosingRequest; |
| 21 | +import org.apache.http.HttpException; |
| 22 | +import org.apache.http.HttpHost; |
| 23 | +import org.apache.http.HttpRequest; |
| 24 | +import org.apache.http.HttpRequestInterceptor; |
| 25 | +import org.apache.http.NameValuePair; |
| 26 | +import org.apache.http.client.utils.URIBuilder; |
| 27 | +import org.apache.http.entity.BasicHttpEntity; |
| 28 | +import org.apache.http.message.BasicHeader; |
| 29 | +import org.apache.http.protocol.HttpContext; |
| 30 | + |
| 31 | +import java.io.ByteArrayInputStream; |
| 32 | +import java.io.IOException; |
| 33 | +import java.net.URI; |
| 34 | +import java.net.URISyntaxException; |
| 35 | +import java.util.ArrayList; |
| 36 | +import java.util.List; |
| 37 | +import java.util.Map; |
| 38 | +import java.util.TreeMap; |
| 39 | + |
| 40 | +import static org.apache.http.protocol.HttpCoreContext.HTTP_TARGET_HOST; |
| 41 | + |
| 42 | +/** |
| 43 | + * THIS IS A HORRIBLE WAY OF USING THIS CLASS BUT: |
| 44 | + * this is recomended by the AWS docs here: https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/es-request-signing.html#es-request-signing-java |
| 45 | + * but the code isn't in maven: https://github.com/awslabs/aws-request-signing-apache-interceptor/issues/2 |
| 46 | + * so this is the only real option... |
| 47 | + * An {@link HttpRequestInterceptor} that signs requests using any AWS {@link Signer} |
| 48 | + * and {@link AWSCredentialsProvider}. |
| 49 | + */ |
| 50 | +public class AWSRequestSigningApacheInterceptor implements HttpRequestInterceptor { |
| 51 | + /** |
| 52 | + * The service that we're connecting to. Technically not necessary. |
| 53 | + * Could be used by a future Signer, though. |
| 54 | + */ |
| 55 | + private final String service; |
| 56 | + |
| 57 | + /** |
| 58 | + * The particular signer implementation. |
| 59 | + */ |
| 60 | + private final Signer signer; |
| 61 | + |
| 62 | + /** |
| 63 | + * The source of AWS credentials for signing. |
| 64 | + */ |
| 65 | + private final AWSCredentialsProvider awsCredentialsProvider; |
| 66 | + |
| 67 | + /** |
| 68 | + * |
| 69 | + * @param service service that we're connecting to |
| 70 | + * @param signer particular signer implementation |
| 71 | + * @param awsCredentialsProvider source of AWS credentials for signing |
| 72 | + */ |
| 73 | + public AWSRequestSigningApacheInterceptor(final String service, |
| 74 | + final Signer signer, |
| 75 | + final AWSCredentialsProvider awsCredentialsProvider) { |
| 76 | + this.service = service; |
| 77 | + this.signer = signer; |
| 78 | + this.awsCredentialsProvider = awsCredentialsProvider; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * {@inheritDoc} |
| 83 | + */ |
| 84 | + @Override |
| 85 | + public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { |
| 86 | + URIBuilder uriBuilder; |
| 87 | + try { |
| 88 | + uriBuilder = new URIBuilder(request.getRequestLine().getUri()); |
| 89 | + } catch (URISyntaxException e) { |
| 90 | + throw new IOException("Invalid URI" , e); |
| 91 | + } |
| 92 | + |
| 93 | + // Copy Apache HttpRequest to AWS DefaultRequest |
| 94 | + DefaultRequest<?> signableRequest = new DefaultRequest<>(service); |
| 95 | + |
| 96 | + HttpHost host = (HttpHost) context.getAttribute(HTTP_TARGET_HOST); |
| 97 | + if (host != null) { |
| 98 | + signableRequest.setEndpoint(URI.create(host.toURI())); |
| 99 | + } |
| 100 | + final HttpMethodName httpMethod = HttpMethodName.fromValue(request.getRequestLine().getMethod()); |
| 101 | + signableRequest.setHttpMethod(httpMethod); |
| 102 | + try { |
| 103 | + signableRequest.setResourcePath(uriBuilder.build().getRawPath()); |
| 104 | + } catch (URISyntaxException e) { |
| 105 | + throw new IOException("Invalid URI" , e); |
| 106 | + } |
| 107 | + |
| 108 | + String tContent = ""; |
| 109 | + if (request instanceof HttpEntityEnclosingRequest) { |
| 110 | + HttpEntityEnclosingRequest httpEntityEnclosingRequest = |
| 111 | + (HttpEntityEnclosingRequest) request; |
| 112 | + if (httpEntityEnclosingRequest.getEntity() == null) { |
| 113 | + signableRequest.setContent(new ByteArrayInputStream(new byte[0])); |
| 114 | + } else { |
| 115 | + signableRequest.setContent(httpEntityEnclosingRequest.getEntity().getContent()); |
| 116 | + } |
| 117 | + } |
| 118 | + signableRequest.setParameters(nvpToMapParams(uriBuilder.getQueryParams())); |
| 119 | + signableRequest.setHeaders(headerArrayToMap(request.getAllHeaders())); |
| 120 | + |
| 121 | + // Sign it |
| 122 | + signer.sign(signableRequest, awsCredentialsProvider.getCredentials()); |
| 123 | + |
| 124 | + // Now copy everything back |
| 125 | + request.setHeaders(mapToHeaderArray(signableRequest.getHeaders())); |
| 126 | + if (request instanceof HttpEntityEnclosingRequest) { |
| 127 | + HttpEntityEnclosingRequest httpEntityEnclosingRequest = (HttpEntityEnclosingRequest) request; |
| 128 | + if (httpEntityEnclosingRequest.getEntity() != null) { |
| 129 | + BasicHttpEntity basicHttpEntity = new BasicHttpEntity(); |
| 130 | + basicHttpEntity.setContent(signableRequest.getContent()); |
| 131 | + httpEntityEnclosingRequest.setEntity(basicHttpEntity); |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * |
| 138 | + * @param params list of HTTP query params as NameValuePairs |
| 139 | + * @return a multimap of HTTP query params |
| 140 | + */ |
| 141 | + private static Map<String, List<String>> nvpToMapParams(final List<NameValuePair> params) { |
| 142 | + Map<String, List<String>> parameterMap = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); |
| 143 | + for (NameValuePair nvp : params) { |
| 144 | + List<String> argsList = |
| 145 | + parameterMap.computeIfAbsent(nvp.getName(), k -> new ArrayList<>()); |
| 146 | + argsList.add(nvp.getValue()); |
| 147 | + } |
| 148 | + return parameterMap; |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * @param headers modeled Header objects |
| 153 | + * @return a Map of header entries |
| 154 | + */ |
| 155 | + private static Map<String, String> headerArrayToMap(final Header[] headers) { |
| 156 | + Map<String, String> headersMap = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); |
| 157 | + for (Header header : headers) { |
| 158 | + if (!skipHeader(header)) { |
| 159 | + headersMap.put(header.getName(), header.getValue()); |
| 160 | + } |
| 161 | + } |
| 162 | + return headersMap; |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * @param header header line to check |
| 167 | + * @return true if the given header should be excluded when signing |
| 168 | + */ |
| 169 | + private static boolean skipHeader(final Header header) { |
| 170 | + return ("content-length".equalsIgnoreCase(header.getName()) |
| 171 | + && "0".equals(header.getValue())) // Strip Content-Length: 0 |
| 172 | + || "host".equalsIgnoreCase(header.getName()); // Host comes from endpoint |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * @param mapHeaders Map of header entries |
| 177 | + * @return modeled Header objects |
| 178 | + */ |
| 179 | + private static Header[] mapToHeaderArray(final Map<String, String> mapHeaders) { |
| 180 | + Header[] headers = new Header[mapHeaders.size()]; |
| 181 | + int i = 0; |
| 182 | + for (Map.Entry<String, String> headerEntry : mapHeaders.entrySet()) { |
| 183 | + headers[i++] = new BasicHeader(headerEntry.getKey(), headerEntry.getValue()); |
| 184 | + } |
| 185 | + return headers; |
| 186 | + } |
| 187 | +} |
| 188 | + |
0 commit comments