Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Cloud Tasks] Add task sample with auth token #1389

Merged
merged 6 commits into from
Apr 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions tasks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
`<SERVICE_ACCOUNT_EMAIL>` to authenticate the OIDC token.

Running the sample with command:
```
mvn exec:java@WithToken"
```
26 changes: 22 additions & 4 deletions tasks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,28 @@ Copyright 2018 Google LLC
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<mainClass>com.example.task.CreateHttpTask</mainClass>
<cleanupDaemonThreads>false</cleanupDaemonThreads>
</configuration>
<executions>
<execution>
<id>HttpTask</id>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>com.example.task.CreateHttpTask</mainClass>
<cleanupDaemonThreads>false</cleanupDaemonThreads>
</configuration>
</execution>
<execution>
<id>WithToken</id>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>com.example.task.CreateHttpTaskWithToken</mainClass>
<cleanupDaemonThreads>false</cleanupDaemonThreads>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Expand Down
71 changes: 71 additions & 0 deletions tasks/src/main/java/com/example/task/CreateHttpTaskWithToken.java
Original file line number Diff line number Diff line change
@@ -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("<SERVICE_ACCOUNT_EMAIL>");

// 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]