@@ -7,14 +7,18 @@ Open source authentication client library for Java.
7
7
8
8
- [ API Documentation] ( https://googleapis.dev/java/google-auth-library/latest )
9
9
10
- This project consists of 3 artifacts:
10
+ This project consists of 4 artifacts:
11
11
12
12
- [ * google-auth-library-credentials* ] ( #google-auth-library-credentials ) : contains base classes and
13
13
interfaces for Google credentials
14
14
- [ * google-auth-library-appengine* ] ( #google-auth-library-appengine ) : contains App Engine
15
15
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.
18
22
19
23
> ⚠️ Important: If you accept a credential configuration (credential JSON/File/Stream) from an external source for
20
24
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.
1011
1015
### Downscoping with Credential Access Boundaries
1012
1016
1013
1017
[ 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 ) ).
1016
1023
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
1022
1025
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 `
1024
1031
which specifies that the downscoped token will have readonly access to objects starting with
1025
1032
"customer-a" in bucket "bucket-123":
1026
1033
``` java
@@ -1042,37 +1049,80 @@ CredentialAccessBoundary credentialAccessBoundary =
1042
1049
CredentialAccessBoundary . newBuilder(). addRule(rule). build();
1043
1050
```
1044
1051
1052
+ #### Common Usage Pattern
1053
+
1045
1054
The common pattern of usage is to have a token broker with elevated access generate these downscoped
1046
1055
credentials from higher access source credentials and pass the downscoped short-lived access tokens
1047
1056
to a token consumer via some secure authenticated channel for limited access to Google Cloud Storage
1048
1057
resources.
1049
1058
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
+
1051
1088
``` java
1052
1089
// Retrieve the source credentials from ADC.
1053
1090
GoogleCredentials sourceCredentials = GoogleCredentials . getApplicationDefault()
1054
1091
.createScoped(" https://www.googleapis.com/auth/cloud-platform" );
1055
1092
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
+
1056
1107
// Initialize the DownscopedCredentials class.
1057
1108
DownscopedCredentials downscopedCredentials =
1058
1109
DownscopedCredentials . newBuilder()
1059
- .setSourceCredential(credentials )
1060
- .setCredentialAccessBoundary(credentialAccessBoundary )
1110
+ .setSourceCredential(sourceCredentials )
1111
+ .setCredentialAccessBoundary(CredentialAccessBoundary . newBuilder() . addRule(rule) . build() )
1061
1112
.build();
1062
1113
1063
1114
// Retrieve the downscoped access token.
1064
1115
// This will need to be passed to the Token Consumer.
1065
1116
AccessToken downscopedAccessToken = downscopedCredentials. refreshAccessToken();
1066
1117
```
1067
1118
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
1071
1120
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.
1074
1125
1075
- Putting it all together:
1076
1126
``` java
1077
1127
// Retrieve the source credentials from ADC.
1078
1128
GoogleCredentials sourceCredentials = GoogleCredentials . getApplicationDefault()
@@ -1092,18 +1142,32 @@ CredentialAccessBoundary.AccessBoundaryRule rule =
1092
1142
new AvailabilityCondition (expression, /* title= */ null , /* description= */ null ))
1093
1143
.build();
1094
1144
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)
1100
1149
.build();
1101
1150
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.
1103
1156
// This will need to be passed to the Token Consumer.
1104
- AccessToken downscopedAccessToken = downscopedCredentials . refreshAccessToken( );
1157
+ AccessToken downscopedAccessToken = factory . generateToken(credentialAccessBoundary );
1105
1158
```
1106
1159
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
+
1107
1171
These downscoped access tokens can be used by the Token Consumer via ` OAuth2Credentials ` or
1108
1172
` OAuth2CredentialsWithRefresh ` . This credential can then be used to initialize a storage client
1109
1173
instance to access Google Cloud Storage resources with restricted access.
@@ -1318,6 +1382,19 @@ Credentials credentials =
1318
1382
** Important: ` com.google.auth.appengine.AppEngineCredentials ` is a separate class from
1319
1383
` com.google.auth.oauth2.AppEngineCredentials ` .**
1320
1384
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
+
1321
1398
## CI Status
1322
1399
1323
1400
Java Version | Status
0 commit comments