netmaker/schema/posture_check.go
Abhishek Kondur 4177348f12
NM-166: add user groups to posture checks (#3766)
* NM-166: add device posture checks apis and logic

* NM-166: add severity level to posture check and api to fetch all available attributes

* NM-166: register posture check schema

* add periodic posture check evaluation of nodes

* NM-166: add os family and kernel version to host model for linux

* add posture check violations on host registration, api to fetch node violations

* NM-166: trigger posture checks on posture checks updates

* NM-166: add version clean func

* NM-166: add allowed values for posture check attributes to api

* NM-166: format violation messages

* NM-166: fix static check

* NM-166: fix static check

* NM-166: add OS info to update extclient api

* NM-166: add sysinfo funcs

* set if only new values are not empty

* format client location

* fix posture violation for static nodes

* skip non user nodes from posture checks

* NM-166: check posture checks by tags

* NM-166: set host location before posture check

* validate posture checks by OR condition

* run posture check violation on node update

* NM-166: allow join on unviolated networks

* NM-166: update response message when posture checks are violated

* NM-166: fix static check

* NM-166: add mutex for posture check runs

* NM-166: add OS family fields to api host model

* NM-166: run posture eval for gateway ops

* NM-166: add user groups to posture checks

* NM-166: add default all user grp

* NM-166: fix posture check eval for users

* NM-166: handle user nodes in the posture checks

* NM-166: fix posture check for new config

* NM-166: skip auto update check on users and show violation on disbaled static nodes

* NM-166: add min verison check for attr

* NM-166: fix static check

* NM-166: add default admin groups
2025-12-11 12:19:30 +04:00

124 lines
3.7 KiB
Go

package schema
import (
"context"
"time"
"github.com/gravitl/netmaker/db"
"github.com/gravitl/netmaker/models"
"gorm.io/datatypes"
)
type Attribute string
type Values string
const (
OS Attribute = "os"
OSVersion Attribute = "os_version"
OSFamily Attribute = "os_family"
KernelVersion Attribute = "kernel_version"
AutoUpdate Attribute = "auto_update"
ClientVersion Attribute = "client_version"
ClientLocation Attribute = "client_location"
)
var PostureCheckAttrs = []Attribute{
ClientLocation,
ClientVersion,
OS,
OSVersion,
OSFamily,
KernelVersion,
AutoUpdate,
}
var PostureCheckAttrValuesMap = map[Attribute]map[string]struct{}{
ClientLocation: {
"any_valid_iso_country_codes": {},
},
ClientVersion: {
"any_valid_semantic_version": {},
},
OS: {
"linux": {},
"darwin": {},
"windows": {},
"ios": {},
"android": {},
},
OSVersion: {
"any_valid_semantic_version": {},
},
OSFamily: {
"linux-debian": {},
"linux-redhat": {},
"linux-suse": {},
"linux-arch": {},
"linux-gentoo": {},
"linux-other": {},
"darwin": {},
"windows": {},
"ios": {},
"android": {},
},
KernelVersion: {
"any_valid_semantic_version": {},
},
AutoUpdate: {
"true": {},
"false": {},
},
}
var PostureCheckAttrValues = map[Attribute][]string{
ClientLocation: {"any_valid_iso_country_codes"},
ClientVersion: {"any_valid_semantic_version"},
OS: {"linux", "darwin", "windows", "ios", "android"},
OSVersion: {"any_valid_semantic_version"},
OSFamily: {"linux-debian", "linux-redhat", "linux-suse", "linux-arch", "linux-gentoo", "linux-other", "darwin", "windows", "ios", "android"},
KernelVersion: {"any_valid_semantic_version"},
AutoUpdate: {"true", "false"},
}
type PostureCheck struct {
ID string `gorm:"primaryKey" json:"id"`
Name string `gorm:"name" json:"name"`
NetworkID string `gorm:"network_id" json:"network_id"`
Description string `gorm:"description" json:"description"`
Attribute Attribute `gorm:"attribute" json:"attribute"`
Values datatypes.JSONSlice[string] `gorm:"values" json:"values"`
Severity models.Severity `gorm:"severity" json:"severity"`
Tags datatypes.JSONMap `gorm:"tags" json:"tags"`
UserGroups datatypes.JSONMap `gorm:"user_groups" json:"user_groups"`
Status bool `gorm:"status" json:"status"`
CreatedBy string `gorm:"created_by" json:"created_by"`
CreatedAt time.Time `gorm:"created_at" json:"created_at"`
UpdatedAt time.Time `gorm:"updated_at" json:"updated_at"`
}
func (p *PostureCheck) Get(ctx context.Context) error {
return db.FromContext(ctx).Model(&PostureCheck{}).First(&p).Where("id = ?", p.ID).Error
}
func (p *PostureCheck) Update(ctx context.Context) error {
return db.FromContext(ctx).Model(&PostureCheck{}).Where("id = ?", p.ID).Updates(&p).Error
}
func (p *PostureCheck) Create(ctx context.Context) error {
return db.FromContext(ctx).Model(&PostureCheck{}).Create(&p).Error
}
func (p *PostureCheck) ListByNetwork(ctx context.Context) (pcli []PostureCheck, err error) {
err = db.FromContext(ctx).Model(&PostureCheck{}).Where("network_id = ?", p.NetworkID).Find(&pcli).Error
return
}
func (p *PostureCheck) Delete(ctx context.Context) error {
return db.FromContext(ctx).Model(&PostureCheck{}).Where("id = ?", p.ID).Delete(&p).Error
}
func (p *PostureCheck) UpdateStatus(ctx context.Context) error {
return db.FromContext(ctx).Model(&PostureCheck{}).Where("id = ?", p.ID).Updates(map[string]any{
"status": p.Status,
}).Error
}