summaryrefslogtreecommitdiff
path: root/src/main/java/com/p4square/grow/config/ConfigCredentialProvider.java
blob: 06abd4837b412836648e6f4433609c28f8b5ddff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.p4square.grow.config;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;

/**
 * AWSCredentials credentials backed by config.
 *
 * Falls back to DefaultAWSCredentialsProviderChain if the credentials are not in the config.
 */
public class ConfigCredentialProvider implements AWSCredentials {

    private AWSCredentials mCredentials;

    public ConfigCredentialProvider(final Config config) {
        String awsAccessKey = config.getString("awsAccessKey");
        if (awsAccessKey != null) {
            mCredentials = new AWSCredentials() {
                @Override
                public String getAWSAccessKeyId() {
                    return config.getString("awsAccessKey");
                }

                @Override
                public String getAWSSecretKey() {
                    return config.getString("awsSecretKey");
                }
            };
        } else {
            mCredentials = new DefaultAWSCredentialsProviderChain().getCredentials();
        }
    }

    @Override
    public String getAWSAccessKeyId() {
        return mCredentials.getAWSAccessKeyId();
    }

    @Override
    public String getAWSSecretKey() {
        return mCredentials.getAWSSecretKey();
    }
}