Skip to content

Commit fbe6abb

Browse files
authored
docs: Update README with client-side CAB instructions (#1607)
* docs: Update README with client-side CAB instructions This commit updates the README file to include instructions for setting up and using the client-side CAB feature. * chore: readme file wording updated based on comments feedback. * Update readme: Mention CAB rule changes and its effect on server vs client side token generation. * Link to wikipedia page for Principle of the Least Privilege concept. * chore: fix spacing. * Add a section for google-auth-library-cab-token-generator
1 parent 02e939e commit fbe6abb

File tree

1 file changed

+104
-27
lines changed

1 file changed

+104
-27
lines changed

README.md

+104-27
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@ Open source authentication client library for Java.
77

88
- [API Documentation](https://googleapis.dev/java/google-auth-library/latest)
99

10-
This project consists of 3 artifacts:
10+
This project consists of 4 artifacts:
1111

1212
- [*google-auth-library-credentials*](#google-auth-library-credentials): contains base classes and
1313
interfaces for Google credentials
1414
- [*google-auth-library-appengine*](#google-auth-library-appengine): contains App Engine
1515
credentials. This artifact depends on the App Engine SDK.
16-
- [*google-auth-library-oauth2-http*](#google-auth-library-oauth2-http): contains a wide variety of
17-
credentials as well as utility methods to create them and to get Application Default Credentials
16+
- [*google-auth-library-oauth2-http*](#google-auth-library-oauth2-http): contains
17+
a wide variety of credentials and utility methods, including functionality to get
18+
Application Default Credentials. Also provides the server-side approach for generating
19+
downscoped tokens.
20+
- [*google-auth-library-cab-token-generator*](#google-auth-library-cab-token-generator):
21+
provides the client-side approach for generating downscoped tokens.
1822

1923
> ⚠️ Important: If you accept a credential configuration (credential JSON/File/Stream) from an external source for
2024
authentication to Google Cloud Platform, you must validate it before providing it to any Google API or library. Providing
@@ -1011,16 +1015,19 @@ googleapis.com domain.
10111015
### Downscoping with Credential Access Boundaries
10121016

10131017
[Downscoping with Credential Access Boundaries](https://cloud.google.com/iam/docs/downscoping-short-lived-credentials)
1014-
enables the ability to downscope, or restrict, the Identity and Access Management (IAM) permissions
1015-
that a short-lived credential can use for Cloud Storage.
1018+
enables restricting the Identity and Access Management (IAM) permissions that a
1019+
short-lived credential can use for Cloud Storage. This involves creating a
1020+
`CredentialAccessBoundary` that defines the restrictions applied to the
1021+
downscoped token. Using downscoped credentials ensures tokens in flight always
1022+
have the least privileges ([Principle of Least Privilege](https://en.wikipedia.org/wiki/Principle_of_least_privilege)).
10161023

1017-
The `DownscopedCredentials` class can be used to produce a downscoped access token from a
1018-
`CredentialAccessBoundary` and a source credential. The Credential Access Boundary specifies which
1019-
resources the newly created credential can access, as well as an upper bound on the permissions that
1020-
are available on each resource. Using downscoped credentials ensures tokens in flight always have
1021-
the least privileges (Principle of Least Privilege).
1024+
#### Creating a CredentialAccessBoundary
10221025

1023-
The snippet below shows how to initialize a CredentialAccessBoundary with one AccessBoundaryRule
1026+
The Credential Access Boundary specifies which resources the newly created credential can access,
1027+
as well as an upper bound on the permissions that are available on each resource.
1028+
It consists of one or more `AccessBoundaryRule` objects.
1029+
1030+
The snippet below shows how to initialize a `CredentialAccessBoundary` with one `AccessBoundaryRule`
10241031
which specifies that the downscoped token will have readonly access to objects starting with
10251032
"customer-a" in bucket "bucket-123":
10261033
```java
@@ -1042,37 +1049,80 @@ CredentialAccessBoundary credentialAccessBoundary =
10421049
CredentialAccessBoundary.newBuilder().addRule(rule).build();
10431050
```
10441051

1052+
#### Common Usage Pattern
1053+
10451054
The common pattern of usage is to have a token broker with elevated access generate these downscoped
10461055
credentials from higher access source credentials and pass the downscoped short-lived access tokens
10471056
to a token consumer via some secure authenticated channel for limited access to Google Cloud Storage
10481057
resources.
10491058

1050-
Using the CredentialAccessBoundary created above in the Token Broker:
1059+
#### Generating Downscoped Tokens
1060+
There are two ways to generate downscoped tokens using a CredentialAccessBoundary:
1061+
1062+
* **Server-side (using `DownscopedCredentials`):** The client calls the Security
1063+
Token Service (STS) each time a downscoped token is needed. This is suitable for
1064+
applications where the Credential Access Boundary rules change infrequently or
1065+
when a single downscoped credential is reused many times. A key consideration
1066+
is that every rule change requires a new call to the STS. This approach is available
1067+
within the `google-auth-library-oauth2-http` library and does not require any additional
1068+
dependencies, making it simpler to integrate. It's a good choice if your use case
1069+
doesn't demand the specific benefits of the client-side approach.
1070+
1071+
1072+
* **Client-side (using `ClientSideCredentialAccessBoundaryFactory`):** The client
1073+
retrieves cryptographic material once and then generates multiple downscoped tokens
1074+
locally. This minimizes calls to the STS and is more efficient when Credential Access
1075+
Boundary rules change frequently, as the client doesn't need to contact the STS
1076+
for each rule change. This is also more efficient for applications that need to
1077+
generate many *unique* downscoped tokens. This approach is available in the
1078+
`google-auth-library-cab-token-generator` module. However, this module comes with
1079+
its own set of dependencies, which can add complexity to your project. Consider
1080+
this approach if minimizing STS calls and generating numerous unique tokens are
1081+
primary concerns and you are willing to manage the additional dependencies.
1082+
1083+
#### Server-side CAB
1084+
1085+
The `DownscopedCredentials` class can be used to produce a downscoped access
1086+
token from a source credential and the `CredentialAccessBoundary`.
1087+
10511088
```java
10521089
// Retrieve the source credentials from ADC.
10531090
GoogleCredentials sourceCredentials = GoogleCredentials.getApplicationDefault()
10541091
.createScoped("https://www.googleapis.com/auth/cloud-platform");
10551092

1093+
// Create an Access Boundary Rule which will restrict the downscoped token to having readonly
1094+
// access to objects starting with "customer-a" in bucket "bucket-123".
1095+
String availableResource = "//storage.googleapis.com/projects/_/buckets/bucket-123";
1096+
String availablePermission = "inRole:roles/storage.objectViewer";
1097+
String expression = "resource.name.startsWith('projects/_/buckets/bucket-123/objects/customer-a')";
1098+
1099+
CredentialAccessBoundary.AccessBoundaryRule rule =
1100+
CredentialAccessBoundary.AccessBoundaryRule.newBuilder()
1101+
.setAvailableResource(availableResource)
1102+
.addAvailablePermission(availablePermission)
1103+
.setAvailabilityCondition(
1104+
new AvailabilityCondition(expression, /* title= */ null, /* description= */ null))
1105+
.build();
1106+
10561107
// Initialize the DownscopedCredentials class.
10571108
DownscopedCredentials downscopedCredentials =
10581109
DownscopedCredentials.newBuilder()
1059-
.setSourceCredential(credentials)
1060-
.setCredentialAccessBoundary(credentialAccessBoundary)
1110+
.setSourceCredential(sourceCredentials)
1111+
.setCredentialAccessBoundary(CredentialAccessBoundary.newBuilder().addRule(rule).build())
10611112
.build();
10621113

10631114
// Retrieve the downscoped access token.
10641115
// This will need to be passed to the Token Consumer.
10651116
AccessToken downscopedAccessToken = downscopedCredentials.refreshAccessToken();
10661117
```
10671118

1068-
A token broker can be set up on a server in a private network. Various workloads
1069-
(token consumers) in the same network will send authenticated requests to that broker for downscoped
1070-
tokens to access or modify specific google cloud storage buckets.
1119+
#### Client-side CAB
10711120

1072-
The broker will instantiate downscoped credentials instances that can be used to generate short
1073-
lived downscoped access tokens which will be passed to the token consumer.
1121+
For client-side CAB, the `ClientSideCredentialAccessBoundaryFactory` is used
1122+
with a source credential. After initializing the factory, the `generateToken()`
1123+
method can be called repeatedly with different `CredentialAccessBoundary`
1124+
objects to create multiple downscoped tokens.
10741125

1075-
Putting it all together:
10761126
```java
10771127
// Retrieve the source credentials from ADC.
10781128
GoogleCredentials sourceCredentials = GoogleCredentials.getApplicationDefault()
@@ -1092,18 +1142,32 @@ CredentialAccessBoundary.AccessBoundaryRule rule =
10921142
new AvailabilityCondition(expression, /* title= */ null, /* description= */ null))
10931143
.build();
10941144

1095-
// Initialize the DownscopedCredentials class.
1096-
DownscopedCredentials downscopedCredentials =
1097-
DownscopedCredentials.newBuilder()
1098-
.setSourceCredential(credentials)
1099-
.setCredentialAccessBoundary(CredentialAccessBoundary.newBuilder().addRule(rule).build())
1145+
// Initialize the ClientSideCredentialAccessBoundaryFactory.
1146+
ClientSideCredentialAccessBoundaryFactory factory =
1147+
ClientSideCredentialAccessBoundaryFactory.newBuilder()
1148+
.setSourceCredential(sourceCredentials)
11001149
.build();
11011150

1102-
// Retrieve the downscoped access token.
1151+
// Create the CredentialAccessBoundary with the rule.
1152+
CredentialAccessBoundary credentialAccessBoundary =
1153+
CredentialAccessBoundary.newBuilder().addRule(rule).build();
1154+
1155+
// Generate the downscoped access token.
11031156
// This will need to be passed to the Token Consumer.
1104-
AccessToken downscopedAccessToken = downscopedCredentials.refreshAccessToken();
1157+
AccessToken downscopedAccessToken = factory.generateToken(credentialAccessBoundary);
11051158
```
11061159

1160+
#### Using Downscoped Access Tokens
1161+
1162+
A token broker can be set up on a server in a private network. Various workloads
1163+
(token consumers) in the same network will send authenticated requests to that
1164+
broker for downscoped tokens to access or modify specific google cloud storage
1165+
buckets.
1166+
1167+
The broker will instantiate downscoped credentials instances that can be used to
1168+
generate short-lived downscoped access tokens which will be passed to the token
1169+
consumer.
1170+
11071171
These downscoped access tokens can be used by the Token Consumer via `OAuth2Credentials` or
11081172
`OAuth2CredentialsWithRefresh`. This credential can then be used to initialize a storage client
11091173
instance to access Google Cloud Storage resources with restricted access.
@@ -1318,6 +1382,19 @@ Credentials credentials =
13181382
**Important: `com.google.auth.appengine.AppEngineCredentials` is a separate class from
13191383
`com.google.auth.oauth2.AppEngineCredentials`.**
13201384

1385+
## google-auth-library-cab-token-generator
1386+
1387+
This module provides the `ClientSideCredentialAccessBoundaryFactory` class,
1388+
enabling client-side generation of downscoped tokens for Cloud Storage using
1389+
Credential Access Boundaries. This approach is particularly useful for applications
1390+
requiring frequent changes to Credential Access Boundary rules or the generation
1391+
of many unique downscoped tokens, as it minimizes calls to the Security Token
1392+
Service (STS). For more details on when to consider this approach and how it
1393+
compares to the server-side method, see [Downscoping with Credential Access Boundaries](#downscoping-with-credential-access-boundaries).
1394+
For usage examples, see the [Client-side CAB](#client-side-cab) section.
1395+
This module comes with its own set of dependencies, so evaluate whether the
1396+
benefits of client-side downscoping outweigh the added complexity for your specific use case.
1397+
13211398
## CI Status
13221399

13231400
Java Version | Status

0 commit comments

Comments
 (0)