diff --git a/tasks/README.md b/tasks/README.md index 760a0ba6605..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 @@ -81,5 +80,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..697eb29e61d 100644 --- a/tasks/pom.xml +++ b/tasks/pom.xml @@ -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..0891d899c85 --- /dev/null +++ b/tasks/src/main/java/com/example/task/CreateHttpTaskWithToken.java @@ -0,0 +1,71 @@ +/* + * 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. + * 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(); + + // 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(""); + + // 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]