| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.atmos.filters; |
| 20 | |
| 21 | import static org.jclouds.Constants.LOGGER_SIGNATURE; |
| 22 | import static org.jclouds.Constants.PROPERTY_CREDENTIAL; |
| 23 | import static org.jclouds.Constants.PROPERTY_IDENTITY; |
| 24 | import static org.jclouds.util.Patterns.NEWLINE_PATTERN; |
| 25 | import static org.jclouds.util.Patterns.TWO_SPACE_PATTERN; |
| 26 | |
| 27 | import java.util.Set; |
| 28 | import java.util.TreeSet; |
| 29 | |
| 30 | import javax.annotation.Resource; |
| 31 | import javax.inject.Inject; |
| 32 | import javax.inject.Named; |
| 33 | import javax.inject.Provider; |
| 34 | import javax.inject.Singleton; |
| 35 | import javax.ws.rs.core.HttpHeaders; |
| 36 | |
| 37 | import org.jclouds.atmos.reference.AtmosHeaders; |
| 38 | import org.jclouds.crypto.Crypto; |
| 39 | import org.jclouds.crypto.CryptoStreams; |
| 40 | import org.jclouds.date.TimeStamp; |
| 41 | import org.jclouds.http.HttpException; |
| 42 | import org.jclouds.http.HttpRequest; |
| 43 | import org.jclouds.http.HttpRequestFilter; |
| 44 | import org.jclouds.http.HttpUtils; |
| 45 | import org.jclouds.http.internal.SignatureWire; |
| 46 | import org.jclouds.http.utils.ModifyRequest; |
| 47 | import org.jclouds.io.InputSuppliers; |
| 48 | import org.jclouds.logging.Logger; |
| 49 | import org.jclouds.util.Strings2; |
| 50 | |
| 51 | import com.google.common.annotations.VisibleForTesting; |
| 52 | import com.google.common.collect.ImmutableMap; |
| 53 | import com.google.common.collect.ImmutableMap.Builder; |
| 54 | import com.google.common.collect.Multimaps; |
| 55 | |
| 56 | /** |
| 57 | * Signs the EMC Atmos Online Storage request. |
| 58 | * |
| 59 | * @see <a href="https://community.emc.com/community/labs/atmos_online" /> |
| 60 | * @author Adrian Cole |
| 61 | * |
| 62 | */ |
| 63 | @Singleton |
| 64 | public class SignRequest implements HttpRequestFilter { |
| 65 | |
| 66 | private final SignatureWire signatureWire; |
| 67 | private final String uid; |
| 68 | private final byte[] key; |
| 69 | private final Provider<String> timeStampProvider; |
| 70 | private final Crypto crypto; |
| 71 | private final HttpUtils utils; |
| 72 | |
| 73 | @Resource |
| 74 | Logger logger = Logger.NULL; |
| 75 | |
| 76 | @Resource |
| 77 | @Named(LOGGER_SIGNATURE) |
| 78 | Logger signatureLog = Logger.NULL; |
| 79 | |
| 80 | @Inject |
| 81 | public SignRequest(SignatureWire signatureWire, @Named(PROPERTY_IDENTITY) String uid, |
| 82 | @Named(PROPERTY_CREDENTIAL) String encodedKey, @TimeStamp Provider<String> timeStampProvider, Crypto crypto, |
| 83 | HttpUtils utils) { |
| 84 | this.signatureWire = signatureWire; |
| 85 | this.uid = uid; |
| 86 | this.key = CryptoStreams.base64(encodedKey); |
| 87 | this.timeStampProvider = timeStampProvider; |
| 88 | this.crypto = crypto; |
| 89 | this.utils = utils; |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public HttpRequest filter(HttpRequest request) throws HttpException { |
| 94 | Builder<String, String> builder = ImmutableMap.builder(); |
| 95 | builder.put(AtmosHeaders.UID, uid); |
| 96 | String date = timeStampProvider.get(); |
| 97 | builder.put(HttpHeaders.DATE, date); |
| 98 | if (request.getHeaders().containsKey(AtmosHeaders.DATE)) |
| 99 | builder.put(AtmosHeaders.DATE, date); |
| 100 | request = ModifyRequest.replaceHeaders(request, Multimaps.forMap(builder.build())); |
| 101 | String signature = calculateSignature(createStringToSign(request)); |
| 102 | request = ModifyRequest.replaceHeader(request, AtmosHeaders.SIGNATURE, signature); |
| 103 | utils.logRequest(signatureLog, request, "<<"); |
| 104 | return request; |
| 105 | } |
| 106 | |
| 107 | public String createStringToSign(HttpRequest request) { |
| 108 | utils.logRequest(signatureLog, request, ">>"); |
| 109 | StringBuilder buffer = new StringBuilder(); |
| 110 | // re-sign the request |
| 111 | appendMethod(request, buffer); |
| 112 | appendPayloadMetadata(request, buffer); |
| 113 | appendHttpHeaders(request, buffer); |
| 114 | appendCanonicalizedResource(request, buffer); |
| 115 | appendCanonicalizedHeaders(request, buffer); |
| 116 | if (signatureWire.enabled()) |
| 117 | signatureWire.output(buffer.toString()); |
| 118 | return buffer.toString(); |
| 119 | } |
| 120 | |
| 121 | private String calculateSignature(String toSign) { |
| 122 | String signature = signString(toSign); |
| 123 | if (signatureWire.enabled()) |
| 124 | signatureWire.input(Strings2.toInputStream(signature)); |
| 125 | return signature; |
| 126 | } |
| 127 | |
| 128 | public String signString(String toSign) { |
| 129 | String signature; |
| 130 | try { |
| 131 | signature = CryptoStreams.base64(CryptoStreams.mac(InputSuppliers.of(toSign), crypto.hmacSHA1(key))); |
| 132 | } catch (Exception e) { |
| 133 | throw new HttpException("error signing request", e); |
| 134 | } |
| 135 | return signature; |
| 136 | } |
| 137 | |
| 138 | private void appendMethod(HttpRequest request, StringBuilder toSign) { |
| 139 | toSign.append(request.getMethod()).append("\n"); |
| 140 | } |
| 141 | |
| 142 | private void appendCanonicalizedHeaders(HttpRequest request, StringBuilder toSign) { |
| 143 | // TreeSet == Sort the headers alphabetically. |
| 144 | Set<String> headers = new TreeSet<String>(request.getHeaders().keySet()); |
| 145 | for (String header : headers) { |
| 146 | if (header.startsWith("x-emc-") && !header.equals(AtmosHeaders.SIGNATURE)) { |
| 147 | // Convert all header names to lowercase. |
| 148 | toSign.append(header.toLowerCase()).append(":"); |
| 149 | // For headers with values that span multiple lines, convert them into one line by |
| 150 | // replacing any |
| 151 | // newline characters and extra embedded white spaces in the value. |
| 152 | for (String value : request.getHeaders().get(header)) { |
| 153 | value = Strings2.replaceAll(value, TWO_SPACE_PATTERN, " "); |
| 154 | value = Strings2.replaceAll(value, NEWLINE_PATTERN, ""); |
| 155 | toSign.append(value).append(' '); |
| 156 | } |
| 157 | toSign.deleteCharAt(toSign.lastIndexOf(" ")); |
| 158 | // Concatenate all headers together, using newlines (\n) separating each header from the |
| 159 | // next one. |
| 160 | toSign.append("\n"); |
| 161 | } |
| 162 | } |
| 163 | // There should be no terminating newline character at the end of the last header. |
| 164 | if (toSign.charAt(toSign.length() - 1) == '\n') |
| 165 | toSign.deleteCharAt(toSign.length() - 1); |
| 166 | } |
| 167 | |
| 168 | private void appendPayloadMetadata(HttpRequest request, StringBuilder buffer) { |
| 169 | buffer.append( |
| 170 | utils.valueOrEmpty(request.getPayload() == null ? null : request.getPayload().getContentMetadata() |
| 171 | .getContentType())).append("\n"); |
| 172 | } |
| 173 | |
| 174 | @VisibleForTesting |
| 175 | void appendHttpHeaders(HttpRequest request, StringBuilder toSign) { |
| 176 | // Only the value is used, not the header |
| 177 | // name. If a request does not include the header, this is an empty string. |
| 178 | for (String header : new String[] { "Range" }) |
| 179 | toSign.append(utils.valueOrEmpty(request.getHeaders().get(header)).toLowerCase()).append("\n"); |
| 180 | // Standard HTTP header, in UTC format. Only the date value is used, not the header name. |
| 181 | toSign.append(request.getFirstHeaderOrNull(HttpHeaders.DATE)).append("\n"); |
| 182 | } |
| 183 | |
| 184 | @VisibleForTesting |
| 185 | void appendCanonicalizedResource(HttpRequest request, StringBuilder toSign) { |
| 186 | // Path portion of the HTTP request URI, in lowercase. |
| 187 | toSign.append(request.getEndpoint().getRawPath().toLowerCase()).append("\n"); |
| 188 | } |
| 189 | |
| 190 | } |