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 2 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
12 changes: 11 additions & 1 deletion tasks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
`<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
70 changes: 70 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,70 @@
/*
* Copyright 2018 Google LLC
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* 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.
* 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("<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]