Skip to content

Commit b020ceb

Browse files
committed
---
yaml --- r: 5951 b: refs/heads/tswast-patch-1 c: 2217787 h: refs/heads/master i: 5949: 7a2aa51 5947: b2d6530 5943: f7189cd 5935: fc0a976 5919: 42ea42a 5887: be8ff24
1 parent 9ae5ba0 commit b020ceb

3 files changed

Lines changed: 62 additions & 21 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,5 @@ refs/tags/v0.18.0: 9d193c4c4b9d1c6f21515dd8e50836b9194ec9bb
5757
refs/tags/v0.19.0: e67b56e4d8dad5f9a7b38c9b2107c23c828f2ed5
5858
refs/tags/v0.20.0: 839f7fb7156535146aa1cb2c5aadd8d375d854e8
5959
refs/tags/v0.20.1: 370471f437f1f4f68a11e068df5cd6bf39edb1fa
60-
refs/heads/tswast-patch-1: 8bd611cc80dcbc9b9a4224709146c90ae347647c
60+
refs/heads/tswast-patch-1: 2217787f1d869b1e45eb57df0a3b519150373d5e
6161
refs/heads/pubsub-streaming-pull: 19262b752ee874eb2ca3b950eb2aef44d5a5267b

branches/tswast-patch-1/src/main/java/com/google/gcloud/AuthCredentials.java

Lines changed: 60 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,23 @@
3030
import com.google.auth.oauth2.GoogleCredentials;
3131

3232
import java.io.IOException;
33+
import java.io.ObjectInputStream;
34+
import java.io.Serializable;
3335
import java.security.GeneralSecurityException;
3436
import java.security.PrivateKey;
3537
import java.util.Set;
3638

3739
/**
3840
* Credentials for accessing Google Cloud services.
3941
*/
40-
public abstract class AuthCredentials {
42+
public abstract class AuthCredentials implements Serializable {
43+
44+
private static final long serialVersionUID = 236297804453464604L;
4145

4246
private static class AppEngineAuthCredentials extends AuthCredentials {
4347

48+
private static final long serialVersionUID = 7931300552744202954L;
49+
4450
@Override
4551
protected HttpRequestInitializer httpRequestInitializer(HttpTransport transport,
4652
Set<String> scopes) {
@@ -50,6 +56,7 @@ protected HttpRequestInitializer httpRequestInitializer(HttpTransport transport,
5056

5157
private static class ServiceAccountAuthCredentials extends AuthCredentials {
5258

59+
private static final long serialVersionUID = 8007708734318445901L;
5360
private final String account;
5461
private final PrivateKey privateKey;
5562

@@ -78,23 +85,64 @@ protected HttpRequestInitializer httpRequestInitializer(
7885
}
7986
}
8087

88+
private static class ComputeEngineAuthCredentials extends AuthCredentials {
89+
90+
private static final long serialVersionUID = -5217355402127260144L;
91+
92+
private transient ComputeCredential computeCredential;
93+
94+
ComputeEngineAuthCredentials() throws IOException, GeneralSecurityException {
95+
computeCredential = getComputeCredential();
96+
}
97+
98+
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
99+
in.defaultReadObject();
100+
try {
101+
computeCredential = getComputeCredential();
102+
} catch (GeneralSecurityException e) {
103+
throw new IOException(e);
104+
}
105+
}
106+
107+
@Override
108+
protected HttpRequestInitializer httpRequestInitializer(HttpTransport transport,
109+
Set<String> scopes) {
110+
return computeCredential;
111+
}
112+
}
113+
114+
private static class ApplicationDefaultAuthCredentials extends AuthCredentials {
115+
116+
private static final long serialVersionUID = -8306873864136099893L;
117+
118+
private transient GoogleCredentials googleCredentials;
119+
120+
ApplicationDefaultAuthCredentials() throws IOException {
121+
googleCredentials = GoogleCredentials.getApplicationDefault();
122+
}
123+
124+
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
125+
in.defaultReadObject();
126+
googleCredentials = GoogleCredentials.getApplicationDefault();
127+
}
128+
129+
@Override
130+
protected HttpRequestInitializer httpRequestInitializer(HttpTransport transport,
131+
Set<String> scopes) {
132+
return new HttpCredentialsAdapter(googleCredentials);
133+
}
134+
}
135+
81136
protected abstract HttpRequestInitializer httpRequestInitializer(HttpTransport transport,
82137
Set<String> scopes);
83138

84139
public static AuthCredentials createForAppEngine() {
85140
return new AppEngineAuthCredentials();
86141
}
87142

88-
public static AuthCredentials createForComputeEngine() throws IOException,
89-
GeneralSecurityException {
90-
final ComputeCredential cred = getComputeCredential();
91-
return new AuthCredentials() {
92-
@Override
93-
protected HttpRequestInitializer httpRequestInitializer(HttpTransport transport,
94-
Set<String> scopes) {
95-
return cred;
96-
}
97-
};
143+
public static AuthCredentials createForComputeEngine()
144+
throws IOException, GeneralSecurityException {
145+
return new ComputeEngineAuthCredentials();
98146
}
99147

100148
/**
@@ -111,14 +159,7 @@ protected HttpRequestInitializer httpRequestInitializer(HttpTransport transport,
111159
* @throws IOException if the credentials cannot be created in the current environment.
112160
*/
113161
public static AuthCredentials createApplicationDefaults() throws IOException {
114-
final GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();
115-
return new AuthCredentials() {
116-
@Override
117-
protected HttpRequestInitializer httpRequestInitializer(HttpTransport transport,
118-
Set<String> scopes) {
119-
return new HttpCredentialsAdapter(credentials);
120-
}
121-
};
162+
return new ApplicationDefaultAuthCredentials();
122163
}
123164

124165
public static AuthCredentials createFor(String account, PrivateKey privateKey) {

0 commit comments

Comments
 (0)