Skip to content

Commit b037146

Browse files
salrashid123chingor13
authored andcommitted
Add ImpersonatedCredentials (#211)
* Add impersonatedcredentials * constructors->private; handle errors as IOException
1 parent 1b5f8ac commit b037146

File tree

4 files changed

+859
-1
lines changed

4 files changed

+859
-1
lines changed

README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ following are searched (in order) to find the Application Default Credentials:
113113

114114
To get Credentials from a Service Account JSON key use `GoogleCredentials.fromStream(InputStream)`
115115
or `GoogleCredentials.fromStream(InputStream, HttpTransportFactory)`. Note that the credentials must
116-
be refreshed before the access token is available.
116+
be refreshed before the access token is available.
117117

118118
```java
119119
GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream("/path/to/credentials.json"));
@@ -123,6 +123,31 @@ AccessToken token = credentials.getAccessToken();
123123
AccessToken token = credentials.refreshAccessToken();
124124
```
125125

126+
### ImpersonatedCredentials
127+
128+
Allows a credentials issued to a user or service account to
129+
impersonate another. The source project using ImpersonaedCredentials must enable the
130+
"IAMCredentials" API. Also, the target service account must grant the orginating principal
131+
the "Service Account Token Creator" IAM role.
132+
133+
```java
134+
String credPath = "/path/to/svc_account.json";
135+
ServiceAccountCredentials sourceCredentials = ServiceAccountCredentials
136+
.fromStream(new FileInputStream(credPath));
137+
sourceCredentials = (ServiceAccountCredentials) sourceCredentials
138+
.createScoped(Arrays.asList("https://www.googleapis.com/auth/iam"));
139+
140+
ImpersonatedCredentials targetCredentials = ImpersonatedCredentials.create(sourceCredentials,
141+
142+
Arrays.asList("https://www.googleapis.com/auth/devstorage.read_only"), 300);
143+
144+
Storage storage_service = StorageOptions.newBuilder().setProjectId("project-id")
145+
.setCredentials(targetCredentials).build().getService();
146+
147+
for (Bucket b : storage_service.list().iterateAll())
148+
System.out.println(b);
149+
```
150+
126151
## CI Status
127152

128153
Java Version | Status

0 commit comments

Comments
 (0)