From ecddc2fe2163c76d6d77aa2340d5815a7d5536d0 Mon Sep 17 00:00:00 2001 From: redanthrax Date: Wed, 15 Jun 2022 14:42:15 -0700 Subject: [PATCH 01/11] refactor --- agent/agent.go | 12 ++++++------ agent/agent_linux.go | 6 +++--- agent/install.go | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/agent/agent.go b/agent/agent.go index 567d56a..5d7a836 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -52,7 +52,7 @@ type Agent struct { EXE string SystemDrive string MeshInstaller string - MeshSystemEXE string + MeshSystemBin string MeshSVC string PyBin string Headers map[string]string @@ -114,15 +114,15 @@ func New(logger *logrus.Logger, version string) *Agent { restyC.SetRootCertificate(ac.Cert) } - var MeshSysExe string + var MeshSysBin string if len(ac.CustomMeshDir) > 0 { - MeshSysExe = filepath.Join(ac.CustomMeshDir, "MeshAgent.exe") + MeshSysBin = filepath.Join(ac.CustomMeshDir, "MeshAgent.exe") } else { - MeshSysExe = filepath.Join(os.Getenv("ProgramFiles"), "Mesh Agent", "MeshAgent.exe") + MeshSysBin = filepath.Join(os.Getenv("ProgramFiles"), "Mesh Agent", "MeshAgent.exe") } if runtime.GOOS == "linux" { - MeshSysExe = "/opt/tacticalmesh/meshagent" + MeshSysBin = "/opt/tacticalmesh/meshagent" } svcConf := &service.Config{ @@ -152,7 +152,7 @@ func New(logger *logrus.Logger, version string) *Agent { EXE: exe, SystemDrive: sd, MeshInstaller: "meshagent.exe", - MeshSystemEXE: MeshSysExe, + MeshSystemBin: MeshSysBin, MeshSVC: meshSvcName, PyBin: pybin, Headers: headers, diff --git a/agent/agent_linux.go b/agent/agent_linux.go index 1027344..2b13410 100644 --- a/agent/agent_linux.go +++ b/agent/agent_linux.go @@ -278,14 +278,14 @@ func (a *Agent) NixMeshNodeID() string { meshSuccess := false a.Logger.Debugln("Getting mesh node id") - if !trmm.FileExists(a.MeshSystemEXE) { - a.Logger.Debugln(a.MeshSystemEXE, "does not exist. Skipping.") + if !trmm.FileExists(a.MeshSystemBin) { + a.Logger.Debugln(a.MeshSystemBin, "does not exist. Skipping.") return "" } opts := a.NewCMDOpts() opts.IsExecutable = true - opts.Shell = a.MeshSystemEXE + opts.Shell = a.MeshSystemBin opts.Command = "-nodeid" for !meshSuccess { diff --git a/agent/install.go b/agent/install.go index 8614794..f941836 100644 --- a/agent/install.go +++ b/agent/install.go @@ -150,7 +150,7 @@ func (a *Agent) Install(i *Installer) { if len(i.MeshDir) > 0 { installerMeshSystemEXE = filepath.Join(i.MeshDir, "MeshAgent.exe") } else { - installerMeshSystemEXE = a.MeshSystemEXE + installerMeshSystemEXE = a.MeshSystemBin } var meshNodeID string From d1df98ad3e58cd8868222002554173b7c366cc71 Mon Sep 17 00:00:00 2001 From: redanthrax Date: Wed, 15 Jun 2022 14:42:40 -0700 Subject: [PATCH 02/11] debug and vscode debug launch --- .gitignore | 1 + .vscode/launch.json | 18 ++++++++++++++++++ agent/rpc.go | 4 ++++ shared/debug.go | 5 +++++ shared/nodebug.go | 5 +++++ 5 files changed, 33 insertions(+) create mode 100644 .vscode/launch.json create mode 100644 shared/debug.go create mode 100644 shared/nodebug.go diff --git a/.gitignore b/.gitignore index 363cf4a..db67ee0 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ *.bmp build/Output tacticalagent-v* +tacticalagent \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..da5b13e --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,18 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Launch file", + "type": "go", + "request": "launch", + "mode": "debug", + "env": {}, + "args": ["-m", "svc", "-logto", "stdout"], + "buildFlags": "-tags=DEBUG", + "program": "${workspaceRoot}" + } + ] +} \ No newline at end of file diff --git a/agent/rpc.go b/agent/rpc.go index 7d24d0b..79f479f 100644 --- a/agent/rpc.go +++ b/agent/rpc.go @@ -49,6 +49,10 @@ var ( ) func (a *Agent) RunRPC() { + if rmm.DEBUG { + a.Logger.Infoln("DEBUG MODE") + } + a.Logger.Infoln("Agent service started") go a.RunAsService() var wg sync.WaitGroup diff --git a/shared/debug.go b/shared/debug.go new file mode 100644 index 0000000..0893720 --- /dev/null +++ b/shared/debug.go @@ -0,0 +1,5 @@ +//go:build DEBUG + +package shared + +const DEBUG = true \ No newline at end of file diff --git a/shared/nodebug.go b/shared/nodebug.go new file mode 100644 index 0000000..e2032ee --- /dev/null +++ b/shared/nodebug.go @@ -0,0 +1,5 @@ +//go:build !DEBUG + +package shared + +const DEBUG = false \ No newline at end of file From 6457ad290fb550bfe919f1f652439221c7758f63 Mon Sep 17 00:00:00 2001 From: redanthrax Date: Wed, 15 Jun 2022 15:03:13 -0700 Subject: [PATCH 03/11] refactor --- agent/install.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/agent/install.go b/agent/install.go index f941836..af3654e 100644 --- a/agent/install.go +++ b/agent/install.go @@ -146,11 +146,11 @@ func (a *Agent) Install(i *Installer) { arch = "32" } - var installerMeshSystemEXE string + var installerMeshSystemBin string if len(i.MeshDir) > 0 { - installerMeshSystemEXE = filepath.Join(i.MeshDir, "MeshAgent.exe") + installerMeshSystemBin = filepath.Join(i.MeshDir, "MeshAgent.exe") } else { - installerMeshSystemEXE = a.MeshSystemBin + installerMeshSystemBin = a.MeshSystemBin } var meshNodeID string @@ -178,7 +178,7 @@ func (a *Agent) Install(i *Installer) { a.Logger.Debugln("Mesh agent:", mesh) time.Sleep(1 * time.Second) - meshNodeID, err = a.installMesh(mesh, installerMeshSystemEXE, i.Proxy) + meshNodeID, err = a.installMesh(mesh, installerMeshSystemBin, i.Proxy) if err != nil { a.installerMsg(fmt.Sprintf("Failed to install mesh agent: %s", err.Error()), "error", i.Silent) } From 7457bf0b935419132a715328ee290fc2766810f0 Mon Sep 17 00:00:00 2001 From: redanthrax Date: Wed, 15 Jun 2022 16:37:35 -0700 Subject: [PATCH 04/11] testing for install - in progress --- .vscode/launch.json | 2 +- README.md | 5 +++++ agent/agent_linux.go | 4 ++++ agent/agent_test.go | 35 +++++++++++++++++++++++++++++++ agent/install_test.go | 49 +++++++++++++++++++++++++++++++++++++++++++ agent/rpc.go | 2 +- agent/testargs.json | 4 ++++ 7 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 agent/agent_test.go create mode 100644 agent/install_test.go create mode 100644 agent/testargs.json diff --git a/.vscode/launch.json b/.vscode/launch.json index da5b13e..432fcfd 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,7 +10,7 @@ "request": "launch", "mode": "debug", "env": {}, - "args": ["-m", "svc", "-logto", "stdout"], + "args": ["-m", "svc", "-log", "DEBUG", "-logto", "stdout"], "buildFlags": "-tags=DEBUG", "program": "${workspaceRoot}" } diff --git a/README.md b/README.md index d065784..e5f2642 100644 --- a/README.md +++ b/README.md @@ -6,4 +6,9 @@ https://github.com/amidaware/tacticalrmm env CGO_ENABLED=0 GOOS= GOARCH= go build -ldflags "-s -w" ``` +### tests +Navigate to agent directory +``` +go test -vet=off +``` \ No newline at end of file diff --git a/agent/agent_linux.go b/agent/agent_linux.go index 2b13410..762ce72 100644 --- a/agent/agent_linux.go +++ b/agent/agent_linux.go @@ -132,6 +132,10 @@ func NewAgentConfig() *rmm.AgentConfig { viper.SetConfigType("json") viper.AddConfigPath("/etc/") viper.AddConfigPath(".") + if strings.HasSuffix(os.Args[0], ".test") { + viper.AddConfigPath("../") + } + err := viper.ReadInConfig() if err != nil { diff --git a/agent/agent_test.go b/agent/agent_test.go new file mode 100644 index 0000000..fad5fa0 --- /dev/null +++ b/agent/agent_test.go @@ -0,0 +1,35 @@ +package agent + +import ( + "bytes" + "io" + "os" + "testing" +) + +func captureOutput(f func()) string { + old := os.Stdout + r, w, _ := os.Pipe() + os.Stdout = w + f() + w.Close() + os.Stdout = old + + var buf bytes.Buffer + io.Copy(&buf, r) + return buf.String() +} + +func TestShowStatus(t *testing.T) { + var ( + version = "2.0.4" + ) + + output := captureOutput(func() { + ShowStatus(version) + }) + + if output != (version + "\n") { + t.Errorf("ShowStatus output not equal to version defined.") + } +} \ No newline at end of file diff --git a/agent/install_test.go b/agent/install_test.go new file mode 100644 index 0000000..2248440 --- /dev/null +++ b/agent/install_test.go @@ -0,0 +1,49 @@ +package agent + +import ( + "testing" + "github.com/sirupsen/logrus" + "github.com/spf13/viper" + "strconv" +) + +func TestInstall(t *testing.T) { + var ( + version = "2.0.4" + log = logrus.New() + ) + + a := New(log, version) + + viper.SetConfigName("testargs.json") + viper.SetConfigType("json") + viper.AddConfigPath(".") + + cid, err := strconv.Atoi(viper.GetString("clientid")) + + if err != nil { + cid = 0 + } + + installer := Installer { + RMM: viper.GetString("api"), + ClientID: cid, + SiteID: *siteID, + Description: *desc, + AgentType: *atype, + Power: *power, + RDP: *rdp, + Ping: *ping, + Token: *token, + LocalMesh: *localMesh, + Cert: *cert, + Proxy: *proxy, + Timeout: *timeout, + Silent: *silent, + NoMesh: *noMesh, + MeshDir: *meshDir, + MeshNodeID: *meshNodeID, + } + + a.Install(&installer) +} diff --git a/agent/rpc.go b/agent/rpc.go index 79f479f..2990135 100644 --- a/agent/rpc.go +++ b/agent/rpc.go @@ -50,7 +50,7 @@ var ( func (a *Agent) RunRPC() { if rmm.DEBUG { - a.Logger.Infoln("DEBUG MODE") + a.Logger.Infoln("DEBUG BUILD STARTED") } a.Logger.Infoln("Agent service started") diff --git a/agent/testargs.json b/agent/testargs.json new file mode 100644 index 0000000..38f27da --- /dev/null +++ b/agent/testargs.json @@ -0,0 +1,4 @@ +{ + "api": "https://api.hothcorp.com:8000", + +} \ No newline at end of file From 7c46970b67a133a23fd3a81299478369657068ba Mon Sep 17 00:00:00 2001 From: redanthrax Date: Wed, 15 Jun 2022 18:26:10 -0700 Subject: [PATCH 05/11] testing setup --- agent/agent_linux.go | 3 --- agent/agent_linux_test.go | 45 +++++++++++++++++++++++++++++++++++++++ agent/agent_test.go | 37 ++++++++++---------------------- agent/install_linux.go | 14 +++++++----- agent/install_test.go | 40 +++++++++++++++------------------- agent/rpc_test.go | 10 +++++++++ agent/testargs.json | 19 +++++++++++++++-- 7 files changed, 109 insertions(+), 59 deletions(-) create mode 100644 agent/agent_linux_test.go create mode 100644 agent/rpc_test.go diff --git a/agent/agent_linux.go b/agent/agent_linux.go index 762ce72..da3b049 100644 --- a/agent/agent_linux.go +++ b/agent/agent_linux.go @@ -132,9 +132,6 @@ func NewAgentConfig() *rmm.AgentConfig { viper.SetConfigType("json") viper.AddConfigPath("/etc/") viper.AddConfigPath(".") - if strings.HasSuffix(os.Args[0], ".test") { - viper.AddConfigPath("../") - } err := viper.ReadInConfig() diff --git a/agent/agent_linux_test.go b/agent/agent_linux_test.go new file mode 100644 index 0000000..570b881 --- /dev/null +++ b/agent/agent_linux_test.go @@ -0,0 +1,45 @@ +package agent + +import ( + "bytes" + "io" + "os" + "testing" +) + +func captureOutput(f func()) string { + old := os.Stdout + r, w, _ := os.Pipe() + os.Stdout = w + f() + w.Close() + os.Stdout = old + + var buf bytes.Buffer + io.Copy(&buf, r) + return buf.String() +} + +func TestShowStatus(t *testing.T) { + var ( + version = "2.0.4" + ) + + output := captureOutput(func() { + ShowStatus(version) + }) + + if output != (version + "\n") { + t.Errorf("ShowStatus output not equal to version defined.") + } +} + +func TestOsString(t *testing.T) { + a := New(lg, version) + osString := a.osString() + if osString == "" { + t.Errorf("Could not get OS String") + } else { + t.Logf("Got OS String: %s", osString) + } +} \ No newline at end of file diff --git a/agent/agent_test.go b/agent/agent_test.go index fad5fa0..d8b745e 100644 --- a/agent/agent_test.go +++ b/agent/agent_test.go @@ -1,35 +1,20 @@ package agent import ( - "bytes" - "io" - "os" "testing" + "github.com/sirupsen/logrus" ) -func captureOutput(f func()) string { - old := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - f() - w.Close() - os.Stdout = old +var ( + version = "2.0.4" + lg = logrus.New() +) - var buf bytes.Buffer - io.Copy(&buf, r) - return buf.String() -} - -func TestShowStatus(t *testing.T) { - var ( - version = "2.0.4" - ) - - output := captureOutput(func() { - ShowStatus(version) - }) - - if output != (version + "\n") { - t.Errorf("ShowStatus output not equal to version defined.") +func TestAgentId(t *testing.T) { + a := New(lg, version) + if a.AgentID == "" { + t.Error("AgentID not set") + } else { + t.Logf("AgentID: %s", a.AgentID) } } \ No newline at end of file diff --git a/agent/install_linux.go b/agent/install_linux.go index b9debcc..3d2251d 100644 --- a/agent/install_linux.go +++ b/agent/install_linux.go @@ -13,14 +13,12 @@ package agent import ( "log" + "os" + "strings" "github.com/spf13/viper" ) -const ( - etcConfig = "/etc/tacticalagent" -) - func (a *Agent) checkExistingAndRemove(silent bool) {} func (a *Agent) installerMsg(msg, alert string, silent bool) { @@ -42,7 +40,13 @@ func createAgentConfig(baseurl, agentid, apiurl, token, agentpk, cert, proxy, me viper.Set("proxy", proxy) viper.Set("meshdir", meshdir) viper.SetConfigPermissions(0660) - err := viper.SafeWriteConfigAs(etcConfig) + configLocation := "/etc/tacticalagent" + if strings.HasSuffix(os.Args[0], ".test") { + configLocation = "tacticalagent" + } + + err := viper.SafeWriteConfigAs(configLocation) + if err != nil { log.Fatalln("createAgentConfig", err) } diff --git a/agent/install_test.go b/agent/install_test.go index 2248440..0f07b6d 100644 --- a/agent/install_test.go +++ b/agent/install_test.go @@ -4,7 +4,6 @@ import ( "testing" "github.com/sirupsen/logrus" "github.com/spf13/viper" - "strconv" ) func TestInstall(t *testing.T) { @@ -18,31 +17,26 @@ func TestInstall(t *testing.T) { viper.SetConfigName("testargs.json") viper.SetConfigType("json") viper.AddConfigPath(".") - - cid, err := strconv.Atoi(viper.GetString("clientid")) - - if err != nil { - cid = 0 - } + viper.ReadInConfig() installer := Installer { RMM: viper.GetString("api"), - ClientID: cid, - SiteID: *siteID, - Description: *desc, - AgentType: *atype, - Power: *power, - RDP: *rdp, - Ping: *ping, - Token: *token, - LocalMesh: *localMesh, - Cert: *cert, - Proxy: *proxy, - Timeout: *timeout, - Silent: *silent, - NoMesh: *noMesh, - MeshDir: *meshDir, - MeshNodeID: *meshNodeID, + ClientID: viper.GetInt("clientid"), + SiteID: viper.GetInt("siteid"), + Description: viper.GetString("description"), + AgentType: viper.GetString("agenttype"), + Power: viper.GetBool("power"), + RDP: viper.GetBool("rdp"), + Ping: viper.GetBool("ping"), + Token: viper.GetString("token"), + LocalMesh: viper.GetString("localmesh"), + Cert: viper.GetString("cert"), + Proxy: viper.GetString("proxy"), + Timeout: viper.GetDuration("timeout"), + Silent: viper.GetBool("silent"), + NoMesh: viper.GetBool("nomesh"), + MeshDir: viper.GetString("meshdir"), + MeshNodeID: viper.GetString("meshnodeid"), } a.Install(&installer) diff --git a/agent/rpc_test.go b/agent/rpc_test.go new file mode 100644 index 0000000..385d3b9 --- /dev/null +++ b/agent/rpc_test.go @@ -0,0 +1,10 @@ +package agent + +import ( + "testing" +) + +func TestRunRPC(t *testing.T) { + a := New(lg, version) + a.RunRPC() +} \ No newline at end of file diff --git a/agent/testargs.json b/agent/testargs.json index 38f27da..9482e42 100644 --- a/agent/testargs.json +++ b/agent/testargs.json @@ -1,4 +1,19 @@ { - "api": "https://api.hothcorp.com:8000", - + "api": "http://api.hothcorp.com:8000", + "clientid": 1, + "siteid": 1, + "description": "", + "agenttype": "workstation", + "power": false, + "rdp": false, + "ping": false, + "token": "ffcc2c22de2aaf3870623c6a2622c06faa56b7d74f80e6c3dd5d24da36a80aaa", + "localmesh": "", + "cert": "", + "proxy": "", + "timeout": 30, + "silent": true, + "nomesh": true, + "meshdir": "", + "meshnodeid": "" } \ No newline at end of file From 5c559318a7dcfaa1472828260bdd74e485123d66 Mon Sep 17 00:00:00 2001 From: redanthrax Date: Wed, 15 Jun 2022 18:32:15 -0700 Subject: [PATCH 06/11] remote testargs local --- agent/testargs.json | 19 ------------------- 1 file changed, 19 deletions(-) delete mode 100644 agent/testargs.json diff --git a/agent/testargs.json b/agent/testargs.json deleted file mode 100644 index 9482e42..0000000 --- a/agent/testargs.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "api": "http://api.hothcorp.com:8000", - "clientid": 1, - "siteid": 1, - "description": "", - "agenttype": "workstation", - "power": false, - "rdp": false, - "ping": false, - "token": "ffcc2c22de2aaf3870623c6a2622c06faa56b7d74f80e6c3dd5d24da36a80aaa", - "localmesh": "", - "cert": "", - "proxy": "", - "timeout": 30, - "silent": true, - "nomesh": true, - "meshdir": "", - "meshnodeid": "" -} \ No newline at end of file From d7936a0e96ab3ec0f1282ffe7b3928bc9104d4d3 Mon Sep 17 00:00:00 2001 From: redanthrax Date: Wed, 15 Jun 2022 18:32:46 -0700 Subject: [PATCH 07/11] add testargs to gitignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index db67ee0..ba76619 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,5 @@ *.bmp build/Output tacticalagent-v* -tacticalagent \ No newline at end of file +tacticalagent +agent/testargs.json \ No newline at end of file From 89669af3aed0699698475458e7563b0cffaaaed8 Mon Sep 17 00:00:00 2001 From: redanthrax Date: Wed, 15 Jun 2022 18:33:33 -0700 Subject: [PATCH 08/11] added testargs example --- agent/testargs.json.example | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 agent/testargs.json.example diff --git a/agent/testargs.json.example b/agent/testargs.json.example new file mode 100644 index 0000000..c4ba6c8 --- /dev/null +++ b/agent/testargs.json.example @@ -0,0 +1,19 @@ +{ + "api": "", + "clientid": 1, + "siteid": 1, + "description": "", + "agenttype": "workstation", + "power": false, + "rdp": false, + "ping": false, + "token": "", + "localmesh": "", + "cert": "", + "proxy": "", + "timeout": 30, + "silent": true, + "nomesh": true, + "meshdir": "", + "meshnodeid": "" +} \ No newline at end of file From dfe2881cd57bafc2f536426aaea5d0511e7c1aab Mon Sep 17 00:00:00 2001 From: redanthrax Date: Wed, 15 Jun 2022 20:02:58 -0700 Subject: [PATCH 09/11] added vscode settings for testing --- README.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e5f2642..aaaeaf6 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,14 @@ env CGO_ENABLED=0 GOOS= GOARCH= go build -ldflags "-s -w" ### tests Navigate to agent directory - ``` go test -vet=off +``` + +Add to settings.json +``` +"go.testFlags": [ + "-vet=off" +], +"go.testTags": "TEST" ``` \ No newline at end of file From 30123bc023e195bf2bbd4102b5414f045d27897b Mon Sep 17 00:00:00 2001 From: redanthrax Date: Wed, 15 Jun 2022 20:04:57 -0700 Subject: [PATCH 10/11] updates for testing and windows --- agent/agent_windows.go | 2 +- agent/install.go | 7 +++++++ agent/install_linux.go | 2 +- agent/install_windows.go | 7 +++++++ shared/notest.go | 5 +++++ shared/test.go | 5 +++++ 6 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 shared/notest.go create mode 100644 shared/test.go diff --git a/agent/agent_windows.go b/agent/agent_windows.go index 0912cd2..9df3eb5 100644 --- a/agent/agent_windows.go +++ b/agent/agent_windows.go @@ -797,7 +797,7 @@ func (a *Agent) RecoverMesh() { } func (a *Agent) getMeshNodeID() (string, error) { - out, err := CMD(a.MeshSystemEXE, []string{"-nodeid"}, 10, false) + out, err := CMD(a.MeshSystemBin, []string{"-nodeid"}, 10, false) if err != nil { a.Logger.Debugln(err) return "", err diff --git a/agent/install.go b/agent/install.go index af3654e..6beaefb 100644 --- a/agent/install.go +++ b/agent/install.go @@ -23,6 +23,7 @@ import ( "strings" "time" + "github.com/amidaware/rmmagent/shared" "github.com/go-resty/resty/v2" trmm "github.com/wh1te909/trmm-shared" ) @@ -251,10 +252,16 @@ func (a *Agent) Install(i *Installer) { time.Sleep(1 * time.Second) a.Logger.Infoln("Starting service...") out := a.ControlService(winSvcName, "start") + + if shared.TEST { + goto SKIPSTART; + } + if !out.Success { a.installerMsg(out.ErrorMsg, "error", i.Silent) } + SKIPSTART: a.Logger.Infoln("Skipping service start in test.") a.Logger.Infoln("Adding windows defender exclusions") a.addDefenderExlusions() diff --git a/agent/install_linux.go b/agent/install_linux.go index 3d2251d..915b111 100644 --- a/agent/install_linux.go +++ b/agent/install_linux.go @@ -41,7 +41,7 @@ func createAgentConfig(baseurl, agentid, apiurl, token, agentpk, cert, proxy, me viper.Set("meshdir", meshdir) viper.SetConfigPermissions(0660) configLocation := "/etc/tacticalagent" - if strings.HasSuffix(os.Args[0], ".test") { + if shared.TEST { configLocation = "tacticalagent" } diff --git a/agent/install_windows.go b/agent/install_windows.go index 3f8b630..f145757 100644 --- a/agent/install_windows.go +++ b/agent/install_windows.go @@ -17,12 +17,14 @@ import ( "os" "path/filepath" + "github.com/amidaware/rmmagent/shared" "github.com/gonutz/w32/v2" "golang.org/x/sys/windows/registry" ) func createAgentConfig(baseurl, agentid, apiurl, token, agentpk, cert, proxy, meshdir string) { k, _, err := registry.CreateKey(registry.LOCAL_MACHINE, `SOFTWARE\TacticalRMM`, registry.ALL_ACCESS) + if err != nil { log.Fatalln("Error creating registry key:", err) } @@ -78,6 +80,11 @@ func createAgentConfig(baseurl, agentid, apiurl, token, agentpk, cert, proxy, me func (a *Agent) checkExistingAndRemove(silent bool) { hasReg := false _, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\TacticalRMM`, registry.ALL_ACCESS) + if shared.TEST { + err = nil + _, err = registry.OpenKey(registry.CURRENT_USER, `SOFTWARE\TacticalRMM`, registry.ALL_ACCESS) + } + if err == nil { hasReg = true } diff --git a/shared/notest.go b/shared/notest.go new file mode 100644 index 0000000..c79656a --- /dev/null +++ b/shared/notest.go @@ -0,0 +1,5 @@ +//go:build !TEST + +package shared + +const TEST = false \ No newline at end of file diff --git a/shared/test.go b/shared/test.go new file mode 100644 index 0000000..c1dbff8 --- /dev/null +++ b/shared/test.go @@ -0,0 +1,5 @@ +//go:build TEST + +package shared + +const TEST = true \ No newline at end of file From 13b5474cd829ebd8a54b0ea8873115490c3e37d2 Mon Sep 17 00:00:00 2001 From: redanthrax Date: Wed, 15 Jun 2022 20:12:48 -0700 Subject: [PATCH 11/11] updates for testing in windows --- agent/agent_windows.go | 10 ++++++++++ agent/install_linux.go | 4 +--- agent/install_windows.go | 4 ++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/agent/agent_windows.go b/agent/agent_windows.go index 9df3eb5..2d6d1e1 100644 --- a/agent/agent_windows.go +++ b/agent/agent_windows.go @@ -48,6 +48,11 @@ var ( func NewAgentConfig() *rmm.AgentConfig { k, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\TacticalRMM`, registry.ALL_ACCESS) + if shared.TEST { + err = nil + k, _, err := registry.OpenKey(registry.CURRENT_USER, `SOFTWARE\TacticalRMM`, registry.ALL_ACCESS) + } + if err != nil { return &rmm.AgentConfig{} } @@ -835,6 +840,11 @@ func (a *Agent) InstallService() error { // skip on first call of inno setup if this is a new install _, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\TacticalRMM`, registry.ALL_ACCESS) + if shared.TEST { + err = nil + k, _, err := registry.OpenKey(registry.CURRENT_USER, `SOFTWARE\TacticalRMM`, registry.ALL_ACCESS) + } + if err != nil { return nil } diff --git a/agent/install_linux.go b/agent/install_linux.go index 915b111..7391609 100644 --- a/agent/install_linux.go +++ b/agent/install_linux.go @@ -13,9 +13,7 @@ package agent import ( "log" - "os" - "strings" - + "github.com/amidaware/rmmagent/shared" "github.com/spf13/viper" ) diff --git a/agent/install_windows.go b/agent/install_windows.go index f145757..db6467b 100644 --- a/agent/install_windows.go +++ b/agent/install_windows.go @@ -24,6 +24,10 @@ import ( func createAgentConfig(baseurl, agentid, apiurl, token, agentpk, cert, proxy, meshdir string) { k, _, err := registry.CreateKey(registry.LOCAL_MACHINE, `SOFTWARE\TacticalRMM`, registry.ALL_ACCESS) + if shared.TEST { + err = nil + k, _, err := registry.OpenKey(registry.CURRENT_USER, `SOFTWARE\TacticalRMM`, registry.ALL_ACCESS) + } if err != nil { log.Fatalln("Error creating registry key:", err)