How to create pubsub topic with Customer-managed encryption key in JAVA - java

I am trying to create Pub/Sub topic with customer-managed encryption keys in Java.
In Python we can create a topic using CMEK location as parameter as below:
topic = client.create_topic(
topic_path,
kms_key_name=cmek_location,
message_storage_policy=get_allowed_region()
)
In java I am using the following:
TopicAdminClient topicAdminClient = TopicAdminClient.create(topicAdminSettings);
topicAdminClient.createTopic(topic);
How can we use the CMEK location in java code?

For that purpose you can use the following code, extracted from the createTopic method documentation:
try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
Topic request =
Topic.newBuilder()
.setName(TopicName.ofProjectTopicName("[PROJECT]", "[TOPIC]").toString())
.putAllLabels(new HashMap<String, String>())
.setMessageStoragePolicy(MessageStoragePolicy.newBuilder().build())
.setKmsKeyName("kmsKeyName412586233")
.setSchemaSettings(SchemaSettings.newBuilder().build())
.setSatisfiesPzs(true)
.setMessageRetentionDuration(Duration.newBuilder().build())
.build();
Topic response = topicAdminClient.createTopic(request);
}
Basically you provide a template of the Topic you want to create.
In your use case I suppose it will look like similar to this:
try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
Topic request =
Topic.newBuilder()
.setName(TopicName.ofProjectTopicName("[PROJECT]", "[TOPIC]").toString())
.setKmsKeyName("kmsKeyName412586233") //cmek location
.setMessageStoragePolicy(
MessageStoragePolicy.newBuilder()
.addAllowedPersistenceRegions("us-central1") // get_allowed_region
.build()
)
.build();
Topic response = topicAdminClient.createTopic(request);
}
Please, pay attention to the setKmsKeyName method.
The API is described in this GCP documentation.

Related

Write camel routes running after last processor ends

I have three component: rest, Cassandra and Kafka and I am using Apache camel. When the request receives, I want to add a record in Cassandra and after that, adding that record to Kafka. Finally generating rest response. May be pipeline is not a good solution for me! Because Cassandra part is InOnly and hasn't any out exchange!
I wrote this route:
rest().path("/addData")
.consumes("text/plain")
.produces("application/json")
.post()
.to("direct:requestData");
from("direct:requestData")
.pipeline("direct:init",
"cql://localhost/myTable?cql=" + CQL,
"direct:addToKafka"
)
.process(exchange -> {
var currentBody = (List<?>) exchange.getIn().getBody();
var body = new Data((String) currentBody.get(0), (Long) currentBody.get(1), (String) currentBody.get(2));
exchange.getIn.setBody(body.toJsonString());
});
from("direct:init")
.process(exchange -> {
var currentBody = exchange.getIn().getBody();
var body = Arrays.asList(generateId(), generateTimeStamp, currentBody);
exchange.getIn().setBody(body);
});
from("direct:addToKafka")
.process(
// do sth to add kafka
);
I tried sth such setting patternExtention to InOut for Cassandra!!! finally understand that this is impossible! because patternExtention used for consumer and I used Cassandra in a route as producer.

How to obtain `ApproximateReceiveCount` from an SQS message

