@@ -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
@@ -1034,16 +1038,19 @@ googleapis.com domain.
1034
1038
### Downscoping with Credential Access Boundaries
1035
1039
1036
1040
[ 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 ) ).
1039
1046
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
1045
1048
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 `
1047
1054
which specifies that the downscoped token will have readonly access to objects starting with
1048
1055
"customer-a" in bucket "bucket-123":
1049
1056
``` java
@@ -1065,37 +1072,80 @@ CredentialAccessBoundary credentialAccessBoundary =
1065
1072
CredentialAccessBoundary . newBuilder(). addRule(rule). build();
1066
1073
```
1067
1074
1075
+ #### Common Usage Pattern
1076
+
1068
1077
The common pattern of usage is to have a token broker with elevated access generate these downscoped
1069
1078
credentials from higher access source credentials and pass the downscoped short-lived access tokens
1070
1079
to a token consumer via some secure authenticated channel for limited access to Google Cloud Storage
1071
1080
resources.
1072
1081
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
+
1074
1111
``` java
1075
1112
// Retrieve the source credentials from ADC.
1076
1113
GoogleCredentials sourceCredentials = GoogleCredentials . getApplicationDefault()
1077
1114
.createScoped(" https://www.googleapis.com/auth/cloud-platform" );
1078
1115
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
+
1079
1130
// Initialize the DownscopedCredentials class.
1080
1131
DownscopedCredentials downscopedCredentials =
1081
1132
DownscopedCredentials . newBuilder()
1082
- .setSourceCredential(credentials )
1083
- .setCredentialAccessBoundary(credentialAccessBoundary )
1133
+ .setSourceCredential(sourceCredentials )
1134
+ .setCredentialAccessBoundary(CredentialAccessBoundary . newBuilder() . addRule(rule) . build() )
1084
1135
.build();
1085
1136
1086
1137
// Retrieve the downscoped access token.
1087
1138
// This will need to be passed to the Token Consumer.
1088
1139
AccessToken downscopedAccessToken = downscopedCredentials. refreshAccessToken();
1089
1140
```
1090
1141
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
1094
1143
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.
1097
1148
1098
- Putting it all together:
1099
1149
``` java
1100
1150
// Retrieve the source credentials from ADC.
1101
1151
GoogleCredentials sourceCredentials = GoogleCredentials . getApplicationDefault()
@@ -1115,18 +1165,32 @@ CredentialAccessBoundary.AccessBoundaryRule rule =
1115
1165
new AvailabilityCondition (expression, /* title= */ null , /* description= */ null ))
1116
1166
.build();
1117
1167
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)
1123
1172
.build();
1124
1173
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.
1126
1179
// This will need to be passed to the Token Consumer.
1127
- AccessToken downscopedAccessToken = downscopedCredentials . refreshAccessToken( );
1180
+ AccessToken downscopedAccessToken = factory . generateToken(credentialAccessBoundary );
1128
1181
```
1129
1182
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
+
1130
1194
These downscoped access tokens can be used by the Token Consumer via ` OAuth2Credentials ` or
1131
1195
` OAuth2CredentialsWithRefresh ` . This credential can then be used to initialize a storage client
1132
1196
instance to access Google Cloud Storage resources with restricted access.
@@ -1341,6 +1405,19 @@ Credentials credentials =
1341
1405
** Important: ` com.google.auth.appengine.AppEngineCredentials ` is a separate class from
1342
1406
` com.google.auth.oauth2.AppEngineCredentials ` .**
1343
1407
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
+
1344
1421
## CI Status
1345
1422
1346
1423
Java Version | Status
0 commit comments