Java Code Examples for com.amazonaws.regions.RegionUtils#getRegion()

The following examples show how to use com.amazonaws.regions.RegionUtils#getRegion() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GenericDynamoDB.java    From strongbox with Apache License 2.0 6 votes vote down vote up
public GenericDynamoDB(AmazonDynamoDB client, AWSCredentialsProvider awsCredentials,
                       ClientConfiguration clientConfiguration,
                       SecretsGroupIdentifier groupIdentifier, Class<Entry> clazz, Converters converters,
                       ReadWriteLock readWriteLock) {
    this.clazz = clazz;
    buildMappings();
    this.converters = converters;
    this.awsCredentials = awsCredentials;
    this.clientConfiguration = clientConfiguration;
    this.client = client;
    this.region = RegionUtils.getRegion(groupIdentifier.region.getName());
    this.readWriteLock = readWriteLock;

    RegionLocalResourceName resourceName = new RegionLocalResourceName(groupIdentifier);
    this.tableName = resourceName.toString();
}
 
Example 2
Source File: ReactCognitoModule.java    From react-native-cognito with MIT License 6 votes vote down vote up
@ReactMethod
public void initCredentialsProvider(String identityPoolId, String token, String region)
{
    RegionUtils regionUtils = new RegionUtils();
    Region awsRegion = regionUtils.getRegion(region);
    
    cognitoCredentialsProvider = new CognitoCachingCredentialsProvider(
        mActivityContext.getApplicationContext(),
        identityPoolId,
        // awsRegion);
        Regions.EU_WEST_1);

    cognitoClient = new CognitoSyncManager(
        mActivityContext.getApplicationContext(),
        // awsRegion,
        Regions.EU_WEST_1,
        cognitoCredentialsProvider);
}
 
Example 3
Source File: ClientHelper.java    From jobcacher-plugin with MIT License 5 votes vote down vote up
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;
}
 
Example 4
Source File: Ec2MetadataRegionProvider.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
protected Region getCurrentRegion() {
	try {
		InstanceInfo instanceInfo = EC2MetadataUtils.getInstanceInfo();
		return instanceInfo != null && instanceInfo.getRegion() != null
				? RegionUtils.getRegion(instanceInfo.getRegion()) : null;
	}
	catch (AmazonClientException e) {
		return null;
	}

}
 
Example 5
Source File: CloudWatchReporterFactory.java    From dropwizard-metrics-cloudwatch with Apache License 2.0 5 votes vote down vote up
protected Region region() {
    String az = null;
    if (isEC2MetadataAvailable()) {
        az = EC2MetadataUtils.getAvailabilityZone();
    }
    String regionName = awsRegion;
    if (!Strings.isNullOrEmpty(az)) {
        regionName = az.substring(0, az.length() - 1); // strip the AZ letter
    }
    return RegionUtils.getRegion(regionName);
}
 
Example 6
Source File: CommandLineInterface.java    From dynamodb-cross-region-library with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
CommandLineInterface(CommandLineArgs params) throws ParameterException {

    // extract streams endpoint, source and destination regions
    sourceRegion = RegionUtils.getRegion(params.getSourceSigningRegion());

    // set the source dynamodb endpoint
    sourceDynamodbEndpoint = Optional.fromNullable(params.getSourceEndpoint());
    sourceDynamodbStreamsEndpoint = Optional.fromNullable(params.getSourceEndpoint());

    // get source table name
    sourceTable = params.getSourceTable();

    // get kcl endpoint and region or null for region if cannot parse region from endpoint
    kclRegion = Optional.fromNullable(RegionUtils.getRegion(params.getKclSigningRegion()));
    kclDynamodbEndpoint = Optional.fromNullable(params.getKclEndpoint());

    // get destination endpoint and region or null for region if cannot parse region from endpoint
    destinationRegion = RegionUtils.getRegion(params.getDestinationSigningRegion());
    destinationDynamodbEndpoint = Optional.fromNullable(params.getDestinationEndpoint());
    destinationTable = params.getDestinationTable();

    // other crr parameters
    getRecordsLimit = Optional.fromNullable(params.getBatchSize());
    isPublishCloudWatch = !params.isDontPublishCloudwatch();
    taskName = params.getTaskName();
    parentShardPollIntervalMillis = Optional.fromNullable(params.getParentShardPollIntervalMillis());
}
 
Example 7
Source File: ECSService.java    From amazon-ecs-plugin with MIT License 5 votes vote down vote up
Region getRegion(String regionName) {
    if (StringUtils.isNotEmpty(regionName)) {
        return RegionUtils.getRegion(regionName);
    } else {
        return Region.getRegion(Regions.US_EAST_1);
    }
}
 
Example 8
Source File: ECSCloud.java    From amazon-ecs-plugin with MIT License 5 votes vote down vote up
public static Region getRegion(String regionName) {
    if (StringUtils.isNotEmpty(regionName)) {
        return RegionUtils.getRegion(regionName);
    } else {
        return Region.getRegion(Regions.US_EAST_1);
    }
}
 
Example 9
Source File: KmsMasterKeyProvider.java    From aws-encryption-sdk-java with Apache License 2.0 5 votes vote down vote up
private static Region getStartingRegion(final String keyArn) {
    final String region = parseRegionfromKeyArn(keyArn);
    if (region != null) {
        return RegionUtils.getRegion(region);
    }
    final Region currentRegion = Regions.getCurrentRegion();
    if (currentRegion != null) {
        return currentRegion;
    }

    return Region.getRegion(Regions.DEFAULT_REGION);
}
 
Example 10
Source File: DynamoDBUtilTest.java    From emr-dynamodb-connector with Apache License 2.0 5 votes vote down vote up
@Test
public void getsEndpointFromDefaultAwsRegion() {
  PowerMockito.mockStatic(RegionUtils.class);
  when(EC2MetadataUtils.getEC2InstanceRegion()).thenThrow(new AmazonClientException("Unable to " +
      "get region from EC2 instance data"));
  when(RegionUtils.getRegion(DynamoDBConstants.DEFAULT_AWS_REGION)).thenReturn(region);
  when(region.getServiceEndpoint(ServiceAbbreviations.Dynamodb)).thenReturn(TEST_ENDPOINT);
  assertEquals(TEST_ENDPOINT, DynamoDBUtil.getDynamoDBEndpoint(conf, null));
  PowerMockito.verifyStatic();
  RegionUtils.getRegion(DynamoDBConstants.DEFAULT_AWS_REGION);
}
 
Example 11
Source File: BucketLocationHandler.java    From teamcity-s3-artifact-storage-plugin with Apache License 2.0 5 votes vote down vote up
public static String getRegionName(@Nullable String location) {
  if (location == null) {
    return Regions.US_EAST_1.getName();
  }

  final Region region = RegionUtils.getRegion(location);
  if (region == null && location.equals("US")) {
    return Regions.US_EAST_1.getName();
  }
  if (region != null) {
    return !"US".equals(region.getName()) ? region.getName() : Regions.US_EAST_1.getName();
  } else {
    return location;
  }
}
 
Example 12
Source File: KMSEncryptor.java    From strongbox with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 13
Source File: AwsLambdaSinkConnectorConfig.java    From kafka-connect-aws-lambda with Apache License 2.0 5 votes vote down vote up
@Override
public void ensureValid(String name, Object region) {
  String regionStr = ((String) region).toLowerCase().trim();
  if (RegionUtils.getRegion(regionStr) == null) {
    throw new ConfigException(name, region, "Value must be one of: " + Utils.join(RegionUtils.getRegions(), ", "));
  }
}
 
Example 14
Source File: SnowflakeS3Client.java    From snowflake-jdbc with Apache License 2.0 4 votes vote down vote up
private void setupSnowflakeS3Client(Map<?, ?> stageCredentials,
                                    ClientConfiguration clientConfig,
                                    RemoteStoreFileEncryptionMaterial encMat,
                                    String stageRegion,
                                    String stageEndPoint)
