Search code examples
javaaws-cdkjira-rest-java-apiaws-parameter-store

Get value from AWS Parameter store in Java


I need to fetch an api token which is stored in the aws parameter store using my Java code. I need the api token to authenticate a JiraClient. But I am not sure of the correct way of retrieving the value. Here is what I have tried.

import com.atlassian.jira.rest.client.api.IssueRestClient;
import com.atlassian.jira.rest.client.api.JiraRestClient;
import com.atlassian.jira.rest.client.api.domain.input.IssueInput;
import com.atlassian.jira.rest.client.api.domain.input.IssueInputBuilder;
import com.atlassian.jira.rest.client.internal.async.AsynchronousJiraRestClientFactory;
import software.amazon.awscdk.services.ssm.StringParameter;

import java.net.URI;

    public class JiraClient {
        private final String email;
        private final String apiToken;
        private final String jiraUrl;
        private final JiraRestClient restClient;
    
        public JiraClient(String email, String jiraUrl) {
    
            this.email = email;
            this.jiraUrl = jiraUrl;
            this.apiToken = StringParameter.valueForSecureStringParameter(this, "/JIRA/Token", 1);
            this.restClient = getJiraRestClient();
        }
    
        private JiraRestClient getJiraRestClient() {
            return new AsynchronousJiraRestClientFactory()
                    .createWithBasicHttpAuthentication(getJiraUri(), this.email, this.apiToken);
        }
    
        public String createIssue(String projectKey, Long issueTypeId, String issueSummary) {
    
            IssueRestClient issueClient = restClient.getIssueClient();
            IssueInput newIssue = new IssueInputBuilder(projectKey, issueTypeId, issueSummary).build();
            return issueClient.createIssue(newIssue).claim().getKey();
        }
    
        private URI getJiraUri() {
            return URI.create(this.jiraUrl);
        }
    }

In the line this.apiToken = StringParameter.valueForSecureStringParameter(this, "/JIRA/Token", 1); the first parameter should be a Construct Object.


Solution

  • You are using the StringParameter.valueForSecureStringParameter method from aws-cdk which is an "Infrastructure as Code" service from AWS.

    You should be using the getParameter() method defined in the AWSSimpleSystemsManagement interface in the AWS Java SDK.