I'm using Amazon SQS. My goal is to read ApproximateReceiveCount attribute from ReceiveMessage API action using the Java SDK (v2.10.4, Java 11).
I tried the following code, but message.attributes() doesn't contain the required key:
String getApproximateReceiveCount() {
var receiveMessageRequest = ReceiveMessageRequest.builder()
.queueUrl("https://sqs.eu-west-1.amazonaws.com/012345678910/my-example-queue")
.build();
var sqsClient = SqsClient.builder().endpointOverride(URI.create("http://localhost:4576")).build();
var response = sqsClient.receiveMessage(receiveMessageRequest);
var message = response.messages().get(0);
return message.attributes().get(MessageSystemAttributeName.APPROXIMATE_RECEIVE_COUNT);
}
How to go about receiving an entry for MessageSystemAttributeName.APPROXIMATE_RECEIVE_COUNT key, in this map?
As per the documentation page to ReceiveMessage which you linked, there is a parameter called AttributeName.N described as
A list of attributes that need to be returned along with each message. These attributes include:
[...]
ApproximateReceiveCount – Returns the number of times a message has been received from the queue but not deleted.
Therefore you need to ask for the attribute in the request, for it to be available in the response. To do that use ReceiveMessageRequestBuilder.attributeNamesWithStrings() method like so:
String getApproximateReceiveCount() {
var receiveMessageRequest = ReceiveMessageRequest.builder()
.queueUrl("https://sqs.eu-west-1.amazonaws.com/012345678910/my-example-queue")
.attributeNamesWithStrings(MessageSystemAttributeName.APPROXIMATE_RECEIVE_COUNT.toString())
.build();
var sqsClient = SqsClient.builder().endpointOverride(URI.create("http://localhost:4576")).build();
var response = sqsClient.receiveMessage(receiveMessageRequest);
var message = response.messages().get(0);
return message.attributes().get(MessageSystemAttributeName.APPROXIMATE_RECEIVE_COUNT);
}
Note that there are two similarly named methods, which you can't use:
.attributeNames() - the parameter enum doesn't list the required key,
.messageAttributeNames() - corresponds to attributes sent along with the message body.

Adding reviewers to pull request using github-java sdk

I am trying to use https://github.com/eclipse/egit-github/tree/master/org.eclipse.egit.github.core to create a pull request, but I can't find a way to add reviews to the pull request. The V3 Github API supports adding reviewers as mentioned here, https://developer.github.com/v3/pulls/review_requests/#create-a-review-request. I am trying to create the pull request as follows.
private void createPullRequest(
String repositoryName,
String repositoryOwner,
String pullRequestTitle,
String pullRequestBody,
String branchSource,
String branchDestination
) throws IOException {
logger.info("starting to create pull request");
var gitHubClient = new GitHubClient();
gitHubClient.setCredentials("x", "y");
var repositoryService = new RepositoryService(gitHubClient);
var repository = repositoryService.getRepository(repositoryOwner, repositoryName);
var pullRequestService = new PullRequestService(gitHubClient);
var pullRequest = new PullRequest();
pullRequest.setTitle(pullRequestTitle);
pullRequest.setBody(pullRequestBody);
pullRequest.setHead(
new PullRequestMarker().setRef(branchSource).setLabel(branchSource)
);
pullRequest.setBase(
new PullRequestMarker().setRef(branchDestination).setLabel(branchDestination)
);
logger.info("Finally starting to push PR");
pullRequestService.createPullRequest(repository, pullRequest);
logger.info("PR should be created now");
}
From the documentation it says, it supports 100% of Github V3 API. But I am unable to find any reference in code & online for adding reviewers.
Thanks!

Get Topic ARN from aws using java

Can someone please let me know how to get the exact topic arn from SNS using AmazonSNSClient in java ?
I want to use it in the following PutMetricAlarmRequest
.withAlarmActions(awsClient.getAmazonSNSClient(). ?)
You can use the following code to get a list of all SNS topics. You can call Topic::getTopicArn() to get the ARN as a String.
AmazonSNSClient snsClient = new AmazonSNSClient(new DefaultAWSCredentialsProviderChain());
snsClient.setRegion(Region.getRegion(Regions.US_WEST_2));
List<Topic> topicArns = new ArrayList<>();
ListTopicsResult result = snsClient.listTopics();
topicArns.addAll(result.getTopics());
while (result.getNextToken() != null) {
result = snsClient.listTopics(result.getNextToken());
topicArns.addAll(result.getTopics());
}
for (Topic topic : topicArns) {
System.out.println(topic.getTopicArn());
}
snsClient.shutdown();
Change the credentials provider and region to match your account, and make sure you have the appropriate permissions set in IAM for your user.

How to perform Amazon Cloud Search with .net code?

