下面列出了com.amazonaws.auth.AWS4Signer#setServiceName ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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));
}
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();
}
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);
}
}
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);
}
@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();
}
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");
}
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"));
}
@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);
}
}