Java import AWS Access Keys from Session

0 votes

I'm creating a Java script that does an API Gateway endpoint call. However, I must supply the AWS access keys and session token to my script for it to function. My code is currently functional, but the AWS keys are hard-coded. How can I transfer the keys without having them hardcoded? My current code is as follows:

import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import auth.AWS4SignerBase;
import auth.AWS4SignerForAuthorizationHeader;
import util.HttpUtils;

class Main {
  public static void main(String[] args) throws Exception {

    URL endpointUrl = new URL("https://lmno456.execute-api.us-east-1.amazonaws.com/prod/controlplane/model/a1");

    var regionName = "us-east-1";
    var awsAccessKey = "abc123";
    var awsSecretKey = "def234";
    var awsSessionToken = "xyz789==";

    // for a simple GET, we have no body so supply the precomputed 'empty' hash
    Map<String, String> headers = new HashMap<String, String>();
    headers.put("x-amz-content-sha256", AWS4SignerBase.EMPTY_BODY_SHA256);
    headers.put("X-Amz-Security-Token", awsSessionToken);

    AWS4SignerForAuthorizationHeader signer = new AWS4SignerForAuthorizationHeader(endpointUrl, "GET", "execute-api", regionName);

    String authorization = signer.computeSignature(headers,
                                            null, // no query parameters
                                            AWS4SignerBase.EMPTY_BODY_SHA256,
                                            awsAccessKey,
                                            awsSecretKey);

    headers.put("Authorization", authorization);
    String response = HttpUtils.invokeHttpRequest(endpointUrl, "GET", headers, null);
    System.out.println("--------- Response content ---------");
    System.out.println(response);
    System.out.println("------------------------------------");
  }
}

To retrieve the access and session keys, I'm ideally searching for a function comparable to boto3 credentials = session.get credentials() in Python. For an AWS lambda function, this is what I'm doing.

Dec 14, 2022 in AWS by Roshan
• 300 points
240 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In AWS

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

how to access AWS S3 from Lambda in VPC

With boto3, the S3 urls are virtual by default, ...READ MORE

answered Sep 28, 2018 in AWS by Priyaj
• 58,090 points
9,616 views
0 votes
1 answer

How to access AWS Lambda environment variables using Java?

In Spring Core change the PropertySourcesPlaceholderConfigurer class can ...READ MORE

answered Oct 24, 2018 in AWS by Priyaj
• 58,090 points
2,542 views
0 votes
1 answer

How to invoke the AWS lambda function from Java code

There are 2 methods in this class which should ...READ MORE

answered Feb 19, 2019 in AWS by Archana
• 5,640 points
5,652 views
+1 vote
0 answers

How can i start my AWS - glue job from my java application

i want to start and stop my ...READ MORE

Jul 15, 2019 in AWS by Dhanesh
1,203 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP