Java Code Examples for com.amazonaws.auth.AWS4Signer#setServiceName()

The following examples show how to use com.amazonaws.auth.AWS4Signer#setServiceName() . 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: AwsUtil.java    From datacollector with Apache License 2.0 6 votes vote down vote up
public static AwsRequestSigningApacheInterceptor getAwsSigV4Interceptor(String awsServiceName,
                                                                        AwsRegion awsRegion,
                                                                        String otherEndpoint,
                                                                        CredentialValue awsAccessKeyId,
                                                                        CredentialValue awsSecretAccessKey)
    throws StageException {
  AWS4Signer signer = new AWS4Signer();
  signer.setServiceName(awsServiceName);

  if (awsRegion == AwsRegion.OTHER) {
    if (otherEndpoint == null || otherEndpoint.isEmpty()) {
      return null;
    }
    signer.setRegionName(otherEndpoint);
  } else {
    signer.setRegionName(awsRegion.getId());
  }

  return new AwsRequestSigningApacheInterceptor(
      awsServiceName,
      signer,
      AwsUtil.getCredentialsProvider(awsAccessKeyId, awsSecretAccessKey));
}
 
Example 2
Source File: Sample.java    From aws-request-signing-apache-interceptor with Apache License 2.0 5 votes vote down vote up
CloseableHttpClient signingClientForServiceName(String serviceName) {
    AWS4Signer signer = new AWS4Signer();
    signer.setServiceName(serviceName);
    signer.setRegionName(AWS_REGION);

    HttpRequestInterceptor interceptor = new AWSRequestSigningApacheInterceptor(serviceName, signer, credentialsProvider);
    return HttpClients.custom()
            .addInterceptorLast(interceptor)
            .build();
}
 
Example 3
Source File: AwsIamAuthentication.java    From spring-vault with Apache License 2.0 5 votes vote down vote up
private static String getSignedHeaders(AwsIamAuthenticationOptions options, AWSCredentials credentials) {

		Map<String, String> headers = createIamRequestHeaders(options);

		AWS4Signer signer = new AWS4Signer();

		DefaultRequest<String> request = new DefaultRequest<>("sts");

		request.setContent(new ByteArrayInputStream(REQUEST_BODY.getBytes()));
		request.setHeaders(headers);
		request.setHttpMethod(HttpMethodName.POST);
		request.setEndpoint(options.getEndpointUri());

		signer.setServiceName(request.getServiceName());
		signer.sign(request, credentials);

		Map<String, Object> map = new LinkedHashMap<>();

		for (Entry<String, String> entry : request.getHeaders().entrySet()) {
			map.put(entry.getKey(), Collections.singletonList(entry.getValue()));
		}

		try {
			return OBJECT_MAPPER.writeValueAsString(map);
		}
		catch (JsonProcessingException e) {
			throw new IllegalStateException("Cannot serialize headers to JSON", e);
		}
	}
 
Example 4
Source File: UrlSigningAuthConfig.java    From bender with Apache License 2.0 5 votes vote down vote up
public HttpRequestInterceptor getHttpInterceptor() {
  DefaultAWSCredentialsProviderChain cp = new DefaultAWSCredentialsProviderChain();

  AWS4Signer signer = new AWS4Signer();
  signer.setServiceName(this.service);
  signer.setRegionName(this.region.getName());

  return new AWSRequestSigningApacheInterceptor(this.service, signer, cp);
}
 
Example 5
Source File: SignedRequest.java    From charles-rest with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public T perform() {
    AWS4Signer signer = new AWS4Signer();
    String region = this.reg.read();
    if(region == null || region.isEmpty()) {
        throw new IllegalStateException("Mandatory sys property aws.es.region not specified!");
    }
    signer.setRegionName(this.reg.read());
    signer.setServiceName(this.base.request().getServiceName());
    signer.sign(this.base.request(), new AwsCredentialsFromSystem(this.accesskey, this.secretKey));
    return this.base.perform();
}
 
