Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ builds:
- amd64
- arm64
ldflags:
- -s -w -X main.version={{.Version}}
- -s -w
- -X main.buildVersion={{.Version}}
- -X main.buildCommit={{.Commit}}
- -X main.buildDate={{.Date}}
main: ./cmd/osctrld
binary: osctrld

Expand All @@ -30,7 +33,10 @@ builds:
- amd64
- arm64
ldflags:
- -s -w -X main.version={{.Version}}
- -s -w
- -X main.buildVersion={{.Version}}
- -X main.buildCommit={{.Commit}}
- -X main.buildDate={{.Date}}
main: ./cmd/osctrld
binary: osctrld

Expand All @@ -42,7 +48,10 @@ builds:
goarch:
- amd64
ldflags:
- -s -w -X main.version={{.Version}}
- -s -w
- -X main.buildVersion={{.Version}}
- -X main.buildCommit={{.Commit}}
- -X main.buildDate={{.Date}}
main: ./cmd/osctrld
binary: osctrld

Expand All @@ -54,7 +63,10 @@ builds:
goarch:
- arm64
ldflags:
- -s -w -X main.version={{.Version}}
- -s -w
- -X main.buildVersion={{.Version}}
- -X main.buildCommit={{.Commit}}
- -X main.buildDate={{.Date}}
main: ./cmd/osctrld
binary: osctrld

Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ OUTPUT = bin
DIST = dist

STATIC_ARGS = -ldflags "-linkmode external -extldflags -static"
BUILD_ARGS = -ldflags "-s -w -X main.buildCommit=$(shell git rev-parse HEAD) -X main.buildDate=$(shell date -u +%Y-%m-%dT%H:%M:%SZ)"

.PHONY: build static clean clean_go tidy install test test_cover release release-snapshot

# Build code according to caller OS and architecture
build:
go build -o $(OUTPUT)/$(NAME) $(CODE_DIR)/$(CODE)
go build $(BUILD_ARGS) -o $(OUTPUT)/$(NAME) $(CODE_DIR)/$(CODE)

# Build everything statically
static:
Expand Down
33 changes: 22 additions & 11 deletions cmd/osctrld/actions.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package main

import (
"context"
"fmt"
"runtime"
"strings"

"github.com/rs/zerolog/log"
"github.com/shirou/gopsutil/v3/process"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v3"
)

