Skip to content

Add AppEngine Standard credentials to auth sample. #1075

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

Merged
merged 1 commit into from
Mar 28, 2018
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
5 changes: 5 additions & 0 deletions auth/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ limitations under the License.
<artifactId>google-cloud-storage</artifactId>
<version>1.14.0</version>
</dependency>
<dependency>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-appengine</artifactId>
<version>0.9.0</version>
</dependency>
<!-- END dependencies -->
<dependency>
<groupId>commons-io</groupId>
Expand Down
24 changes: 21 additions & 3 deletions auth/src/main/java/com/google/cloud/auth/samples/AuthExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@

import com.google.api.gax.paging.Page;
import com.google.auth.oauth2.ComputeEngineCredentials;
import com.google.auth.appengine.AppEngineCredentials;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;

import java.io.FileInputStream;
Expand Down Expand Up @@ -63,7 +63,7 @@ static void authExplicit(String jsonPath) throws IOException {
// [END auth_cloud_explicit]

// [START auth_cloud_explicit_compute_engine]
static void authComputeExplicit() {
static void authCompute() {
// Explicitly request service account credentials from the compute engine instance.
GoogleCredentials credentials = ComputeEngineCredentials.create();
Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();
Expand All @@ -76,6 +76,20 @@ static void authComputeExplicit() {
}
// [END auth_cloud_explicit_compute_engine]

// [START auth_cloud_explicit_app_engine]
static void authAppEngineStandard() throws IOException {
// Explicitly request service account credentials from the app engine standard instance.
GoogleCredentials credentials = AppEngineCredentials.getApplicationDefault();
Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();

System.out.println("Buckets:");
Page<Bucket> buckets = storage.list();
for (Bucket bucket : buckets.iterateAll()) {
System.out.println(bucket.toString());
}
}
// [END auth_cloud_explicit_app_engine]

public static void main(String[] args) throws IOException {
if (args.length == 0) {
authImplicit();
Expand All @@ -90,7 +104,11 @@ public static void main(String[] args) throws IOException {
return;
}
if ("compute".equals(args[0])) {
authComputeExplicit();
authCompute();
return;
}
if ("appengine".equals(args[0])) {
authAppEngineStandard();
return;
}
authImplicit();
Expand Down