software.amazon.awssdk.regions.providers.DefaultAwsRegionProviderChain Java Examples

The following examples show how to use software.amazon.awssdk.regions.providers.DefaultAwsRegionProviderChain. 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: StsProfileCredentialsProviderFactory.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
private void configureEndpoint(StsClientBuilder stsClientBuilder, Profile profile) {
    Region stsRegion = profile.property(ProfileProperty.REGION)
                              .map(Region::of)
                              .orElseGet(() -> {
                                  try {
                                      return new DefaultAwsRegionProviderChain().getRegion();
                                  } catch (RuntimeException e) {
                                      return null;
                                  }
                              });

    if (stsRegion != null) {
        stsClientBuilder.region(stsRegion);
    } else {
        stsClientBuilder.region(Region.US_EAST_1);
        stsClientBuilder.endpointOverride(URI.create("https://sts.amazonaws.com"));
    }
}
 
Example #2
Source File: CredentialsAndRegionFactory.java    From micronaut-aws with Apache License 2.0 5 votes vote down vote up
/**
 * @param environment The {@link Environment}
 * @return An {@link AwsRegionProviderChain} that attempts to read the values from the Micronaut environment
 * first, then delegates to {@link DefaultAwsRegionProviderChain}.
 */
@Singleton
public AwsRegionProviderChain awsRegionProvider(Environment environment) {
    return new AwsRegionProviderChain(
            new EnvironmentAwsRegionProvider(environment),
            new DefaultAwsRegionProviderChain()
    );
}
 
Example #3
Source File: StsWebIdentityCredentialsProviderFactory.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private void configureEndpoint(StsClientBuilder stsClientBuilder) {
    Region stsRegion;
    try {
        stsRegion = new DefaultAwsRegionProviderChain().getRegion();
    } catch (RuntimeException e) {
        stsRegion = null;
    }

    if (stsRegion != null) {
        stsClientBuilder.region(stsRegion);
    } else {
        stsClientBuilder.region(Region.US_EAST_1);
        stsClientBuilder.endpointOverride(URI.create("https://sts.amazonaws.com"));
    }
}
 
Example #4
Source File: AsyncClientInterface.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private MethodSpec create() {
    return MethodSpec.methodBuilder("create")
                     .returns(className)
                     .addModifiers(Modifier.STATIC, Modifier.PUBLIC)
                     .addJavadoc("Create a {@link $T} with the region loaded from the {@link $T} and credentials loaded "
                                 + "from the {@link $T}.",
                                 className,
                                 DefaultAwsRegionProviderChain.class,
                                 DefaultCredentialsProvider.class)
                     .addStatement("return builder().build()")
                     .build();
}
 
Example #5
Source File: SyncClientInterface.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private MethodSpec create() {
    return MethodSpec.methodBuilder("create")
                     .returns(className)
                     .addModifiers(Modifier.STATIC, Modifier.PUBLIC)
                     .addJavadoc(
                             "Create a {@link $T} with the region loaded from the {@link $T} and credentials loaded from the "
                             + "{@link $T}.", className, DefaultAwsRegionProviderChain.class,
                             DefaultCredentialsProvider.class)
                     .addStatement("return builder().build()")
                     .build();
}
 
Example #6
Source File: AwsDefaultClientBuilder.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * Load the region from the default region provider if enabled.
 */
private Region regionFromDefaultProvider(SdkClientConfiguration config) {
    Boolean defaultRegionDetectionEnabled = config.option(AwsAdvancedClientOption.ENABLE_DEFAULT_REGION_DETECTION);
    if (defaultRegionDetectionEnabled != null && !defaultRegionDetectionEnabled) {
        throw new IllegalStateException("No region was configured, and use-region-provider-chain was disabled.");
    }

    ProfileFile profileFile = config.option(SdkClientOption.PROFILE_FILE);
    String profileName = config.option(SdkClientOption.PROFILE_NAME);
    return DefaultAwsRegionProviderChain.builder()
                                        .profileFile(() -> profileFile)
                                        .profileName(profileName)
                                        .build()
                                        .getRegion();
}
 
