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

docs(samples): added auth samples and tests #927

Merged
merged 30 commits into from Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
3bd604d
docs(samples): added client code for idtoken, adc and metadata server
Sita04 Jun 6, 2022
bd1bc55
docs(samples): added authexplicit and copyright
Sita04 Jun 8, 2022
ece1c56
docs(samples): add auth with metadata server
Sita04 Jun 8, 2022
0c26c33
docs(samples): minor refactoring and added tests
Sita04 Jun 8, 2022
45449b2
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Jun 8, 2022
7ef36df
Merge branch 'main' into auth-samples
Shabirmean Jun 14, 2022
b2f367e
refactored acc to review comments
Sita04 Jul 18, 2022
01b57ad
Merge remote-tracking branch 'origin/auth-samples' into auth-samples
Sita04 Jul 18, 2022
b9dee60
Merge branch 'main' into auth-samples
Sita04 Jul 18, 2022
3ed6be0
refactored acc to review comments
Sita04 Jul 22, 2022
dd39fd4
Merge remote-tracking branch 'origin/auth-samples' into auth-samples
Sita04 Jul 22, 2022
a46ccc4
Merge branch 'main' into auth-samples
Sita04 Jul 25, 2022
5eb6506
refactored acc to review comments
Sita04 Jul 28, 2022
cc6a5a5
Merge remote-tracking branch 'origin/auth-samples' into auth-samples
Sita04 Jul 28, 2022
7c68634
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Jul 28, 2022
1baeebe
minor comment update
Sita04 Jul 28, 2022
9fc3a7a
Merge remote-tracking branch 'origin/auth-samples' into auth-samples
Sita04 Jul 28, 2022
b8c5f3f
Merge branch 'main' into auth-samples
Sita04 Jul 29, 2022
680cfdd
modified google id token verification and removed third party dependency
Sita04 Jul 29, 2022
1944e5d
Merge remote-tracking branch 'origin/auth-samples' into auth-samples
Sita04 Jul 29, 2022
a943290
removed third party deps from pom
Sita04 Jul 29, 2022
d7d6257
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Jul 29, 2022
cf11754
Merge branch 'auth-samples' of https://github.com/googleapis/google-a…
gcf-owl-bot[bot] Jul 29, 2022
b910be5
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Jul 29, 2022
e23fe35
Merge branch 'auth-samples' of https://github.com/googleapis/google-a…
gcf-owl-bot[bot] Jul 29, 2022
ada91fb
Merge branch 'main' into auth-samples
Sita04 Jul 29, 2022
8573bb3
Merge branch 'main' into auth-samples
Sita04 Aug 4, 2022
591d0e6
included comment about verifying Google ID tokens
Sita04 Aug 4, 2022
82d7350
Merge remote-tracking branch 'origin/auth-samples' into auth-samples
Sita04 Aug 4, 2022
54013b5
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Aug 4, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
83 changes: 83 additions & 0 deletions samples/snippets/pom.xml
@@ -0,0 +1,83 @@
<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.cloud</groupId>
<artifactId>google-iam-admin</artifactId>
<version>1.2.1</version>
</dependency>

<!-- GCloud dependency-->
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-compute</artifactId>
</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>

72 changes: 72 additions & 0 deletions samples/snippets/src/main/java/AuthenticateExplicit.java
@@ -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.
*/

// [START auth_cloud_explicit_adc]

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 project variable below.
// 2. Make sure you have the necessary permission to list storage buckets
// "storage.buckets.list"

String projectId = "your-google-cloud-project-id";

authenticateExplicit(projectId);
}

