Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion cmd/api/handlers/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,8 @@ func (h *HandlersApi) LookupNodeHandler(w http.ResponseWriter, r *http.Request)
} else {
apiErrorResponse(w, "error getting node", http.StatusInternalServerError, err)
}
return
}
// Serialize and serve JSON
utils.HTTPResponse(w, utils.JSONApplicationUTF8, http.StatusOK, _nodeToApiLookupResponse(n))
utils.HTTPResponse(w, utils.JSONApplicationUTF8, http.StatusOK, n)
}
18 changes: 0 additions & 18 deletions cmd/api/handlers/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"net/http"

"github.com/jmpsec/osctrl/pkg/logging"
"github.com/jmpsec/osctrl/pkg/nodes"
"github.com/jmpsec/osctrl/pkg/types"
"github.com/jmpsec/osctrl/pkg/utils"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -54,20 +53,3 @@ func checkValidPlatform(platforms []string, platform string) bool {
}
return false
}

// Helper to convert a node into a ApiLookupResponse
func _nodeToApiLookupResponse(node nodes.OsqueryNode) types.ApiLookupResponse {
return types.ApiLookupResponse{
UUID: node.UUID,
Platform: node.Platform,
PlatformVersion: node.PlatformVersion,
OsqueryVersion: node.OsqueryVersion,
Hostname: node.Hostname,
Localname: node.Localname,
IPAddress: node.IPAddress,
Username: node.Username,
Environment: node.Environment,
HardwareSerial: node.HardwareSerial,
LastSeen: node.LastSeen.String(),
}
}
22 changes: 22 additions & 0 deletions cmd/cli/api-node.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,25 @@ func (api *OsctrlAPI) DeleteNode(env, identifier string) error {
func (api *OsctrlAPI) TagNode(env, identifier, tag string) error {
return nil
}

// LookupNode to look up node from osctrl by identifier (UUID, localname or hostname)
func (api *OsctrlAPI) LookupNode(identifier string) (nodes.OsqueryNode, error) {
var node nodes.OsqueryNode
l := types.ApiLookupRequest{
Identifier: identifier,
}
jsonMessage, err := json.Marshal(l)
if err != nil {
return node, fmt.Errorf("error marshaling data %w", err)
}
jsonParam := strings.NewReader(string(jsonMessage))
Comment thread
javuto marked this conversation as resolved.
Outdated
reqURL := fmt.Sprintf("%s%s%s/lookup", api.Configuration.URL, APIPath, APINodes)
Comment thread
javuto marked this conversation as resolved.
Outdated
rawNode, err := api.PostGeneric(reqURL, jsonParam)
if err != nil {
return node, fmt.Errorf("error api request - %w - %s", err, string(rawNode))
}
if err := json.Unmarshal(rawNode, &node); err != nil {
return node, fmt.Errorf("can not parse body - %w", err)
}
return node, nil
}
17 changes: 15 additions & 2 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1205,6 +1205,19 @@ func init() {
},
Action: cliWrapper(showNode),
},
{
Name: "lookup",
Aliases: []string{"f"},
Usage: "Lookup existing nodes by identifier (UUID, hostname or localname)",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "identifier",
Aliases: []string{"id", "i"},
Usage: "Node identifier to be looked up (UUID, hostname or localname)",
},
},
Action: cliWrapper(lookupNode),
},
},
},
{
Expand Down Expand Up @@ -1688,12 +1701,12 @@ func checkDB(c *cli.Context) error {
// Initialize backend
db, err = backend.CreateDBManagerFile(dbConfigFile)
if err != nil {
return fmt.Errorf("Failed to create backend - %w", err)
return fmt.Errorf("failed to create backend - %w", err)
}
} else {
db, err = backend.CreateDBManager(dbConfig)
if err != nil {
return fmt.Errorf("Failed to create backend - %w", err)
return fmt.Errorf("failed to create backend - %w", err)
}
}
if err := db.Check(); err != nil {
Expand Down
74 changes: 50 additions & 24 deletions cmd/cli/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,30 +190,7 @@ func tagNode(c *cli.Context) error {
return nil
}

func showNode(c *cli.Context) error {
// Get values from flags
uuid := c.String("uuid")
if uuid == "" {
fmt.Println("❌ UUID is required")
os.Exit(1)
}
env := c.String("env")
if env == "" {
fmt.Println("❌ environment is required")
os.Exit(1)
}
var node nodes.OsqueryNode
if dbFlag {
node, err = nodesmgr.GetByUUID(uuid)
if err != nil {
return fmt.Errorf("error getting node - %w", err)
}
} else if apiFlag {
node, err = osctrlAPI.GetNode(env, uuid)
if err != nil {
return fmt.Errorf("error getting node - %w", err)
}
}
func _showNode(node nodes.OsqueryNode) error {
header := []string{
"Hostname",
"UUID",
Expand Down Expand Up @@ -247,3 +224,52 @@ func showNode(c *cli.Context) error {
}
return nil
}

func showNode(c *cli.Context) error {
// Get values from flags
uuid := c.String("uuid")
if uuid == "" {
fmt.Println("❌ UUID is required")
os.Exit(1)
}
env := c.String("env")
if env == "" {
fmt.Println("❌ environment is required")
os.Exit(1)
}
var node nodes.OsqueryNode
if dbFlag {
node, err = nodesmgr.GetByUUID(uuid)
if err != nil {
return fmt.Errorf("error getting node - %w", err)
}
} else if apiFlag {
node, err = osctrlAPI.GetNode(env, uuid)
if err != nil {
return fmt.Errorf("error getting node - %w", err)
}
}
return _showNode(node)
}

func lookupNode(c *cli.Context) error {
// Get values from flags
identifier := c.String("identifier")
if identifier == "" {
fmt.Println("❌ identifier is required")
os.Exit(1)
}
var node nodes.OsqueryNode
if dbFlag {
node, err = nodesmgr.GetByIdentifier(identifier)
if err != nil {
return fmt.Errorf("error getting node - %w", err)
}
} else if apiFlag {
node, err = osctrlAPI.LookupNode(identifier)
if err != nil {
return fmt.Errorf("error getting node - %w", err)
}
}
return _showNode(node)
}
2 changes: 1 addition & 1 deletion osctrl-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ paths:
content:
application/json:
schema:
$ref: "#/components/schemas/ApiLookupResponse"
$ref: "#/components/schemas/OsqueryNode"
400:
description: bad request
content:
Expand Down
17 changes: 0 additions & 17 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,20 +114,3 @@ type ApiTagsRequest struct {
type ApiLookupRequest struct {
Identifier string `json:"identifier"`
}

// ApiLookupResponse to be returned to API lookup requests
type ApiLookupResponse struct {
ID uint `json:"id"`
UUID string `json:"uuid"`
Hostname string `json:"hostname"`
Localname string `json:"localname"`
IPAddress string `json:"ip_address"`
Username string `json:"username"`
HardwareSerial string `json:"hardware_serial"`
Platform string `json:"platform"`
PlatformVersion string `json:"platform_version"`
OsqueryVersion string `json:"osquery_version"`
Environment string `json:"environment"`
EnvironmentUUID string `json:"environment_uuid"`
LastSeen string `json:"last_seen"`
}
Loading