2

Amazon Java SDK と IAm ユーザー資格情報を使用して、Amazon エラスティック検索クラスターから結果を取得する際に問題があります。ここでの問題は、PATH 文字列が「/」に等しい場合、結果を正しく取得できますが、「/private-search」などの別のパスを使用しようとすると、403 禁止エラーが発生することです。パブリック アクセスがあるパスの場合でも、この IAm ユーザーに対して 403 禁止エラーが発生しますが、「signer.sign(requestToSign, credentials);」を削除すると機能します。performSigningSteps メソッドの行 (パブリック リソースのみ)。

AWS の私のポリシーは、この IAM ユーザーに私のエラスティック検索サービスのすべてへのアクセスを許可します。また、ソース コードでアクセス キーと秘密キーをハードコーディングしないようにするにはどうすればよいですか?

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 "";
}
4

0 に答える 0