// List storage buckets by authenticating with ADC.
public static void authenticateExplicit(String projectId) throws IOException {
// Construct the GoogleCredentials object which obtains the default configuration from your
// working environment.
// GoogleCredentials.getApplicationDefault() will give you ComputeEngineCredentials
// if you are on a GCE (or other metadata server supported environments).
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();
// If you are authenticating to a Cloud API, you can let the library include the default scope,
// https://www.googleapis.com/auth/cloud-platform, because IAM is used to provide fine-grained
// permissions for Cloud.
// If you need to provide a scope, specify it as follows:
// GoogleCredentials credentials = GoogleCredentials.getApplicationDefault()
// .createScoped(scope);
// For more information on scopes to use,
// see: https://developers.google.com/identity/protocols/oauth2/scopes

// Construct the Storage client.
Storage storage =

Choose a reason for hiding this comment

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

This should use a try/catch and include the warning about making sure to close: disingenuous: https://googlecloudplatform.github.io/samples-style-guide/#clients

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not sure of this as many of the storage samples look like they don't follow the auto close try-catch..
https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/bucket/ListBuckets.java

StorageOptions.newBuilder()
.setCredentials(credentials)
.setProjectId(projectId)
.build()
.getService();

System.out.println("Buckets:");
Page<Bucket> buckets = storage.list();
for (Bucket bucket : buckets.iterateAll()) {
System.out.println(bucket.toString());
}
System.out.println("Listed all storage buckets.");
}
}
// [END auth_cloud_explicit_adc]
60 changes: 60 additions & 0 deletions samples/snippets/src/main/java/AuthenticateImplicitWithAdc.java
@@ -0,0 +1,60 @@
/*
* 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.
*/

// [START auth_cloud_implicit_adc]

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,
// set up ADC as described in https://cloud.google.com/docs/authentication/external/set-up-adc
// 2. Replace the project variable below.
// 3. Make sure that the user account or service account that you are using
// has the required permissions. For this sample, you must have "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.
public static void authenticateImplicitWithAdc(String project) throws IOException {

String zone = "us-central1-a";
// This snippet demonstrates how to list instances.
// *NOTE*: Replace the client created below with the client required for your application.
// Note that the credentials are not specified when constructing the client.
// Hence, the client library will look for credentials using ADC.
//
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the `instancesClient.close()` method on the client to safely
// clean up any remaining background resources.
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 #######");
}
}
}
// [END auth_cloud_implicit_adc]
@@ -0,0 +1,87 @@
/*
* 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.
*/

// [auth_cloud_idtoken_impersonated_credentials]

