Search in sources :

Example 1 with ProfilesConfigFile

use of com.amazonaws.auth.profile.ProfilesConfigFile in project openhab1-addons by openhab.

the class DynamoDBConfig method fromConfig.

/**
     *
     * @param config persistence service configuration
     * @return DynamoDB configuration. Returns null in case of configuration errors
     */
public static DynamoDBConfig fromConfig(Map<String, Object> config) {
    if (config == null || config.isEmpty()) {
        logger.error("Configuration not provided! At least AWS region and credentials must be provided.");
        return null;
    }
    try {
        String regionName = (String) config.get("region");
        if (isBlank(regionName)) {
            invalidRegionLogHelp(regionName);
            return null;
        }
        final Region region;
        try {
            region = Region.getRegion(Regions.fromName(regionName));
        } catch (IllegalArgumentException e) {
            invalidRegionLogHelp(regionName);
            return null;
        }
        AWSCredentials credentials;
        String accessKey = (String) config.get("accessKey");
        String secretKey = (String) config.get("secretKey");
        if (!isBlank(accessKey) && !isBlank(secretKey)) {
            logger.debug("accessKey and secretKey specified. Using those.");
            credentials = new BasicAWSCredentials(accessKey, secretKey);
        } else {
            logger.debug("accessKey and/or secretKey blank. Checking profilesConfigFile and profile.");
            String profilesConfigFile = (String) config.get("profilesConfigFile");
            String profile = (String) config.get("profile");
            if (isBlank(profilesConfigFile) || isBlank(profile)) {
                logger.error("Specify either 1) accessKey and secretKey; or 2) profilesConfigFile and " + "profile for providing AWS credentials");
                return null;
            }
            credentials = new ProfilesConfigFile(profilesConfigFile).getCredentials(profile);
        }
        String table = (String) config.get("tablePrefix");
        if (isBlank(table)) {
            logger.debug("Using default table name {}", DEFAULT_TABLE_PREFIX);
            table = DEFAULT_TABLE_PREFIX;
        }
        final boolean createTable;
        String createTableParam = (String) config.get("createTable");
        if (isBlank(createTableParam)) {
            logger.debug("Creating table on demand: {}", DEFAULT_CREATE_TABLE_ON_DEMAND);
            createTable = DEFAULT_CREATE_TABLE_ON_DEMAND;
        } else {
            createTable = Boolean.parseBoolean(createTableParam);
        }
        final long readCapacityUnits;
        String readCapacityUnitsParam = (String) config.get("readCapacityUnits");
        if (isBlank(readCapacityUnitsParam)) {
            logger.debug("Read capacity units: {}", DEFAULT_READ_CAPACITY_UNITS);
            readCapacityUnits = DEFAULT_READ_CAPACITY_UNITS;
        } else {
            readCapacityUnits = Long.parseLong(readCapacityUnitsParam);
        }
        final long writeCapacityUnits;
        String writeCapacityUnitsParam = (String) config.get("writeCapacityUnits");
        if (isBlank(writeCapacityUnitsParam)) {
            logger.debug("Write capacity units: {}", DEFAULT_WRITE_CAPACITY_UNITS);
            writeCapacityUnits = DEFAULT_WRITE_CAPACITY_UNITS;
        } else {
            writeCapacityUnits = Long.parseLong(writeCapacityUnitsParam);
        }
        return new DynamoDBConfig(region, credentials, table, createTable, readCapacityUnits, writeCapacityUnits);
    } catch (Exception e) {
        logger.error("Error with configuration", e);
        return null;
    }
}
Also used : Region(com.amazonaws.regions.Region) ProfilesConfigFile(com.amazonaws.auth.profile.ProfilesConfigFile) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) AWSCredentials(com.amazonaws.auth.AWSCredentials) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials)

Example 2 with ProfilesConfigFile

use of com.amazonaws.auth.profile.ProfilesConfigFile in project intellij-idea-plugin-connector-for-aws-lambda by satr.

the class FunctionConnectorModel method getCredentialProfiles.

public OperationValueResult<List<CredentialProfileEntity>> getCredentialProfiles() {
    OperationValueResult<List<CredentialProfileEntity>> valueResult = new OperationValueResultImpl<>();
    ArrayList<CredentialProfileEntity> credentialProfilesEntries = new ArrayList<>();
    valueResult.setValue(credentialProfilesEntries);
    if (!validateCredentialProfilesExist()) {
        valueResult.addWarning("No credential profiles file found.\n To create one - please follow the instruction:\n https://docs.aws.amazon.com/cli/latest/userguide/cli-multiple-profiles.html ");
        return valueResult;
    }
    ProfilesConfigFile profilesConfigFile = new ProfilesConfigFile();
    Map<String, BasicProfile> profiles = profilesConfigFile.getAllBasicProfiles();
    for (String credentialProfileName : profiles.keySet()) {
        credentialProfilesEntries.add(new CredentialProfileEntity(credentialProfileName, profiles.get(credentialProfileName)));
    }
    valueResult.addInfo("Found %d credential profiles.", credentialProfilesEntries.size());
    return valueResult;
}
Also used : OperationValueResultImpl(io.github.satr.common.OperationValueResultImpl) BasicProfile(com.amazonaws.auth.profile.internal.BasicProfile) ProfilesConfigFile(com.amazonaws.auth.profile.ProfilesConfigFile)

Aggregations

ProfilesConfigFile (com.amazonaws.auth.profile.ProfilesConfigFile)2 AWSCredentials (com.amazonaws.auth.AWSCredentials)1 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)1 BasicProfile (com.amazonaws.auth.profile.internal.BasicProfile)1 Region (com.amazonaws.regions.Region)1 OperationValueResultImpl (io.github.satr.common.OperationValueResultImpl)1