Skip to content

Commit

Permalink
WIP: reproduce panic if 403 from vault
Browse files Browse the repository at this point in the history
* adds testcase for termission denied error in config and restrouter
  • Loading branch information
azak-azkaran committed Nov 3, 2020
1 parent 21c2df6 commit b768d43
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 21 deletions.
1 change: 0 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ func GetResticConfig(config *vault.Config, token string, path string) (*ResticCo
data, err := getDataFromSecret(config, token, "restic/data/"+path)
if err != nil {
return nil, err

}

var conf ResticConfig
Expand Down
6 changes: 5 additions & 1 deletion config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ func TestConfigGetResticConfig(t *testing.T) {
require.NoError(t, err)
assert.False(t, seal, ERROR_VAULT_SEALED)

conf, err := GetResticConfig(testconfig.config, testconfig.token, testconfig.resticpath)
conf, err := GetResticConfig(testconfig.config, testconfig.token, "forbidden")
assert.Error(t, err)
assert.Nil(t, conf)

conf, err = GetResticConfig(testconfig.config, testconfig.token, testconfig.resticpath)
assert.NoError(t, err)
assert.NotNil(t, conf.Path)
assert.NotNil(t, conf.Password)
Expand Down
2 changes: 2 additions & 0 deletions restrouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ func postBackup(c *gin.Context) {
return
}

log.Println("config:", config)

var cmd *exec.Cmd
switch msg.Mode {
case "init":
Expand Down
24 changes: 24 additions & 0 deletions restrouter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,32 @@ 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)
setupRestrouterTest(t)
server, fun := RunRestServer(MAIN_TEST_ADDRESS)
mountMsg := MountMessage{
Expand Down
2 changes: 1 addition & 1 deletion vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func getDataFromSecret(config *vault.Config, token string, path string) (map[str
return nil, err
}

if secret == nil {
if secret == nil || secret.Data == nil {
return nil, errors.New(ERROR_VAULT_NO_SECRET)
}

Expand Down
48 changes: 30 additions & 18 deletions vault_gin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var multipleKey bool = false

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

func StartServer(t *testing.T, address string) {
if running {
Expand Down Expand Up @@ -63,23 +64,8 @@ func createHandler() http.Handler {
sealStatus = true
c.JSON(http.StatusOK, nil)
})
r.GET("/v1/restic/data/resticpath", func(c *gin.Context) {
log.Println("MOCK-Server: called resticpath")
var msg vault.Secret
data := make(map[string]interface{})
secret := make(map[string]string)
secret["path"] = "~/"
secret["repo"] = VAULT_TEST_BACKUP_PATH
secret["pw"] = VAULT_TEST_PASSWORD
secret["exclude"] = VAULT_TEST_BACKUP_EXCLUDE_FILE
secret["access_key"] = VAULT_TEST_BACKUP_ACCESS_KEY
secret["secret_key"] = VAULT_TEST_BACKUP_SECRET_KEY
data["data"] = secret
msg.Data = data
c.JSON(http.StatusOK, msg)
})
//r.GET("/v1/config/"+Hostname, config)
//r.GET("/v1/config/configpath", config)
r.GET("/v1/restic/data/resticpath", test_restic)
r.GET("/v1/restic/data/forbidden", test_forbidden)
r.GET("/v1/config/:name", func(c *gin.Context) {
name := c.Param("name")

Expand Down Expand Up @@ -124,7 +110,7 @@ func test_config(c *gin.Context) {
log.Println(err)
}

data["restic"] = "resticpath"
data["restic"] = ResticPath
data["gocryptfs"] = VAULT_TEST_CONFIGPATH
data["git"] = "gitpath,vimrc"
data["home"] = pwd
Expand Down Expand Up @@ -195,3 +181,29 @@ func test_unseal(c *gin.Context) {

c.JSON(http.StatusOK, msg)
}

func test_forbidden(c *gin.Context) {
log.Println("MOCK-Server: called forbidden")
var msg vault.Secret
data := make(map[string]interface{})
secret := make(map[string]string)
data["data"] = secret
msg.Data = data
c.JSON(http.StatusForbidden, msg)
}

func test_restic(c *gin.Context) {
log.Println("MOCK-Server: called resticpath")
var msg vault.Secret
data := make(map[string]interface{})
secret := make(map[string]string)
secret["path"] = "~/"
secret["repo"] = VAULT_TEST_BACKUP_PATH
secret["pw"] = VAULT_TEST_PASSWORD
secret["exclude"] = VAULT_TEST_BACKUP_EXCLUDE_FILE
secret["access_key"] = VAULT_TEST_BACKUP_ACCESS_KEY
secret["secret_key"] = VAULT_TEST_BACKUP_SECRET_KEY
data["data"] = secret
msg.Data = data
c.JSON(http.StatusOK, msg)
}

0 comments on commit b768d43

Please sign in to comment.