var (
Expand Down Expand Up @@ -69,9 +70,11 @@ type ExtensionsRequest struct {
}

// Function to action on enroll command
func enrollNode(c *cli.Context) error {
func enrollNode(ctx context.Context, cmd *cli.Command) error {
log.Debug().Str("url", osctrlURLs.Enroll).Msg("enrolling node")
script, err := retrieveScript(appConfig.OsctrlSecret, osctrlURLs.Enroll, appConfig.Insecure)
script, err := spin("retrieving enroll script from osctrl", func() (string, error) {
return retrieveScript(appConfig.OsctrlSecret, osctrlURLs.Enroll, appConfig.Insecure)
})
if err != nil {
return fmt.Errorf("error retrieving enroll - %v", err)
}
Expand All @@ -80,9 +83,11 @@ func enrollNode(c *cli.Context) error {
}

// Function to action on flags command
func getFlags(c *cli.Context) (bool, error) {
func getFlags(ctx context.Context, cmd *cli.Command) (bool, error) {
log.Debug().Str("url", osctrlURLs.Flags).Msg("getting flags")
flags, err := retrieveFlags(appConfig.OsctrlSecret, appConfig.OsquerySecretFile, appConfig.OsqueryCertFile)
flags, err := spin("retrieving flags from osctrl", func() (string, error) {
return retrieveFlags(appConfig.OsctrlSecret, appConfig.OsquerySecretFile, appConfig.OsqueryCertFile)
})
if err != nil {
return false, fmt.Errorf("error retrieving flags - %v", err)
}
Expand All @@ -96,9 +101,11 @@ func getFlags(c *cli.Context) (bool, error) {
}

// Function to action on cert command
func getCert(c *cli.Context) (bool, error) {
func getCert(ctx context.Context, cmd *cli.Command) (bool, error) {
log.Debug().Str("url", osctrlURLs.Cert).Msg("getting cert")
cert, err := retrieveCert(appConfig.OsctrlSecret, osctrlURLs.Cert, appConfig.Insecure)
cert, err := spin("retrieving certificate from osctrl", func() (string, error) {
return retrieveCert(appConfig.OsctrlSecret, osctrlURLs.Cert, appConfig.Insecure)
})
if err != nil {
return false, fmt.Errorf("error retrieving cert - %v", err)
}
Expand All @@ -112,9 +119,11 @@ func getCert(c *cli.Context) (bool, error) {
}

// Function to action on remove command. It retrieves the script to run the removal from osctrl
func removeNode(c *cli.Context) error {
func removeNode(ctx context.Context, cmd *cli.Command) error {
log.Debug().Str("url", osctrlURLs.Remove).Msg("removing node")
script, err := retrieveScript(appConfig.OsctrlSecret, osctrlURLs.Remove, appConfig.Insecure)
script, err := spin("retrieving remove script from osctrl", func() (string, error) {
return retrieveScript(appConfig.OsctrlSecret, osctrlURLs.Remove, appConfig.Insecure)
})
if err != nil {
return fmt.Errorf("error retrieving remove - %v", err)
}
Expand All @@ -124,7 +133,7 @@ func removeNode(c *cli.Context) error {
}

// Function to action on verify command. It verifies flags, cert and secret for and enrolled node in osctrl
func verifyNode(c *cli.Context) error {
func verifyNode(ctx context.Context, cmd *cli.Command) error {
// Compare secret with local
log.Debug().Str("path", appConfig.OsquerySecretFile).Msg("comparing secret")
if checkFileContent(appConfig.OsquerySecretFile, appConfig.OsctrlSecret) {
Expand All @@ -134,7 +143,9 @@ func verifyNode(c *cli.Context) error {
}
// Retrieve verification
log.Debug().Str("url", osctrlURLs.Verify).Msg("retrieving verification")
verification, err := retrieveVerify(appConfig.OsctrlSecret, appConfig.OsquerySecretFile, appConfig.OsqueryCertFile, osctrlURLs.Verify, appConfig.Insecure)
verification, err := spin("verifying node with osctrl", func() (VerifyResponse, error) {
return retrieveVerify(appConfig.OsctrlSecret, appConfig.OsquerySecretFile, appConfig.OsqueryCertFile, osctrlURLs.Verify, appConfig.Insecure)
})
if err != nil {
return fmt.Errorf("error retrieving verification - %v", err)
}
Expand Down
44 changes: 22 additions & 22 deletions cmd/osctrld/actions_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
Expand All @@ -10,7 +11,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v3"
)

func setupTestConfig(t *testing.T, server *httptest.Server) (cleanup func()) {
Expand Down Expand Up @@ -38,9 +39,8 @@ func setupTestConfig(t *testing.T, server *httptest.Server) (cleanup func()) {
}
}

func newTestCLIContext() *cli.Context {
app := cli.NewApp()
return cli.NewContext(app, nil, nil)
func newTestCLICommand() *cli.Command {
return &cli.Command{Name: appName}
}

func TestGetFlags_Success(t *testing.T) {
Expand All @@ -54,8 +54,8 @@ func TestGetFlags_Success(t *testing.T) {
defer cleanup()
osctrlURLs.Flags = server.URL + "/flags"

c := newTestCLIContext()
changed, err := getFlags(c)
c := newTestCLICommand()
changed, err := getFlags(context.Background(), c)
assert.NoError(t, err)
assert.True(t, changed, "new flags file should report changed")

Expand All @@ -75,8 +75,8 @@ func TestGetFlags_ServerError(t *testing.T) {
defer cleanup()
osctrlURLs.Flags = server.URL + "/flags"

c := newTestCLIContext()
_, err := getFlags(c)
c := newTestCLICommand()
_, err := getFlags(context.Background(), c)
assert.Error(t, err)
assert.Contains(t, err.Error(), "error retrieving flags")
}
Expand All @@ -93,8 +93,8 @@ func TestGetCert_Success(t *testing.T) {
defer cleanup()
osctrlURLs.Cert = server.URL + "/cert"

c := newTestCLIContext()
changed, err := getCert(c)
c := newTestCLICommand()
changed, err := getCert(context.Background(), c)
assert.NoError(t, err)
assert.True(t, changed, "new cert file should report changed")

Expand All @@ -114,8 +114,8 @@ func TestGetCert_ServerError(t *testing.T) {
defer cleanup()
osctrlURLs.Cert = server.URL + "/cert"

c := newTestCLIContext()
_, err := getCert(c)
c := newTestCLICommand()
_, err := getCert(context.Background(), c)
assert.Error(t, err)
assert.Contains(t, err.Error(), "error retrieving cert")
}
Expand All @@ -131,8 +131,8 @@ func TestEnrollNode_Success(t *testing.T) {
defer cleanup()
osctrlURLs.Enroll = server.URL + "/enroll"

c := newTestCLIContext()
err := enrollNode(c)
c := newTestCLICommand()
err := enrollNode(context.Background(), c)
assert.NoError(t, err)
}

Expand All @@ -147,8 +147,8 @@ func TestEnrollNode_ServerError(t *testing.T) {
defer cleanup()
osctrlURLs.Enroll = server.URL + "/enroll"

c := newTestCLIContext()
err := enrollNode(c)
c := newTestCLICommand()
err := enrollNode(context.Background(), c)
assert.Error(t, err)
assert.Contains(t, err.Error(), "error retrieving enroll")
}
Expand All @@ -164,8 +164,8 @@ func TestRemoveNode_Success(t *testing.T) {
defer cleanup()
osctrlURLs.Remove = server.URL + "/remove"

c := newTestCLIContext()
err := removeNode(c)
c := newTestCLICommand()
err := removeNode(context.Background(), c)
assert.NoError(t, err)
}

Expand All @@ -180,8 +180,8 @@ func TestRemoveNode_ServerError(t *testing.T) {
defer cleanup()
osctrlURLs.Remove = server.URL + "/remove"

c := newTestCLIContext()
err := removeNode(c)
c := newTestCLICommand()
err := removeNode(context.Background(), c)
assert.Error(t, err)
assert.Contains(t, err.Error(), "error retrieving remove")
}
Expand Down Expand Up @@ -221,7 +221,7 @@ func TestVerifyNode_Success(t *testing.T) {
}
osctrlURLs.Verify = server.URL + "/verify"

c := newTestCLIContext()
err := verifyNode(c)
c := newTestCLICommand()
err := verifyNode(context.Background(), c)
assert.NoError(t, err)
}
Loading
Loading