diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/Bridge.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/Bridge.java index c7c27564f2..bf7316ae55 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/Bridge.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/Bridge.java @@ -35,6 +35,7 @@ import uk.ac.ic.wlgitbridge.git.handler.hook.WriteLatexPutHook; import uk.ac.ic.wlgitbridge.server.FileHandler; import uk.ac.ic.wlgitbridge.server.PostbackContents; import uk.ac.ic.wlgitbridge.server.PostbackHandler; +import uk.ac.ic.wlgitbridge.snapshot.base.DisabledRepositoryException; import uk.ac.ic.wlgitbridge.snapshot.base.ForbiddenException; import uk.ac.ic.wlgitbridge.snapshot.getforversion.SnapshotAttachment; import uk.ac.ic.wlgitbridge.snapshot.push.PostbackManager; @@ -383,6 +384,7 @@ public class Bridge { * @param hostname * @throws SnapshotPostException * @throws IOException + * @throws DisabledRepositoryException * @throws ForbiddenException */ public void push( @@ -391,7 +393,7 @@ public class Bridge { RawDirectory directoryContents, RawDirectory oldDirectoryContents, String hostname - ) throws SnapshotPostException, IOException, ForbiddenException { + ) throws SnapshotPostException, IOException, DisabledRepositoryException, ForbiddenException { try (LockGuard __ = lock.lockGuard(projectName)) { pushCritical( oauth2, @@ -460,6 +462,7 @@ public class Bridge { * @param directoryContents * @param oldDirectoryContents * @throws IOException + * @throws DisabledRepositoryException * @throws ForbiddenException * @throws SnapshotPostException */ @@ -468,7 +471,7 @@ public class Bridge { String projectName, RawDirectory directoryContents, RawDirectory oldDirectoryContents - ) throws IOException, ForbiddenException, SnapshotPostException { + ) throws IOException, DisabledRepositoryException, ForbiddenException, SnapshotPostException { Log.info("[{}] Pushing", projectName); String postbackKey = postbackManager.makeKeyForProject(projectName); Log.info( diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/snapshot/SnapshotApi.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/snapshot/SnapshotApi.java index d4edf22189..7b842779dd 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/snapshot/SnapshotApi.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/snapshot/SnapshotApi.java @@ -2,6 +2,7 @@ package uk.ac.ic.wlgitbridge.bridge.snapshot; import com.google.api.client.auth.oauth2.Credential; import uk.ac.ic.wlgitbridge.data.CandidateSnapshot; +import uk.ac.ic.wlgitbridge.snapshot.base.DisabledRepositoryException; import uk.ac.ic.wlgitbridge.snapshot.base.ForbiddenException; import uk.ac.ic.wlgitbridge.snapshot.exception.FailedConnectionException; import uk.ac.ic.wlgitbridge.snapshot.getdoc.GetDocResult; @@ -33,13 +34,14 @@ public interface SnapshotApi { String postbackKey); static T getResult(CompletableFuture result) - throws FailedConnectionException, ForbiddenException { + throws DisabledRepositoryException, FailedConnectionException, ForbiddenException { try { return result.join(); } catch (CompletionException e) { try { throw e.getCause(); - } catch (FailedConnectionException + } catch (DisabledRepositoryException + | FailedConnectionException | ForbiddenException | RuntimeException r) { throw r; diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/snapshot/SnapshotApiFacade.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/snapshot/SnapshotApiFacade.java index ea4d388101..69a7d0a317 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/snapshot/SnapshotApiFacade.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/snapshot/SnapshotApiFacade.java @@ -4,6 +4,7 @@ import com.google.api.client.auth.oauth2.Credential; import uk.ac.ic.wlgitbridge.data.CandidateSnapshot; import uk.ac.ic.wlgitbridge.data.model.Snapshot; import uk.ac.ic.wlgitbridge.git.exception.GitUserException; +import uk.ac.ic.wlgitbridge.snapshot.base.DisabledRepositoryException; import uk.ac.ic.wlgitbridge.snapshot.base.ForbiddenException; import uk.ac.ic.wlgitbridge.snapshot.exception.FailedConnectionException; import uk.ac.ic.wlgitbridge.snapshot.getdoc.GetDocResult; @@ -65,7 +66,7 @@ public class SnapshotApiFacade { Optional oauth2, CandidateSnapshot candidateSnapshot, String postbackKey - ) throws FailedConnectionException, ForbiddenException { + ) throws DisabledRepositoryException, FailedConnectionException, ForbiddenException { return SnapshotApi.getResult(api.push( oauth2, candidateSnapshot, postbackKey)); } diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/server/Oauth2Filter.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/server/Oauth2Filter.java index e33294f75e..54f5754ace 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/server/Oauth2Filter.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/server/Oauth2Filter.java @@ -6,6 +6,7 @@ import org.apache.commons.codec.binary.Base64; import org.eclipse.jetty.server.Request; import uk.ac.ic.wlgitbridge.application.config.Oauth2; import uk.ac.ic.wlgitbridge.bridge.snapshot.SnapshotApi; +import uk.ac.ic.wlgitbridge.snapshot.base.DisabledRepositoryException; import uk.ac.ic.wlgitbridge.snapshot.base.ForbiddenException; import uk.ac.ic.wlgitbridge.snapshot.getdoc.GetDocRequest; import uk.ac.ic.wlgitbridge.util.Instance; @@ -76,6 +77,9 @@ public class Oauth2Filter implements Filter { filterChain ); return; + } catch (DisabledRepositoryException e) { + Log.info("[{}] Git access disabled", project); + throw new ServletException(e); } Log.info("[{}] Auth not needed", project); filterChain.doFilter(servletRequest, servletResponse); diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/snapshot/base/DisabledRepositoryException.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/snapshot/base/DisabledRepositoryException.java new file mode 100644 index 0000000000..3a2640d6be --- /dev/null +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/snapshot/base/DisabledRepositoryException.java @@ -0,0 +1,24 @@ +package uk.ac.ic.wlgitbridge.snapshot.base; + +import com.google.gson.JsonElement; +import uk.ac.ic.wlgitbridge.git.exception.SnapshotAPIException; + +import java.util.Arrays; +import java.util.List; + +public class DisabledRepositoryException extends SnapshotAPIException { + + @Override + public void fromJSON(JsonElement json) {} + + @Override + public String getMessage() { + return "project not accessible over git"; + } + + @Override + public List getDescriptionLines() { + return Arrays.asList(getMessage()); + } + +} diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/snapshot/base/Request.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/snapshot/base/Request.java index 58491fb736..ee0849493e 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/snapshot/base/Request.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/snapshot/base/Request.java @@ -50,7 +50,7 @@ public abstract class Request { return ret; } - private T getResult() throws FailedConnectionException, ForbiddenException { + private T getResult() throws DisabledRepositoryException, FailedConnectionException, ForbiddenException { try { HttpResponse response = future.get(); Log.info( @@ -68,12 +68,18 @@ public abstract class Request { throw new FailedConnectionException(); } catch (ExecutionException e) { Throwable cause = e.getCause(); - if (cause instanceof HttpResponseException && - (((HttpResponseException) cause).getStatusCode() == - HttpServletResponse.SC_UNAUTHORIZED || - ((HttpResponseException) cause).getStatusCode() == - HttpServletResponse.SC_FORBIDDEN)) { - throw new ForbiddenException(); + if (cause instanceof HttpResponseException) { + HttpResponseException httpCause = (HttpResponseException) cause; + switch (httpCause.getStatusCode()) { + case HttpServletResponse.SC_UNAUTHORIZED: + case HttpServletResponse.SC_FORBIDDEN: + throw new ForbiddenException(); + case HttpServletResponse.SC_GONE: + throw new DisabledRepositoryException(); + default: + break; + } + throw new FailedConnectionException(cause); } else { throw new FailedConnectionException(cause); }