All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.jsign.jca.AmazonCredentials Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2023 Emmanuel Bourg
 *
 * 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.
 */

package net.jsign.jca;

import java.io.IOException;

/**
 * AWS credentials
 *
 * @since 5.0
 */
public class AmazonCredentials {

    private final String accessKey;
    private final String secretKey;
    private final String sessionToken;

    public AmazonCredentials(String accessKey, String secretKey, String sessionToken) {
        this.accessKey = accessKey;
        this.secretKey = secretKey;
        this.sessionToken = sessionToken;
    }

    public String getAccessKey() {
        return accessKey;
    }

    public String getSecretKey() {
        return secretKey;
    }

    public String getSessionToken() {
        return sessionToken;
    }

    /**
     * Parses the concatenated AWS credentials
     *
     * @param credentials accessKey|secretKey|sessionToken (the session token is optional)
     */
    public static AmazonCredentials parse(String credentials) throws IllegalArgumentException {
        // parse the credentials
        String[] elements = credentials.split("\\|", 3);
        if (elements.length < 2) {
            throw new IllegalArgumentException("Invalid AWS credentials: " + credentials);
        }
        String accessKey = elements[0];
        String secretKey = elements[1];
        String sessionToken = elements.length > 2 ? elements[2] : null;

        return new AmazonCredentials(accessKey, secretKey, sessionToken);   
    }

    /**
     * Returns the default AWS credentials, fetched from the following sources in order:
     * 
    *
  • The environment variables AWS_ACCESS_KEY_ID (or AWS_ACCESS_KEY), * AWS_SECRET_KEY (or AWS_SECRET_ACCESS_KEY) and AWS_SESSION_TOKEN
  • *
  • The EC2 instance metadata service (IMDSv2)
  • *
*/ public static AmazonCredentials getDefault() throws IOException { if (getenv("AWS_ACCESS_KEY_ID") != null || getenv("AWS_ACCESS_KEY") != null) { String accesKey = getenv("AWS_ACCESS_KEY_ID"); if (accesKey == null) { accesKey = getenv("AWS_ACCESS_KEY"); } String secretKey = getenv("AWS_SECRET_KEY"); if (secretKey == null) { secretKey = getenv("AWS_SECRET_ACCESS_KEY"); } String sessionToken = getenv("AWS_SESSION_TOKEN"); return new AmazonCredentials(accesKey, secretKey, sessionToken); } else { return new AmazonIMDS2Client().getCredentials(); } } static String getenv(String name) { return System.getenv(name); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy