从AWS Parameter Store在Java中获取值

huangapple go评论79阅读模式
英文:

Get value from AWS Parameter store in Java

问题

以下是已翻译的内容:

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);
    }
}

this.apiToken = StringParameter.valueForSecureStringParameter(this, "/JIRA/Token", 1);这一行中,第一个参数应为 Construct 对象。

英文:

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.

答案1

得分: 1

你正在使用来自aws-cdk的StringParameter.valueForSecureStringParameter方法,它是AWS的“基础设施即代码”服务。

你应该使用AWS Java SDK中AWSSimpleSystemsManagement接口中定义的getParameter()方法。

英文:

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.

huangapple
  • 本文由 发表于 2020年9月17日 18:02:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63935696.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定