com.amazonaws.regions.Region

Here are the examples of the java api com.amazonaws.regions.Region taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

124 Examples 7

19 Source : RegionEntity.java
with MIT License
from satr

public clreplaced RegionEnreplacedy {

    private final String name;

    private final Region region;

    private final String description;

    public RegionEnreplacedy(Region region, String description) {
        this.name = region.getName();
        this.region = region;
        this.description = description;
    }

    public Region getRegion() {
        return region;
    }

    @Override
    public String toString() {
        return String.format("%s : %s", name, description);
    }

    public String getName() {
        return name;
    }
}

19 Source : GenericApiGatewayClientBuilder.java
with Apache License 2.0
from rpgreen

public GenericApiGatewayClientBuilder withRegion(Region region) {
    this.region = region;
    return this;
}

19 Source : KmsClientFactoryTest.java
with Apache License 2.0
from Nike-Inc

/**
 * Tests the KmsClientFactory clreplaced
 */
public clreplaced KmsClientFactoryTest {

    private static final String goodRegionName = "us-west-2";

    private final Region goodRegion = Region.getRegion(Regions.fromName(goodRegionName));

    private static final String badRegionName = "zz-space-1";

    private KmsClientFactory subject;

    @Before
    public void setup() {
        subject = new KmsClientFactory();
    }

    @Test
    public void get_client_by_region_returns_configured_kms_client() {
        AWSKMSClient client = subject.getClient(goodRegion);
        replacedertThat(client).isNotNull();
    }

    @Test
    public void get_client_by_region_string_returns_configured_kms_client() {
        AWSKMSClient client = subject.getClient(goodRegionName);
        replacedertThat(client).isNotNull();
    }

    @Test(expected = ApiException.clreplaced)
    public void get_client_by_region_string_throws_exception_if_bad_region_preplaceded() {
        subject.getClient(badRegionName);
    }
}

19 Source : EncryptionService.java
with Apache License 2.0
from Nike-Inc

/**
 * Initialize a Multi-KMS-MasterKeyProvider.
 *
 * <p>For encrypt, KMS in all regions must be available. For decrypt, KMS in at least one region
 * must be available.
 */
public static MasterKeyProvider<KmsMasterKey> initializeKeyProvider(String cmkArns, Region currentRegion) {
    return initializeKeyProvider(splitArns(cmkArns), currentRegion);
}

19 Source : AWSKafkaConfig.java
with BSD 2-Clause "Simplified" License
from datamachines

public void setRegion(Region region) {
    this.region = region;
}

19 Source : AmazonTestWebserviceClient.java
with Apache License 2.0
from awspring

/**
 * Test stub used by {@link AmazonWebserviceClientConfigurationUtilsTest}
 *
 * @author Agim Emruli
 * @author Eddú Meléndez
 */
clreplaced AmazonTestWebserviceClient extends AmazonWebServiceClient {

    private Region region;

    AmazonTestWebserviceClient(AWSCredentialsProvider awsCredentialsProvider) {
        super(SpringCloudClientConfiguration.getClientConfiguration());
        notNull(awsCredentialsProvider, "CredentialsProvider must not be null");
    }

    Region getRegion() {
        return this.region;
    }

    @SuppressWarnings("deprecation")
    @Override
    public void setRegion(Region region) {
        this.region = region;
    }
}

19 Source : StaticRegionProvider.java
with Apache License 2.0
from awspring

/**
 * Static {@link RegionProvider} implementation that can used to statically configure a
 * region. The region could be provided through a configuration file at configuration
 * time.
 *
 * @author Agim Emruli
 * @since 1.0
 */
public clreplaced StaticRegionProvider implements RegionProvider {

    private final Region configuredRegion;

    /**
     * Constructs and configures the static region for this RegionProvider implementation.
     * @param configuredRegion - the region that will be statically returned in
     * {@link #getRegion()}
     */
    @RuntimeUse
    public StaticRegionProvider(String configuredRegion) {
        try {
            this.configuredRegion = Region.getRegion(Regions.fromName(configuredRegion));
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException("The region '" + configuredRegion + "' is not a valid region!", e);
        }
    }

    /**
     * Return the configured Region configured at construction time.
     * @return the configured region, for every call the same
     */
    @Override
    public Region getRegion() {
        return this.configuredRegion;
    }
}

18 Source : AbstractS3Processor.java
with Apache License 2.0
from wangrenlei

protected String getUrlForObject(final String bucket, final String key) {
    Region region = getRegion();
    if (region == null) {
        return DEFAULT_PROTOCOL.toString() + "://s3.amazonaws.com/" + bucket + "/" + key;
    } else {
        final String endpoint = region.getServiceEndpoint("s3");
        return DEFAULT_PROTOCOL.toString() + "://" + endpoint + "/" + bucket + "/" + key;
    }
}

18 Source : AwsCurrentRegionHolder.java
with Apache License 2.0
from trinodb

/**
 * @throws IllegalStateException when no region is resolved to avoid memoizing a transient failure
 */
private static Region loadCurrentRegionOrThrowOnNull() throws IllegalStateException {
    Region result = Regions.getCurrentRegion();
    if (result == null) {
        throw new IllegalStateException("Failed to resolve current AWS region from EC2 metadata");
    }
    return result;
}

18 Source : SNSInventoryUtilTest.java
with Apache License 2.0
from tmobile

/**
 * Gets the regions.
 *
 * @return the regions
 */
private List<Region> getRegions() {
    List<Region> regions = new ArrayList<>();
    Region region = new Region(new RegionImpl() {

        @Override
        public boolean isServiceSupported(String serviceName) {
            return false;
        }

        @Override
        public boolean hasHttpsEndpoint(String serviceName) {
            return false;
        }

        @Override
        public boolean hasHttpEndpoint(String serviceName) {
            return false;
        }

        @Override
        public String getServiceEndpoint(String serviceName) {
            return null;
        }

        @Override
        public String getParreplacedion() {
            return null;
        }

        @Override
        public String getName() {
            return "north";
        }

        @Override
        public String getDomain() {
            return null;
        }

        @Override
        public Collection<String> getAvailableEndpoints() {
            return null;
        }
    });
    regions.add(region);
    return regions;
}

18 Source : AwsClientFactory.java
with Apache License 2.0
from pinterest

public static AmazonS3Client createS3Client(Region region) {
    Preconditions.checkNotNull(region);
    AmazonS3Client s3Client = new AmazonS3Client(getCredentialProvider());
    s3Client.setRegion(region);
    return s3Client;
}

18 Source : AwsClientFactory.java
with Apache License 2.0
from pinterest

public static AmazonSQSClient createSQSClient(Region region) {
    Preconditions.checkNotNull(region);
    AmazonSQSClient client = new AmazonSQSClient(getCredentialProvider());
    client.setRegion(region);
    return client;
}

18 Source : EncryptionService.java
with Apache License 2.0
from Nike-Inc

private CryptoMaterialsManager getCryptoMaterialsManager(List<String> cmkArns, Region currentRegion) {
    if (cmkArnList.containsAll(cmkArns)) {
        return decryptCryptoMaterialsManager;
    } else {
        MasterKeyProvider<KmsMasterKey> provider = initializeKeyProvider(cmkArns, currentRegion);
        return new DefaultCryptoMaterialsManager(provider);
    }
}

18 Source : AmazonSNSMock.java
with Apache License 2.0
from newrelic

@Override
public void setRegion(Region region) {
}

18 Source : AmazonS3ClientMock.java
with Apache License 2.0
from minio

@Override
public void setRegion(Region region) throws IllegalArgumentException {
    throw new UnsupportedOperationException();
}

18 Source : ClientHelper.java
with MIT License
from jenkinsci

private static Region getRegionFromString(String regionName) {
    // In 0.7, selregion comes from Regions#name
    Region region = RegionUtils.getRegion(regionName);
    // In 0.6, selregion comes from Regions#valueOf
    if (region == null) {
        region = RegionUtils.getRegion(Regions.valueOf(regionName).getName());
    }
    return region;
}

18 Source : AmazonTestWebserviceClient.java
with Apache License 2.0
from awspring

@SuppressWarnings("deprecation")
@Override
public void setRegion(Region region) {
    this.region = region;
}

18 Source : Ec2MetadataRegionProvider.java
with Apache License 2.0
from awspring

@Override
public Region getRegion() {
    Region currentRegion = getCurrentRegion();
    replacedert.state(currentRegion != null, "There is no EC2 meta data available, because the application is not running " + "in the EC2 environment. Region detection is only possible if the application is running on a EC2 instance");
    return currentRegion;
}

18 Source : Loader.java
with Apache License 2.0
from amazon-archives

public void teardown(String stack, Region region) throws SQLException {
    CloudFormationManager cf = new CloudFormationManager(region);
    cf.terminateStack(stack);
    spark.stop();
}

17 Source : AwsConfig.java
with Apache License 2.0
from shinesolutions

@Bean
public Region awsRegion() {
    Region region;
    if (regionString != null && !regionString.isEmpty()) {
        region = RegionUtils.getRegion(regionString);
    } else {
        AwsRegionProvider regionProvider = new DefaultAwsRegionProviderChain();
        region = RegionUtils.getRegion(regionProvider.getRegion());
    }
    if (region == null) {
        throw new BeanInitializationException("Unable to determine AWS region");
    }
    return region;
}

17 Source : KMSEncryptor.java
with Apache License 2.0
from schibsted

protected KmsMasterKeyProvider getProvider() {
    if (!prov.isPresent()) {
        Region region = RegionUtils.getRegion(groupIdentifier.region.getName());
        prov = Optional.of(new KmsMasterKeyProvider(awsCredentials, region, transformAndVerifyOrThrow(clientConfiguration), getKeyArn()));
    }
    return prov.get();
}

17 Source : FunctionConnectorModel.java
with MIT License
from satr

public List<RegionEnreplacedy> getRegions() {
    if (regionEntries != null) {
        return regionEntries;
    }
    regionEntries = new ArrayList<>();
    for (Region region : RegionUtils.getRegions()) {
        String description = regionDescriptions.get(region.getName());
        if (description != null)
            regionEntries.add(new RegionEnreplacedy(region, description));
    }
    return regionEntries;
}

17 Source : DailyInstanceCountPerTypeJob.java
with Apache License 2.0
from pinterest

