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  public class CodeCommitRequestSigner extends RequestSignerBase {
27    final String repoName;
28  
29    public CodeCommitRequestSigner(AWSCredentialsProvider credentialsProvider, String repoName, Date date) {
30      super(credentialsProvider.getCredentials(), "us-east-1", "codecommit", date);
31      this.repoName = repoName;
32    }
33  
34    public String getPushUrl() {
35      String user = awsCredentials.getAWSAccessKeyId();
36  
37      String host = "git-codecommit.us-east-1.amazonaws.com";
38  
39      String path = "/v1/repos/" + repoName;
40  
41      String scope = String.format("%s/%s/%s/%s", strDate, region, service, TERMINATOR);
42  
43      StringBuilder stringToSign = new StringBuilder();
44  
45      stringToSign.append(String.format("%s-%s\n%s\n%s\n", SCHEME, AWS_ALGORITHM, strDateTime, scope));
46  
47      stringToSign.append(DigestUtils.sha256Hex(String.format("GIT\n%s\n\nhost:%s\n\nhost\n", path, host).getBytes()));
48  
49      byte[] key = deriveKey();
50  
51      byte[] digest = hash(key, stringToSign.toString());
52  
53      String signature = Hex.encodeHexString(digest);
54  
55      String password = strDateTime.concat("Z").concat(signature);
56  
57      String returnUrl = String.format("https://%s:%s@%s%s", user, password, host, path);
58  
59      return returnUrl;
60    }
61  }