8000 Add auth example for https://cloud.google.com/docs/authentication/pro… · ScarTheSilent/java-docs-samples@8d4c81b · GitHub
[go: up one dir, main page]

Skip to content

Commit 8d4c81b

Browse files
authored
1 parent c5efc3b commit 8d4c81b

File tree

5 files changed

+256
-0
lines changed

5 files changed

+256
-0
lines changed

auth/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Getting Started with Google Cloud Authentication
2+
3+
<a href="https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&page=editor&open_in_editor=auth/README.md">
4+
<img alt="Open in Cloud Shell" src ="http://gstatic.com/cloudssh/images/open-btn.png"></a>
5+
6+
See the [documentation][auth-docs] for more information about authenticating for Google Cloud APIs.
7+
8+
[auth-docs]: https://cloud.google.com/docs/authentication/production
9+
10+
## Quickstart
11+
12+
Install [Maven](http://maven.apache.org/).
13+
14+
Build your project with:
15+
16+
mvn clean package -DskipTests
17+
18+
You can then run a given `ClassName` via:
19+
20+
mvn exec:java -Dexec.mainClass=com.example.storage.ClassName \
21+
-DpropertyName=propertyValue \
22+
-Dexec.args="any arguments to the app"
23+
24+
### Listing buckets with default credentials
25+
26+
mvn exec:java -Dexec.mainClass=com.google.cloud.auth.samples.AuthExample
27+
28+
### Listing buckets with credentials in json file
29+
30+
mvn exec:java -Dexec.mainClass=com.google.cloud.auth.samples.AuthExample
31+
-Dexec.args="explicit [path-to-credentials-json]"
32+
33+
### Listing buckets while running on a Google Compute Engine instance
34+
35+
mvn exec:java -Dexec.mainClass=com.google.cloud.auth.samples.AuthExample
36+
-Dexec.args="compute"

auth/pom.xml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<!--
2+
Copyright 2016 Google Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
<project>
17+
<modelVersion>4.0.0</modelVersion>
18+
<groupId>com.example.cloud.auth.samples</groupId>
19+
<artifactId>auth</artifactId>
20+
<version>1.0</version>
21+
<name>auth</name>
22+
<url>http://maven.apache.org</url>
23+
24+
<!--
25+
The parent pom defines common style checks and testing strategies for our samples.
26+
Removing or replacing it should not affect the execution of the samples in anyway.
27+
-->
28+
<parent>
29+
<groupId>com.google.cloud.samples</groupId>
30+
<artifactId>shared-configuration</artifactId>
31+
<version>1.0.8</version>
32+
</parent>
33+
34+
<properties>
35+
<maven.compiler.target>1.8</maven.compiler.target>
36+
<maven.compiler.source>1.8</maven.compiler.source>
37+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
38+
</properties>
39+
40+
<dependencies>
41+
<!-- START dependencies -->
42+
<dependency>
43+
<groupId>com.google.cloud</groupId>
44+
<artifactId>google-cloud-storage</artifactId>
45+
<version>1.14.0</version>
46+
</dependency>
47+
<!-- END dependencies -->
48+
<dependency>
49+
<groupId>commons-io</groupId>
50+
<artifactId>commons-io</artifactId>
51+
<version>1.4< 236B /version>
52+
</dependency>
53+
<dependency>
54+
<groupId>junit</groupId>
55+
<artifactId>junit</artifactId>
56+
<version>4.12</version>
57+
<scope>test</scope>
58+
</dependency>
59+
</dependencies>
60+
</project>
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright 2018 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.auth.samples;
18+
19+
import com.google.api.gax.paging.Page;
20+
import com.google.auth.oauth2.ComputeEngineCredentials;
21+
import com.google.auth.oauth2.GoogleCredentials;
22+
import com.google.cloud.storage.Bucket;
23+
import com.google.cloud.storage.Storage;
24+
import com.google.cloud.storage.StorageOptions;
25+
import com.google.common.base.Strings;
26+
import com.google.common.collect.Lists;
27+
28+
import java.io.FileInputStream;
29+
import java.io.IOException;
30+
31+
/**
32+
* Demonstrate various ways to authenticate requests using Cloud Storage as an example call.
33+
*/
34+
public class AuthExample {
35+
// [START auth_cloud_implicit]
36+
static void authImplicit() {
37+
// If you don't specify credentials when constructing the client, the client library will
38+
// look for credentials via the environment variable GOOGLE_APPLICATION_CREDENTIALS.
39+
Storage storage = StorageOptions.getDefaultInstance().getService();
40+
41+
System.out.println("Buckets:");
42+
Page<Bucket> buckets = storage.list();
43+
for (Bucket bucket : buckets.iterateAll()) {
44+
System.out.println(bucket.toString());
45+
}
46+
}
47+
// [END auth_cloud_implicit]
48+
49+
// [START auth_cloud_explicit]
50+
static void authExplicit(String jsonPath) throws IOException {
51+
// You can specify a credential file by providing a path to GoogleCredentials.
52+
// Otherwise credentials are read from the GOOGLE_APPLICATION_CREDENTIALS environment variable.
53+
GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(jsonPath))
54+
.createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));
55+
Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();
56+
57+
System.out.println("Buckets:");
58+
Page<Bucket> buckets = storage.list();
59+
for (Bucket bucket : buckets.iterateAll()) {
60+
System.out.println(bucket.toString());
61+
}
62+
}
63+
// [END auth_cloud_explicit]
64+
65+
// [START auth_cloud_explicit_compute_engine]
66+
static void authComputeExplicit() {
67+
// Explicitly request service account credentials from the compute engine instance.
68+
GoogleCredentials credentials = ComputeEngineCredentials.create();
69+
Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();
70+
71+
System.out.println("Buckets:");
72+
Page<Bucket> buckets = storage.list();
73+
for (Bucket bucket : buckets.iterateAll()) {
74+
System.out.println(bucket.toString());
75+
}
76+
}
77+
// [END auth_cloud_explicit_compute_engine]
78+
79+
public static void main(String[] args) throws IOException {
80+
if (args.length == 0) {
81+
authImplicit();
82+
return;
83+
}
84+
if ("explicit".equals(args[0])) {
85+
if (args.length >= 2) {
86+
authExplicit(args[1]);
87+
} else {
88+
throw new IllegalArgumentException("Path to credential file required with 'explicit'.");
89+
}
90+
return;
91+
}
92+
if ("compute".equals(args[0])) {
93+
authComputeExplicit();
94+
return;
95+
}
96+
authImplicit();
97+
}
98+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2018 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.auth.samples;
18+
19+
import static org.junit.Assert.assertNotNull;
20+
import static org.junit.Assert.assertTrue;
21+
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.IOException;
24+
import java.io.PrintStream;
25+
26+
import org.junit.Before;
27+
import org.junit.Test;
28+
import org.junit.runner.RunWith;
29+
import org.junit.runners.JUnit4;
30+
31+
@RunWith(JUnit4.class)
32+
//CHECKSTYLE OFF: AbbreviationAsWordInName
33+
public class AuthExampleIT {
34+
//CHECKSTYLE ON: AbbreviationAsWordInName
35+
private ByteArrayOutputStream bout;
36+
private PrintStream out;
37+
private String credentials;
38+
39+
@Before
40+
public void setUp() {
41+
bout = new ByteArrayOutputStream();
42+< 9920 /span>
out = new PrintStream(bout);
43+
System.setOut(out);
44+
credentials = System.getenv("GOOGLE_APPLICATION_CREDENTIALS");
45+
assertNotNull(credentials);
46+
}
47+
48+
@Test
49+
public void testAuthImplicit() throws IOException {
50+
AuthExample.main(new String[] {});
51+
String output = bout.toString();
52+
assertTrue(output.contains("Buckets:"));
53+
}
54+
55+
@Test
56+
public void testAuthExplicitNoPath() throws IOException {
57+
AuthExample.main(new String[] {"explicit", credentials});
58+
String output = bout.toString();
59+
assertTrue(output.contains("Buckets:"));
60+
}
61+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
<!-- App Engine Standard -->
4040
<module>appengine</module>
4141
<module>appengine-java8</module>
42+
<module>auth</module>
4243
<module>flexible</module>
4344

4445
<module>bigquery/cloud-client</module>

0 commit comments

Comments
 (0)
0