br.com.ingenieux.jenkins.plugins.codecommit.RequestSignerBase.java Source code

Java tutorial

Introduction

Here is the source code for br.com.ingenieux.jenkins.plugins.codecommit.RequestSignerBase.java

Source

package br.com.ingenieux.jenkins.plugins.codecommit;

/*
 * Copyright (c) 2016 ingenieux Labs
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import com.amazonaws.auth.AWSCredentials;

import org.apache.commons.codec.binary.Hex;

import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.SimpleTimeZone;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

@SuppressFBWarnings({ "STCAL_STATIC_SIMPLE_DATE_FORMAT_INSTANCE", "STCAL_INVOKE_ON_STATIC_DATE_FORMAT_INSTANCE" })
public class RequestSignerBase {
    public static final SimpleDateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyyMMdd'T'HHmmss");

    public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");

    protected static final String AWS_ALGORITHM = "HMAC-SHA256";

    protected static final String TERMINATOR = "aws4_request";

    protected static final String SCHEME = "AWS4";

    protected static final Charset DEFAULT_CHARSET = Charset.forName("ASCII");

    static {
        SimpleTimeZone timezone = new SimpleTimeZone(0, "UTC");

        DATE_TIME_FORMAT.setTimeZone(timezone);
        DATE_FORMAT.setTimeZone(timezone);
    }

    final AWSCredentials awsCredentials;

    final String region;

    final String service;

    final Date date;

    final String strDate;

    final String strDateTime;

    protected RequestSignerBase(AWSCredentials awsCredentials, String region, String service, Date date) {
        this.awsCredentials = awsCredentials;
        this.region = region;
        this.service = service;
        this.date = date;
        this.strDate = DATE_FORMAT.format(date);
        this.strDateTime = DATE_TIME_FORMAT.format(date);
    }

    protected byte[] deriveKey() {
        String secret = SCHEME.concat(awsCredentials.getAWSSecretKey());
        byte[] kSecret = secret.getBytes(DEFAULT_CHARSET);
        byte[] kDate = hash(kSecret, strDate);
        byte[] kRegion = hash(kDate, region);
        byte[] kService = hash(kRegion, service);
        byte[] key = hash(kService, TERMINATOR);
        return key;
    }

    protected byte[] hash(byte[] kSecret, String obj) {
        try {
            SecretKeySpec keySpec = new SecretKeySpec(kSecret, "HmacSHA256");

            Mac mac = Mac.getInstance("HmacSHA256");

            mac.init(keySpec);

            return mac.doFinal(obj.getBytes(DEFAULT_CHARSET));
        } catch (Exception exc) {
            throw new RuntimeException(exc);
        }
    }

    protected String hexEncode(String obj) {
        return Hex.encodeHexString(obj.getBytes(DEFAULT_CHARSET));
    }
}