I am learning Amazon Cloud Search but I couldn't find any code in either C# or Java (though I am creating in C# but if I can get code in Java then I can try converting in C#).
This is just 1 code I found in C#: https://github.com/Sitefinity-SDK/amazon-cloud-search-sample/tree/master/SitefinityWebApp.
This is 1 method i found in this code:
public IResultSet Search(ISearchQuery query)
{
AmazonCloudSearchDomainConfig config = new AmazonCloudSearchDomainConfig();
config.ServiceURL = "http://search-index2-cdduimbipgk3rpnfgny6posyzy.eu-west-1.cloudsearch.amazonaws.com/";
AmazonCloudSearchDomainClient domainClient = new AmazonCloudSearchDomainClient("AKIAJ6MPIX37TLIXW7HQ", "DnrFrw9ZEr7g4Svh0rh6z+s3PxMaypl607eEUehQ", config);
SearchRequest searchRequest = new SearchRequest();
List<string> suggestions = new List<string>();
StringBuilder highlights = new StringBuilder();
highlights.Append("{\'");
if (query == null)
throw new ArgumentNullException("query");
foreach (var field in query.HighlightedFields)
{
if (highlights.Length > 2)
{
highlights.Append(", \'");
}
highlights.Append(field.ToUpperInvariant());
highlights.Append("\':{} ");
SuggestRequest suggestRequest = new SuggestRequest();
Suggester suggester = new Suggester();
suggester.SuggesterName = field.ToUpperInvariant() + "_suggester";
suggestRequest.Suggester = suggester.SuggesterName;
suggestRequest.Size = query.Take;
suggestRequest.Query = query.Text;
SuggestResponse suggestion = domainClient.Suggest(suggestRequest);
foreach (var suggest in suggestion.Suggest.Suggestions)
{
suggestions.Add(suggest.Suggestion);
}
}
highlights.Append("}");
if (query.Filter != null)
{
searchRequest.FilterQuery = this.BuildQueryFilter(query.Filter);
}
if (query.OrderBy != null)
{
searchRequest.Sort = string.Join(",", query.OrderBy);
}
if (query.Take > 0)
{
searchRequest.Size = query.Take;
}
if (query.Skip > 0)
{
searchRequest.Start = query.Skip;
}
searchRequest.Highlight = highlights.ToString();
searchRequest.Query = query.Text;
searchRequest.QueryParser = QueryParser.Simple;
var result = domainClient.Search(searchRequest).SearchResult;
//var result = domainClient.Search(searchRequest).SearchResult;
return new AmazonResultSet(result, suggestions);
}
I have already created domain in Amazon Cloud Search using AWS console and uploaded document using Amazon predefine configuration option that is movie Imdb json file provided by Amazon for demo.
But in this method I am not getting how to use this method, like if I want to search Director name then how do I pass in this method as because this method parameter is of type ISearchQuery?
I'd suggest using the official AWS CloudSearch .NET SDK. The library you were looking at seems fine (although I haven't look at it any detail) but the official version is more likely to expose new CloudSearch features as soon as they're released, will be supported if you need to talk to AWS support, etc, etc.
Specifically, take a look at the SearchRequest class -- all its params are strings so I think that obviates your question about ISearchQuery.
I wasn't able to find an example of a query in .NET but this shows someone uploading docs using the AWS .NET SDK. It's essentially the same procedure as querying: creating and configuring a Request object and passing it to the client.
EDIT:
Since you're still having a hard time, here's an example. Bear in mind that I am unfamiliar with C# and have not attempted to run or even compile this but I think it should at least be close to working. It's based off looking at the docs at http://docs.aws.amazon.com/sdkfornet/v3/apidocs/
// Configure the Client that you'll use to make search requests
string queryUrl = #"http://search-<domainname>-xxxxxxxxxxxxxxxxxxxxxxxxxx.us-east-1.cloudsearch.amazonaws.com";
AmazonCloudSearchDomainClient searchClient = new AmazonCloudSearchDomainClient(queryUrl);
// Configure a search request with your query
SearchRequest searchRequest = new SearchRequest();
searchRequest.Query = "potato";
// TODO Set your other params like parser, suggester, etc
// Submit your request via the client and get back a response containing search results
SearchResponse searchResponse = searchClient.Search(searchRequest);

Categories