From 3a77d12151426f62c6d6225792bbcd925c4d3422 Mon Sep 17 00:00:00 2001 From: yaoharry Date: Thu, 14 Feb 2019 16:28:44 -0800 Subject: [PATCH 1/6] Switched home fetch to local function with windows compatability --- cmd/inpututil/input.go | 10 +++++++--- daemon/inertiad/build/util.go | 5 ++++- provision/ec2.go | 8 ++++++-- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/cmd/inpututil/input.go b/cmd/inpututil/input.go index 77031fdb..9d9b5519 100644 --- a/cmd/inpututil/input.go +++ b/cmd/inpututil/input.go @@ -4,9 +4,10 @@ import ( "errors" "fmt" "io" - "os" "path/filepath" + "github.com/ubclaunchpad/inertia/local" + "github.com/ubclaunchpad/inertia/cfg" "github.com/ubclaunchpad/inertia/common" ) @@ -26,13 +27,16 @@ func AddRemoteWalkthrough( in io.Reader, config *cfg.Config, name, port, sshPort, currBranch string, ) error { - homeEnvVar := os.Getenv("HOME") + homeEnvVar, err := local.GetHomePath() + if err != nil { + return err + } sshDir := filepath.Join(homeEnvVar, ".ssh") defaultSSHLoc := filepath.Join(sshDir, "id_rsa") var response string fmt.Println("Enter location of PEM file (leave blank to use '" + defaultSSHLoc + "'):") - _, err := fmt.Fscanln(in, &response) + _, err = fmt.Fscanln(in, &response) if err != nil { response = defaultSSHLoc } diff --git a/daemon/inertiad/build/util.go b/daemon/inertiad/build/util.go index 94a92085..71fcb031 100644 --- a/daemon/inertiad/build/util.go +++ b/daemon/inertiad/build/util.go @@ -8,6 +8,8 @@ import ( "os" "path/filepath" "strings" + + "github.com/ubclaunchpad/inertia/local" ) // getTrueDirectory converts given filepath to host-based filepath if applicable @@ -15,7 +17,8 @@ import ( // executed on the host, using the host's filepaths, which means Docker client // commands must use this function when dealing with paths func getTrueDirectory(path string) string { - return strings.Replace(path, "/app/host", os.Getenv("HOME"), 1) + homeDir, _ := local.GetHomePath() + return strings.Replace(path, "/app/host", homeDir, 1) } // buildTar takes a source and variable writers and walks 'source' writing each file diff --git a/provision/ec2.go b/provision/ec2.go index f8efd5c8..ee8d1a87 100644 --- a/provision/ec2.go +++ b/provision/ec2.go @@ -5,7 +5,6 @@ import ( "fmt" "io" "net" - "os" "path/filepath" "sort" "strconv" @@ -151,8 +150,13 @@ func (p *EC2Provisioner) CreateInstance(opts EC2CreateInstanceOptions) (*cfg.Rem return nil, err } + homeDir, err := local.GetHomePath() + if err != nil { + return nil, err + } + // Save key - keyPath := filepath.Join(os.Getenv("HOME"), ".ssh", *keyResp.KeyName) + keyPath := filepath.Join(homeDir, ".ssh", *keyResp.KeyName) fmt.Printf("Saving key to %s...\n", keyPath) if err = local.SaveKey(*keyResp.KeyMaterial, keyPath); err != nil { return nil, err From 42f069edee535263de08078145666405a9642d01 Mon Sep 17 00:00:00 2001 From: Robert Lin Date: Thu, 14 Feb 2019 23:50:26 -0800 Subject: [PATCH 2/6] Update cmd/inpututil/input.go Co-Authored-By: yaoharry --- cmd/inpututil/input.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cmd/inpututil/input.go b/cmd/inpututil/input.go index 9d9b5519..42433116 100644 --- a/cmd/inpututil/input.go +++ b/cmd/inpututil/input.go @@ -7,7 +7,6 @@ import ( "path/filepath" "github.com/ubclaunchpad/inertia/local" - "github.com/ubclaunchpad/inertia/cfg" "github.com/ubclaunchpad/inertia/common" ) From 0d5219b66289c087bb31c8c4661e9b7944994f0a Mon Sep 17 00:00:00 2001 From: yaoharry Date: Thu, 14 Feb 2019 23:54:54 -0800 Subject: [PATCH 3/6] error one liner --- cmd/inpututil/input.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/cmd/inpututil/input.go b/cmd/inpututil/input.go index 9d9b5519..9ab98f92 100644 --- a/cmd/inpututil/input.go +++ b/cmd/inpututil/input.go @@ -6,10 +6,9 @@ import ( "io" "path/filepath" - "github.com/ubclaunchpad/inertia/local" - "github.com/ubclaunchpad/inertia/cfg" "github.com/ubclaunchpad/inertia/common" + "github.com/ubclaunchpad/inertia/local" ) var ( @@ -36,8 +35,7 @@ func AddRemoteWalkthrough( var response string fmt.Println("Enter location of PEM file (leave blank to use '" + defaultSSHLoc + "'):") - _, err = fmt.Fscanln(in, &response) - if err != nil { + if _, err = fmt.Fscanln(in, &response); err != nil { response = defaultSSHLoc } pemLoc := response From 942ee143bdb167797478406ea2c6c4819856183d Mon Sep 17 00:00:00 2001 From: yaoharry Date: Fri, 15 Feb 2019 00:05:33 -0800 Subject: [PATCH 4/6] Reverted daemon file --- daemon/inertiad/build/util.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/daemon/inertiad/build/util.go b/daemon/inertiad/build/util.go index 71fcb031..94a92085 100644 --- a/daemon/inertiad/build/util.go +++ b/daemon/inertiad/build/util.go @@ -8,8 +8,6 @@ import ( "os" "path/filepath" "strings" - - "github.com/ubclaunchpad/inertia/local" ) // getTrueDirectory converts given filepath to host-based filepath if applicable @@ -17,8 +15,7 @@ import ( // executed on the host, using the host's filepaths, which means Docker client // commands must use this function when dealing with paths func getTrueDirectory(path string) string { - homeDir, _ := local.GetHomePath() - return strings.Replace(path, "/app/host", homeDir, 1) + return strings.Replace(path, "/app/host", os.Getenv("HOME"), 1) } // buildTar takes a source and variable writers and walks 'source' writing each file From 8cd8b0d2841a76b65d73423302dbdf582c009870 Mon Sep 17 00:00:00 2001 From: yaoharry Date: Fri, 15 Feb 2019 22:55:54 -0800 Subject: [PATCH 5/6] Added TOTP tests --- client/client_test.go | 83 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/client/client_test.go b/client/client_test.go index 87987a83..16c3a884 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -130,6 +130,24 @@ func TestDaemonUp(t *testing.T) { assert.Equal(t, actualCommand, session.Calls[0]) } +func TestDaemonDown(t *testing.T) { + session := &mockSSHRunner{} + client := newMockSSHClient(session) + client.version = "latest" + client.IP = "0.0.0.0" + client.Daemon.Port = "4303" + script, err := ioutil.ReadFile("scripts/daemon-up.sh") + assert.Nil(t, err) + actualCommand := fmt.Sprintf(string(script), "latest", "4303", "0.0.0.0") + + // Make sure the right command is run. + err = client.DaemonUp("latest") + err = client.DaemonDown() + assert.Nil(t, err) + println(actualCommand) + assert.Equal(t, actualCommand, session.Calls[0]) +} + func TestKeyGen(t *testing.T) { session := &mockSSHRunner{} remote := newMockSSHClient(session) @@ -561,3 +579,68 @@ func TestLogIn(t *testing.T) { assert.Nil(t, err) assert.Equal(t, http.StatusOK, resp.StatusCode) } + +func TestEnableTotp(t *testing.T) { + testServer := httptest.NewTLSServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { + rw.WriteHeader(http.StatusOK) + + // Check request method + assert.Equal(t, "POST", req.Method) + + // Check correct endpoint called + endpoint := req.URL.Path + assert.Equal(t, "/user/totp/enable", endpoint) + + // Check auth + assert.Equal(t, "Bearer "+fakeAuth, req.Header.Get("Authorization")) + })) + defer testServer.Close() + + d := newMockClient(testServer) + resp, err := d.EnableTotp("", "") + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, resp.StatusCode) +} + +func TestDisableTotp(t *testing.T) { + testServer := httptest.NewTLSServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { + rw.WriteHeader(http.StatusOK) + + // Check request method + assert.Equal(t, "POST", req.Method) + + // Check correct endpoint called + endpoint := req.URL.Path + assert.Equal(t, "/user/totp/disable", endpoint) + + // Check auth + assert.Equal(t, "Bearer "+fakeAuth, req.Header.Get("Authorization")) + })) + defer testServer.Close() + + d := newMockClient(testServer) + resp, err := d.DisableTotp() + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, resp.StatusCode) +} + +func TestSetSSLVerification(t *testing.T) { + testServer := httptest.NewTLSServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { + rw.WriteHeader(http.StatusOK) + + // Check request method + assert.Equal(t, "POST", req.Method) + + // Check correct endpoint called + endpoint := req.URL.Path + assert.Equal(t, "/user/totp/disable", endpoint) + + // Check auth + assert.Equal(t, "Bearer "+fakeAuth, req.Header.Get("Authorization")) + })) + defer testServer.Close() + + d := newMockClient(testServer) + d.SetSSLVerification(true) + assert.Equal(t, d.verifySSL, true) +} From f22f926666c8a2d5fa94d6eb7ed9c4d328a04c7b Mon Sep 17 00:00:00 2001 From: yaoharry Date: Sat, 16 Feb 2019 11:39:01 -0800 Subject: [PATCH 6/6] Fixed daemon down test --- client/client_test.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/client/client_test.go b/client/client_test.go index 16c3a884..0ff5d8a0 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -136,12 +136,11 @@ func TestDaemonDown(t *testing.T) { client.version = "latest" client.IP = "0.0.0.0" client.Daemon.Port = "4303" - script, err := ioutil.ReadFile("scripts/daemon-up.sh") + script, err := ioutil.ReadFile("scripts/daemon-down.sh") assert.Nil(t, err) - actualCommand := fmt.Sprintf(string(script), "latest", "4303", "0.0.0.0") + actualCommand := fmt.Sprintf(string(script)) // Make sure the right command is run. - err = client.DaemonUp("latest") err = client.DaemonDown() assert.Nil(t, err) println(actualCommand)