public clreplaced DailyInstanceCountPerTypeJob implements Callable<Boolean> {

    private static final Logger logger = LoggerFactory.getLogger(DailyInstanceCountPerTypeJob.clreplaced);

    private final CloudInstanceStore cloudInstanceStore;

    private final InstanceCounterStore instanceCounterStore;

    private final Region region;

    public DailyInstanceCountPerTypeJob(CloudInstanceStore cloudInstanceStore, EsInstanceCounterStore instanceCounterStore, Region region) {
        Preconditions.checkNotNull(cloudInstanceStore);
        Preconditions.checkNotNull(instanceCounterStore);
        Preconditions.checkNotNull(region);
        this.cloudInstanceStore = cloudInstanceStore;
        this.instanceCounterStore = instanceCounterStore;
        this.region = region;
    }

    @Override
    public Boolean call() throws Exception {
        OperationStats operationStats = new OperationStats("job", "DailyInstanceCountPerTypeJob");
        Boolean ret = calculateDailyInstanceCounts();
        if (ret) {
            logger.info("DailyInstanceCountPerTypeJob completed successfully");
            operationStats.succeed();
        } else {
            logger.error("DailyInstanceCountPerTypeJob failed");
            operationStats.failed();
        }
        return ret;
    }

    private Boolean calculateDailyInstanceCounts() {
        try {
            DateTime utcNow = DateTime.now(DateTimeZone.UTC);
            List<Instance> instances = cloudInstanceStore.getInstances(region);
            List<ReservedInstances> reservedInstances = cloudInstanceStore.getReservedInstances(region);
            // Generate instance counts per type per Availability zone
            List<EsInstanceCountRecord> instanceCountRecords = getInstanceCountRecords(instances, reservedInstances, utcNow);
            logger.info("Number of instance count records {}", instanceCountRecords.size());
            // Insert records into soundwave store.
            instanceCounterStore.bulkInsert(instanceCountRecords);
            logger.info("Bulk insert succeeded for instance count records");
            return true;
        } catch (Exception e) {
            logger.error(ExceptionUtils.getRootCauseMessage(e));
            return false;
        }
    }

    /**
     * Get instance count records for a specfic zone.
     *
     * @param instances         a list of active instances
     * @param reservedInstances a list of active reservations
     * @return a list of instancecount records for that zone.
     * @throws Exception
     */
    public List<EsInstanceCountRecord> getInstanceCountRecords(List<Instance> instances, List<ReservedInstances> reservedInstances, DateTime utcNow) throws Exception {
        List<EsInstanceCountRecord> ret = new ArrayList<>();
        Map<String, Map<String, Integer>> instanceTypeCounter = new HashMap<>();
        // Process and add all zones Running instances first.
        for (Instance instance : instances) {
            String instanceType = instance.getInstanceType();
            boolean isSpotInstance = StringUtils.equalsIgnoreCase(instance.getInstanceLifecycle(), InstanceLifecycleType.Spot.toString());
            if (instanceTypeCounter.containsKey(instanceType)) {
                instanceTypeCounter.get(instanceType).put("active", instanceTypeCounter.get(instanceType).get("active") + 1);
                if (isSpotInstance) {
                    instanceTypeCounter.get(instanceType).put("spot", instanceTypeCounter.get(instanceType).get("spot") + 1);
                }
            } else {
                // Instance type doesnot exist in counter. initialize its values
                instanceTypeCounter.put(instanceType, new HashMap<>());
                instanceTypeCounter.get(instanceType).put("active", 1);
                instanceTypeCounter.get(instanceType).put("reserved", 0);
                instanceTypeCounter.get(instanceType).put("ondemand", 0);
                instanceTypeCounter.get(instanceType).put("unused", 0);
                if (isSpotInstance) {
                    instanceTypeCounter.get(instanceType).put("spot", 1);
                } else {
                    instanceTypeCounter.get(instanceType).put("spot", 0);
                }
            }
        }
        // Process all reserved instances here
        for (ReservedInstances instance : reservedInstances) {
            String instanceType = instance.getInstanceType();
            int instanceCount = instance.getInstanceCount();
            boolean isActive = StringUtils.equalsIgnoreCase(instance.getState(), ReservedInstanceState.Active.toString());
            if (isActive) {
                instanceTypeCounter.get(instanceType).put("reserved", instanceTypeCounter.get(instanceType).get("reserved") + instanceCount);
            }
        }
        // For each instance type in a given zone create one count record
        for (String instanceType : instanceTypeCounter.keySet()) {
            int onDemandCount = instanceTypeCounter.get(instanceType).get("active") - instanceTypeCounter.get(instanceType).get("reserved") - instanceTypeCounter.get(instanceType).get("spot");
            if (onDemandCount < 0) {
                // Reserved instances are unused
                instanceTypeCounter.get(instanceType).put("unused", Math.abs(onDemandCount));
                instanceTypeCounter.get(instanceType).put("ondemand", 0);
            } else {
                instanceTypeCounter.get(instanceType).put("unused", 0);
                instanceTypeCounter.get(instanceType).put("ondemand", onDemandCount);
            }
            // Unused is set to 0. No way to get how many we have bought.
            EsInstanceCountRecord record = new EsInstanceCountRecord(region.toString(), instanceType, instanceTypeCounter.get(instanceType).get("reserved"), instanceTypeCounter.get(instanceType).get("active"), instanceTypeCounter.get(instanceType).get("spot"), instanceTypeCounter.get(instanceType).get("ondemand"), instanceTypeCounter.get(instanceType).get("unused"), utcNow.toDate());
            ret.add(record);
        }
        return ret;
    }
    // End of clreplaced
}

17 Source : EsInstanceStore.java
with Apache License 2.0
from pinterest

@Override
public Iterator<EsInstance> getRunningInstances(Region region) throws Exception {
    return getRunningInstances(region, EsMapper.getIncludeFields(getInstanceClreplaced()));
}

17 Source : AwsClientFactory.java
with Apache License 2.0
from pinterest

public static AmazonEC2Client createEC2Client(Region region) {
    Preconditions.checkNotNull(region);
    AmazonEC2Client client = new AmazonEC2Client(getCredentialProvider());
    client.setRegion(region);
    return client;
}

17 Source : EncryptionService.java
with Apache License 2.0
from Nike-Inc

/**
 * Decrypt the encryptedPayload.
 *
 * @param parsedCiphertext encryptedPayload
 */
public static String decrypt(ParsedCiphertext parsedCiphertext, AwsCrypto awsCrypto, Region currentRegion) {
    // Parses the ARNs out of the encryptedPayload so that you can manually rotate the CMKs, if
    // desired
    // Whatever CMKs were used in the encrypt operation will be used to decrypt
    List<String> cmkArns = CiphertextUtils.getCustomerMasterKeyArns(parsedCiphertext);
    MasterKeyProvider<KmsMasterKey> decryptProvider = initializeKeyProvider(cmkArns, currentRegion);
    return new String(awsCrypto.decryptData(decryptProvider, parsedCiphertext).getResult(), StandardCharsets.UTF_8);
}

17 Source : EncryptionService.java
with Apache License 2.0
from Nike-Inc

/**
 * Initialize a Multi-KMS-MasterKeyProvider.
 *
 * <p>For encrypt, KMS in all regions must be available. For decrypt, KMS in at least one region
 * must be available.
 */
public static MasterKeyProvider<KmsMasterKey> initializeKeyProvider(List<String> cmkArns, Region currentRegion) {
    List<MasterKeyProvider<KmsMasterKey>> providers = getSortedArnListByCurrentRegion(cmkArns, currentRegion).stream().map(KmsMasterKeyProvider::new).collect(Collectors.toList());
    return (MasterKeyProvider<KmsMasterKey>) MultipleProviderFactory.buildMultiProvider(providers);
}

17 Source : KmsClientFactory.java
with Apache License 2.0
from Nike-Inc

/**
 * Returns a KMS client for the given region name. Clients are cached by region.
 *
 * @param regionName Region to configure a client for
 * @return AWS KMS client
 */
public AWSKMSClient getClient(String regionName) {
    try {
        final Region region = Region.getRegion(Regions.fromName(regionName));
        return getClient(region);
    } catch (IllegalArgumentException iae) {
        throw ApiException.newBuilder().withApiErrors(DefaultApiError.AUTHENTICATION_ERROR_INVALID_REGION).withExceptionCause(iae.getCause()).withExceptionMessage("Specified region is not valid.").build();
    }
}

17 Source : S3RegionalResource.java
with MIT License
from ghnor

public clreplaced S3RegionalResource {

    private static final Pattern REGIONAL_ENDPOINT_PATTERN = Pattern.compile("^s3:\\/\\/(.+)?\\.s3[.-]([a-z0-9-]+)\\.amazonaws\\.com(\\.[a-z]+)?\\/(.+)");

    private static final Region DEFAULT_REGION = Region.getRegion(Regions.US_EAST_1);

    private final URI uri;

    private Region region;

    private String bucketName;

    private String key;

    public S3RegionalResource(URI uri) {
        this.uri = uri;
        configure();
    }

    public Region getRegion() {
        return region;
    }

    public String getBucketName() {
        return bucketName;
    }

    public String getKey() {
        return key;
    }

    private void configure() {
        Matcher matcher = REGIONAL_ENDPOINT_PATTERN.matcher(uri.toString());
        if (matcher.find()) {
            String bucketName = matcher.group(1);
            String region = matcher.group(2);
            String key = matcher.group(4);
            Region derivedRegion;
            if (region.equals("external-1")) {
                derivedRegion = Region.getRegion(Regions.US_EAST_1);
            } else {
                derivedRegion = RegionUtils.getRegion(region);
            }
            this.region = derivedRegion;
            this.bucketName = bucketName;
            this.key = key;
        } else {
            this.region = DEFAULT_REGION;
            this.bucketName = getBucketName(uri.getHost());
            this.key = getS3BucketKey(uri);
        }
    }

    private String getS3BucketKey(URI destination) {
        String path = destination.getPath();
        return path.startsWith("/") ? path.substring(1) : path;
    }

    private String getBucketName(String bucket) {
        return bucket.replaceAll("\\.s3\\.amazonaws\\.com", "").replaceAll("\\.s3-external-1\\.amazonaws\\.com", "");
    }
}

17 Source : AWSKafkaConfig.java
with BSD 2-Clause "Simplified" License
from datamachines