Example #7
Source File: AWSFileStore.java    From para with Apache License 2.0 5 votes vote down vote up
@Override
public String store(String path, InputStream data) {
	if (StringUtils.startsWith(path, "/")) {
		path = path.substring(1);
	}
	if (StringUtils.isBlank(path) || data == null) {
		return null;
	}
	int maxFileSizeMBytes = Config.getConfigInt("para.s3.max_filesize_mb", 10);
	try {
		if (data.available() > 0 && data.available() <= (maxFileSizeMBytes * 1024 * 1024)) {
			Map<String, String> om = new HashMap<String, String>(3);
			om.put(HttpHeaders.CACHE_CONTROL, "max-age=15552000, must-revalidate");	// 180 days
			if (path.endsWith(".gz")) {
				om.put(HttpHeaders.CONTENT_ENCODING, "gzip");
				path = path.substring(0, path.length() - 3);
			}
			PutObjectRequest por = PutObjectRequest.builder().
					bucket(bucket).key(path).
					metadata(om).
					acl(ObjectCannedACL.PUBLIC_READ).
					storageClass(StorageClass.REDUCED_REDUNDANCY).build();
			s3.putObject(por, RequestBody.fromInputStream(data, data.available())); //.bucket, path, data, om
			return Utils.formatMessage(S3_URL, new DefaultAwsRegionProviderChain().getRegion().id(), bucket, path);
		}
	} catch (IOException e) {
		logger.error(null, e);
	} finally {
		try {
			data.close();
		} catch (IOException ex) {
			logger.error(null, ex);
		}
	}
	return null;
}
 
Example #8
Source File: AWSIoTService.java    From para with Apache License 2.0 4 votes vote down vote up
@Override
public Thing createThing(Thing thing) {
	if (thing == null || StringUtils.isBlank(thing.getName()) || StringUtils.isBlank(thing.getAppid()) ||
			existsThing(thing)) {
		return null;
	}
	thing.setId(Utils.getNewId());
	String id = cloudIDForThing(thing);
	String appid = thing.getAppid();
	String region = new DefaultAwsRegionProviderChain().getRegion().id();

	// STEP 1: Create thing
	CreateThingResponse resp1 = getClient().createThing(CreateThingRequest.builder().thingName(id).
			attributePayload(AttributePayload.builder().attributes(Collections.singletonMap(Config._APPID, appid)).
					build()).build());

	// STEP 2: Create certificate
	CreateKeysAndCertificateResponse resp2 = getClient().createKeysAndCertificate(
			CreateKeysAndCertificateRequest.builder().setAsActive(true).build());

	String accountId = getAccountIdFromARN(resp1.thingArn());
	String policyString = (String) (thing.getDeviceMetadata().containsKey("policyJSON") ?
			thing.getDeviceMetadata().get("policyJSON") : getDefaultPolicyDocument(accountId, id, region));

	// STEP 3: Create policy
	getClient().createPolicy(CreatePolicyRequest.builder().
			policyDocument(policyString).policyName(id + "-Policy").build());

	// STEP 4: Attach policy to certificate
	getClient().attachPolicy(AttachPolicyRequest.builder().policyName(id + "-Policy").build());

	// STEP 5: Attach thing to certificate
	getClient().attachThingPrincipal(AttachThingPrincipalRequest.builder().
			principal(resp2.certificateArn()).thingName(id).build());

	thing.getDeviceMetadata().remove("policyJSON");

	thing.setServiceBroker("AWS");
	thing.getDeviceMetadata().put("thingId", thing.getId());
	thing.getDeviceMetadata().put("thingName", id);
	thing.getDeviceMetadata().put("thingARN", resp1.thingArn());
	thing.getDeviceMetadata().put("clientId", id);
	thing.getDeviceMetadata().put("clientCertId", resp2.certificateId());
	thing.getDeviceMetadata().put("clientCertARN", resp2.certificateArn());
	thing.getDeviceMetadata().put("clientCert", resp2.certificatePem());
	thing.getDeviceMetadata().put("privateKey", resp2.keyPair().privateKey());
	thing.getDeviceMetadata().put("publicKey", resp2.keyPair().publicKey());
	thing.getDeviceMetadata().put("region", region);
	thing.getDeviceMetadata().put("port", 8883);
	thing.getDeviceMetadata().put("host", getClient().
			describeEndpoint(DescribeEndpointRequest.builder().build()).endpointAddress());

	return thing;
}