-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove backup api at /database/backup; change method from GET to POST #33
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,14 @@ | ||
val ScalatraVersion = "2.7.1" | ||
|
||
organization := "fr.brouillard.gitbucket" | ||
name := "gitbucket-h2-backup-plugin" | ||
version := "1.9.0" | ||
scalaVersion := "2.13.3" | ||
gitbucketVersion := "4.35.0" | ||
scalacOptions += "-deprecation" | ||
|
||
libraryDependencies ++= Seq( | ||
"org.scalatest" %% "scalatest-funspec" % "3.2.3" % "test", | ||
"org.scalatest" %% "scalatest-funsuite" % "3.2.3" % "test", | ||
"org.scalatra" %% "scalatra-scalatest" % ScalatraVersion % "test", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
src/test/scala/fr/brouillard/gitbucket/h2/controller/H2BackupControllerTests.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package fr.brouillard.gitbucket.h2.controller | ||
|
||
import gitbucket.core.model.Account | ||
import gitbucket.core.servlet.ApiAuthenticationFilter | ||
import org.scalatest.funsuite.AnyFunSuite | ||
import org.scalatest.matchers.should.Matchers.{convertToAnyShouldWrapper, equal} | ||
import org.scalatra.{Ok, Params, ScalatraParams} | ||
import org.scalatra.test.scalatest.ScalatraFunSuite | ||
|
||
import java.io.File | ||
import java.util.Date | ||
|
||
class H2BackupControllerTests extends ScalatraFunSuite { | ||
c0llab0rat0r marked this conversation as resolved.
Show resolved
Hide resolved
|
||
addFilter(classOf[ApiAuthenticationFilter], path="/api/*") | ||
addFilter(classOf[H2BackupController], "/*") | ||
|
||
test("get database backup api") { | ||
get("/api/v3/plugins/database/backup") { | ||
status should equal (405) | ||
body should include ("This has moved") | ||
} | ||
} | ||
|
||
test("get database backup legacy") { | ||
get("/database/backup") { | ||
status should equal (405) | ||
body should include ("This has moved") | ||
} | ||
} | ||
|
||
test("post database backup without credentials is unauthorized") { | ||
post("/api/v3/plugins/database/backup") { | ||
status should equal (401) | ||
} | ||
} | ||
|
||
} | ||
|
||
class H2BackupControllerObjectTests extends AnyFunSuite { | ||
private def assertDefaultFileName(name: String): Unit = { | ||
assert(name.startsWith("gitbucket-db")) | ||
assert(name.endsWith(".zip")) | ||
} | ||
|
||
private def buildAccount(isAdmin: Boolean) = { | ||
Account( | ||
userName = "a", | ||
fullName = "b", | ||
mailAddress = "c", | ||
password = "d", | ||
isAdmin = isAdmin, | ||
url = None, | ||
registeredDate = new Date(), | ||
updatedDate = new Date(), | ||
lastLoginDate = None, | ||
image = None, | ||
isGroupAccount = false, | ||
isRemoved = false, | ||
description = None) | ||
} | ||
|
||
test("generates default file name") { | ||
assertDefaultFileName(H2BackupController.defaultBackupFileName()) | ||
} | ||
|
||
test("post database backup with admin credentials is executed with default file name") { | ||
val account = buildAccount(true) | ||
val params: Params = new ScalatraParams(Map()) | ||
|
||
var executed = false; | ||
|
||
val exportDatabase = (file: File) => { | ||
assert(!executed) | ||
assertDefaultFileName(file.getName) | ||
|
||
executed = true | ||
} | ||
|
||
val action = H2BackupController.doBackup(exportDatabase, Some(account), params) | ||
|
||
assert(executed) | ||
assert(action.status == 200) | ||
|
||
// Not JSON and not HTML | ||
assert(action.headers.get("Content-Type").contains("text/plain")) | ||
} | ||
|
||
test("post database backup with admin credentials is executed with specific file name") { | ||
val fileName = "foo.zip" | ||
val account = buildAccount(true) | ||
val params: Params = new ScalatraParams(Map("dest" -> Seq(fileName))) | ||
|
||
var executed = false; | ||
|
||
val exportDatabase = (file: File) => { | ||
assert(!executed) | ||
assert(file.getName.equals(fileName)) | ||
|
||
executed = true | ||
} | ||
|
||
val action = H2BackupController.doBackup(exportDatabase, Some(account), params) | ||
|
||
assert(executed) | ||
assert(action.status == 200) | ||
|
||
// Not JSON and not HTML | ||
assert(action.headers.get("Content-Type").contains("text/plain")) | ||
} | ||
|
||
test("post database backup with unprivileged credentials is unauthorized") { | ||
val account = buildAccount(false) | ||
val params: Params = new ScalatraParams(Map()) | ||
|
||
val exportDatabase = (file: File) => { | ||
fail() | ||
} | ||
|
||
val action = H2BackupController.doBackup(exportDatabase, Some(account), params) | ||
assert(action.status == 401) | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the design in this pull request (moving functional methods to the companion object), this method also can be moved to the companion object.
Connection
can be taken as an argument.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hadn't made any changes to
exportDatabase
and was trying to minimize the scope of the code changes while still having good hygiene (like writing tests) - so I did the minimum refactor to have tests.Testing
exportDatabase
is a bit more involved since it would require calling a live database. I didn't want to change that area of the code and thus become responsible for it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Separately, I feel like there's some design or constraint issues with how backup file names are dealt with.
It seems problematic that an external service can pass a
dest
with an absolute (from root/
) folder or an upwardly-mobile relative folder (../../somewhere-else
) to a path on the GitBucket server. I think the path should be, at minimum, constrained to a subfolder that's determined by the GitBucket server configuration.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that is true. Also, I found that actions provided by this plugin don't have permission checking although usage of these actions should be limited to admin only. Anyway, they are out of the scope of this pull request.