import com.google.auth.oauth2.GoogleCredentials;
import com.google.auth.oauth2.IdTokenCredentials;
import com.google.auth.oauth2.IdTokenProvider.Option;
import com.google.auth.oauth2.ImpersonatedCredentials;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class IdTokenFromImpersonatedCredentials {

public static void main(String[] args) throws IOException {
// TODO(Developer): Replace the below variables before running the code.

// Provide the scopes that you might need to request to access Google APIs,
Sita04 marked this conversation as resolved.
Show resolved Hide resolved
// depending on the level of access you need.
// The best practice is to use the cloud-wide scope and use IAM to narrow the permissions.
// https://cloud.google.com/docs/authentication#authorization_for_services
// For more information, see: https://developers.google.com/identity/protocols/oauth2/scopes
String scope = "https://www.googleapis.com/auth/cloud-platform";

// The service name for which the id token is requested. Service name refers to the
// logical identifier of an API service, such as "pubsub.googleapis.com".
String targetAudience = "iap.googleapis.com";

// The name of the privilege-bearing service account for whom the credential is created.
String impersonatedServiceAccount = "name@project.service.gserviceaccount.com";

getIdTokenUsingOAuth2(impersonatedServiceAccount, scope, targetAudience);
}

// Use a service account (SA1) to impersonate as another service account (SA2) and obtain id token
// for the impersonated account.
// To obtain token for SA2, SA1 should have the "roles/iam.serviceAccountTokenCreator" permission
// on SA2.
public static void getIdTokenUsingOAuth2(
String impersonatedServiceAccount, String scope, String targetAudience) throws IOException {

// Construct the GoogleCredentials object which obtains the default configuration from your
// working environment.
GoogleCredentials googleCredentials = GoogleCredentials.getApplicationDefault();

// delegates: The chained list of delegates required to grant the final accessToken.
Copy link
Contributor

Choose a reason for hiding this comment

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

I've also separated out the delegation flow from the single service account flow. It doesn't make sense to create an ID token from a delegation chain, because there's no authz included for ID tokens. So let's just stick to a single privilege-bearing (current term, subject to change) service account (no delegation chain).

Copy link
Contributor

Choose a reason for hiding this comment

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

Reopening because you are still including delegate chain. There should be just one privilege-bearing service account and one caller, which can be represented by either a service account or a user account.
https://cloud.google.com/iam/docs/create-short-lived-credentials-direct

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The delegates parameter is mandatory in the "Impersonated Credentials" API, unlike the other one we used from IAM library.
Hence, I am unable to remove it. Added clarifying comments stating that it's optional. wdyt?

Copy link
Contributor

Choose a reason for hiding this comment

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

Could we just say
// delegates: Unused for this call
or something like that? I'm concerned that the subject of chaining is very complex, and that folks will spend a lot of time trying to parse and understand what identities need to have what permission on what-- only to get to the bottom and find out it's unused. Why put them through that?

// For more information, see:
// https://cloud.google.com/iam/docs/create-short-lived-credentials-direct#sa-credentials-permissions
// Delegate is NOT USED here.
List<String> delegates = null;

// Create the impersonated credential.
ImpersonatedCredentials impersonatedCredentials =
ImpersonatedCredentials.create(
googleCredentials, impersonatedServiceAccount, delegates, Arrays.asList(scope), 300);

// Set the impersonated credential, target audience and token options.
IdTokenCredentials idTokenCredentials =
IdTokenCredentials.newBuilder()
.setIdTokenProvider(impersonatedCredentials)
.setTargetAudience(targetAudience)
// Setting this will include email in the id token.
.setOptions(Arrays.asList(Option.INCLUDE_EMAIL))
.build();

// Get the ID token.
// Once you've obtained the ID token, use it to make an authenticated call
// to the target audience.
String idToken = idTokenCredentials.refreshAccessToken().getTokenValue();
System.out.println("Generated ID token.");
}
}
// [auth_cloud_idtoken_impersonated_credentials]
61 changes: 61 additions & 0 deletions samples/snippets/src/main/java/IdTokenFromMetadataServer.java
@@ -0,0 +1,61 @@
/*
* 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.
*/

// [START auth_cloud_idtoken_metadata_server]

import com.google.auth.oauth2.GoogleCredentials;
import com.google.auth.oauth2.IdTokenCredentials;
import com.google.auth.oauth2.IdTokenProvider;
import com.google.auth.oauth2.IdTokenProvider.Option;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Arrays;

public class IdTokenFromMetadataServer {

public static void main(String[] args) throws IOException, GeneralSecurityException {
// TODO(Developer): Replace the below variables before running the code.

// The url or target audience to obtain the ID token for.
String url = "http://www.abc.com";

getIdTokenFromMetadataServer(url);
}

// Use the Google Cloud metadata server in the Cloud Run (or AppEngine or Kubernetes etc.,)
// environment to create an identity token and add it to the HTTP request as part of an
// Authorization header.
public static void getIdTokenFromMetadataServer(String url) throws IOException {
// Construct the GoogleCredentials object which obtains the default configuration from your
// working environment.
GoogleCredentials googleCredentials = GoogleCredentials.getApplicationDefault();

IdTokenCredentials idTokenCredentials =
IdTokenCredentials.newBuilder()
.setIdTokenProvider((IdTokenProvider) googleCredentials)
.setTargetAudience(url)
// Setting the ID token options.
.setOptions(Arrays.asList(Option.FORMAT_FULL, Option.LICENSES_TRUE))
.build();

// Get the ID token.
// Once you've obtained the ID token, use it to make an authenticated call
// to the target audience.
String idToken = idTokenCredentials.refreshAccessToken().getTokenValue();
System.out.println("Generated ID token.");
}
}
// [END auth_cloud_idtoken_metadata_server]