throws SnowflakeSQLException
{
  // Save the client creation parameters so that we can reuse them,
  // to reset the AWS client. We won't save the awsCredentials since
  // we will be refreshing that, every time we reset the AWS client
  this.clientConfig = clientConfig;
  this.stageRegion = stageRegion;
  this.encMat = encMat;
  this.stageEndPoint = stageEndPoint; // FIPS endpoint, if needed

  logger.debug("Setting up AWS client ");

  // Retrieve S3 stage credentials
  String awsID = (String) stageCredentials.get("AWS_KEY_ID");
  String awsKey = (String) stageCredentials.get("AWS_SECRET_KEY");
  String awsToken = (String) stageCredentials.get("AWS_TOKEN");

  // initialize aws credentials
  AWSCredentials awsCredentials = (awsToken != null) ?
                                  new BasicSessionCredentials(awsID, awsKey, awsToken)
                                                     : new BasicAWSCredentials(awsID, awsKey);


  clientConfig.withSignerOverride("AWSS3V4SignerType");
  clientConfig.getApacheHttpClientConfig().setSslSocketFactory(
      getSSLConnectionSocketFactory());
  HttpUtil.setProxyForS3(clientConfig);
  AmazonS3Builder<?, ?> amazonS3Builder = AmazonS3Client.builder();
  if (encMat != null)
  {
    byte[] decodedKey = Base64.decode(encMat.getQueryStageMasterKey());
    encryptionKeySize = decodedKey.length * 8;

    if (encryptionKeySize == 256)
    {
      SecretKey queryStageMasterKey =
          new SecretKeySpec(decodedKey, 0, decodedKey.length, AES);
      EncryptionMaterials encryptionMaterials =
          new EncryptionMaterials(queryStageMasterKey);
      encryptionMaterials.addDescription("queryId",
                                         encMat.getQueryId());
      encryptionMaterials.addDescription("smkId",
                                         Long.toString(encMat.getSmkId()));
      CryptoConfiguration cryptoConfig =
          new CryptoConfiguration(CryptoMode.EncryptionOnly);

      amazonS3Builder = AmazonS3EncryptionClient.encryptionBuilder()
          .withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
          .withEncryptionMaterials(new StaticEncryptionMaterialsProvider(encryptionMaterials))
          .withClientConfiguration(clientConfig)
          .withCryptoConfiguration(cryptoConfig);

    }
    else if (encryptionKeySize == 128)
    {
      amazonS3Builder = AmazonS3Client.builder()
          .withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
          .withClientConfiguration(clientConfig);
    }
    else
    {
      throw new SnowflakeSQLException(SqlState.INTERNAL_ERROR,
                                      ErrorCode.INTERNAL_ERROR.getMessageCode(),
                                      "unsupported key size", encryptionKeySize);
    }
  }
  else
  {
    amazonS3Builder = AmazonS3Client.builder()
        .withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
        .withClientConfiguration(clientConfig);
  }

  if (stageRegion != null)
  {
    Region region = RegionUtils.getRegion(stageRegion);
    if (region != null)
    {
      amazonS3Builder.withRegion(region.getName());
    }
  }
  // Explicitly force to use virtual address style
  amazonS3Builder.withPathStyleAccessEnabled(false);

  amazonClient = (AmazonS3) amazonS3Builder.build();
  if (this.stageEndPoint != null && this.stageEndPoint != "")
  {
    // Set the FIPS endpoint if we need it. GS will tell us if we do by
    // giving us an endpoint to use if required and supported by the region.
    amazonClient.setEndpoint(this.stageEndPoint);
  }
}
 
Example 15
Source File: EC2Api.java    From ec2-spot-jenkins-plugin with Apache License 2.0 4 votes vote down vote up
/**
 * Derive EC2 API endpoint. If <code>endpoint</code> parameter not empty will use
 * it as first priority, otherwise will try to find region in {@link RegionUtils} by <code>regionName</code>
 * and use endpoint from it, if not available will generate endpoint as string and check if
 * region name looks like China <code>cn-</code> prefix.
 * <p>
 * Implementation details
 * <p>
 * {@link RegionUtils} is static information, and to get new region required to be updated,
 * as it's not possible too fast as you need to check new version of lib, moreover new version of lib
 * could be pointed to new version of Jenkins which is not a case for our plugin as some of installation
 * still on <code>1.6.x</code>
 * <p>
 * For example latest AWS SDK lib depends on Jackson2 plugin which starting from version <code>2.8.7.0</code>
 * require Jenkins at least <code>2.60</code> https://plugins.jenkins.io/jackson2-api
 * <p>
 * List of all AWS endpoints
 * https://docs.aws.amazon.com/general/latest/gr/rande.html
 *
 * @param regionName like us-east-1 not a airport code, could be <code>null</code>
 * @param endpoint   custom endpoint could be <code>null</code>
 * @return <code>null</code> or actual endpoint
 */
@Nullable
public String getEndpoint(@Nullable final String regionName, @Nullable final String endpoint) {
    if (StringUtils.isNotEmpty(endpoint)) {
        return endpoint;
    } else if (StringUtils.isNotEmpty(regionName)) {
        final Region region = RegionUtils.getRegion(regionName);
        if (region != null && region.isServiceSupported(endpoint)) {
            return region.getServiceEndpoint(endpoint);
        } else {
            final String domain = regionName.startsWith("cn-") ? "amazonaws.com.cn" : "amazonaws.com";
            return "https://ec2." + regionName + "." + domain;
        }
    } else {
        return null;
    }
}
 
Example 16
Source File: DefaultAwsRegionProviderChainDelegate.java    From spring-cloud-aws with Apache License 2.0 4 votes vote down vote up
@Override
public Region getRegion() {
	return RegionUtils.getRegion(delegate.getRegion());
}
 
Example 17
Source File: AmazonWebserviceClientFactoryBean.java    From spring-cloud-aws with Apache License 2.0 4 votes vote down vote up
public void setCustomRegion(String customRegionName) {
	this.customRegion = RegionUtils.getRegion(customRegionName);
}