Skip to content

Commit

Permalink
Use null instead of NO_CREDENTIALS
Browse files Browse the repository at this point in the history
  • Loading branch information
Ajay Kannan committed Dec 1, 2015
1 parent 57d6e3f commit b5c1cae
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,6 @@ public static class ServiceAccountAuthCredentials extends AuthCredentials {
private final String account;
private final PrivateKey privateKey;

private static final AuthCredentials NO_CREDENTIALS = new ServiceAccountAuthCredentials();

private static class ServiceAccountAuthCredentialsState
implements RestorableState<AuthCredentials>, Serializable {

Expand All @@ -153,9 +151,6 @@ private ServiceAccountAuthCredentialsState(String account, PrivateKey privateKey

@Override
public AuthCredentials restore() {
if (account == null && privateKey == null) {
return NO_CREDENTIALS;
}
return new ServiceAccountAuthCredentials(account, privateKey);
}

Expand All @@ -180,16 +175,9 @@ public boolean equals(Object obj) {
this.privateKey = checkNotNull(privateKey);
}

ServiceAccountAuthCredentials() {
account = null;
privateKey = null;
}

@Override
protected GoogleCredentials credentials() {
return (privateKey == null)
? new GoogleCredentials(null)
: new ServiceAccountCredentials(null, account, privateKey, null, null);
return new ServiceAccountCredentials(null, account, privateKey, null, null);
}

public String account() {
Expand Down Expand Up @@ -327,8 +315,4 @@ public static ServiceAccountAuthCredentials createForJson(InputStream jsonCreden
"The given JSON Credentials Stream is not a service account credential.");
}
}

public static AuthCredentials noCredentials() {
return ServiceAccountAuthCredentials.NO_CREDENTIALS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,9 @@ protected ServiceOptions(Class<? extends ServiceFactory<ServiceT, OptionsT>> ser
httpTransportFactory = firstNonNull(builder.httpTransportFactory,
getFromServiceLoader(HttpTransportFactory.class, DefaultHttpTransportFactory.INSTANCE));
httpTransportFactoryClassName = httpTransportFactory.getClass().getName();
authCredentials = firstNonNull(builder.authCredentials, defaultAuthCredentials());
authCredentialsState = authCredentials.capture();
authCredentials =
builder.authCredentials != null ? builder.authCredentials : defaultAuthCredentials();
authCredentialsState = authCredentials != null ? authCredentials.capture() : null;
retryParams = builder.retryParams;
serviceFactory = firstNonNull(builder.serviceFactory,
getFromServiceLoader(serviceFactoryClass, defaultServiceFactory()));
Expand Down Expand Up @@ -349,7 +350,7 @@ private static AuthCredentials defaultAuthCredentials() {
try {
return AuthCredentials.createApplicationDefaults();
} catch (Exception ex) {
return AuthCredentials.noCredentials();
return null;
}
}

Expand Down Expand Up @@ -509,12 +510,15 @@ public RetryParams retryParams() {
* options.
*/
public HttpRequestInitializer httpRequestInitializer() {
final HttpRequestInitializer baseRequestInitializer =
new HttpCredentialsAdapter(authCredentials().credentials().createScoped(scopes()));
final HttpRequestInitializer delegate = authCredentials() != null
? new HttpCredentialsAdapter(authCredentials().credentials().createScoped(scopes()))
: null;
return new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest httpRequest) throws IOException {
baseRequestInitializer.initialize(httpRequest);
if (delegate != null) {
delegate.initialize(httpRequest);
}
if (connectTimeout >= 0) {
httpRequest.setConnectTimeout(connectTimeout);
}
Expand Down Expand Up @@ -580,7 +584,7 @@ private void readObject(ObjectInputStream input) throws IOException, ClassNotFou
httpTransportFactory = newInstance(httpTransportFactoryClassName);
serviceFactory = newInstance(serviceFactoryClassName);
serviceRpcFactory = newInstance(serviceRpcFactoryClassName);
authCredentials = authCredentialsState.restore();
authCredentials = authCredentialsState != null ? authCredentialsState.restore() : null;
}

private static <T> T newInstance(String className) throws IOException, ClassNotFoundException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public void testServiceOptions() throws Exception {
options = options.toBuilder()
.namespace("ns1")
.retryParams(RetryParams.defaultInstance())
.authCredentials(AuthCredentials.noCredentials())
.authCredentials(null)
.force(true)
.build();
serializedCopy = serializeAndDeserialize(options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void testServiceOptions() throws Exception {
options = options.toBuilder()
.projectId("p2")
.retryParams(RetryParams.defaultInstance())
.authCredentials(AuthCredentials.noCredentials())
.authCredentials(null)
.pathDelimiter(":")
.build();
serializedCopy = serializeAndDeserialize(options);
Expand Down Expand Up @@ -111,7 +111,6 @@ public void testReadChannelState() throws IOException, ClassNotFoundException {
StorageOptions options = StorageOptions.builder()
.projectId("p2")
.retryParams(RetryParams.defaultInstance())
.authCredentials(AuthCredentials.noCredentials())
.build();
BlobReadChannel reader =
new BlobReadChannelImpl(options, BlobId.of("b", "n"), EMPTY_RPC_OPTIONS);
Expand All @@ -127,7 +126,6 @@ public void testWriteChannelState() throws IOException, ClassNotFoundException {
StorageOptions options = StorageOptions.builder()
.projectId("p2")
.retryParams(RetryParams.defaultInstance())
.authCredentials(AuthCredentials.noCredentials())
.build();
BlobWriteChannelImpl writer = new BlobWriteChannelImpl(
options, BlobInfo.builder(BlobId.of("b", "n")).build(), "upload-id");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.common.io.BaseEncoding;
import com.google.gcloud.AuthCredentials;
import com.google.gcloud.AuthCredentials.ServiceAccountAuthCredentials;
import com.google.gcloud.Page;
import com.google.gcloud.RetryParams;
import com.google.gcloud.ServiceOptions;
import com.google.gcloud.Page;
import com.google.gcloud.spi.StorageRpc;
import com.google.gcloud.spi.StorageRpc.Tuple;
import com.google.gcloud.spi.StorageRpcFactory;
Expand Down Expand Up @@ -260,7 +259,6 @@ public void setUp() throws IOException, InterruptedException {
EasyMock.replay(rpcFactoryMock);
options = StorageOptions.builder()
.projectId("projectId")
.authCredentials(AuthCredentials.noCredentials())
.clock(TIME_SOURCE)
.serviceRpcFactory(rpcFactoryMock)
.build();
Expand Down

0 comments on commit b5c1cae

Please sign in to comment.