/** * Common support code: attempts to configure client endpoint and/or region. * * @param client A constructed writer-specific service client. * @param endpoint A possibly-null endpoint specification. */ protected <T extends AmazonWebServiceClient> T tryConfigureEndpointOrRegion(T client, String endpoint) { // explicit endpoint takes precedence over region retrieved from environment if (endpoint != null) { LogLog.debug(getClass().getSimpleName() + ": configuring endpoint: " + endpoint); client.setEndpoint(endpoint); return client; } String region = System.getenv("AWS_REGION"); if (region != null) { LogLog.debug(getClass().getSimpleName() + ": configuring region: " + region); client.configureRegion(Regions.fromName(region)); return client; } return client; }
Optional<AmazonWebServiceClient> initS3ClientIfEnabled() { Optional<S3Configuration> s3 = Optional.empty(); if ((null != s3Bucket) && (null != s3Path)) { S3Configuration config = new S3Configuration(); config.setBucket(s3Bucket); config.setPath(s3Path); config.setRegion(s3Region); config.setAccessKey(s3AwsKey); config.setSecretKey(s3AwsSecret); s3 = Optional.of(config); } return s3.map(config -> new AwsClientBuilder(config.getRegion(), config.getAccessKey(), config.getSecretKey()).build(AmazonS3Client.class)); }
@Test @SuppressWarnings("deprecation") public void testBuildWithRegion() { Region region = Region.getRegion(Regions.US_WEST_1); AmazonWebServiceClient mockedS3 = createMock(AmazonWebServiceClient.class); AwsClientBuilder builder = createPartialMock(AwsClientBuilder.class, new String[] {"instantiateClient"}, region); try { expect(builder.instantiateClient(AmazonS3Client.class)) .andReturn(mockedS3); mockedS3.setRegion(region); expectLastCall(); replay(mockedS3, builder); builder.build(AmazonS3Client.class); verify(mockedS3, builder); } catch (Exception ex) { fail("Unexpected exception: " + ex.getMessage()); } }
@Test public void testBuildWithoutRegion() { AmazonWebServiceClient mockedS3 = createMock(AmazonWebServiceClient.class); try { AwsClientBuilder builder = createPartialMockAndInvokeDefaultConstructor(AwsClientBuilder.class, "instantiateClient"); expect(builder.instantiateClient(AmazonS3Client.class)) .andReturn(mockedS3); replay(mockedS3, builder); builder.build(AmazonS3Client.class); verify(mockedS3, builder); } catch (Exception ex) { fail("Unexpected exception: " + ex.getMessage()); } }
@PostConstruct public void init() { // TODO // this parameters have to be configurable cache = CacheBuilder.newBuilder() .maximumSize(500) .expireAfterAccess(50, TimeUnit.MINUTES) .removalListener((RemovalNotification<Key<?>, AmazonWebServiceClient> notification) -> { logger.debug("Shutting down expired client for key: {}", notification.getKey()); notification.getValue().shutdown(); }).build(new CacheLoader<Key<?>, AmazonWebServiceClient>() { @Override public AmazonWebServiceClient load(@Nonnull final Key<?> key) throws Exception { logger.debug("CacheLoader active for Key : {}", key); return key.region.createClient( key.type, new STSAssumeRoleSessionCredentialsProvider( buildRoleArn(key.accountId), ROLE_SESSION_NAME), new ClientConfiguration().withMaxErrorRetry(MAX_ERROR_RETRY)); } }); }
@Test public void testCachingClientProvider() throws InterruptedException { final AmazonWebServiceClient client = provider.getClient( AmazonEC2Client.class, "", Region.getRegion(Regions.EU_CENTRAL_1)); Assertions.assertThat(client).isNotNull(); System.out.println(client.toString()); for (int i = 0; i < 10; i++) { final AmazonEC2Client other = provider.getClient( AmazonEC2Client.class, "", Region.getRegion(Regions.EU_CENTRAL_1)); Assertions.assertThat(other).isNotNull(); Assertions.assertThat(other).isEqualTo(client); System.out.println(other.toString()); TimeUnit.SECONDS.sleep(2); } }
static void configureServiceClient(AmazonWebServiceClient client, Optional<String> endpoint, Optional<String> regionName) { // Configure endpoint or region. Endpoint takes precedence over region. if (endpoint.isPresent()) { client.setEndpoint(endpoint.get()); } else if (regionName.isPresent()) { Regions region; try { region = Regions.fromName(regionName.get()); } catch (IllegalArgumentException e) { throw new ConfigException("Illegal AWS region: " + regionName.get()); } client.setRegion(Region.getRegion(region)); } }
private C createClient() { Class<? extends C> clientImplType = factory.getClientImplType(); C client = ReflectionUtils.newInstance( clientImplType, credentials, configuration); if (client instanceof AmazonWebServiceClient) { AmazonWebServiceClient awsc = (AmazonWebServiceClient) client; if (region != null) { awsc.setRegion(region); } if (endpoint != null) { awsc.setEndpoint(endpoint); } } return client; }
@Override public void configure(final Env env, final Config config, final Binder binder) { callbacks.build().forEach(it -> { ConfigCredentialsProvider creds = new ConfigCredentialsProvider(config); AmazonWebServiceClient service = it.apply(creds, config); creds.service(service.getServiceName()); Class serviceType = service.getClass(); Class[] interfaces = serviceType.getInterfaces(); if (interfaces.length > 0) { // pick first binder.bind(interfaces[0]).toInstance(service); } binder.bind(serviceType).toInstance(service); env.onStop(new AwsShutdownSupport(service)); after(env, binder, config, service); }); }
static <T extends AmazonWebServiceClient> T createClient( Class<T> clientType, Identity identity ) { Regions r = Regions.US_EAST_1; if ( StringUtils.isNotBlank( identity.getAwsRegionName() ) ) { r = Regions.fromName( identity.getAwsRegionName() ); } AWSCredentialsProvider creds = new StaticCredentialsProvider( new BasicAWSCredentials( identity.getAwsAccessKeyId(), identity.getAwsSecretKey() ) ); return Region.getRegion( r ).createClient( clientType, creds, null ); }
/** * Creates a new service client of the class given and configures it. If * credentials or config are null, defaults will be used. * * @param serviceClass The service client class to instantiate, e.g. AmazonS3Client.class * @param credentials The credentials provider to use, or null for the default * credentials provider * @param config The configuration to use, or null for the default * configuration * @deprecated use appropriate {@link com.amazonaws.client.builder.AwsClientBuilder} implementation * for the service being constructed. For example: * {@code AmazonSNSClientBuilder.standard().withRegion(region).build();} */ @Deprecated public <T extends AmazonWebServiceClient> T createClient(Class<T> serviceClass, AWSCredentialsProvider credentials, ClientConfiguration config) { Constructor<T> constructor; T client; try { if (credentials == null && config == null) { constructor = serviceClass.getConstructor(); client = constructor.newInstance(); } else if (credentials == null) { constructor = serviceClass.getConstructor(ClientConfiguration.class); client = constructor.newInstance(config); } else if (config == null) { constructor = serviceClass.getConstructor(AWSCredentialsProvider.class); client = constructor.newInstance(credentials); } else { constructor = serviceClass.getConstructor(AWSCredentialsProvider.class, ClientConfiguration.class); client = constructor.newInstance(credentials, config); } client.setRegion(this); return client; } catch (Exception e) { throw new RuntimeException("Couldn't instantiate instance of " + serviceClass, e); } }
@Deprecated public ExecutionContext(List<RequestHandler2> requestHandler2s, boolean isMetricEnabled, AmazonWebServiceClient awsClient) { this.requestHandler2s = requestHandler2s; awsRequestMetrics = isMetricEnabled ? new AWSRequestMetricsFullSupport() : new AWSRequestMetrics(); this.awsClient = awsClient; this.signerProvider = new SignerProvider() { @Override public Signer getSigner(SignerProviderContext context) { return getSignerByURI(context.getUri()); } }; }
/** * Region and endpoint logic is tightly coupled to the client class right now so it's easier to * set them after client creation and let the normal logic kick in. Ideally this should resolve * the endpoint and signer information here and just pass that information as is to the client. * * @param clientInterface Client to configure */ @SdkInternalApi final TypeToBuild configureMutableProperties(TypeToBuild clientInterface) { AmazonWebServiceClient client = (AmazonWebServiceClient) clientInterface; setRegion(client); client.makeImmutable(); return clientInterface; }
@Test public void legacyGetSignerBehavior() throws Exception { AmazonWebServiceClient webServiceClient = mock(AmazonWebServiceClient.class); ExecutionContext executionContext = new ExecutionContext(null, false, webServiceClient); URI testUri = new URI("http://foo.amazon.com"); executionContext.getSigner(SignerProviderContext.builder().withUri(testUri).build()); verify(webServiceClient, times(1)).getSignerByURI(testUri); }
@Inject public AWSCucumberStepdefs(AmazonWebServiceClient client) { this.client = client; this.client.setRegion(RegionUtils.getRegion("us-east-1")); Class<?> httpClientClass = Classes.childClassOf(AmazonWebServiceClient.class, this.client); this.packageName = httpClientClass.getPackage().getName(); }
@Test public void test() { Regions[] gotRegion = new Regions[1]; String[] gotEndpoint = new String[1]; AmazonWebServiceClient client = new AmazonWebServiceClient(new ClientConfiguration()){ @Override public AmazonWebServiceClient withRegion(Regions region) { gotRegion[0] = region; return this; } @Override public AmazonWebServiceClient withEndpoint(String endpoint) { gotEndpoint[0] = endpoint; return this; } }; AmazonWebServiceClient got = _amazonWebServiceClients.withEndpoint(client, URI.create("ddb://us-east-1")); assertEquals(client, got); assertEquals(Regions.US_EAST_1, gotRegion[0]); assertNull(gotEndpoint[0]); gotRegion[0] = null; gotEndpoint[0] = null; got = _amazonWebServiceClients.withEndpoint(client, URI.create("https://aws.example.com")); assertEquals(client, got); assertNull(gotRegion[0]); assertEquals("https://aws.example.com", gotEndpoint[0]); gotRegion[0] = null; gotEndpoint[0] = null; got = _amazonWebServiceClients.withEndpoint(client, URI.create("ddb://aws.example.com")); assertEquals(client, got); assertNull(gotRegion[0]); assertEquals("https://aws.example.com", gotEndpoint[0]); }
static void shutdown(AmazonWebServiceClient client) { try { client.shutdown(); } catch (RuntimeException e) { // ignore } }
private void after(final Env env, final Binder binder, final Config config, final AmazonWebServiceClient service) { after.forEach(it -> { try { Object dep = it.apply(service, config); requireNonNull(dep, "A nonnull value is required."); Class type = dep.getClass(); binder.bind(type).toInstance(dep); env.onStop(new AwsShutdownSupport(dep)); } catch (ClassCastException ex) { log.debug("ignoring callback {}", it); } }); }
@SuppressWarnings({"rawtypes", "unchecked" }) @Test public void withServiceWithoutInterface() throws Exception { AmazonWebServiceClient aws = new AmazonWebServiceClient(new ClientConfiguration()) { @Override public String getServiceName() { return "s3"; } }; new MockUnit(Env.class, Config.class, Binder.class) .expect(unit -> { Config config = unit.get(Config.class); expect(config.hasPath("aws.s3.accessKey")).andReturn(false); expect(config.hasPath("aws.s3.secretKey")).andReturn(false); expect(config.hasPath("aws.s3.sessionToken")).andReturn(false); expect(config.hasPath("aws.sessionToken")).andReturn(false); expect(config.getString("aws.accessKey")).andReturn("accessKey"); expect(config.getString("aws.secretKey")).andReturn("secretKey"); }) .expect(unit -> { AnnotatedBindingBuilder abbAWSC = unit.mock(AnnotatedBindingBuilder.class); abbAWSC.toInstance(aws); Binder binder = unit.get(Binder.class); expect(binder.bind(aws.getClass())).andReturn(abbAWSC); }) .expect(unit -> { Env env = unit.get(Env.class); expect(env.onStop(isA(AwsShutdownSupport.class))).andReturn(env); }) .run(unit -> { new Aws() .with(creds -> aws) .configure(unit.get(Env.class), unit.get(Config.class), unit.get(Binder.class)); }); }
@Test public void defaults() throws Exception { new MockUnit(AmazonWebServiceClient.class) .run(unit -> { new AwsShutdownSupport(unit.get(AmazonWebServiceClient.class)); }); }
@Test public void stop() throws Exception { new MockUnit(AmazonWebServiceClient.class) .expect(unit -> { unit.get(AmazonWebServiceClient.class).shutdown(); }) .run(unit -> { AwsShutdownSupport aws = new AwsShutdownSupport(unit.get(AmazonWebServiceClient.class)); aws.run(); aws.run(); }); }
@SuppressWarnings("unchecked") public <T> T getService(Class<T> serviceClazz) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { Class<?> paramTypes[] = new Class<?>[]{AWSCredentialsProvider.class, ClientConfiguration.class}; ClientConfiguration newClientConfiguration = new ClientConfiguration(this.clientConfiguration); if (AmazonS3.class.isAssignableFrom(serviceClazz)) { newClientConfiguration = newClientConfiguration.withSignerOverride("AWSS3V4SignerType"); } else { newClientConfiguration = newClientConfiguration.withSignerOverride(null); } Object params[] = new Object[]{creds, newClientConfiguration}; T resultObj = (T) ConstructorUtils.invokeConstructor(serviceClazz, params, paramTypes); if (DEFAULT_REGION.equals(defaultString(region, DEFAULT_REGION))) { return resultObj; } else { for (ServiceEndpointFormatter formatter : ServiceEndpointFormatter.values()) { if (formatter.matches(resultObj)) { ((AmazonWebServiceClient) resultObj).setEndpoint(getEndpointFor(formatter)); break; } } } return resultObj; }
public <T extends AmazonWebServiceClient> String getEndpointFor(T client) { try { URI endpointUri = (URI) FieldUtils.readField(client, "endpoint", true); return endpointUri.toASCIIString(); } catch (Exception e) { return null; } }
protected AmazonWebServiceClient getAwsClient() { return awsClient; }
public AmazonWebServiceClient getAwsClient() { return awsClient; }
public void setAwsClient(final AmazonWebServiceClient awsClient) { this.awsClient = awsClient; }
public Builder withAwsClient(final AmazonWebServiceClient awsClient) { setAwsClient(awsClient); return this; }
public DefaultSignerProvider(final AmazonWebServiceClient awsClient, final Signer defaultSigner) { this.awsClient = awsClient; this.defaultSigner = defaultSigner; }
public ServiceClientHolderInputStream(InputStream in, AmazonWebServiceClient client) { super(in); this.client = client; }
public S3SignerProvider(final AmazonWebServiceClient awsClient, final Signer defaultSigner) { this.awsClient = awsClient; this.signer = defaultSigner; }
@Override protected void configure() { bind(AmazonWebServiceClient.class).to(AWSKMSClient.class); }
private <T extends AmazonWebServiceClient> T withProvider(T client) { client.setEndpoint("http://localhost:" + port); return client; }
private static <M extends AmazonWebServiceClient> M createAmazonClientInstance(Class<M> clientClass, Region region) { return region.createClient(clientClass, getAWSCredentialsProviderChain(), new ClientConfiguration()); }
@Override public <T extends AmazonWebServiceClient> T getClient(final Class<T> type, final String accountId, final Region region) { @SuppressWarnings("unchecked") final Key k = new Key(type, accountId, region); return type.cast(cache.getUnchecked(k)); }
@Override public <T extends AmazonWebServiceClient> T getClient(final Class<T> type) { return clientProvider.getClient(type, getAccountId(), getRegion()); }
/** * see factory comment * * @param <T> the class of the client to create * @param clientClass the class of the client to create * @return the created client */ public static <T extends AmazonWebServiceClient> T createClient(Class<T> clientClass) { String regionName = System.getProperty("aws.region", "eu-west-1"); Region region = Region.getRegion(Regions.fromName(regionName)); return region.createClient(clientClass, null, null); }
/** * Bind an {@link AmazonWebServiceClient} instances as Guice service. * * <pre> * { * use(new Aws() * .with(creds {@literal ->} new AmazonS3Client(creds)) * ); * </pre> * * @param callback A creation callback. * @return This module. */ public Aws with( final BiFunction<AWSCredentialsProvider, Config, AmazonWebServiceClient> callback) { requireNonNull(callback, "Callback is required."); callbacks.add(callback); return this; }
/** * Like {@link #with(BiFunction)} but it depends on a previously created service. * * <pre> * { * use(new Aws() * .with(creds {@literal ->} new AmazonS3Client(creds)) * .with(creds {@literal ->} new AmazonSQSClient(creds)) * .doWith((AmazonS3Client s3) {@literal ->} new TransferManager(s3)) * ); * </pre> * * It will bind a <code>TransferManager</code> as a Guice service. * * @param callback A creation callback. * @param <T> Aws service type. * @return This module. */ public <T extends AmazonWebServiceClient> Aws doWith( final BiFunction<T, Config, Object> callback) { requireNonNull(callback, "Callback is required."); after.add(callback); return this; }
/** * Bind an {@link AmazonWebServiceClient} instances as Guice service. * * <pre> * { * use(new Aws() * .with((creds, conf) {@literal ->} { * AmazonS3Client s3 = new AmazonS3Client(creds); * s3.setXXX(conf.getString("XXXX")); * return s3; * }) * ); * </pre> * * @param callback A creation callback. * @return This module. */ public Aws with(final Function<AWSCredentialsProvider, AmazonWebServiceClient> callback) { return with((creds, conf) -> callback.apply(creds)); }
/** * Like {@link #with(Function)} but it depends on a previously created service. * * <pre> * { * use(new Aws() * .with(creds {@literal ->} new AmazonS3Client(creds)) * .with(creds {@literal ->} new AmazonSQSClient(creds)) * .doWith((AmazonS3Client s3) {@literal ->} new TransferManager(s3)) * ); * </pre> * * It will bind a <code>TransferManager</code> as a Guice service. * * @param callback A creation callback. * @param <T> Aws service type. * @return This module. */ public <T extends AmazonWebServiceClient> Aws doWith(final Function<T, Object> callback) { requireNonNull(callback, "Callback is required."); return doWith((s, c) -> callback.apply((T) s)); }