Skip to content

Commit

Permalink
Merge 11b4a6f into 21c2df6
Browse files Browse the repository at this point in the history
  • Loading branch information
azak-azkaran committed Nov 3, 2020
2 parents 21c2df6 + 11b4a6f commit e232cce
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 45 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
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
13 changes: 11 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,12 +261,14 @@ func postBackup(c *gin.Context) {
return
}

config.GetResticConfig()
err = config.GetResticConfig()
if err != nil {
returnErr(err, ERROR_CONFIG, c)
return
}

log.Println("config:", config)

var cmd *exec.Cmd
switch msg.Mode {
case "init":
Expand Down
67 changes: 46 additions & 21 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 @@ -181,6 +207,7 @@ func TestRestPostBackup(t *testing.T) {

func TestRestPostMount(t *testing.T) {
fmt.Println("running: TestRestPostMount")
t.Cleanup(clear)
setupRestrouterTest(t)
server, fun := RunRestServer(MAIN_TEST_ADDRESS)
mountMsg := MountMessage{
Expand Down Expand Up @@ -432,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)
}
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
52 changes: 34 additions & 18 deletions vault_gin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ 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
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,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 Expand Up @@ -195,3 +185,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 e232cce

Please sign in to comment.