diff --git a/services/git-bridge/README.md b/services/git-bridge/README.md index 1b01ddc49e..4b43c1b4bf 100644 --- a/services/git-bridge/README.md +++ b/services/git-bridge/README.md @@ -97,23 +97,20 @@ You have to restart the server for configuration changes to take effect. ## Creating OAuth app -In dev-env, run `bin/run rails_v1 rake db:seed`, or, if using this solo, run the following in the v1 -database: +In dev-env, run the following command in mongo to create the oauth application +for git-bridge. -```sql -INSERT INTO public.oauth_applications ( - "name", uid, secret, redirect_uri, scopes, skip_authorization, - created_at, updated_at, partner, confidential -) VALUES ( - 'gitbridge', - '264c723c925c13590880751f861f13084934030c13b4452901e73bdfab226edc', - 'e6b2e9eee7ae2bb653823250bb69594a91db0547fe3790a7135acb497108e62d', - 'http://www.overleaf.test:5000/no-callback-required', - 'git_bridge', - true, - now(), - now(), - null, - true -); +``` +db.oauthApplications.insert({ + "clientSecret" : "e6b2e9eee7ae2bb653823250bb69594a91db0547fe3790a7135acb497108e62d", + "grants" : [ + "password" + ], + "id" : "264c723c925c13590880751f861f13084934030c13b4452901e73bdfab226edc", + "name" : "Overleaf Git Bridge", + "redirectUris" : [], + "scopes" : [ + "git_bridge" + ] +}) ``` diff --git a/services/git-bridge/conf/example_config.json b/services/git-bridge/conf/example_config.json index 3b844ef099..7965ce9570 100644 --- a/services/git-bridge/conf/example_config.json +++ b/services/git-bridge/conf/example_config.json @@ -12,6 +12,7 @@ "oauth2Server": "https://localhost" }, "repoStore": { + "maxFileNum": 2000, "maxFileSize": 52428800 }, "swapStore": { diff --git a/services/git-bridge/conf/local.json b/services/git-bridge/conf/local.json index c119c85075..0acb24ac0a 100644 --- a/services/git-bridge/conf/local.json +++ b/services/git-bridge/conf/local.json @@ -12,6 +12,7 @@ "oauth2Server": "http://v2.overleaf.test:4000" }, "repoStore": { + "maxFileNum": 2000, "maxFileSize": 52428800 }, "swapStore": { 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 3c543f50f5..9246eb988f 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 @@ -28,6 +28,7 @@ import uk.ac.ic.wlgitbridge.data.filestore.RawFile; import uk.ac.ic.wlgitbridge.data.model.Snapshot; import uk.ac.ic.wlgitbridge.git.exception.GitUserException; import uk.ac.ic.wlgitbridge.git.exception.SizeLimitExceededException; +import uk.ac.ic.wlgitbridge.git.exception.FileLimitExceededException; import uk.ac.ic.wlgitbridge.git.handler.WLReceivePackFactory; import uk.ac.ic.wlgitbridge.git.handler.WLRepositoryResolver; import uk.ac.ic.wlgitbridge.git.handler.WLUploadPackFactory; @@ -426,6 +427,7 @@ public class Bridge { * @throws IOException * @throws MissingRepositoryException * @throws ForbiddenException + * @throws GitUserException */ public void push( Optional oauth2, @@ -433,7 +435,7 @@ public class Bridge { RawDirectory directoryContents, RawDirectory oldDirectoryContents, String hostname - ) throws SnapshotPostException, IOException, MissingRepositoryException, ForbiddenException { + ) throws SnapshotPostException, IOException, MissingRepositoryException, ForbiddenException, GitUserException { try (LockGuard __ = lock.lockGuard(projectName)) { pushCritical( oauth2, @@ -507,13 +509,23 @@ public class Bridge { * @throws MissingRepositoryException * @throws ForbiddenException * @throws SnapshotPostException + * @throws GitUserException */ private void pushCritical( Optional oauth2, String projectName, RawDirectory directoryContents, RawDirectory oldDirectoryContents - ) throws IOException, MissingRepositoryException, ForbiddenException, SnapshotPostException { + ) throws IOException, MissingRepositoryException, ForbiddenException, SnapshotPostException, GitUserException { + Optional maxFileNum = config + .getRepoStore() + .flatMap(RepoStoreConfig::getMaxFileNum); + if (maxFileNum.isPresent()) { + long maxFileNum_ = maxFileNum.get(); + if (directoryContents.getFileTable().size() > maxFileNum_) { + throw new FileLimitExceededException(directoryContents.getFileTable().size(), maxFileNum_); + } + } Log.info("[{}] Pushing", projectName); String postbackKey = postbackManager.makeKeyForProject(projectName); Log.info( @@ -529,7 +541,7 @@ public class Bridge { ); ) { Log.info( - "[{}] Candindate snapshot created: {}", + "[{}] Candidate snapshot created: {}", projectName, candidate ); diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/repo/RepoStoreConfig.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/repo/RepoStoreConfig.java index 3bfd308dcd..52d41104a1 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/repo/RepoStoreConfig.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/repo/RepoStoreConfig.java @@ -11,11 +11,19 @@ public class RepoStoreConfig { @Nullable private final Long maxFileSize; - public RepoStoreConfig(Long maxFileSize) { + @Nullable + private final Long maxFileNum; + + public RepoStoreConfig(Long maxFileSize, Long maxFileNum) { this.maxFileSize = maxFileSize; + this.maxFileNum = maxFileNum; } public Optional getMaxFileSize() { return Optional.ofNullable(maxFileSize); } + + public Optional getMaxFileNum() { + return Optional.ofNullable(maxFileNum); + } } diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/git/exception/FileLimitExceededException.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/git/exception/FileLimitExceededException.java new file mode 100644 index 0000000000..f25767a4db --- /dev/null +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/git/exception/FileLimitExceededException.java @@ -0,0 +1,34 @@ +package uk.ac.ic.wlgitbridge.git.exception; + +import uk.ac.ic.wlgitbridge.util.Util; + +import java.util.Arrays; +import java.util.List; +import java.util.Optional; + +public class FileLimitExceededException extends GitUserException { + + private final long numFiles; + + private final long maxFiles; + + public FileLimitExceededException(long numFiles, long maxFiles) { + this.numFiles = numFiles; + this.maxFiles = maxFiles; + } + + @Override + public String getMessage() { + return "too many files"; + } + + @Override + public List getDescriptionLines() { + return Arrays.asList( + "repository contains " + + numFiles + " files, which exceeds the limit of " + + maxFiles + " files" + ); + } + +}