Skip to content

Commit

Permalink
RestRouter: Fixed Panic if Vault gives 403
Browse files Browse the repository at this point in the history
* If Vault Token is invalid or Config returns null, the restrouter used
  to panic.
  • Loading branch information
azak-azkaran committed Nov 3, 2020
1 parent b768d43 commit 11b4a6f
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 49 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
github.com/hashicorp/go-retryablehttp v0.6.6 // indirect
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
github.com/hashicorp/vault/api v1.0.5-0.20190730042357-746c0b111519
github.com/jstemmer/gotags v1.4.1 // indirect
github.com/kr/pretty v0.2.0 // indirect
github.com/mattn/go-colorable v0.1.6 // indirect
github.com/mitchellh/mapstructure v1.3.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJS
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/jstemmer/gotags v1.4.1 h1:aWIyXsU3lTDqhsEC49MP85p2cUUWr2ptvdGNqqGA3r4=
github.com/jstemmer/gotags v1.4.1/go.mod h1:b6J3X0bsLbR4C5SgSx3V3KjuWTtmRzcmWPbTkWZ49PA=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
Expand Down
2 changes: 1 addition & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func clear() {
fmt.Println("Error cleaning up: ", err.Error())
}
os.Remove(test_folder)

forbidden = false
}

func TestMainInit(t *testing.T) {
Expand Down
11 changes: 9 additions & 2 deletions restrouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,14 @@ func HandleMountFolders(cmds []*exec.Cmd, printOutput bool, test bool, run bool,

func returnErr(err error, source string, c *gin.Context) {
log.Println(ERROR_PREFIX+source, err.Error())
c.JSON(http.StatusInternalServerError, gin.H{

var code int
if source == ERROR_CONFIG {
code = http.StatusForbidden
} else {
code = http.StatusInternalServerError
}
c.JSON(code, gin.H{
JSON_MESSAGE: err.Error(),
})
}
Expand Down Expand Up @@ -254,7 +261,7 @@ func postBackup(c *gin.Context) {
return
}

config.GetResticConfig()
err = config.GetResticConfig()
if err != nil {
returnErr(err, ERROR_CONFIG, c)
return
Expand Down
89 changes: 45 additions & 44 deletions restrouter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,32 @@ func setupRestrouterTest(t *testing.T) {
}
}

func sendingPost(t *testing.T, endpoint string, statusCode int, msg interface{}) string {
reqBody, err := json.Marshal(msg)
require.NoError(t, err)
fmt.Println("Sending Body:", string(reqBody))
resp, err := http.Post(endpoint,
"application/json", bytes.NewBuffer(reqBody))
require.NoError(t, err)
defer resp.Body.Close()

assert.Equal(t, statusCode, resp.StatusCode)
bodyBytes, err := ioutil.ReadAll(resp.Body)
require.NoError(t, err)
return string(bodyBytes)
}

func sendingGet(t *testing.T, endpoint string, statusCode int) string {
resp, err := http.Get(endpoint)
require.NoError(t, err)

defer resp.Body.Close()
require.Equal(t, statusCode, resp.StatusCode)
bodyBytes, err := ioutil.ReadAll(resp.Body)
require.NoError(t, err)
return string(bodyBytes)
}

func TestRestCreateRestHandler(t *testing.T) {
fmt.Println("running: TestRestCreateRestHandler")
setupRestrouterTest(t)
Expand Down Expand Up @@ -179,29 +205,6 @@ func TestRestPostBackup(t *testing.T) {
assert.NoError(t, err)
}

func TestRestForbidden(t *testing.T) {
fmt.Println("running: TestRestPostBackup")
t.Cleanup(clear)
setupRestrouterTest(t)
server, fun := RunRestServer(MAIN_TEST_ADDRESS)

go fun()
time.Sleep(1 * time.Millisecond)

msg := BackupMessage{
Mode: "backup",
Test: true,
Run: true,
Debug: true,
PrintOutput: true,
Token: "randomtoken",
}
sendingPost(t, REST_TEST_BACKUP, http.StatusOK, msg)

err := server.Shutdown(context.Background())
assert.NoError(t, err)
}

func TestRestPostMount(t *testing.T) {
fmt.Println("running: TestRestPostMount")
t.Cleanup(clear)
Expand Down Expand Up @@ -456,28 +459,26 @@ func TestRestPostGit(t *testing.T) {
assert.DirExists(t, test_folder)
}

func sendingPost(t *testing.T, endpoint string, statusCode int, msg interface{}) string {
reqBody, err := json.Marshal(msg)
require.NoError(t, err)
fmt.Println("Sending Body:", string(reqBody))
resp, err := http.Post(endpoint,
"application/json", bytes.NewBuffer(reqBody))
require.NoError(t, err)
defer resp.Body.Close()
func TestRestForbidden(t *testing.T) {
fmt.Println("running: TestRestPostBackup")
t.Cleanup(clear)
forbidden = true
setupRestrouterTest(t)
server, fun := RunRestServer(MAIN_TEST_ADDRESS)

assert.Equal(t, statusCode, resp.StatusCode)
bodyBytes, err := ioutil.ReadAll(resp.Body)
require.NoError(t, err)
return string(bodyBytes)
}
go fun()
time.Sleep(1 * time.Millisecond)

func sendingGet(t *testing.T, endpoint string, statusCode int) string {
resp, err := http.Get(endpoint)
require.NoError(t, err)
msg := BackupMessage{
Mode: "backup",
Test: true,
Run: true,
Debug: true,
PrintOutput: true,
Token: "randomtoken",
}
sendingPost(t, REST_TEST_BACKUP, http.StatusForbidden, msg)

defer resp.Body.Close()
require.Equal(t, statusCode, resp.StatusCode)
bodyBytes, err := ioutil.ReadAll(resp.Body)
require.NoError(t, err)
return string(bodyBytes)
err := server.Shutdown(context.Background())
assert.NoError(t, err)
}
8 changes: 6 additions & 2 deletions vault_gin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ var server *http.Server
var running bool = false
var sealStatus bool = false
var multipleKey bool = false
var forbidden bool = false

var Progress = 0
var Hostname string
var ResticPath = "resticpath"

func StartServer(t *testing.T, address string) {
if running {
Expand Down Expand Up @@ -110,7 +110,11 @@ func test_config(c *gin.Context) {
log.Println(err)
}

data["restic"] = ResticPath
if forbidden {
data["restic"] = "forbidden"
} else {
data["restic"] = "resticpath"
}
data["gocryptfs"] = VAULT_TEST_CONFIGPATH
data["git"] = "gitpath,vimrc"
data["home"] = pwd
Expand Down

0 comments on commit 11b4a6f

Please sign in to comment.