Example 6
Source File: SkdSignerUtil.java    From aws-signing-request-interceptor with MIT License 5 votes vote down vote up
static public String getExpectedAuthorizationHeader(Request request) throws Exception {
    // create the signable request
    DefaultRequest signableRequest = new DefaultRequest(null, request.getServiceName());
    signableRequest.setEndpoint(new URI("http://" + request.getHost()));
    signableRequest.setResourcePath(request.getUri());
    signableRequest.setHttpMethod(HttpMethodName.valueOf(request.getHttpMethod()));
    signableRequest.setContent(new StringInputStream(request.getBody()));
    if (request.getHeaders() != null)
        signableRequest.setHeaders(request.getHeaders());
    if (request.getQueryParams() != null) {
        Map<String, List<String>> convertedQueryParams = new HashMap<>();
        for (String paramName : request.getQueryParams().keySet()) {
            convertedQueryParams.put(paramName, new ArrayList<>(request.getQueryParams().get(paramName)));
        }
        signableRequest.setParameters(convertedQueryParams);
    }

    /*
       Init the signer class

       Note: Double uri encoding is off simple before the signature does not match the expected signature of the test cases
       if it is enabled.  This was a bit unexpected because AWSElasticsearchClient (AWS SDK Class) enabled double URI encoding
       in the signer by default.  I can only assume that double encoding is needed when accessing the service but not when accessing
       elasticsearch.
     */
    AWS4Signer aws4Signer = new AWS4Signer(false);
    aws4Signer.setServiceName(request.getServiceName());
    aws4Signer.setRegionName(request.getRegion());
    Method method1 = AWS4Signer.class.getDeclaredMethod("setOverrideDate", Date.class);
    method1.setAccessible(true);
    method1.invoke(aws4Signer, request.getDate());
    aws4Signer.sign(signableRequest, request.getCredentialsProvider().getCredentials());

    return (String) signableRequest.getHeaders().get("Authorization");
}
 
Example 7
Source File: IvonaSpeechCloudClient.java    From ivona-speechcloud-sdk-java with Apache License 2.0 5 votes vote down vote up
private void init() {
    exceptionUnmarshallers = new ArrayList<JsonErrorUnmarshaller>();
    exceptionUnmarshallers.add(new JsonErrorUnmarshaller());

    signer = new AWS4Signer();
    signer.setServiceName(SERVICE_NAME);

    setServiceNameIntern(SERVICE_NAME);

    HandlerChainFactory chainFactory = new HandlerChainFactory();
    requestHandler2s.addAll(chainFactory.newRequestHandlerChain("/com.ivona.services/tts/request.handlers"));
    requestHandler2s.addAll(chainFactory.newRequestHandlerChain("/com.ivona.services/tts/request.handler2s"));
}
 
Example 8
Source File: ElasticsearchBackendClient.java    From jmeter-elasticsearch-backend-listener with MIT License 4 votes vote down vote up
@Override
public void setupTest(BackendListenerContext context) throws Exception {
    try {
        this.filters = new HashSet<>();
        this.fields = new HashSet<>();
        this.modes = new HashSet<>(Arrays.asList("info", "debug", "error", "quiet"));
        this.bulkSize = Integer.parseInt(context.getParameter(ES_BULK_SIZE));
        this.timeoutMs = Integer.parseInt((context.getParameter(ES_TIMEOUT_MS)));
        this.buildNumber = (JMeterUtils.getProperty(ElasticsearchBackendClient.BUILD_NUMBER) != null
                && !JMeterUtils.getProperty(ElasticsearchBackendClient.BUILD_NUMBER).trim().equals(""))
                        ? Integer.parseInt(JMeterUtils.getProperty(ElasticsearchBackendClient.BUILD_NUMBER)) : 0;

        setSSLConfiguration(context);

        if (context.getParameter(ES_AWS_ENDPOINT).equalsIgnoreCase("")) {
            client = RestClient
                    .builder(new HttpHost(context.getParameter(ES_HOST),
                            Integer.parseInt(context.getParameter(ES_PORT)), context.getParameter(ES_SCHEME)))
                    .setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder.setConnectTimeout(5000)
                            .setSocketTimeout((int) timeoutMs))
                    .setFailureListener(new RestClient.FailureListener() {
                        @Override
                        public void onFailure(Node node) {
                            logger.error("Error with node: " + node.toString());
                        }
                    }).build();
        } else {
            AWS4Signer signer = new AWS4Signer();
            signer.setServiceName(SERVICE_NAME);
            signer.setRegionName(context.getParameter(ES_AWS_REGION));
            HttpRequestInterceptor interceptor = new AWSRequestSigningApacheInterceptor(SERVICE_NAME, signer,
                    credentialsProvider);
            client = RestClient.builder(HttpHost.create(context.getParameter(ES_AWS_ENDPOINT)))
                    .setHttpClientConfigCallback(hacb -> hacb.addInterceptorLast(interceptor)).build();
        }

        convertParameterToSet(context, ES_SAMPLE_FILTER, this.filters);
        convertParameterToSet(context, ES_FIELDS, this.fields);

        this.sender = new ElasticSearchMetricSender(client, context.getParameter(ES_INDEX).toLowerCase(),
                context.getParameter(ES_AUTH_USER), context.getParameter(ES_AUTH_PWD),
                context.getParameter(ES_AWS_ENDPOINT));
        this.sender.createIndex();
        this.esVersion = sender.getElasticSearchVersion();

        checkTestMode(context.getParameter(ES_TEST_MODE));
        super.setupTest(context);
    } catch (Exception e) {
        throw new IllegalStateException("Unable to connect to the ElasticSearch engine", e);
    }
}