Skip to content

Commit 0dbdc3a

Browse files
committed
docs: Update README with client-side CAB instructions (googleapis#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 d138023 commit 0dbdc3a

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
@@ -1034,16 +1038,19 @@ googleapis.com domain.
10341038
### Downscoping with Credential Access Boundaries
10351039

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

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

1046-
The snippet below shows how to initialize a CredentialAccessBoundary with one AccessBoundaryRule
1049+
The Credential Access Boundary specifies which resources the newly created credential can access,
1050+
as well as an upper bound on the permissions that are available on each resource.
1051+
It consists of one or more `AccessBoundaryRule` objects.
1052+
1053+
The snippet below shows how to initialize a `CredentialAccessBoundary` with one `AccessBoundaryRule`
10471054
which specifies that the downscoped token will have readonly access to objects starting with
10481055
"customer-a" in bucket "bucket-123":
10491056
```java
@@ -1065,37 +1072,80 @@ CredentialAccessBoundary credentialAccessBoundary =
10651072
CredentialAccessBoundary.newBuilder().addRule(rule).build();
10661073
```
10671074

1075+
#### Common Usage Pattern
1076+
10681077
The common pattern of usage is to have a token broker with elevated access generate these downscoped
10691078
credentials from higher access source credentials and pass the downscoped short-lived access tokens
10701079
to a token consumer via some secure authenticated channel for limited access to Google Cloud Storage
10711080
resources.
10721081

1073-
Using the CredentialAccessBoundary created above in the Token Broker:
1082+
#### Generating Downscoped Tokens
1083+
There are two ways to generate downscoped tokens using a CredentialAccessBoundary:
1084+
1085+
* **Server-side (using `DownscopedCredentials`):** The client calls the Security
1086+
Token Service (STS) each time a downscoped token is needed. This is suitable for
1087+
applications where the Credential Access Boundary rules change infrequently or
1088+
when a single downscoped credential is reused many times. A key consideration
1089+
is that every rule change requires a new call to the STS. This approach is available
1090+
within the `google-auth-library-oauth2-http` library and does not require any additional
1091+
dependencies, making it simpler to integrate. It's a good choice if your use case
1092+
doesn't demand the specific benefits of the client-side approach.
1093+
1094+
1095+
* **Client-side (using `ClientSideCredentialAccessBoundaryFactory`):** The client
1096+
retrieves cryptographic material once and then generates multiple downscoped tokens
1097+
locally. This minimizes calls to the STS and is more efficient when Credential Access
1098+
Boundary rules change frequently, as the client doesn't need to contact the STS
1099+
for each rule change. This is also more efficient for applications that need to
1100+
generate many *unique* downscoped tokens. This approach is available in the
1101+
`google-auth-library-cab-token-generator` module. However, this module comes with
1102+
its own set of dependencies, which can add complexity to your project. Consider
1103+
this approach if minimizing STS calls and generating numerous unique tokens are
1104+
primary concerns and you are willing to manage the additional dependencies.
1105+
1106+
#### Server-side CAB
1107+
1108+
The `DownscopedCredentials` class can be used to produce a downscoped access
1109+
token from a source credential and the `CredentialAccessBoundary`.
1110+
10741111
```java
10751112
// Retrieve the source credentials from ADC.
10761113
GoogleCredentials sourceCredentials = GoogleCredentials.getApplicationDefault()
10771114
.createScoped("https://www.googleapis.com/auth/cloud-platform");
10781115

1116+
// Create an Access Boundary Rule which will restrict the downscoped token to having readonly
1117+
// access to objects starting with "customer-a" in bucket "bucket-123".
1118+
String availableResource = "//storage.googleapis.com/projects/_/buckets/bucket-123";
1119+
String availablePermission = "inRole:roles/storage.objectViewer";
1120+
String expression = "resource.name.startsWith('projects/_/buckets/bucket-123/objects/customer-a')";
1121+
1122+
CredentialAccessBoundary.AccessBoundaryRule rule =
1123+
CredentialAccessBoundary.AccessBoundaryRule.newBuilder()
1124+
.setAvailableResource(availableResource)
1125+
.addAvailablePermission(availablePermission)
1126+
.setAvailabilityCondition(
1127+
new AvailabilityCondition(expression, /* title= */ null, /* description= */ null))
1128+
.build();
1129+
10791130
// Initialize the DownscopedCredentials class.
10801131
DownscopedCredentials downscopedCredentials =
10811132
DownscopedCredentials.newBuilder()
1082-
.setSourceCredential(credentials)
1083-
.setCredentialAccessBoundary(credentialAccessBoundary)
1133+
.setSourceCredential(sourceCredentials)
1134+
.setCredentialAccessBoundary(CredentialAccessBoundary.newBuilder().addRule(rule).build())
10841135
.build();
10851136

10861137
// Retrieve the downscoped access token.
10871138
// This will need to be passed to the Token Consumer.
10881139
AccessToken downscopedAccessToken = downscopedCredentials.refreshAccessToken();
10891140
```
10901141

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

1095-
The broker will instantiate downscoped credentials instances that can be used to generate short
1096-
lived downscoped access tokens which will be passed to the token consumer.
1144+
For client-side CAB, the `ClientSideCredentialAccessBoundaryFactory` is used
1145+
with a source credential. After initializing the factory, the `generateToken()`
1146+
method can be called repeatedly with different `CredentialAccessBoundary`
1147+
objects to create multiple downscoped tokens.
10971148

1098-
Putting it all together:
10991149
```java
11001150
// Retrieve the source credentials from ADC.
11011151
GoogleCredentials sourceCredentials = GoogleCredentials.getApplicationDefault()
@@ -1115,18 +1165,32 @@ CredentialAccessBoundary.AccessBoundaryRule rule =
11151165
new AvailabilityCondition(expression, /* title= */ null, /* description= */ null))
11161166
.build();
11171167

1118-
// Initialize the DownscopedCredentials class.
1119-
DownscopedCredentials downscopedCredentials =
1120-
DownscopedCredentials.newBuilder()
1121-
.setSourceCredential(credentials)
1122-
.setCredentialAccessBoundary(CredentialAccessBoundary.newBuilder().addRule(rule).build())
1168+
// Initialize the ClientSideCredentialAccessBoundaryFactory.
1169+
ClientSideCredentialAccessBoundaryFactory factory =
1170+
ClientSideCredentialAccessBoundaryFactory.newBuilder()
1171+
.setSourceCredential(sourceCredentials)
11231172
.build();
11241173

1125-
// Retrieve the downscoped access token.
1174+
// Create the CredentialAccessBoundary with the rule.
1175+
CredentialAccessBoundary credentialAccessBoundary =
1176+
CredentialAccessBoundary.newBuilder().addRule(rule).build();
1177+
1178+
// Generate the downscoped access token.
11261179
// This will need to be passed to the Token Consumer.
1127-
AccessToken downscopedAccessToken = downscopedCredentials.refreshAccessToken();
1180+
AccessToken downscopedAccessToken = factory.generateToken(credentialAccessBoundary);
11281181
```
11291182

1183+
#### Using Downscoped Access Tokens
1184+
1185+
A token broker can be set up on a server in a private network. Various workloads
1186+
(token consumers) in the same network will send authenticated requests to that
1187+
broker for downscoped tokens to access or modify specific google cloud storage
1188+
buckets.
1189+
1190+
The broker will instantiate downscoped credentials instances that can be used to
1191+
generate short-lived downscoped access tokens which will be passed to the token
1192+
consumer.
1193+
11301194
These downscoped access tokens can be used by the Token Consumer via `OAuth2Credentials` or
11311195
`OAuth2CredentialsWithRefresh`. This credential can then be used to initialize a storage client
11321196
instance to access Google Cloud Storage resources with restricted access.
@@ -1341,6 +1405,19 @@ Credentials credentials =
13411405
**Important: `com.google.auth.appengine.AppEngineCredentials` is a separate class from
13421406
`com.google.auth.oauth2.AppEngineCredentials`.**
13431407

1408+
## google-auth-library-cab-token-generator
1409+
1410+
This module provides the `ClientSideCredentialAccessBoundaryFactory` class,
1411+
enabling client-side generation of downscoped tokens for Cloud Storage using
1412+
Credential Access Boundaries. This approach is particularly useful for applications
1413+
requiring frequent changes to Credential Access Boundary rules or the generation
1414+
of many unique downscoped tokens, as it minimizes calls to the Security Token
1415+
Service (STS). For more details on when to consider this approach and how it
1416+
compares to the server-side method, see [Downscoping with Credential Access Boundaries](#downscoping-with-credential-access-boundaries).
1417+
For usage examples, see the [Client-side CAB](#client-side-cab) section.
1418+
This module comes with its own set of dependencies, so evaluate whether the
1419+
benefits of client-side downscoping outweigh the added complexity for your specific use case.
1420+
13441421
## CI Status
13451422

13461423
Java Version | Status

0 commit comments

Comments
 (0)