-
Notifications
You must be signed in to change notification settings - Fork 259
docs(samples): added auth samples and tests #927
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
Changes from 9 commits
3bd604d
bd1bc55
ece1c56
0c26c33
45449b2
7ef36df
b2f367e
01b57ad
b9dee60
3ed6be0
dd39fd4
a46ccc4
5eb6506
cc6a5a5
7c68634
1baeebe
9fc3a7a
b8c5f3f
680cfdd
1944e5d
a943290
d7d6257
cf11754
b910be5
e23fe35
ada91fb
8573bb3
591d0e6
82d7350
54013b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <groupId>com.google.auth.samples</groupId> | ||
| <artifactId>authsamples</artifactId> | ||
| <version>1.0.0</version> | ||
| <name>auth-samples</name> | ||
|
|
||
|
|
||
| <!-- | ||
| The parent pom defines common style checks and testing strategies for our samples. | ||
| Removing or replacing it should not affect the execution of the samples in any way. | ||
| --> | ||
| <parent> | ||
| <groupId>com.google.cloud.samples</groupId> | ||
| <artifactId>shared-configuration</artifactId> | ||
| <version>1.2.0</version> | ||
| </parent> | ||
|
|
||
| <properties> | ||
| <maven.compiler.target>1.8</maven.compiler.target> | ||
| <maven.compiler.source>1.8</maven.compiler.source> | ||
| <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| </properties> | ||
|
|
||
| <!-- START dependencies --> | ||
| <!-- Using libraries-bom to manage versions. | ||
| See https://github.com/GoogleCloudPlatform/cloud-opensource-java/wiki/The-Google-Cloud-Platform-Libraries-BOM --> | ||
| <dependencyManagement> | ||
| <dependencies> | ||
| <dependency> | ||
| <groupId>com.google.cloud</groupId> | ||
| <artifactId>libraries-bom</artifactId> | ||
| <version>25.0.0</version> | ||
| <type>pom</type> | ||
| <scope>import</scope> | ||
| </dependency> | ||
| </dependencies> | ||
| </dependencyManagement> | ||
|
|
||
|
|
||
| <dependencies> | ||
| <!-- OAuth dependency--> | ||
| <dependency> | ||
| <groupId>com.google.auth</groupId> | ||
| <artifactId>google-auth-library-oauth2-http</artifactId> | ||
| <version>1.3.0</version> | ||
| </dependency> | ||
|
|
||
| <!-- IAM dependency--> | ||
| <dependency> | ||
| <groupId>com.google.apis</groupId> | ||
| <artifactId>google-api-services-iam</artifactId> | ||
| <version>v1-rev20220509-1.32.1</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.google.apis</groupId> | ||
| <artifactId>google-api-services-iamcredentials</artifactId> | ||
| <version>v1-rev20211203-1.32.1</version> | ||
| </dependency> | ||
|
|
||
| <!-- JWT dependency--> | ||
| <dependency> | ||
| <groupId>com.auth0</groupId> | ||
| <artifactId>java-jwt</artifactId> | ||
| <version>3.16.0</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.auth0</groupId> | ||
| <artifactId>jwks-rsa</artifactId> | ||
| <version>0.18.0</version> | ||
| </dependency> | ||
|
|
||
| <!-- GCloud dependency--> | ||
| <dependency> | ||
| <artifactId>google-cloud-compute</artifactId> | ||
| <groupId>com.google.cloud</groupId> | ||
| <version>1.8.1</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.google.cloud</groupId> | ||
| <artifactId>google-cloud-storage</artifactId> | ||
| </dependency> | ||
|
|
||
| <!-- Test dependencies--> | ||
| <dependency> | ||
| <groupId>junit</groupId> | ||
| <artifactId>junit</artifactId> | ||
| <version>4.13.1</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <artifactId>truth</artifactId> | ||
| <groupId>com.google.truth</groupId> | ||
| <scope>test</scope> | ||
| <version>1.1.3</version> | ||
| </dependency> | ||
|
|
||
| </dependencies> | ||
|
|
||
| </project> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /* | ||
| * Copyright 2022 Google Inc. | ||
| * | ||
| * 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. | ||
| */ | ||
|
|
||
| import com.google.api.gax.paging.Page; | ||
| import com.google.auth.oauth2.ComputeEngineCredentials; | ||
| 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 java.io.IOException; | ||
| import java.security.GeneralSecurityException; | ||
|
|
||
| public class AuthWithCredentialsFromMetadataServer { | ||
|
|
||
| public static void main(String[] args) throws IOException, GeneralSecurityException { | ||
| // TODO(Developer): | ||
| // 1. Replace the below variable. | ||
| // 2. Make sure you have the necessary permission to list storage buckets | ||
| // "storage.buckets.list" | ||
| String projectId = "your-google-cloud-project-id"; | ||
|
|
||
| authWithCredentialsFromMetadataServer(projectId); | ||
| } | ||
|
|
||
| // In this snippet, we demonstrate "Authentication with account credentials | ||
| // obtained from a metadata server". | ||
| public static void authWithCredentialsFromMetadataServer(String project) { | ||
|
|
||
| // This snippet demonstrates how to initialize Cloud Storage and list buckets. | ||
| // Note that the credentials are requested from the ComputeEngine metadata server. | ||
| Storage storage = initService(project); | ||
Sita04 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| System.out.println("Buckets:"); | ||
| Page<Bucket> buckets = storage.list(); | ||
| for (Bucket bucket : buckets.iterateAll()) { | ||
| System.out.println(bucket.toString()); | ||
| } | ||
| System.out.println("Authentication complete."); | ||
Sita04 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // Initialize the Storage client by getting the credentials | ||
| // from a Metadata server. | ||
| private static Storage initService(String projectId) { | ||
| // Explicitly request the credentials from the ComputeEngine metadata server. | ||
| GoogleCredentials credentials = ComputeEngineCredentials.create(); | ||
|
||
|
|
||
| // Alternately, if executing within AppEngine, you can get credentials as follows: | ||
| // GoogleCredentials credentials = AppEngineCredentials.getApplicationDefault(); | ||
|
|
||
| // Construct the Storage client. | ||
| // Note that, here we explicitly specify the service account to use. | ||
Sita04 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return StorageOptions.newBuilder() | ||
| .setCredentials(credentials) | ||
| .setProjectId(projectId) | ||
| .build() | ||
| .getService(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /* | ||
| * Copyright 2022 Google Inc. | ||
| * | ||
| * 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. | ||
| */ | ||
|
|
||
| import com.google.api.gax.paging.Page; | ||
| 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 java.io.IOException; | ||
| import java.security.GeneralSecurityException; | ||
|
|
||
| public class AuthenticateExplicit { | ||
Sita04 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| public static void main(String[] args) throws IOException, GeneralSecurityException { | ||
| // TODO(Developer): | ||
| // 1. Replace the below variable. | ||
Sita04 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // 2. Make sure you have the necessary permission to list storage buckets "storage.buckets.list" | ||
| String projectId = "your-google-cloud-project-id"; | ||
|
|
||
| // Provide the scopes that you might need to request to access Google APIs, | ||
Sita04 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // depending on the level of access you need. | ||
| // For more information, see: https://developers.google.com/identity/protocols/oauth2/scopes | ||
| // The best practice is to use the cloud-wide scope and use IAM to narrow the permissions. | ||
Sita04 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // https://cloud.google.com/docs/authentication#authorization_for_services | ||
| String scope = "https://www.googleapis.com/auth/cloud-platform"; | ||
Sita04 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| authenticateExplicit(projectId, scope); | ||
| } | ||
|
|
||
| // List storage buckets by authenticating with ADC. | ||
| public static void authenticateExplicit(String project, String scope) | ||
| throws IOException { | ||
|
|
||
| // Initialize the storage client. | ||
| Storage storage = initService(project, scope); | ||
|
|
||
| System.out.println("Buckets:"); | ||
| Page<Bucket> buckets = storage.list(); | ||
| for (Bucket bucket : buckets.iterateAll()) { | ||
| System.out.println(bucket.toString()); | ||
| } | ||
| System.out.println("Authentication complete."); | ||
| } | ||
|
|
||
| // Initialize the Storage client using ADC (Application Default Credentials). | ||
| private static Storage initService(String projectId, String scope) | ||
| throws IOException { | ||
| // Construct the GoogleCredentials object which obtains the default configuration from your | ||
| // working environment. | ||
| GoogleCredentials credentials = GoogleCredentials.getApplicationDefault().createScoped(scope); | ||
|
|
||
| // Construct the Storage client. | ||
| return StorageOptions.newBuilder() | ||
| .setCredentials(credentials) | ||
| .setProjectId(projectId) | ||
| .build() | ||
| .getService(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /* | ||
| * Copyright 2022 Google Inc. | ||
| * | ||
| * 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. | ||
| */ | ||
|
|
||
| import com.google.cloud.compute.v1.Instance; | ||
| import com.google.cloud.compute.v1.InstancesClient; | ||
| import java.io.IOException; | ||
|
|
||
| public class AuthenticateImplicitWithAdc { | ||
|
|
||
| public static void main(String[] args) throws IOException { | ||
| // TODO(Developer): | ||
Sita04 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // 1. Before running this sample, authenticate using ADC as mentioned in: | ||
Sita04 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // https://cloud.google.com/docs/authentication/provide-credentials-adc#how_to_provide_credentials_to_adc | ||
| // 2. Replace the projectId variable. | ||
| // 3. Make sure you have the necessary permission "compute.instances.list" | ||
| String projectId = "your-google-cloud-project-id"; | ||
| authenticateImplicitWithAdc(projectId); | ||
| } | ||
|
|
||
| // When interacting with Google Cloud Client libraries, the library can auto-detect the | ||
| // credentials to use. | ||
| // ADC detection is independent of the client library and language and works with all Cloud Client | ||
Sita04 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // libraries. | ||
| public static void authenticateImplicitWithAdc(String project) throws IOException { | ||
|
|
||
| String zone = "us-central1-a"; | ||
| // This snippet demonstrates how to initialize Cloud Compute Engine and list instances. | ||
Sita04 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // Note that the credentials are not specified when constructing the client. | ||
| // Hence, the client library will look for credentials using ADC. | ||
| try (InstancesClient instancesClient = InstancesClient.create()) { | ||
Sita04 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Set the project and zone to retrieve instances present in the zone. | ||
| System.out.printf("Listing instances from %s in %s:", project, zone); | ||
| for (Instance zoneInstance : instancesClient.list(project, zone).iterateAll()) { | ||
| System.out.println(zoneInstance.getName()); | ||
| } | ||
| System.out.println("####### Listing instances complete #######"); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.