View Javadoc
1   /*
2    * Copyright (c) 2016 ingenieux Labs
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 br.com.ingenieux.mojo.beanstalk.bundle;
18  
19  import com.amazonaws.auth.AWSCredentialsProvider;
20  
21  import org.apache.commons.codec.binary.Hex;
22  import org.apache.commons.codec.digest.DigestUtils;
23  
24  import java.util.Date;
25  
26  import static org.apache.commons.lang.StringUtils.isNotBlank;
27  
28  public class RequestSigner extends RequestSignerBase {
29    String applicationId;
30  
31    String commitId;
32  
33    String environmentName;
34  
35    Date date;
36  
37    public RequestSigner(AWSCredentialsProvider awsCredentials, String applicationId, String region, String commitId, String environmentName, Date date) {
38      super(awsCredentials.getCredentials(), region, "devtools", date);
39      this.applicationId = applicationId;
40      this.commitId = commitId;
41      this.environmentName = environmentName;
42    }
43  
44    public String getPushUrl() {
45      String user = awsCredentials.getAWSAccessKeyId();
46  
47      String host = String.format("git.elasticbeanstalk.%s.amazonaws.com", region);
48  
49      String path = String.format("/v1/repos/%s/commitid/%s", hexEncode(applicationId), hexEncode(commitId));
50  
51      if (isNotBlank(environmentName)) {
52        path += String.format("/environment/%s", hexEncode(environmentName));
53      }
54  
55      String scope = String.format("%s/%s/%s/%s", strDate, region, service, TERMINATOR);
56  
57      StringBuilder stringToSign = new StringBuilder();
58  
59      stringToSign.append(String.format("%s-%s\n%s\n%s\n", SCHEME, AWS_ALGORITHM, strDateTime, scope));
60  
61      stringToSign.append(DigestUtils.sha256Hex(String.format("GIT\n%s\n\nhost:%s\n\nhost\n", path, host).getBytes()));
62  
63      byte[] key = deriveKey();
64  
65      byte[] digest = hash(key, stringToSign.toString());
66  
67      String signature = Hex.encodeHexString(digest);
68  
69      String password = strDateTime.concat("Z").concat(signature);
70  
71      String returnUrl = String.format("https://%s:%s@%s%s", user, password, host, path);
72  
73      return returnUrl;
74    }
75  }