Skip to content

Commit 1f1412a

Browse files
committed
fix: add null checks for getCause() usages in exception handling
1 parent 12e8d55 commit 1f1412a

4 files changed

Lines changed: 29 additions & 11 deletions

File tree

jib-core/src/main/java/com/google/cloud/tools/jib/api/RegistryUnauthorizedException.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.google.api.client.http.HttpResponseException;
2020
import com.google.cloud.tools.jib.http.ResponseException;
21+
import javax.annotation.Nullable;
2122

2223
/** Thrown when a registry request was unauthorized and therefore authentication is needed. */
2324
public class RegistryUnauthorizedException extends RegistryException {
@@ -43,7 +44,12 @@ public String getImageReference() {
4344
return registry + "/" + repository;
4445
}
4546

47+
@Nullable
4648
public HttpResponseException getHttpResponseException() {
47-
return (HttpResponseException) getCause().getCause();
49+
if (getCause() != null && getCause().getCause() != null) {
50+
return (HttpResponseException) getCause().getCause();
51+
} else {
52+
return null;
53+
}
4854
}
4955
}

jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/PullBaseImageStep.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,10 @@ public ImagesAndRegistryClient call()
172172
.setCredential(credential)
173173
.newRegistryClient();
174174

175-
String wwwAuthenticate = ex.getHttpResponseException().getHeaders().getAuthenticate();
175+
String wwwAuthenticate =
176+
ex.getHttpResponseException() != null
177+
? ex.getHttpResponseException().getHeaders().getAuthenticate()
178+
: null;
176179
if (wwwAuthenticate != null) {
177180
eventHandlers.dispatch(
178181
LogEvent.debug("WWW-Authenticate for " + imageReference + ": " + wwwAuthenticate));

jib-core/src/main/java/com/google/cloud/tools/jib/registry/RegistryClient.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -623,16 +623,20 @@ private <T> T callRegistryEndpoint(RegistryEndpointProvider<T> registryEndpointP
623623
.call();
624624

625625
} catch (RegistryUnauthorizedException ex) {
626-
if (ex.getHttpResponseException().getStatusCode()
627-
!= HttpStatusCodes.STATUS_CODE_UNAUTHORIZED
626+
if ((ex.getHttpResponseException() != null
627+
&& ex.getHttpResponseException().getStatusCode()
628+
!= HttpStatusCodes.STATUS_CODE_UNAUTHORIZED)
628629
|| !isBearerAuth(authorization.get())
629630
|| ++bearerTokenRefreshes >= MAX_BEARER_TOKEN_REFRESH_TRIES) {
630631
throw ex;
631632
}
632633

633634
// Because we successfully did bearer authentication initially, getting 401 here probably
634635
// means the token was expired.
635-
String wwwAuthenticate = ex.getHttpResponseException().getHeaders().getAuthenticate();
636+
String wwwAuthenticate =
637+
ex.getHttpResponseException() != null
638+
? ex.getHttpResponseException().getHeaders().getAuthenticate()
639+
: null;
636640
authorization.set(refreshBearerAuth(wwwAuthenticate));
637641
}
638642
}

jib-plugins-common/src/main/java/com/google/cloud/tools/jib/plugins/common/JibBuildRunner.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,9 @@ private static void handleRegistryUnauthorizedException(
170170
RegistryUnauthorizedException registryUnauthorizedException,
171171
HelpfulSuggestions helpfulSuggestions)
172172
throws BuildStepsExecutionException {
173-
if (registryUnauthorizedException.getHttpResponseException().getStatusCode()
174-
== HttpStatusCodes.STATUS_CODE_FORBIDDEN) {
173+
if (registryUnauthorizedException.getHttpResponseException() != null
174+
&& registryUnauthorizedException.getHttpResponseException().getStatusCode()
175+
== HttpStatusCodes.STATUS_CODE_FORBIDDEN) {
175176
// No permissions for registry/repository.
176177
throw new BuildStepsExecutionException(
177178
helpfulSuggestions.forHttpStatusCodeForbidden(
@@ -280,10 +281,14 @@ public JibContainer runBuild()
280281
throw new BuildStepsExecutionException(message, ex);
281282

282283
} catch (ExecutionException ex) {
283-
String message = ex.getCause().getMessage();
284-
throw new BuildStepsExecutionException(
285-
message == null ? "(null exception message)" : message, ex.getCause());
286-
284+
if (ex.getCause() != null) {
285+
String message =ex.getCause().getMessage();
286+
throw new BuildStepsExecutionException(
287+
message == null ? "(null exception message)" : message, ex.getCause());
288+
} else {
289+
// Unknown cause
290+
throw new BuildStepsExecutionException(helpfulSuggestions.none(), ex);
291+
}
287292
} catch (InterruptedException ex) {
288293
Thread.currentThread().interrupt();
289294
throw new BuildStepsExecutionException(helpfulSuggestions.none(), ex);

0 commit comments

Comments
 (0)