2

I am having trouble fetching result from my amazon elastic search cluster using the amazon java SDK and an IAm user credential. Now the issue is that when the PATH string is equal to "/" then I am able to fetch the result correctly but when I try with a different path for e.g "/private-search" then I get a 403 forbidden error. Even when for the path that has public access I am getting a 403 forbidden error for this IAm user but it works if I remove "signer.sign(requestToSign, credentials);" line in performSigningSteps method(for public resource only).

My policy in AWS gives this IAM user access to everything in my elastic search service. And also what can I do to avoid hard-coding the access key and secret key in source code?

private static final String SERVICE_NAME = "es";

private static final String REGION = "region-name";

private static final String HOST = "host-name";

private static final String ENDPOINT_ROOT = "http://" + HOST;

private static final String PATH = "/private-search";

private static final String ENDPOINT = ENDPOINT_ROOT + PATH;

private static String accessKey = "IAmUserAccesskey"

private static String secretKey = "IAmUserSecretkey"

public static void main(String[] args) {
       // Generate the request
       Request<?> request = generateRequest();
      // Perform Signature Version 4 signing
       performSigningSteps(request);
     // Send the request to the server
       sendRequest(request);
}

private static Request<?> generateRequest() {
    Request<?> request = new DefaultRequest<Void>(SERVICE_NAME);
    request.setContent(new ByteArrayInputStream("".getBytes()));
    request.setEndpoint(URI.create(ENDPOINT));
    request.setHttpMethod(HttpMethodName.GET);
    return request;
}

private static void performSigningSteps(Request<?> requestToSign) {
    AWS4Signer signer = new AWS4Signer();
    signer.setServiceName(requestToSign.getServiceName());
    signer.setRegionName(REGION);       
    AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
    signer.sign(requestToSign, credentials);
}

private static void sendRequest(Request<?> request) {
    ExecutionContext context = new ExecutionContext();

    ClientConfiguration clientConfiguration = new ClientConfiguration();
    AmazonHttpClient client = new AmazonHttpClient(clientConfiguration);

    MyHttpResponseHandler<Void> responseHandler = new MyHttpResponseHandler<Void>();
    MyErrorHandler errorHandler = new MyErrorHandler();
    Void response = client.execute(request, responseHandler, errorHandler, context);
}


public static class MyHttpResponseHandler<T> implements HttpResponseHandler<AmazonWebServiceResponse<T>> {

    @Override
    public AmazonWebServiceResponse<T> handle(com.amazonaws.http.HttpResponse response) throws Exception {

        InputStream responseStream = response.getContent();
        String responseString = convertStreamToString(responseStream);
        System.out.println(responseString);

        AmazonWebServiceResponse<T> awsResponse = new AmazonWebServiceResponse<T>();
        return awsResponse;
    }

    @Override
    public boolean needsConnectionLeftOpen() {
        return false;
    }
}



public static class MyErrorHandler implements HttpResponseHandler<AmazonServiceException> {

    @Override
    public AmazonServiceException handle(com.amazonaws.http.HttpResponse response) throws Exception {
        System.out.println("In exception handler!");

        AmazonServiceException ase = new AmazonServiceException("exception.");
        ase.setStatusCode(response.getStatusCode());
        ase.setErrorCode(response.getStatusText());
        return ase;
    }

    @Override
    public boolean needsConnectionLeftOpen() {
        return false;
    }
}

public static String convertStreamToString(InputStream is) throws IOException {
    // To convert the InputStream to String we use the
    // Reader.read(char[] buffer) method. We iterate until the
    // Reader return -1 which means there's no more data to
    // read. We use the StringWriter class to produce the string.
    if (is != null) {
        Writer writer = new StringWriter();

        char[] buffer = new char[1024];
        try {
            Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        }
        finally {
            is.close();
        }
        return writer.toString();
    }
    return "";
}

0 Answers0