Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Environment ID added to node #294

Merged
merged 1 commit into from
Aug 30, 2022
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
2 changes: 1 addition & 1 deletion environments/environments.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ type TLSEnvironment struct {
CarverInitPath string
CarverBlockPath string
AcceptEnrolls bool
UserID int
UserID uint
}

// MapEnvironments to hold the TLS environments by name and UUID
Expand Down
15 changes: 13 additions & 2 deletions nodes/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ type OsqueryNode struct {
LastConfig time.Time
LastQueryRead time.Time
LastQueryWrite time.Time
UserID int
UserID uint
EnvironmentID uint
}

// ArchiveOsqueryNode as abstraction of an archived node
Expand Down Expand Up @@ -66,7 +67,8 @@ type ArchiveOsqueryNode struct {
LastConfig time.Time
LastQueryRead time.Time
LastQueryWrite time.Time
UserID int
UserID uint
EnvironmentID uint
}

// StatsData to display node stats
Expand Down Expand Up @@ -136,6 +138,14 @@ func (n *NodeManager) CheckByUUIDEnv(uuid, environment string) bool {
return (results > 0)
}

// CheckByUUIDEnvID to check if node exists by UUID in a specific environment
// UUID is expected uppercase
func (n *NodeManager) CheckByUUIDEnvID(uuid string, envID int) bool {
var results int64
n.DB.Model(&OsqueryNode{}).Where("uuid = ? AND environment_id = ?", strings.ToUpper(uuid), envID).Count(&results)
return (results > 0)
}

// CheckByHost to check if node exists by Hostname
func (n *NodeManager) CheckByHost(host string) bool {
var results int64
Expand Down Expand Up @@ -468,6 +478,7 @@ func nodeArchiveFromNode(node OsqueryNode, trigger string) ArchiveOsqueryNode {
LastQueryRead: node.LastQueryRead,
LastQueryWrite: node.LastQueryWrite,
UserID: node.UserID,
EnvironmentID: node.EnvironmentID,
}
}

Expand Down
2 changes: 1 addition & 1 deletion tls/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func (h *HandlersTLS) EnrollHandler(w http.ResponseWriter, r *http.Request) {
if h.checkValidSecret(t.EnrollSecret, env) {
// Generate node_key using UUID as entropy
nodeKey = generateNodeKey(t.HostIdentifier, time.Now())
newNode = nodeFromEnroll(t, env.Name, utils.GetIP(r), nodeKey, len(body), env.UserID)
newNode = nodeFromEnroll(t, env, utils.GetIP(r), nodeKey, len(body))
// Check if UUID exists already, if so archive node and enroll new node
if h.Nodes.CheckByUUIDEnv(t.HostIdentifier, env.Name) {
if err := h.Nodes.Archive(t.HostIdentifier, "exists"); err != nil {
Expand Down
7 changes: 4 additions & 3 deletions tls/handlers/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (h *HandlersTLS) checkExpiredPath(maybeExpired time.Time) bool {
}

// Helper to convert an enrollment request into a osquery node
func nodeFromEnroll(req types.EnrollRequest, environment, ipaddress, nodekey string, recBytes, envUserID int) nodes.OsqueryNode {
func nodeFromEnroll(req types.EnrollRequest, env environments.TLSEnvironment, ipaddress, nodekey string, recBytes int) nodes.OsqueryNode {
// Prepare the enrollment request to be stored as raw JSON
enrollRaw, err := json.Marshal(req)
if err != nil {
Expand All @@ -88,7 +88,7 @@ func nodeFromEnroll(req types.EnrollRequest, environment, ipaddress, nodekey str
IPAddress: ipaddress,
Username: "unknown",
OsqueryUser: "unknown",
Environment: environment,
Environment: env.Name,
CPU: strings.TrimRight(req.HostDetails.EnrollSystemInfo.CPUBrand, "\x00"),
Memory: req.HostDetails.EnrollSystemInfo.PhysicalMemory,
HardwareSerial: req.HostDetails.EnrollSystemInfo.HardwareSerial,
Expand All @@ -100,7 +100,8 @@ func nodeFromEnroll(req types.EnrollRequest, environment, ipaddress, nodekey str
LastConfig: time.Time{},
LastQueryRead: time.Time{},
LastQueryWrite: time.Time{},
UserID: envUserID,
UserID: env.UserID,
EnvironmentID: env.ID,
}
}

Expand Down