public clreplaced AWSKafkaConfig {

    public static final String DEFAULT_QUEUE_NAME = "SQSJMSClientExampleQueue";

    public static final ArrayList<String> DEFAULT_KAFKA_TOPICS = new ArrayList<>();

    public static final Region DEFAULT_REGION = Region.getRegion(Regions.US_EAST_2);

    public static final String DEFAULT_NA = "NA";

    public static final int MAX_BIN_SIZE = 1024 * 256;

    private static String getParameter(String[] args, int i) {
        if (i + 1 >= args.length) {
            throw new IllegalArgumentException("Missing parameter for " + args[i]);
        }
        return args[i + 1];
    }

    /**
     * Parse the command line and return the resulting config. If the config parsing fails
     * print the error and the usage message and then call System.exit
     *
     * @param app the app to use when printing the usage string
     * @param args the command line arguments
     * @return the parsed config
     */
    public static AWSKafkaConfig parseConfig(String app, String[] args) {
        try {
            return new AWSKafkaConfig(args);
        } catch (IllegalArgumentException e) {
            System.err.println("ERROR: " + e.getMessage());
            System.err.println();
            System.err.println("Usage: " + app + " [--queue <queue>] [--region <region>] [--credentials <credentials>] [--bootstrap.servers <IP:Port>] [--group.id <id>] [--topics <topics>]");
            System.err.println("Usage: " + app + " --queue dmTestQueue.fifo --region us-east-2 --credentials credential.properties --bootstrap.servers localhost:9092 --group.id testGrp1 --topics test1,test2,test3");
            System.exit(-1);
            return null;
        }
    }

    public static void ParamsErrorMessage(String prm, String Option) {
        System.err.println(prm + " is missing from the " + Option + " section in the yaml config files");
    }

    public static AWSKafkaConfig parseYaml(String file) {
        ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
        AWSKafkaYaml cfg = null;
        boolean requiredParmsInYaml = true;
        try {
            cfg = mapper.readValue(new File(file), AWSKafkaYaml.clreplaced);
            System.out.println(ReflectionToStringBuilder.toString(cfg, ToStringStyle.MULTI_LINE_STYLE));
            Map<String, String> aws = cfg.getAwsParms();
            Map<String, String> kafka = cfg.getKafkaParms();
            Map<String, String> data = cfg.getDataProcessing();
            for (String parm : AWSRequireParms) {
                if (aws.get(parm) == null) {
                    requiredParmsInYaml = false;
                    ParamsErrorMessage(parm, "awsParms");
                }
            }
            for (String parm : KafkaRequireParms) {
                if (kafka.get(parm) == null) {
                    requiredParmsInYaml = false;
                    ParamsErrorMessage(parm, "kafkaParms");
                }
            }
            // check conflicting Parameters combinations
            int numEx = 0;
            StringBuilder partErr = new StringBuilder();
            for (String parm : ExclusiveParms) {
                String test = data.get(parm);
                if (test != null && test.equals("true")) {
                    if (numEx > 0) {
                        partErr.append(", ");
                    }
                    partErr.append(parm);
                    numEx++;
                }
            }
            if (numEx > 1) {
                requiredParmsInYaml = false;
                System.err.println(partErr.toString() + " are mutually exclusive.  Only one of them may be set to true");
            }
            // Check Optional Parameters combinations
            String xAES = data.get("AES");
            String xBinAes = data.get("binAES");
            if ((xAES != null && xAES.equals("true")) || (xBinAes != null && xBinAes.equals("true"))) {
                String xAesPw = data.get("AESPW");
                if (xAesPw == null) {
                    requiredParmsInYaml = false;
                    ParamsErrorMessage("AESPW", "AES or binAES");
                } else {
                    String checkKeyRet = AESEncrypt.checkKey(xAesPw);
                    if (checkKeyRet != null) {
                        requiredParmsInYaml = false;
                        System.err.println("Error with AES Key: " + checkKeyRet);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (requiredParmsInYaml)
            return new AWSKafkaConfig(cfg);
        else
            return null;
    }

    enum DataProcessing {

        NA, Base64, AES
    }

    enum XmitReplacement {

        NA, Bin, BinZip, OutTest
    }

    DataProcessing mProcessor = DataProcessing.NA;

    // boolean mUseBin = false;
    XmitReplacement mXmitReplacement = XmitReplacement.NA;

    String AESPreplacedword = null;

    boolean mDisableXmit = false;

    static String[] AWSRequireParms = { "queue", "region", "credentials" };

    static String[] KafkaRequireParms = { "bootstrap.servers", "topics", "group.id" };

    static String[] ExclusiveParms = { "binBase64", "AES", "binAES", "binZip", "outTest" };

    private AWSKafkaConfig(AWSKafkaYaml cfg) {
        Map<String, String> aws = cfg.getAwsParms();
        Map<String, String> kafka = cfg.getKafkaParms();
        Map<String, String> data = cfg.getDataProcessing();
        // Require Parameters
        setQueueName(aws.get("queue"));
        cfgRegion(aws.get("region"));
        setCredentialsProvider(new PropertiesFileCredentialsProvider(aws.get("credentials")));
        kafkaServers = kafka.get("bootstrap.servers");
        kafkaTopics = Arrays.asList(kafka.get("topics").split(","));
        kafkaGrpId = kafka.get("group.id");
        // Optional Parameters
        String depParms = aws.get("dedupPrefix");
        if (depParms != null) {
            queueDedupPrefix = depParms;
        }
        String sBin = data.get("binSize");
        if (sBin != null) {
            try {
                mBinSizeBytes = Integer.valueOf(sBin) * 1024;
            } catch (Throwable e) {
            }
        }
        String sBase64 = data.get("binBase64");
        if (sBase64 != null && sBase64.equals("true")) {
            mProcessor = DataProcessing.Base64;
            mXmitReplacement = XmitReplacement.Bin;
        }
        String disableXmit = aws.get("disableXmit");
        if (disableXmit != null && disableXmit.equals("true")) {
            mDisableXmit = true;
        }
        String xAES = data.get("AES");
        String xBinAes = data.get("binAES");
        if ((xAES != null && xAES.equals("true")) || (xBinAes != null && xBinAes.equals("true"))) {
            String xAesPw = data.get("AESPW");
            if (xAesPw == null) {
                ParamsErrorMessage("AESPW", "AES");
            } else {
                AESPreplacedword = xAesPw;
                mProcessor = DataProcessing.AES;
            }
        }
        if (xBinAes != null && xBinAes.equals("true")) {
            mXmitReplacement = XmitReplacement.Bin;
        }
        String binZip = data.get("binZip");
        if (binZip != null && binZip.equals("true")) {
            mXmitReplacement = XmitReplacement.BinZip;
            mProcessor = DataProcessing.NA;
        }
        String outTest = data.get("outTest");
        if (outTest != null && outTest.equals("true")) {
            mXmitReplacement = XmitReplacement.OutTest;
            mProcessor = DataProcessing.NA;
        }
        String binTime = data.get("binTime");
        if (binTime != null) {
            try {
                mBinTimeInMinutes = Integer.valueOf(binTime);
            } catch (Throwable e) {
            }
        }
    }

    public DataTransformer getPre() {
        DataTransformer mRet = null;
        switch(mProcessor) {
            case Base64:
                mRet = new EncodeBase64();
                break;
            case AES:
                mRet = new AESEncrypt(AESPreplacedword);
                break;
            default:
                mRet = null;
                break;
        }
        return mRet;
    }

    public DataReplaceXmit getReplaceXmit(DataSink inf) {
        DataReplaceXmit ret = null;
        switch(mXmitReplacement) {
            case Bin:
                ret = new BinData(inf, mBinSizeBytes);
                break;
            case BinZip:
                ret = new BinZipData(inf, mBinSizeBytes, mBinTimeInMinutes);
                break;
            case OutTest:
                ret = new OutTest(inf, mBinSizeBytes);
                break;
            default:
                ret = null;
                break;
        }
        return ret;
    }

    private void cfgRegion(String regionName) {
        try {
            Regions tmpRegions = Regions.fromName(regionName);
            setRegion(Region.getRegion(tmpRegions));
            setRegions(tmpRegions);
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException("Unrecognized region " + regionName);
        }
    }

    private AWSKafkaConfig(String[] args) {
        if (DEFAULT_KAFKA_TOPICS.size() == 0) {
            DEFAULT_KAFKA_TOPICS.add("Test");
        }
        for (int i = 0; i < args.length; ++i) {
            String arg = args[i];
            if (arg.equals("--queue")) {
                // Done
                setQueueName(getParameter(args, i));
                i++;
            } else if (arg.equals("--region")) {
                // Done
                String regionName = getParameter(args, i);
                try {
                    Regions tmpRegions = Regions.fromName(regionName);
                    setRegion(Region.getRegion(tmpRegions));
                    setRegions(tmpRegions);
                } catch (IllegalArgumentException e) {
                    throw new IllegalArgumentException("Unrecognized region " + regionName);
                }
                i++;
            } else if (arg.equals("--credentials")) {
                // Done
                String credsFile = getParameter(args, i);
                try {
                    setCredentialsProvider(new PropertiesFileCredentialsProvider(credsFile));
                } catch (AmazonClientException e) {
                    throw new IllegalArgumentException("Error reading credentials from " + credsFile, e);
                }
                i++;
            } else if (arg.equals("--base64-bin")) {
                try {
                    mBinSizeBytes = Integer.valueOf(getParameter(args, i)) * 1024;
                    if (mBinSizeBytes > MAX_BIN_SIZE) {
                        mBinSizeBytes = MAX_BIN_SIZE;
                    }
                    if (mBinSizeBytes < 0) {
                        mBinSizeBytes = 0;
                    }
                } catch (Throwable e) {
                    mBinSizeBytes = 0;
                }
                i++;
            } else if (arg.equals("--dedupPrefix")) {
                // Done
                queueDedupPrefix = getParameter(args, i);
                i++;
            } else if (arg.equals("--bootstrap.servers")) {
                kafkaServers = getParameter(args, i);
                i++;
            } else if (arg.equals("--topics")) {
                String topicStr = getParameter(args, i);
                kafkaTopics = Arrays.asList(topicStr.split(","));
                i++;
            } else if (arg.equals("--group.id")) {
                kafkaGrpId = getParameter(args, i);
                i++;
            } else {
                throw new IllegalArgumentException("Unrecognized option " + arg);
            }
        }
    }

    private int mBinTimeInMinutes = 10;

    private int mBinSizeBytes = 1024;

    private String queueDedupPrefix = DEFAULT_NA;

    private List<String> kafkaTopics = DEFAULT_KAFKA_TOPICS;

    private String kafkaServers = DEFAULT_NA;

    private String kafkaGrpId = DEFAULT_NA;

    private String queueName = DEFAULT_QUEUE_NAME;

    private Region region = DEFAULT_REGION;

    private Regions regions = Regions.US_EAST_2;

    private AWSCredentialsProvider credentialsProvider = new DefaultAWSCredentialsProviderChain();

    public String testRst() {
        return "queue:" + queueName + "  region:" + regions + " bootstrap.servers:" + kafkaServers + " group.id:" + kafkaGrpId;
    }

    public String getKafkaServers() {
        return kafkaServers;
    }

    public String getKafkaGrpId() {
        return kafkaGrpId;
    }

    public List<String> getKafkaTopics() {
        return kafkaTopics;
    }

    public boolean getAwsXmitDisable() {
        return mDisableXmit;
    }

    public String getQueueName() {
        return queueName;
    }

    public void setQueueName(String queueName) {
        this.queueName = queueName;
    }

    public String getDeDeupPrefix() {
        return queueDedupPrefix;
    }

    public Region getRegion() {
        return region;
    }

    public void setRegion(Region region) {
        this.region = region;
    }

    public Regions getRegions() {
        return regions;
    }

    public void setRegions(Regions regions) {
        this.regions = regions;
    }

    public AWSCredentialsProvider getCredentialsProvider() {
        return credentialsProvider;
    }

    public void setCredentialsProvider(AWSCredentialsProvider credentialsProvider) {
        // Make sure they're usable first
        credentialsProvider.getCredentials();
        this.credentialsProvider = credentialsProvider;
    }
}

17 Source : Ec2MetadataRegionProviderTest.java
with Apache License 2.0
from awspring

@Test
void getRegion_availabilityZoneWithMatchingRegion_returnsRegion() throws Exception {
    // Arrange
    Ec2MetadataRegionProvider regionProvider = new Ec2MetadataRegionProvider() {

        @Override
        protected Region getCurrentRegion() {
            return Region.getRegion(Regions.EU_WEST_1);
        }
    };
    // Act
    Region region = regionProvider.getRegion();
    // replacedert
    replacedertThat(region).isEqualTo(Region.getRegion(Regions.EU_WEST_1));
}

17 Source : JavaKinesisVideoServiceClient.java
with Apache License 2.0
from awslabs

@Nonnull
public static AmazonKinesisVideo getAmazonKinesisVideoClient(@Nonnull final AWSCredentialsProvider credentialsProvider, @Nonnull final Region region, @Nonnull final String endpoint, final int timeoutInMillis) throws KinesisVideoException {
    return createAmazonKinesisVideoClient(credentialsProvider, region, endpoint, timeoutInMillis);
}

17 Source : JavaKinesisVideoServiceClient.java
with Apache License 2.0
from awslabs

private static AmazonKinesisVideo createAmazonKinesisVideoClient(final KinesisVideoCredentialsProvider credentialsProvider, final Region region, final String endpoint, final int timeoutInMillis) throws KinesisVideoException {
    final AWSCredentials credentials = createAwsCredentials(credentialsProvider);
    return createAwsKinesisVideoClient(credentials, region, endpoint, timeoutInMillis);
}

17 Source : LocalStackClientFactoryBean.java
with Apache License 2.0
from atlassian-labs

public clreplaced LocalStackClientFactoryBean<T extends AmazonWebServiceClient> extends AmazonWebserviceClientFactoryBean<T> {

    private final String endpointURL;

    private final Clreplaced<? extends AmazonWebServiceClient> clientClreplaced;

    private final AWSCredentialsProvider credentialsProvider;

    private RegionProvider regionProvider;

    private Region customRegion;

    private ExecutorService executor;

    public LocalStackClientFactoryBean(Clreplaced clientClreplaced, AWSCredentialsProvider credentialsProvider, String endpointURL) {
        super(clientClreplaced, credentialsProvider);
        this.endpointURL = endpointURL;
        this.clientClreplaced = clientClreplaced;
        this.credentialsProvider = credentialsProvider;
    }

    public LocalStackClientFactoryBean(Clreplaced clientClreplaced, AWSCredentialsProvider credentialsProvider, RegionProvider regionProvider, String endpointURL) {
        super(clientClreplaced, credentialsProvider, regionProvider);
        this.endpointURL = endpointURL;
        this.clientClreplaced = clientClreplaced;
        this.credentialsProvider = credentialsProvider;
        this.regionProvider = regionProvider;
    }

    @Override
    public T createInstance() throws Exception {
        String builderName = this.clientClreplaced.getName() + "Builder";
        Clreplaced<?> clreplacedName = ClreplacedUtils.resolveClreplacedName(builderName, ClreplacedUtils.getDefaultClreplacedLoader());
        Method method = ClreplacedUtils.getStaticMethod(clreplacedName, "standard");
        replacedert.notNull(method, "Could not find standard() method in clreplaced:'" + clreplacedName.getName() + "'");
        AwsClientBuilder<?, T> builder = (AwsClientBuilder) ReflectionUtils.invokeMethod(method, null);
        if (this.executor != null) {
            AwsAsyncClientBuilder<?, T> asyncBuilder = (AwsAsyncClientBuilder) builder;
            asyncBuilder.withExecutorFactory(() -> this.executor);
        }
        if (this.credentialsProvider != null) {
            builder.withCredentials(this.credentialsProvider);
        }
        if (builder.getClreplaced() == AmazonS3ClientBuilder.clreplaced) {
            Method pathStyleMethod = builder.getClreplaced().getMethod("withPathStyleAccessEnabled", Boolean.clreplaced);
            pathStyleMethod.invoke(builder, true);
        }
        if (this.customRegion != null) {
            AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration(this.endpointURL, this.customRegion.getName());
            builder.withEndpointConfiguration(endpointConfiguration);
        } else if (this.regionProvider != null) {
            AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration(this.endpointURL, this.regionProvider.getRegion().getName());
            builder.withEndpointConfiguration(endpointConfiguration);
        } else {
            AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration(this.endpointURL, Regions.DEFAULT_REGION.getName());
            builder.withEndpointConfiguration(endpointConfiguration);
        }
        return builder.build();
    }
}

16 Source : AbstractAWSProcessor.java
with Apache License 2.0
from wangrenlei

/**
 * Abstract base clreplaced for aws processors.  This clreplaced uses aws credentials for creating aws clients
 *
 * @deprecated use {@link AbstractAWSCredentialsProviderProcessor} instead which uses credentials providers or creating aws clients
 * @see AbstractAWSCredentialsProviderProcessor
 */
@Deprecated
public abstract clreplaced AbstractAWSProcessor<ClientType extends AmazonWebServiceClient> extends AbstractProcessor {

    public static final Relationship REL_SUCCESS = new Relationship.Builder().name("success").description("FlowFiles are routed to success relationship").build();

    public static final Relationship REL_FAILURE = new Relationship.Builder().name("failure").description("FlowFiles are routed to failure relationship").build();

    public static final Set<Relationship> relationships = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(REL_SUCCESS, REL_FAILURE)));

    public static final PropertyDescriptor CREDENTIALS_FILE = CredentialPropertyDescriptors.CREDENTIALS_FILE;

    public static final PropertyDescriptor ACCESS_KEY = CredentialPropertyDescriptors.ACCESS_KEY;

    public static final PropertyDescriptor SECRET_KEY = CredentialPropertyDescriptors.SECRET_KEY;

    public static final PropertyDescriptor PROXY_HOST = new PropertyDescriptor.Builder().name("Proxy Host").description("Proxy host name or IP").expressionLanguageSupported(true).required(false).addValidator(StandardValidators.NON_EMPTY_VALIDATOR).build();

    public static final PropertyDescriptor PROXY_HOST_PORT = new PropertyDescriptor.Builder().name("Proxy Host Port").description("Proxy host port").expressionLanguageSupported(true).required(false).addValidator(StandardValidators.PORT_VALIDATOR).build();

    public static final PropertyDescriptor REGION = new PropertyDescriptor.Builder().name("Region").required(true).allowableValues(getAvailableRegions()).defaultValue(createAllowableValue(Regions.DEFAULT_REGION).getValue()).build();

    public static final PropertyDescriptor TIMEOUT = new PropertyDescriptor.Builder().name("Communications Timeout").required(true).addValidator(StandardValidators.TIME_PERIOD_VALIDATOR).defaultValue("30 secs").build();

    public static final PropertyDescriptor SSL_CONTEXT_SERVICE = new PropertyDescriptor.Builder().name("SSL Context Service").description("Specifies an optional SSL Context Service that, if provided, will be used to create connections").required(false).identifiesControllerService(SSLContextService.clreplaced).build();

    public static final PropertyDescriptor ENDPOINT_OVERRIDE = new PropertyDescriptor.Builder().name("Endpoint Override URL").description("Endpoint URL to use instead of the AWS default including scheme, host, port, and path. " + "The AWS libraries select an endpoint URL based on the AWS region, but this property overrides " + "the selected endpoint URL, allowing use with other S3-compatible endpoints.").required(false).addValidator(StandardValidators.URL_VALIDATOR).build();

    protected volatile ClientType client;

    protected volatile Region region;

    // If protocol is changed to be a property, ensure other uses are also changed
    protected static final Protocol DEFAULT_PROTOCOL = Protocol.HTTPS;

    protected static final String DEFAULT_USER_AGENT = "NiFi";

    private static AllowableValue createAllowableValue(final Regions regions) {
        return new AllowableValue(regions.getName(), regions.getName(), regions.getName());
    }

    private static AllowableValue[] getAvailableRegions() {
        final List<AllowableValue> values = new ArrayList<>();
        for (final Regions regions : Regions.values()) {
            values.add(createAllowableValue(regions));
        }
        return (AllowableValue[]) values.toArray(new AllowableValue[values.size()]);
    }

    @Override
    public Set<Relationship> getRelationships() {
        return relationships;
    }

    @Override
    protected Collection<ValidationResult> customValidate(final ValidationContext validationContext) {
        final List<ValidationResult> problems = new ArrayList<>(super.customValidate(validationContext));
        final boolean accessKeySet = validationContext.getProperty(ACCESS_KEY).isSet();
        final boolean secretKeySet = validationContext.getProperty(SECRET_KEY).isSet();
        if ((accessKeySet && !secretKeySet) || (secretKeySet && !accessKeySet)) {
            problems.add(new ValidationResult.Builder().input("Access Key").valid(false).explanation("If setting Secret Key or Access Key, must set both").build());
        }
        final boolean credentialsFileSet = validationContext.getProperty(CREDENTIALS_FILE).isSet();
        if ((secretKeySet || accessKeySet) && credentialsFileSet) {
            problems.add(new ValidationResult.Builder().input("Access Key").valid(false).explanation("Cannot set both Credentials File and Secret Key/Access Key").build());
        }
        final boolean proxyHostSet = validationContext.getProperty(PROXY_HOST).isSet();
        final boolean proxyHostPortSet = validationContext.getProperty(PROXY_HOST_PORT).isSet();
        if (((!proxyHostSet) && proxyHostPortSet) || (proxyHostSet && (!proxyHostPortSet))) {
            problems.add(new ValidationResult.Builder().input("Proxy Host Port").valid(false).explanation("Both proxy host and port must be set").build());
        }
        return problems;
    }

    protected ClientConfiguration createConfiguration(final ProcessContext context) {
        final ClientConfiguration config = new ClientConfiguration();
        config.setMaxConnections(context.getMaxConcurrentTasks());
        config.setMaxErrorRetry(0);
        config.setUserAgent(DEFAULT_USER_AGENT);
        // If this is changed to be a property, ensure other uses are also changed
        config.setProtocol(DEFAULT_PROTOCOL);
        final int commsTimeout = context.getProperty(TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue();
        config.setConnectionTimeout(commsTimeout);
        config.setSocketTimeout(commsTimeout);
        final SSLContextService sslContextService = context.getProperty(SSL_CONTEXT_SERVICE).asControllerService(SSLContextService.clreplaced);
        if (sslContextService != null) {
            final SSLContext sslContext = sslContextService.createSSLContext(SSLContextService.ClientAuth.NONE);
            SdkTLSSocketFactory sdkTLSSocketFactory = new SdkTLSSocketFactory(sslContext, null);
            config.getApacheHttpClientConfig().setSslSocketFactory(sdkTLSSocketFactory);
        }
        if (context.getProperty(PROXY_HOST).isSet()) {
            String proxyHost = context.getProperty(PROXY_HOST).getValue();
            config.setProxyHost(proxyHost);
            Integer proxyPort = context.getProperty(PROXY_HOST_PORT).asInteger();
            config.setProxyPort(proxyPort);
        }
        return config;
    }

    @OnScheduled
    public void onScheduled(final ProcessContext context) {
        final ClientType awsClient = createClient(context, getCredentials(context), createConfiguration(context));
        this.client = awsClient;
        initializeRegionAndEndpoint(context);
    }

    protected void initializeRegionAndEndpoint(ProcessContext context) {
        // if the processor supports REGION, get the configured region.
        if (getSupportedPropertyDescriptors().contains(REGION)) {
            final String region = context.getProperty(REGION).getValue();
            if (region != null) {
                this.region = Region.getRegion(Regions.fromName(region));
                client.setRegion(this.region);
            } else {
                this.region = null;
            }
        }
        // if the endpoint override has been configured, set the endpoint.
        // (per Amazon docs this should only be configured at client creation)
        final String urlstr = StringUtils.trimToEmpty(context.getProperty(ENDPOINT_OVERRIDE).getValue());
        if (!urlstr.isEmpty()) {
            this.client.setEndpoint(urlstr);
        }
    }

    /**
     * Create client from the arguments
     * @param context process context
     * @param credentials static aws credentials
     * @param config aws client configuration
     * @return ClientType aws client
     *
     * @deprecated use {@link AbstractAWSCredentialsProviderProcessor#createClient(ProcessContext, AWSCredentialsProvider, ClientConfiguration)}
     */
    @Deprecated
    protected abstract ClientType createClient(final ProcessContext context, final AWSCredentials credentials, final ClientConfiguration config);

    protected ClientType getClient() {
        return client;
    }

    protected Region getRegion() {
        return region;
    }

    protected AWSCredentials getCredentials(final ProcessContext context) {
        final String accessKey = context.getProperty(ACCESS_KEY).evaluateAttributeExpressions().getValue();
        final String secretKey = context.getProperty(SECRET_KEY).evaluateAttributeExpressions().getValue();
        final String credentialsFile = context.getProperty(CREDENTIALS_FILE).getValue();
        if (credentialsFile != null) {
            try {
                return new PropertiesCredentials(new File(credentialsFile));
            } catch (final IOException ioe) {
                throw new ProcessException("Could not read Credentials File", ioe);
            }
        }
        if (accessKey != null && secretKey != null) {
            return new BasicAWSCredentials(accessKey, secretKey);
        }
        return new AnonymousAWSCredentials();
    }

    @OnShutdown
    public void onShutdown() {
        if (getClient() != null) {
            getClient().shutdown();
        }
    }
}

16 Source : GenericApiGatewayClientBuilder.java
with Apache License 2.0
from rpgreen

public clreplaced GenericApiGatewayClientBuilder {

    private String endpoint;

    private Region region;

    private AWSCredentialsProvider credentials;

    private ClientConfiguration clientConfiguration;

    private String apiKey;

    private AmazonHttpClient httpClient;

    public GenericApiGatewayClientBuilder withEndpoint(String endpoint) {
        this.endpoint = endpoint;
        return this;
    }

    public GenericApiGatewayClientBuilder withRegion(Region region) {
        this.region = region;
        return this;
    }

    public GenericApiGatewayClientBuilder withClientConfiguration(ClientConfiguration clientConfiguration) {
        this.clientConfiguration = clientConfiguration;
        return this;
    }

    public GenericApiGatewayClientBuilder withCredentials(AWSCredentialsProvider credentials) {
        this.credentials = credentials;
        return this;
    }

    public GenericApiGatewayClientBuilder withApiKey(String apiKey) {
        this.apiKey = apiKey;
        return this;
    }

    public GenericApiGatewayClientBuilder withHttpClient(AmazonHttpClient client) {
        this.httpClient = client;
        return this;
    }

    public AWSCredentialsProvider getCredentials() {
        return credentials;
    }

    public String getApiKey() {
        return apiKey;
    }

    public AmazonHttpClient getHttpClient() {
        return httpClient;
    }

    public String getEndpoint() {
        return endpoint;
    }

    public Region getRegion() {
        return region;
    }

    public ClientConfiguration getClientConfiguration() {
        return clientConfiguration;
    }

    public GenericApiGatewayClient build() {
        Validate.notEmpty(endpoint, "Endpoint");
        Validate.notNull(region, "Region");
        return new GenericApiGatewayClient(clientConfiguration, endpoint, region, credentials, apiKey, httpClient);
    }
}

16 Source : AwsInstanceStatusJob.java
with Apache License 2.0
from pinterest

/**
 * This is used to fetch instance state from AWS.
 * If the system status or instance status is not returned as ok
 * then add that tag to the instance in Cmdb Database
 */
public clreplaced AwsInstanceStatusJob implements Callable<Boolean> {

    private static final Logger logger = LoggerFactory.getLogger(AwsInstanceStatusJob.clreplaced);

    private final CloudInstanceStore cloudInstanceStore;

    private final Region region;

    private CmdbInstanceStore cmdbInstanceStore;

    private int total;

    private int badInstanceCount;

    public AwsInstanceStatusJob(CloudInstanceStore cloudInstanceStore, CmdbInstanceStore cmdbInstanceStore, Region region) {
        Preconditions.checkNotNull(cmdbInstanceStore);
        Preconditions.checkNotNull(cloudInstanceStore);
        Preconditions.checkNotNull(region);
        this.cmdbInstanceStore = cmdbInstanceStore;
        this.cloudInstanceStore = cloudInstanceStore;
        this.region = region;
        this.total = 0;
        this.badInstanceCount = 0;
    }

    /**
     * Computes a result, or throws an exception if unable to do so.
     *
     * @return computed result
     * @throws Exception if unable to compute a result
     */
    @Override
    public Boolean call() throws Exception {
        OperationStats op = new OperationStats("job", "AwsInstanceStatusJob");
        boolean ret = checkInstancesStatus();
        if (ret) {
            op.succeed();
        } else {
            op.failed();
        }
        return ret;
    }

    private Boolean checkInstancesStatus() {
        try {
            // Type cast cloudInstanceStore to use ec2 functions
            Ec2InstanceStore ec2Store = (Ec2InstanceStore) cloudInstanceStore;
            // Fetch aws status for all instances in the given region
            List<InstanceStatus> statuses = ec2Store.describeInstancesStatusAsync(region);
            if (statuses == null) {
                logger.warn("AWS did not return any InstanceStatus");
                return false;
            }
            // Creates a reverse map of id to Status
            Map<String, InstanceStatus> idToInstanceStatus = new HashMap<>(statuses.size());
            for (InstanceStatus status : statuses) {
                idToInstanceStatus.put(status.getInstanceId(), status);
            }
            // Iterate over all Running CmdbInstances & add status updated ones to list
            Iterator<EsAwsStatus> iterator = cmdbInstanceStore.getRunningAndTerminatedAwsStatus(region, 1);
            List<EsAwsStatus> updateInstanceList = getUpdatedInstanceList(iterator, idToInstanceStatus);
            logger.info("Number of instances found to update AwsStatus = {}", updateInstanceList.size());
            // Log the count of healthy instances & unhealthy ones.
            logger.info("Total count: = {} and unhealthy nodes count with status" + " not ok = {}", total, badInstanceCount);
            // Update all instances healthy + non healthy in CMDB
            logger.info("Starting a bulk update for awsStatus tag...");
            cmdbInstanceStore.bulkUpdateAwsStatus(updateInstanceList);
            // Reset the counters to 0
            total = 0;
            badInstanceCount = 0;
            logger.info("Re initialized total to {} and badInstanceCount to {}", total, badInstanceCount);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    private List<EsAwsStatus> getUpdatedInstanceList(Iterator<EsAwsStatus> iterator, Map<String, InstanceStatus> idToInstanceStatus) throws Exception {
        List<EsAwsStatus> updateInstanceList = new ArrayList<>();
        while (iterator.hasNext()) {
            EsAwsStatus cmdbInstanceStatus = iterator.next();
            String instanceId = cmdbInstanceStatus.getId();
            // Consider this cmdbInstance for update only if aws returned a Status for this instanceID
            if (idToInstanceStatus.containsKey(instanceId)) {
                InstanceStatus status = idToInstanceStatus.get(instanceId);
                String instanceStatus = status.getInstanceStatus().getStatus();
                String systemStatus = status.getSystemStatus().getStatus();
                List<InstanceStatusEvent> events = status.getEvents();
                total++;
                if (events.size() > 0) {
                    List<String> codes = new ArrayList<>();
                    for (InstanceStatusEvent event : events) {
                        // If description contains completed then ignore.
                        if (!event.getDescription().contains("Completed")) {
                            codes.add(event.getCode());
                        }
                    }
                    if (codes.size() > 0) {
                        logger.info("Instance {} with non-zero events reported by aws codes:{}", instanceId, codes);
                        // Create aws status hierarchy
                        AwsStatus awsStatus = new AwsStatus();
                        awsStatus.setRaw(status);
                        awsStatus.setCodes(codes);
                        // Update awsStatus of the instance
                        cmdbInstanceStatus.setAwsStatus(awsStatus);
                        // Add the instance to bulk update list
                        updateInstanceList.add(cmdbInstanceStatus);
                    } else if (cmdbInstanceStatus.getAwsStatus() != null) {
                        // Clear the awsStatus because no new codes are reported
                        cmdbInstanceStatus.setAwsStatus(null);
                        // Add instance to bulk update call
                        updateInstanceList.add(cmdbInstanceStatus);
                    }
                } else if (!StringUtils.equalsIgnoreCase("ok", instanceStatus) || !StringUtils.equalsIgnoreCase("ok", systemStatus)) {
                    // one of the two statuses was returned as not ok by aws
                    logger.warn("Unhealthy instance reported by aws instance Id : {}," + " instanceStatus : {}, systemStatus : {}", instanceId, instanceStatus, systemStatus);
                    badInstanceCount++;
                    // Add a new field to CMDB Instance Object called aws status
                    List<String> codes = new ArrayList<>();
                    // Get all event codes
                    for (InstanceStatusEvent event : events) {
                        codes.add(event.getCode());
                    }
                    // Create the aws Status hierarchy
                    AwsStatus awsStatus = new AwsStatus();
                    awsStatus.setRaw(status);
                    awsStatus.setCodes(codes);
                    // Update awsStatus of the instance
                    cmdbInstanceStatus.setAwsStatus(awsStatus);
                    // Add the instance to bulk update list
                    updateInstanceList.add(cmdbInstanceStatus);
                } else if (cmdbInstanceStatus.getAwsStatus() != null) {
                    // If both instanceStatus and SystemStatus is "ok"
                    // then check if instance has an awsStatus attribute.
                    // This was an unhealthy instance in last check
                    logger.info("Previously marked unhealthy instance is now reported ok {}", instanceId);
                    // Set aws Status to null.
                    // This is mimicked from previous job script. It should be set to status ok
                    cmdbInstanceStatus.setAwsStatus(null);
                    // Add the instance to bulk update list
                    updateInstanceList.add(cmdbInstanceStatus);
                }
            }
        }
        return updateInstanceList;
    }
    // Clreplaced ends
}

16 Source : EncryptionService.java
with Apache License 2.0
from Nike-Inc

/**
 * Service for performing encryption and decryption of secrets using the 'AWS Encryption SDK'.
 */
@Component
public clreplaced EncryptionService {

    /**
     * Property name for current SDB path in the EncryptionContext
     */
    public static final String SDB_PATH_PROPERTY_NAME = "sdb_path";

    private final Logger log = LoggerFactory.getLogger(getClreplaced());

    private final AwsCrypto awsCrypto;

    private List<String> cmkArnList;

    private final Region currentRegion;

    private final CryptoMaterialsManager decryptCryptoMaterialsManager;

    private final CryptoMaterialsManager encryptCryptoMaterialsManager;

    @Autowired
    public EncryptionService(AwsCrypto awsCrypto, @Value("${cerberus.encryption.cmk.arns}") String cmkArns, @Qualifier("decryptCryptoMaterialsManager") CryptoMaterialsManager decryptCryptoMaterialsManager, @Qualifier("encryptCryptoMaterialsManager") CryptoMaterialsManager encryptCryptoMaterialsManager, Region currentRegion) {
        this.currentRegion = currentRegion;
        this.awsCrypto = awsCrypto;
        log.info("CMK ARNs " + cmkArns);
        this.cmkArnList = splitArns(cmkArns);
        this.decryptCryptoMaterialsManager = decryptCryptoMaterialsManager;
        this.encryptCryptoMaterialsManager = encryptCryptoMaterialsManager;
    }

    /**
     * Encrypt the plainTextPayload.
     *
     * <p>Generates a Base64 encoded String the the 'AWS Encryption SDK Message Format'
     *
     * <p>http://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/message-format.html
     *
     * @param plainTextPayload the secrets to encrypt
     * @param sdbPath the SDB path where these secrets are being stored (added to EncryptionContext)
     */
    public String encrypt(String plainTextPayload, String sdbPath) {
        return awsCrypto.encryptString(encryptCryptoMaterialsManager, plainTextPayload, buildEncryptionContext(sdbPath)).getResult();
    }

    public byte[] encrypt(byte[] bytes, String sdbPath) {
        return awsCrypto.encryptData(encryptCryptoMaterialsManager, bytes, buildEncryptionContext(sdbPath)).getResult();
    }

    /**
     * Decrypt the encryptedPayload.
     *
     * <p>Expects a Base64 encoded String the the 'AWS Encryption SDK Message Format'.
     *
     * <p>http://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/message-format.html
     */
    public String decrypt(String encryptedPayload, String sdbPath) {
        ParsedCiphertext parsedCiphertext = CiphertextUtils.parse(encryptedPayload);
        try {
            return decrypt(parsedCiphertext, sdbPath);
        } catch (RuntimeException e) {
            log.error("Decrypt operation failed " + CiphertextUtils.toJson(parsedCiphertext), e);
            throw e;
        }
    }

    /**
     * Decrypt the encryptedPayload.
     *
     * <p>Expects a Base64 encoded String the the 'AWS Encryption SDK Message Format'.
     *
     * <p>http://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/message-format.html
     */
    public byte[] decrypt(byte[] encryptedPayload, String sdbPath) {
        ParsedCiphertext parsedCiphertext = CiphertextUtils.parse(encryptedPayload);
        try {
            return decryptToBytes(parsedCiphertext, sdbPath);
        } catch (RuntimeException e) {
            log.error("Decrypt operation failed " + CiphertextUtils.toJson(parsedCiphertext), e);
            throw e;
        }
    }

    /**
     * Decrypt the encryptedPayload.
     *
     * @param parsedCiphertext encryptedPayload
     * @param sdbPath the current SDB path
     */
    private String decrypt(ParsedCiphertext parsedCiphertext, String sdbPath) {
        validateEncryptionContext(parsedCiphertext, sdbPath);
        // Parses the ARNs out of the encryptedPayload so that you can manually rotate the CMKs, if
        // desired
        // Whatever CMKs were used in the encrypt operation will be used to decrypt
        List<String> cmkArns = CiphertextUtils.getCustomerMasterKeyArns(parsedCiphertext);
        CryptoMaterialsManager cryptoMaterialsManager = getCryptoMaterialsManager(cmkArns, currentRegion);
        return new String(awsCrypto.decryptData(cryptoMaterialsManager, parsedCiphertext).getResult(), StandardCharsets.UTF_8);
    }

    private CryptoMaterialsManager getCryptoMaterialsManager(List<String> cmkArns, Region currentRegion) {
        if (cmkArnList.containsAll(cmkArns)) {
            return decryptCryptoMaterialsManager;
        } else {
            MasterKeyProvider<KmsMasterKey> provider = initializeKeyProvider(cmkArns, currentRegion);
            return new DefaultCryptoMaterialsManager(provider);
        }
    }

    /**
     * Re-encrypt (i.e. decrypt then encrypt) String ciphertext
     *
     * @param encryptedPayload encryptedPayload
     * @param sdbPath the current SDB path
     * @return re-encrypted ciphertext
     */
    public String reencrypt(String encryptedPayload, String sdbPath) {
        String plaintext = decrypt(encryptedPayload, sdbPath);
        return encrypt(plaintext, sdbPath);
    }

    /**
     * Re-encrypt (i.e. decrypt then encrypt) byte array ciphertext
     *
     * @param encryptedPayload encryptedPayload
     * @param sdbPath the current SDB path
     * @return re-encrypted ciphertext
     */
    public byte[] reencrypt(byte[] encryptedPayload, String sdbPath) {
        byte[] plaintextBytes = decrypt(encryptedPayload, sdbPath);
        return encrypt(plaintextBytes, sdbPath);
    }

    /**
     * Decrypt the encryptedPayload.
     *
     * @param parsedCiphertext encryptedPayload
     * @param sdbPath the current SDB path
     */
    private byte[] decryptToBytes(ParsedCiphertext parsedCiphertext, String sdbPath) {
        validateEncryptionContext(parsedCiphertext, sdbPath);
        // Parses the ARNs out of the encryptedPayload so that you can manually rotate the CMKs, if
        // desired
        // Whatever CMKs were used in the encrypt operation will be used to decrypt
        List<String> cmkArns = CiphertextUtils.getCustomerMasterKeyArns(parsedCiphertext);
        CryptoMaterialsManager cryptoMaterialsManager = getCryptoMaterialsManager(cmkArns, currentRegion);
        return awsCrypto.decryptData(cryptoMaterialsManager, parsedCiphertext).getResult();
    }

    /**
     * Decrypt the encryptedPayload.
     *
     * @param parsedCiphertext encryptedPayload
     */
    public static String decrypt(ParsedCiphertext parsedCiphertext, AwsCrypto awsCrypto, Region currentRegion) {
        // Parses the ARNs out of the encryptedPayload so that you can manually rotate the CMKs, if
        // desired
        // Whatever CMKs were used in the encrypt operation will be used to decrypt
        List<String> cmkArns = CiphertextUtils.getCustomerMasterKeyArns(parsedCiphertext);
        MasterKeyProvider<KmsMasterKey> decryptProvider = initializeKeyProvider(cmkArns, currentRegion);
        return new String(awsCrypto.decryptData(decryptProvider, parsedCiphertext).getResult(), StandardCharsets.UTF_8);
    }

    /**
     * Validate the encryptionContext for the parsedCiphertext includes the expected sdbPath.
     *
     * <p>This step validates that the encrypted payload was created for the SDB that is currently
     * being decrypted. It is an integrity check. If this validation fails then the encrypted payload
     * may have been tampered with, e.g. copying the encrypted payload between two SDBs.
     *
     * @param parsedCiphertext the ciphertext to read the encryptionContext from
     * @param sdbPath the path expected in the encryptionContext
     */
    private void validateEncryptionContext(ParsedCiphertext parsedCiphertext, String sdbPath) {
        Map<String, String> encryptionContext = parsedCiphertext.getEncryptionContextMap();
        String pathFromEncryptionContext = encryptionContext.getOrDefault(SDB_PATH_PROPERTY_NAME, null);
        if (!StringUtils.equals(pathFromEncryptionContext, sdbPath)) {
            log.error("EncryptionContext did not have expected path, possible tampering: " + sdbPath);
            throw new IllegalArgumentException("EncyptionContext did not have expected path, possible tampering: " + sdbPath);
        }
    }

    /**
     * Split the ARNs from a single comma delimited string into a list.
     */
    public static List<String> splitArns(String cmkArns) {
        List<String> keyArns = Lists.newArrayList(StringUtils.split(cmkArns, ","));
        if (keyArns.size() < 2) {
            throw new IllegalArgumentException("At least 2 CMK ARNs are required for high availability, size:" + keyArns.size());
        }
        return keyArns;
    }

    /**
     * Initialize a Multi-KMS-MasterKeyProvider.
     *
     * <p>For encrypt, KMS in all regions must be available. For decrypt, KMS in at least one region
     * must be available.
     */
    public static MasterKeyProvider<KmsMasterKey> initializeKeyProvider(List<String> cmkArns, Region currentRegion) {
        List<MasterKeyProvider<KmsMasterKey>> providers = getSortedArnListByCurrentRegion(cmkArns, currentRegion).stream().map(KmsMasterKeyProvider::new).collect(Collectors.toList());
        return (MasterKeyProvider<KmsMasterKey>) MultipleProviderFactory.buildMultiProvider(providers);
    }

    /**
     * Initialize a Multi-KMS-MasterKeyProvider.
     *
     * <p>For encrypt, KMS in all regions must be available. For decrypt, KMS in at least one region
     * must be available.
     */
    public static MasterKeyProvider<KmsMasterKey> initializeKeyProvider(String cmkArns, Region currentRegion) {
        return initializeKeyProvider(splitArns(cmkArns), currentRegion);
    }

    /**
     * ARN with current region should always go first to minimize latency
     */
    protected static List<String> getSortedArnListByCurrentRegion(List<String> cmkArns, Region currentRegion) {
        return cmkArns.stream().sorted((s1, s2) -> {
            if (s1.contains(currentRegion.getName())) {
                // ARN with current region should always go first
                return -1;
            } else if (s2.contains(currentRegion.getName())) {
                // ARN with current region should always go first
                return 1;
            } else {
                // otherwise order isn't that important
                return s1.compareTo(s2);
            }
        }).collect(Collectors.toList());
    }

    /**
     * Generate an encryption context (additional information about the payload). This context is not
     * encrypted and should not contain secrets.
     */
    protected Map<String, String> buildEncryptionContext(String sdbPath) {
        Map<String, String> context = new HashMap<>();
        context.put("created_on", DateFormatUtils.format(new Date(), "yyyy-MM-dd"));
        context.put(SDB_PATH_PROPERTY_NAME, sdbPath);
        return context;
    }
}

16 Source : KmsClientFactory.java
with Apache License 2.0
from Nike-Inc

/**
 * Returns a KMS client for the given region. Clients are cached by region.
 *
 * @param region Region to configure a client for
 * @return AWS KMS client
 */
public AWSKMSClient getClient(Region region) {
    AWSKMSClient client = kmsClientMap.get(region);
    if (client == null) {
        final AWSKMSClient newClient = new AWSKMSClient();
        newClient.setRegion(region);
        kmsClientMap.put(region, newClient);
        client = newClient;
    }
    return client;
}

16 Source : Main.java
with Apache License 2.0
from Netflix

private static AwsInstanceCloudConnector createConnector() {
    AWSCredentialsProvider baseCredentials = new ProfileCredentialsProvider("default");
    AWSSecurityTokenServiceAsync stsClient = new AmazonStsAsyncProvider(CONFIGURATION, baseCredentials, replacedusRuntimes.internal()).get();
    AWSCredentialsProvider credentialsProvider = new DataPlaneControllerCredentialsProvider(CONFIGURATION, stsClient, baseCredentials).get();
    Region currentRegion = Regions.getCurrentRegion();
    if (currentRegion == null) {
        currentRegion = Region.getRegion(Regions.US_EAST_1);
    }
    return new AwsInstanceCloudConnector(CONFIGURATION, AmazonEC2AsyncClientBuilder.standard().withRegion(currentRegion.getName()).withCredentials(credentialsProvider).build(), AmazonAutoScalingAsyncClientBuilder.standard().withRegion(currentRegion.getName()).withCredentials(credentialsProvider).build());
}

16 Source : Main.java
with Apache License 2.0
from Netflix

private static AwsIamConnector createConnector() {
    AWSCredentialsProvider baseCredentials = new ProfileCredentialsProvider("default");
    AWSSecurityTokenServiceAsync stsClient = new AmazonStsAsyncProvider(CONFIGURATION, baseCredentials, replacedusRuntimes.internal()).get();
    AWSCredentialsProvider credentialsProvider = new DataPlaneAgentCredentialsProvider(CONFIGURATION, stsClient, baseCredentials).get();
    Region currentRegion = Regions.getCurrentRegion();
    if (currentRegion == null) {
        currentRegion = Region.getRegion(Regions.US_EAST_1);
    }
    return new AwsIamConnector(CONFIGURATION, AmazonIdenreplacedyManagementAsyncClientBuilder.standard().withRegion(currentRegion.getName()).withCredentials(credentialsProvider).build(), AWSSecurityTokenServiceAsyncClientBuilder.standard().withRegion(currentRegion.getName()).withCredentials(credentialsProvider).build(), new DefaultRegistry());
}

16 Source : S3RegionalResource.java
with GNU General Public License v3.0
from catofmrlu

public clreplaced S3RegionalResource {

    private static final Pattern REGIONAL_ENDPOINT_PATTERN = Pattern.compile("^s3:\\/\\/(.+)?\\.s3[.-]([a-z0-9-]+)\\.amazonaws\\.com(\\.[a-z]+)?\\/(.+)");

    private static final Region DEFAULT_REGION = Region.getRegion(Regions.US_EAST_1);

    private final URI uri;

    private Optional<Region> region;

    private String bucketName;

    private String key;

    public S3RegionalResource(URI uri) {
        this.uri = uri;
        configure();
    }

    public Optional<Region> getRegion() {
        return region;
    }

    public String getBucketName() {
        return bucketName;
    }

    public String getKey() {
        return key;
    }

    private void configure() {
        Matcher matcher = REGIONAL_ENDPOINT_PATTERN.matcher(uri.toString());
        if (matcher.find()) {
            String bucketName = matcher.group(1);
            String region = matcher.group(2);
            String key = matcher.group(4);
            Region derivedRegion;
            if (region.equals("external-1")) {
                derivedRegion = Region.getRegion(Regions.US_EAST_1);
            } else {
                derivedRegion = RegionUtils.getRegion(region);
            }
            this.region = Optional.of(derivedRegion);
            this.bucketName = bucketName;
            this.key = key;
        } else {
            this.region = Optional.absent();
            this.bucketName = getBucketName(uri.getHost());
            this.key = getS3BucketKey(uri);
        }
    }

    private String getS3BucketKey(URI destination) {
        String path = destination.getPath();
        return path.startsWith("/") ? path.substring(1) : path;
    }

    private String getBucketName(String bucket) {
        return bucket.replaceAll("\\.s3\\.amazonaws\\.com", "").replaceAll("\\.s3-external-1\\.amazonaws\\.com", "");
    }
}

16 Source : AmazonRdsDataSourceUserTagsFactoryBean.java
with Apache License 2.0
from awspring

/**
 * @author Agim Emruli
 */
public clreplaced AmazonRdsDataSourceUserTagsFactoryBean extends AbstractFactoryBean<Map<String, String>> {

    private final AmazonRDS amazonRds;

    private final String dbInstanceIdentifier;

    private final AmazonIdenreplacedyManagement idenreplacedyManagement;

    private ResourceIdResolver resourceIdResolver;

    private Region region;

    public AmazonRdsDataSourceUserTagsFactoryBean(AmazonRDS amazonRds, String dbInstanceIdentifier, AmazonIdenreplacedyManagement idenreplacedyManagement) {
        this.amazonRds = amazonRds;
        this.dbInstanceIdentifier = dbInstanceIdentifier;
        this.idenreplacedyManagement = idenreplacedyManagement;
    }

    @Override
    public Clreplaced<?> getObjectType() {
        return Map.clreplaced;
    }

    @Override
    protected Map<String, String> createInstance() throws Exception {
        LinkedHashMap<String, String> userTags = new LinkedHashMap<>();
        ListTagsForResourceResult tagsForResource = this.amazonRds.listTagsForResource(new ListTagsForResourceRequest().withResourceName(getDbInstanceResourceName()));
        for (Tag tag : tagsForResource.getTagList()) {
            userTags.put(tag.getKey(), tag.getValue());
        }
        return userTags;
    }

    public void setResourceIdResolver(ResourceIdResolver resourceIdResolver) {
        this.resourceIdResolver = resourceIdResolver;
    }

    private String getDbInstanceIdentifier() {
        return this.resourceIdResolver != null ? this.resourceIdResolver.resolveToPhysicalResourceId(this.dbInstanceIdentifier) : this.dbInstanceIdentifier;
    }

    private Region getRegion() {
        if (this.region != null) {
            return this.region;
        }
        return Region.getRegion(Regions.DEFAULT_REGION);
    }

    public void setRegion(Region region) {
        this.region = region;
    }

    /**
     * Unfortunately Amazon AWS mandates to use ARN notation to get the tags. Therefore we
     * first need to get the account number through the IAM service and then construct the
     * ARN out of the account no and region
     * @return the arn string used to query the tags
     */
    private String getDbInstanceResourceName() {
        String userArn = this.idenreplacedyManagement.getUser().getUser().getArn();
        AmazonResourceName userResourceName = AmazonResourceName.fromString(userArn);
        AmazonResourceName dbResourceArn = new AmazonResourceName.Builder().withService("rds").withRegion(getRegion()).withAccount(userResourceName.getAccount()).withResourceType("db").withResourceName(getDbInstanceIdentifier()).withResourceTypeDelimiter(":").build();
        return dbResourceArn.toString();
    }
}

16 Source : SimpleStorageResource.java
with Apache License 2.0
from awspring

@Override
public URL getURL() throws IOException {
    Region region = this.amazonS3.getRegion().toAWSRegion();
    String encodedObjectName = URLEncoder.encode(this.objectName, StandardCharsets.UTF_8.toString());
    return new URL("https", region.getServiceEndpoint(AmazonS3Client.S3_SERVICE_NAME), "/" + this.bucketName + "/" + encodedObjectName);
}

16 Source : JavaKinesisVideoServiceClient.java
with Apache License 2.0
from awslabs

private static AmazonKinesisVideo createAmazonKinesisVideoClient(final AWSCredentialsProvider awsCredentialsProvider, final Region region, final String endpoint, final int timeoutInMillis) throws KinesisVideoException {
    final AWSCredentials credentials = awsCredentialsProvider.getCredentials();
    return createAwsKinesisVideoClient(credentials, region, endpoint, timeoutInMillis);
}

16 Source : AWSS3StorageService.java
with Apache License 2.0
from aws-amplify

private AmazonS3Client createS3Client(@NonNull Region region) throws StorageException {
    ClientConfiguration configuration = new ClientConfiguration();
    configuration.setUserAgent(UserAgent.string());
    return new AmazonS3Client(cognitoAuthProvider.getCredentialsProvider(), region, configuration);
}

16 Source : AWSPredictionsPluginConfiguration.java
with Apache License 2.0
from aws-amplify

/**
 * Configuration options for the {@link AWSPredictionsPlugin}.
 * Contains settings for different types of predictions operations
 * as well as their network policy.
 */
public final clreplaced AWSPredictionsPluginConfiguration {

    private final Region defaultRegion;

    private final NetworkPolicy defaultNetworkPolicy;

    private final SpeechGeneratorConfiguration speechGeneratorConfiguration;

    private final TranslateTextConfiguration translateTextConfiguration;

    private final IdentifyLabelsConfiguration identifyLabelsConfiguration;

    private final IdentifyEnreplacediesConfiguration identifyEnreplacediesConfiguration;

    private final IdentifyTextConfiguration identifyTextConfiguration;

    private final InterpretTextConfiguration interpretTextConfiguration;

    private AWSPredictionsPluginConfiguration(Region defaultRegion, SpeechGeneratorConfiguration speechGeneratorConfiguration, TranslateTextConfiguration translateTextConfiguration, IdentifyLabelsConfiguration identifyLabelsConfiguration, IdentifyEnreplacediesConfiguration identifyEnreplacediesConfiguration, IdentifyTextConfiguration identifyTextConfiguration, InterpretTextConfiguration interpretTextConfiguration) {
        this.defaultRegion = defaultRegion;
        this.defaultNetworkPolicy = NetworkPolicy.AUTO;
        this.speechGeneratorConfiguration = speechGeneratorConfiguration;
        this.translateTextConfiguration = translateTextConfiguration;
        this.identifyLabelsConfiguration = identifyLabelsConfiguration;
        this.identifyEnreplacediesConfiguration = identifyEnreplacediesConfiguration;
        this.identifyTextConfiguration = identifyTextConfiguration;
        this.interpretTextConfiguration = interpretTextConfiguration;
    }

    /**
     * Constructs an instance of {@link AWSPredictionsPluginConfiguration} from
     * the plugin configuration JSON object.
     * @param configurationJson the plugin configuration
     * @return the configuration object for AWS Predictions Plugin
     * @throws PredictionsException if configuration is missing or malformed
     */
    @NonNull
    static AWSPredictionsPluginConfiguration fromJson(JSONObject configurationJson) throws PredictionsException {
        if (configurationJson == null) {
            throw new PredictionsException("Could not locate predictions configuration for AWS Predictions Plugin.", "Verify that amplifyconfiguration.json contains a section for \"awsPredictionsPlugin\".");
        }
        final Region defaultRegion;
        final SpeechGeneratorConfiguration speechGeneratorConfiguration;
        final TranslateTextConfiguration translateTextConfiguration;
        final IdentifyLabelsConfiguration identifyLabelsConfiguration;
        final IdentifyEnreplacediesConfiguration identifyEnreplacediesConfiguration;
        final IdentifyTextConfiguration identifyTextConfiguration;
        final InterpretTextConfiguration interpretConfiguration;
        try {
            // Get default region
            String regionString = configurationJson.getString(ConfigKey.DEFAULT_REGION.key());
            defaultRegion = Region.getRegion(regionString);
            if (configurationJson.has(ConfigKey.CONVERT.key())) {
                JSONObject convertJson = configurationJson.getJSONObject(ConfigKey.CONVERT.key());
                speechGeneratorConfiguration = SpeechGeneratorConfiguration.fromJson(convertJson);
                translateTextConfiguration = TranslateTextConfiguration.fromJson(convertJson);
            } else {
                speechGeneratorConfiguration = null;
                translateTextConfiguration = null;
            }
            if (configurationJson.has(ConfigKey.IDENTIFY.key())) {
                JSONObject identifyJson = configurationJson.getJSONObject(ConfigKey.IDENTIFY.key());
                identifyLabelsConfiguration = IdentifyLabelsConfiguration.fromJson(identifyJson);
                identifyEnreplacediesConfiguration = IdentifyEnreplacediesConfiguration.fromJson(identifyJson);
                identifyTextConfiguration = IdentifyTextConfiguration.fromJson(identifyJson);
            } else {
                identifyLabelsConfiguration = null;
                identifyEnreplacediesConfiguration = null;
                identifyTextConfiguration = null;
            }
            if (configurationJson.has(ConfigKey.INTERPRET.key())) {
                JSONObject interpretJson = configurationJson.getJSONObject(ConfigKey.INTERPRET.key());
                interpretConfiguration = InterpretTextConfiguration.fromJson(interpretJson);
            } else {
                interpretConfiguration = null;
            }
        } catch (JSONException | IllegalArgumentException exception) {
            throw new PredictionsException("Issue encountered while parsing configuration JSON", exception, "Check the attached exception for more details.");
        }
        return new AWSPredictionsPluginConfiguration(defaultRegion, speechGeneratorConfiguration, translateTextConfiguration, identifyLabelsConfiguration, identifyEnreplacediesConfiguration, identifyTextConfiguration, interpretConfiguration);
    }

    /**
     * Gets the plugin-level default for the AWS endpoint region.
     * @return the default AWS endpoint region
     */
    @NonNull
    public Region getDefaultRegion() {
        return defaultRegion;
    }

    /**
     * Gets the plugin-level default for the network policy.
     * @return the default network policy
     */
    @NonNull
    public NetworkPolicy getDefaultNetworkPolicy() {
        return defaultNetworkPolicy;
    }

    /**
     * Gets the configuration for speech generation.
     * Null if not configured.
     * @return the configuration for speech generation
     * @throws PredictionsException if not configured
     */
    @NonNull
    public SpeechGeneratorConfiguration getSpeechGeneratorConfiguration() throws PredictionsException {
        if (speechGeneratorConfiguration == null) {
            throw new PredictionsException("Speech generation is not configured.", "Verify that speechGenerator is configured under " + ConfigKey.CONVERT.key());
        }
        return speechGeneratorConfiguration;
    }

    /**
     * Gets the configuration for text translation.
     * Null if not configured.
     * @return the configuration for text translation
     * @throws PredictionsException if not configured
     */
    @NonNull
    public TranslateTextConfiguration getTranslateTextConfiguration() throws PredictionsException {
        if (translateTextConfiguration == null) {
            throw new PredictionsException("Text translation is not configured.", "Verify that translateText is configured under " + ConfigKey.CONVERT.key());
        }
        return translateTextConfiguration;
    }

    /**
     * Gets the configuration for labels detection.
     * Null if not configured.
     * @return the configuration for labels detection
     * @throws PredictionsException if not configured
     */
    @NonNull
    public IdentifyLabelsConfiguration getIdentifyLabelsConfiguration() throws PredictionsException {
        if (identifyLabelsConfiguration == null) {
            throw new PredictionsException("Labels detection is not configured.", "Verify that identifyLabels is configured under " + ConfigKey.IDENTIFY.key());
        }
        return identifyLabelsConfiguration;
    }

    /**
     * Gets the configuration for enreplacedies detection.
     * Null if not configured.
     * @return the configuration for enreplacedies detection
     * @throws PredictionsException if not configured
     */
    @NonNull
    public IdentifyEnreplacediesConfiguration getIdentifyEnreplacediesConfiguration() throws PredictionsException {
        if (identifyEnreplacediesConfiguration == null) {
            throw new PredictionsException("Enreplacedies detection is not configured.", "Verify that identifyEnreplacedies is configured under " + ConfigKey.IDENTIFY.key());
        }
        return identifyEnreplacediesConfiguration;
    }

    /**
     * Gets the configuration for text detection.
     * Null if not configured.
     * @return the configuration for text detection
     * @throws PredictionsException if not configured
     */
    @NonNull
    public IdentifyTextConfiguration getIdentifyTextConfiguration() throws PredictionsException {
        if (identifyTextConfiguration == null) {
            throw new PredictionsException("Text detection is not configured.", "Verify that identifyText is configured under " + ConfigKey.IDENTIFY.key());
        }
        return identifyTextConfiguration;
    }

    /**
     * Gets the configuration for text interpretation.
     * Null if not configured.
     * @return the configuration for text interpretation
     * @throws PredictionsException if not configured
     */
    @NonNull
    public InterpretTextConfiguration getInterpretTextConfiguration() throws PredictionsException {
        if (interpretTextConfiguration == null) {
            throw new PredictionsException("Text interpretation is not configured.", "Verify that interpretText is configured under " + ConfigKey.INTERPRET.key());
        }
        return interpretTextConfiguration;
    }

    /**
     * An enumeration of the various keys that we expect to see in
     * AWS Predictions configuration json.
     */
    enum ConfigKey {

        DEFAULT_REGION("defaultRegion"), CONVERT("convert"), IDENTIFY("identify"), INTERPRET("interpret");

        private final String key;

        ConfigKey(String key) {
            this.key = key;
        }

        String key() {
            return key;
        }
    }
}

16 Source : AWSEmailProvider.java
with Apache License 2.0
from AthenZ

private static AmazonSimpleEmailService initSES() {
    // /CLOVER:OFF
    Region region = Regions.getCurrentRegion();
    if (region == null) {
        region = Region.getRegion(Regions.US_EAST_1);
    }
    return AmazonSimpleEmailServiceClientBuilder.standard().withRegion(region.getName()).build();
// /CLOVER:ON
}

15 Source : AwsConfig.java
with Apache License 2.0
from shinesolutions

@Bean
public AmazonElasticLoadBalancing amazonElbClient(final AWSCredentialsProvider awsCredentialsProvider, final ClientConfiguration awsClientConfig, final Region awsRegion) {
    return AmazonElasticLoadBalancingClientBuilder.standard().withCredentials(awsCredentialsProvider).withClientConfiguration(awsClientConfig).withRegion(awsRegion.getName()).build();
}

15 Source : AudioTransferService.java
with GNU Affero General Public License v3.0
from shiehn

public void uploadAudioDirectory(String bucketName, String key, File filePath) throws InterruptedException {
    AmazonS3 s3 = new AmazonS3Client();
    Region usWest2 = Region.getRegion(Regions.US_WEST_2);
    s3.setRegion(usWest2);
    TransferManager tm = new TransferManager(s3);
    MultipleFileUpload upload = tm.uploadDirectory(bucketName, key, filePath, true);
    upload.waitForCompletion();
}

See More Examples