From 6b2cbd63e8edcac0f1b19935663d95800ee34cb0 Mon Sep 17 00:00:00 2001 From: averikitsch Date: Fri, 19 Apr 2019 13:03:40 -0700 Subject: [PATCH 1/3] Add task sample with auth token --- tasks/README.md | 12 +++- tasks/pom.xml | 30 ++++++-- .../example/task/CreateHttpTaskWithToken.java | 70 +++++++++++++++++++ 3 files changed, 105 insertions(+), 7 deletions(-) create mode 100644 tasks/src/main/java/com/example/task/CreateHttpTaskWithToken.java diff --git a/tasks/README.md b/tasks/README.md index 760a0ba6605..82d2bb53051 100644 --- a/tasks/README.md +++ b/tasks/README.md @@ -81,5 +81,15 @@ Running the sample will create a task and send the task to the specific URL endpoint, with a payload specified: ``` -mvn exec:java -Dexec.mainClass="com.example.task.CreateHttpTask" +mvn exec:java@HttpTask" +``` + +### Using HTTP Targets with Authentication Headers + +In `CreateHttpTaskWithToken.java`, add your service account email in place of +`` to authenticate the OIDC token. + +Running the sample with command: +``` +mvn exec:java@WithToken" ``` diff --git a/tasks/pom.xml b/tasks/pom.xml index 5151ee378c0..c3a738c63b6 100644 --- a/tasks/pom.xml +++ b/tasks/pom.xml @@ -27,12 +27,12 @@ Copyright 2018 Google LLC The parent pom defines common style checks and testing strategies for our samples. Removing or replacing it should not affect the execution of the samples in anyway. --> - + 1.8 @@ -54,10 +54,28 @@ Copyright 2018 Google LLC org.codehaus.mojo exec-maven-plugin 1.4.0 - - com.example.task.CreateHttpTask - false - + + + HttpTask + + java + + + com.example.task.CreateHttpTask + false + + + + WithToken + + java + + + com.example.task.CreateHttpTaskWithToken + false + + + diff --git a/tasks/src/main/java/com/example/task/CreateHttpTaskWithToken.java b/tasks/src/main/java/com/example/task/CreateHttpTaskWithToken.java new file mode 100644 index 00000000000..f23aa45ee70 --- /dev/null +++ b/tasks/src/main/java/com/example/task/CreateHttpTaskWithToken.java @@ -0,0 +1,70 @@ +/* + * Copyright 2018 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.task; + +// [START cloud_tasks_create_http_task_with_token] +import com.google.cloud.tasks.v2beta3.CloudTasksClient; +import com.google.cloud.tasks.v2beta3.HttpMethod; +import com.google.cloud.tasks.v2beta3.HttpRequest; +import com.google.cloud.tasks.v2beta3.OidcToken; +import com.google.cloud.tasks.v2beta3.QueueName; +import com.google.cloud.tasks.v2beta3.Task; +import com.google.protobuf.ByteString; +import java.nio.charset.Charset; + +public class CreateHttpTaskWithToken { + + public static void main(String[] args) throws Exception { + String projectId = System.getenv("PROJECT_ID"); + String queueName = System.getenv("QUEUE_ID"); + String location = System.getenv("LOCATION_ID"); + String url = System.getenv("URL"); + + // Instantiates a client. + try (CloudTasksClient client = CloudTasksClient.create()) { + // Variables provided by the system variables. + // projectId = "my-project-id"; + // queueName = "my-appengine-queue"; + // location = "us-central1"; + // url = "https://example.com/tasks/create"; + String payload = "hello"; + + // Construct the fully qualified queue name. + String queuePath = QueueName.of(projectId, location, queueName).toString(); + + // Construct OIDC token + OidcToken.Builder oidcTokenBuilder = + OidcToken.newBuilder().setServiceAccountEmail(""); + + // Construct the task body. + Task.Builder taskBuilder = + Task.newBuilder() + .setHttpRequest( + HttpRequest.newBuilder() + .setBody(ByteString.copyFrom(payload, Charset.defaultCharset())) + .setHttpMethod(HttpMethod.POST) + .setUrl(url) + .setOidcToken(oidcTokenBuilder) + .build()); + + // Send create task request. + Task task = client.createTask(queuePath, taskBuilder.build()); + System.out.println("Task created: " + task.getName()); + } + } +} +// [END cloud_tasks_create_http_task_with_token] From c6abcfa0c0963f1a1ec3f66155fd101eed3af693 Mon Sep 17 00:00:00 2001 From: averikitsch Date: Fri, 19 Apr 2019 13:05:06 -0700 Subject: [PATCH 2/3] add parent to pom --- tasks/pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tasks/pom.xml b/tasks/pom.xml index c3a738c63b6..697eb29e61d 100644 --- a/tasks/pom.xml +++ b/tasks/pom.xml @@ -27,12 +27,12 @@ Copyright 2018 Google LLC The parent pom defines common style checks and testing strategies for our samples. Removing or replacing it should not affect the execution of the samples in anyway. --> - + 1.8 From 6fedeb8b85701d6bb6131663539357b7b931dfe8 Mon Sep 17 00:00:00 2001 From: averikitsch Date: Mon, 22 Apr 2019 09:19:43 -0700 Subject: [PATCH 3/3] Update comments --- tasks/README.md | 13 ++++++------- .../com/example/task/CreateHttpTaskWithToken.java | 5 +++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/tasks/README.md b/tasks/README.md index 82d2bb53051..dd13bd1d576 100644 --- a/tasks/README.md +++ b/tasks/README.md @@ -7,12 +7,11 @@ App Engine queues push tasks to an App Engine HTTP target. This directory contains both the App Engine app to deploy, as well as the snippets to run locally to push tasks to it, which could also be called on App Engine. -`CreateTask.java` is a simple command-line program to create -tasks to be pushed to the App Engine app. - -`TaskServlet.java` is the main App Engine app. This app serves as an endpoint to receive -App Engine task attempts. +`CreateHTTPTask.java` constructs a task with an HTTP target and pushes it +to your queue. +`CreateHTTPTask.java` constructs a task with an HTTP target and OIDC token and pushes it +to your queue. ## Initial Setup @@ -28,7 +27,7 @@ App Engine task attempts. To create a queue using the Cloud SDK, use the following gcloud command: ``` -gcloud beta tasks queues create-app-engine-queue my-appengine-queue +gcloud beta tasks queues create-app-engine-queue my-queue ``` Note: A newly created queue will route to the default App Engine service and @@ -56,7 +55,7 @@ Then the queue ID, as specified at queue creation time. Queue IDs already created can be listed with `gcloud beta tasks queues list`. ``` -export QUEUE_ID=my-appengine-queue +export QUEUE_ID=my-queue ``` And finally the location ID, which can be discovered with diff --git a/tasks/src/main/java/com/example/task/CreateHttpTaskWithToken.java b/tasks/src/main/java/com/example/task/CreateHttpTaskWithToken.java index f23aa45ee70..0891d899c85 100644 --- a/tasks/src/main/java/com/example/task/CreateHttpTaskWithToken.java +++ b/tasks/src/main/java/com/example/task/CreateHttpTaskWithToken.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 Google LLC + * Copyright 2019 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -46,7 +46,8 @@ public static void main(String[] args) throws Exception { // Construct the fully qualified queue name. String queuePath = QueueName.of(projectId, location, queueName).toString(); - // Construct OIDC token + // Add your service account email to construct the OIDC token. + // in order to add an authentication header to the request. OidcToken.Builder oidcTokenBuilder = OidcToken.newBuilder().setServiceAccountEmail("");