Files
Abhishek Kondur edda2868fc NM-163: Users, Groups, Roles, Networks and Hosts Table Migration (#3910)
* feat(go): add user schema;

* feat(go): migrate to user schema;

* feat(go): add audit fields;

* feat(go): remove unused fields from the network model;

* feat(go): add network schema;

* feat(go): migrate to network schema;

* refactor(go): add comment to clarify migration logic;

* fix(go): test failures;

* fix(go): test failures;

* feat(go): change membership table to store memberships at all scopes;

* feat(go): add schema for access grants;

* feat(go): remove nameservers from new networks table; ensure db passed for schema functions;

* feat(go): set max conns for sqlite to 1;

* fix(go): issues updating user account status;

* refactor(go): remove converters and access grants;

* refactor(go): add json tags in schema models;

* refactor(go): rename file to migrate_v1_6_0.go;

* refactor(go): add user groups and user roles tables; use schema tables;

* refactor(go): inline get and list from schema package;

* refactor(go): inline get network and list users from schema package;

* fix(go): staticcheck issues;

* fix(go): remove test not in use; fix test case;

* fix(go): validate network;

* fix(go): resolve static checks;

* fix(go): new models errors;

* fix(go): test errors;

* fix(go): handle no records;

* fix(go): add validations for user object;

* fix(go): set correct extclient status;

* fix(go): test error;

* feat(go): make schema the base package;

* feat(go): add host schema;

* feat(go): use schema host everywhere;

* feat(go): inline get host, list hosts and delete host;

* feat(go): use non-ptr value;

* feat(go): use save to upsert all fields;

* feat(go): use save to upsert all fields;

* feat(go): save turn endpoint as string;

* feat(go): check for gorm error record not found;

* fix(go): test failures;

* fix(go): update all network fields;

* fix(go): update all network fields;

* feat(go): add paginated list networks api;

* feat(go): add paginated list users api;

* feat(go): add paginated list hosts api;

* feat(go): add pagination to list groups api;

* fix(go): comment;

* fix(go): implement marshal and unmarshal text for custom types;

* fix(go): implement marshal and unmarshal json for custom types;

* fix(go): just use the old model for unmarshalling;

* fix(go): implement marshal and unmarshal json for custom types;

* feat(go): remove paginated list networks api;

* feat(go): use custom paginated response object;

* fix(go): ensure default values for page and per_page are used when not passed;

* fix(go): rename v1.6.0 to v1.5.1;

* fix(go): check for gorm.ErrRecordNotFound instead of database.IsEmptyRecord;

* fix(go): use host id, not pending host id;

* feat(go): add filters to paginated apis;

* feat(go): add filters to paginated apis;

* feat(go): remove check for max username length;

* feat(go): add filters to count as well;

* feat(go): use library to check email address validity;

* feat(go): ignore pagination if params not passed;

* fix(go): pagination issues;

* fix(go): check exists before using;

* fix(go): remove debug log;

* fix(go): use gorm err record not found;

* fix(go): use gorm err record not found;

* fix(go): use user principal name when creating pending user;

* fix(go): use schema package for consts;

* fix(go): prevent disabling superadmin user;

Co-authored-by: tenki-reviewer[bot] <262613592+tenki-reviewer[bot]@users.noreply.github.com>

* fix(go): swap is admin and is superadmin;

Co-authored-by: tenki-reviewer[bot] <262613592+tenki-reviewer[bot]@users.noreply.github.com>

* fix(go): remove dead code block;

https://github.com/gravitl/netmaker/pull/3910#discussion_r2928837937

* fix(go): incorrect message when trying to disable self;

https://github.com/gravitl/netmaker/pull/3910#discussion_r2928837934

* fix(go): use correct header;

Co-authored-by: tenki-reviewer[bot] <262613592+tenki-reviewer[bot]@users.noreply.github.com>

* fix(go): return after error response;

Co-authored-by: tenki-reviewer[bot] <262613592+tenki-reviewer[bot]@users.noreply.github.com>

* fix(go): use correct order of params;

https://github.com/gravitl/netmaker/pull/3910#discussion_r2929593036

* fix(go): set default values for page and page size; use v2 instead of /list;

* Update logic/auth.go

Co-authored-by: tenki-reviewer[bot] <262613592+tenki-reviewer[bot]@users.noreply.github.com>

* Update schema/user_roles.go

Co-authored-by: tenki-reviewer[bot] <262613592+tenki-reviewer[bot]@users.noreply.github.com>

* fix(go): syntax error;

* fix(go): set default values when page and per_page are not passed or 0;

* fix(go): use uuid.parse instead of uuid.must parse;

* fix(go): review errors;

* fix(go): review errors;

* Update controllers/user.go

Co-authored-by: tenki-reviewer[bot] <262613592+tenki-reviewer[bot]@users.noreply.github.com>

* Update controllers/user.go

Co-authored-by: tenki-reviewer[bot] <262613592+tenki-reviewer[bot]@users.noreply.github.com>

* NM-163: fix errors:

* Update db/types/options.go

Co-authored-by: tenki-reviewer[bot] <262613592+tenki-reviewer[bot]@users.noreply.github.com>

* fix(go): persist return user in event;

* Update db/types/options.go

Co-authored-by: tenki-reviewer[bot] <262613592+tenki-reviewer[bot]@users.noreply.github.com>

* NM-163: duplicate lines of code

* NM-163: fix(go): fix missing return and filter parsing in user controller

- Add missing return after error response in updateUserAccountStatus
  to prevent double-response and spurious ext-client side-effects
- Use switch statements in listUsers to skip unrecognized
  account_status and mfa_status filter values

* fix(go): check for both min and max page size;

* fix(go): enclose transfer superadmin in transaction;

* fix(go): review errors;

* fix(go): remove free tier checks;

* fix(go): review fixes;

---------

Co-authored-by: VishalDalwadi <dalwadivishal26@gmail.com>
Co-authored-by: Vishal Dalwadi <51291657+VishalDalwadi@users.noreply.github.com>
Co-authored-by: tenki-reviewer[bot] <262613592+tenki-reviewer[bot]@users.noreply.github.com>
2026-03-17 19:36:52 +05:30

193 lines
7.8 KiB
Go

package models
import (
"net"
"net/netip"
"time"
"github.com/google/uuid"
"github.com/gravitl/netmaker/schema"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)
// OS_Types - list of OS types Netmaker cares about
var OS_Types = struct {
Linux string
Windows string
Mac string
FreeBSD string
IoT string
}{
Linux: "linux",
Windows: "windows",
Mac: "darwin",
FreeBSD: "freebsd",
IoT: "iot",
}
// NAT_Types - the type of NAT in which a HOST currently resides (simplified)
var NAT_Types = struct {
Public string
BehindNAT string
}{
Public: "public",
BehindNAT: "behind_nat",
}
// WIREGUARD_INTERFACE name of wireguard interface
const (
WIREGUARD_INTERFACE = "netmaker"
DefaultPersistentKeepAlive = 20 * time.Second
)
// Host - represents a host on the network
type Host struct {
ID uuid.UUID `json:"id" yaml:"id"`
Verbosity int `json:"verbosity" yaml:"verbosity"`
FirewallInUse string `json:"firewallinuse" yaml:"firewallinuse"`
Version string `json:"version" yaml:"version"`
IPForwarding bool `json:"ipforwarding" yaml:"ipforwarding"`
DaemonInstalled bool `json:"daemoninstalled" yaml:"daemoninstalled"`
AutoUpdate bool `json:"autoupdate" yaml:"autoupdate"`
HostPass string `json:"hostpass" yaml:"hostpass"`
Name string `json:"name" yaml:"name"`
OS string `json:"os" yaml:"os"`
OSFamily string `json:"os_family" yaml:"os_family"`
OSVersion string `json:"os_version" yaml:"os_version"`
KernelVersion string `json:"kernel_version" yaml:"kernel_version"`
Interface string `json:"interface" yaml:"interface"`
Debug bool `json:"debug" yaml:"debug"`
ListenPort int `json:"listenport" yaml:"listenport"`
WgPublicListenPort int `json:"wg_public_listen_port" yaml:"wg_public_listen_port"`
MTU int `json:"mtu" yaml:"mtu"`
PublicKey wgtypes.Key `json:"publickey" yaml:"publickey"`
MacAddress net.HardwareAddr `json:"macaddress" yaml:"macaddress"`
TrafficKeyPublic []byte `json:"traffickeypublic" yaml:"traffickeypublic"`
Nodes []string `json:"nodes" yaml:"nodes"`
Interfaces []schema.Iface `json:"interfaces" yaml:"interfaces"`
DefaultInterface string `json:"defaultinterface" yaml:"defaultinterface"`
EndpointIP net.IP `json:"endpointip" yaml:"endpointip"`
EndpointIPv6 net.IP `json:"endpointipv6" yaml:"endpointipv6"`
IsDocker bool `json:"isdocker" yaml:"isdocker"`
IsK8S bool `json:"isk8s" yaml:"isk8s"`
IsStaticPort bool `json:"isstaticport" yaml:"isstaticport"`
IsStatic bool `json:"isstatic" yaml:"isstatic"`
IsDefault bool `json:"isdefault" yaml:"isdefault"`
DNS string `json:"dns_status" yaml:"dns_status"`
NatType string `json:"nat_type,omitempty" yaml:"nat_type,omitempty"`
TurnEndpoint *netip.AddrPort `json:"turn_endpoint,omitempty" yaml:"turn_endpoint,omitempty"`
PersistentKeepalive time.Duration `json:"persistentkeepalive" swaggertype:"primitive,integer" format:"int64" yaml:"persistentkeepalive"`
Location string `json:"location"` // Format: "lat,lon"
CountryCode string `json:"country_code"`
EnableFlowLogs bool `json:"enable_flow_logs" yaml:"enable_flow_logs"`
}
// FormatBool converts a boolean to a [yes|no] string
func FormatBool(b bool) string {
s := "no"
if b {
s = "yes"
}
return s
}
// ParseBool parses a [yes|no] string to boolean value
func ParseBool(s string) bool {
b := false
if s == "yes" {
b = true
}
return b
}
// HostMqAction - type for host update action
type HostMqAction string
const (
// Upgrade - const to request host to update it's client
Upgrade HostMqAction = "UPGRADE"
// ForceUpgrade - const for forcing a host to upgrade its client binary
ForceUpgrade HostMqAction = "FORCE_UPGRADE"
// SignalHost - const for host signal action
SignalHost HostMqAction = "SIGNAL_HOST"
// UpdateHost - constant for host update action
UpdateHost HostMqAction = "UPDATE_HOST"
// UpdateNode - constant for Node update action
UpdateNode HostMqAction = "UPDATE_NODE"
// DeleteHost - constant for host delete action
DeleteHost HostMqAction = "DELETE_HOST"
// JoinHostToNetwork - constant for host network join action
JoinHostToNetwork HostMqAction = "JOIN_HOST_TO_NETWORK"
// Acknowledgement - ACK response for hosts
Acknowledgement HostMqAction = "ACK"
// RequestAck - request an ACK
RequestAck HostMqAction = "REQ_ACK"
// CheckIn - update last check in times and public address and interfaces
CheckIn HostMqAction = "CHECK_IN"
// UpdateKeys - update wireguard private/public keys
UpdateKeys HostMqAction = "UPDATE_KEYS"
// RequestPull - request a pull from a host
RequestPull HostMqAction = "REQ_PULL"
// SignalPull - request a pull from a host without restart
SignalPull HostMqAction = "SIGNAL_PULL"
// UpdateMetrics - updates metrics data
UpdateMetrics HostMqAction = "UPDATE_METRICS"
// EgressUpdate - const for egress update action
EgressUpdate HostMqAction = "EGRESS_UPDATE"
// CHECK_ASSIGN_GW - const for to auto assign gw action
CheckAutoAssignGw HostMqAction = "CHECK_AUTO_ASSIGN_GW"
)
// SignalAction - turn peer signal action
type SignalAction string
const (
// ConnNegotiation - action to negotiate connection between peers
ConnNegotiation SignalAction = "CONNECTION_NEGOTIATION"
// RelayME - action to relay the peer
RelayME SignalAction = "RELAY_ME"
)
// HostUpdate - struct for host update
type HostUpdate struct {
Action HostMqAction
Host schema.Host
Node Node
Signal Signal
EgressDomain EgressDomain
NewMetrics Metrics
}
// HostTurnRegister - struct for host turn registration
type HostTurnRegister struct {
HostID string `json:"host_id"`
HostPassHash string `json:"host_pass_hash"`
}
// Signal - struct for signalling peer
type Signal struct {
Server string `json:"server"`
FromHostPubKey string `json:"from_host_pubkey"`
ToHostPubKey string `json:"to_host_pubkey"`
FromHostID string `json:"from_host_id"`
ToHostID string `json:"to_host_id"`
FromNodeID string `json:"from_node_id"`
ToNodeID string `json:"to_node_id"`
NetworkID string `json:"networkID"`
Reply bool `json:"reply"`
AutoRelayNodeMetrics map[string]int64 `json:"auto_relay_node_metrics"`
Action SignalAction `json:"action"`
IsPro bool `json:"is_pro"`
TimeStamp int64 `json:"timestamp"`
}
// RegisterMsg - login message struct for hosts to join via SSO login
type RegisterMsg struct {
RegisterHost schema.Host `json:"host"`
Network string `json:"network,omitempty"`
User string `json:"user,omitempty"`
Password string `json:"password,omitempty"`
JoinAll bool `json:"join_all,omitempty"`
Relay string `json:"relay,omitempty"`
}