mirror of
https://github.com/gravitl/netmaker.git
synced 2025-09-04 04:04:17 +08:00
* feat: api access tokens * revoke all user tokens * redefine access token api routes, add auto egress option to enrollment keys * fix revoked tokens to be unauthorized * remove unused functions * convert access token to sql schema * switch access token to sql schema * revoke token generated by an user * add user token creation restriction by user role * add forbidden check for access token creation * revoke user token when group or role is changed * add default group to admin users on update * fix token removal on user update * fix token removal on user update
224 lines
4.9 KiB
Go
224 lines
4.9 KiB
Go
// package for logicing client and server code
|
|
package logic
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/base32"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log/slog"
|
|
"net"
|
|
"os"
|
|
"reflect"
|
|
"strings"
|
|
"time"
|
|
"unicode"
|
|
|
|
"github.com/blang/semver"
|
|
"github.com/c-robinson/iplib"
|
|
"github.com/gravitl/netmaker/database"
|
|
"github.com/gravitl/netmaker/logger"
|
|
)
|
|
|
|
// IsBase64 - checks if a string is in base64 format
|
|
// This is used to validate public keys (make sure they're base64 encoded like all public keys should be).
|
|
func IsBase64(s string) bool {
|
|
_, err := base64.StdEncoding.DecodeString(s)
|
|
return err == nil
|
|
}
|
|
|
|
// CheckEndpoint - checks if an endpoint is valid
|
|
func CheckEndpoint(endpoint string) bool {
|
|
endpointarr := strings.Split(endpoint, ":")
|
|
return len(endpointarr) == 2
|
|
}
|
|
|
|
// FileExists - checks if local file exists
|
|
func FileExists(f string) bool {
|
|
info, err := os.Stat(f)
|
|
if os.IsNotExist(err) {
|
|
return false
|
|
}
|
|
return !info.IsDir()
|
|
}
|
|
|
|
// IsAddressInCIDR - util to see if an address is in a cidr or not
|
|
func IsAddressInCIDR(address net.IP, cidr string) bool {
|
|
var _, currentCIDR, cidrErr = net.ParseCIDR(cidr)
|
|
if cidrErr != nil {
|
|
return false
|
|
}
|
|
return currentCIDR.Contains(address)
|
|
}
|
|
|
|
// SetNetworkNodesLastModified - sets the network nodes last modified
|
|
func SetNetworkNodesLastModified(networkName string) error {
|
|
|
|
timestamp := time.Now().Unix()
|
|
|
|
network, err := GetParentNetwork(networkName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
network.NodesLastModified = timestamp
|
|
data, err := json.Marshal(&network)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = database.Insert(networkName, string(data), database.NETWORKS_TABLE_NAME)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// RandomString - returns a random string in a charset
|
|
func RandomString(length int) string {
|
|
randombytes := make([]byte, length)
|
|
_, err := rand.Read(randombytes)
|
|
if err != nil {
|
|
logger.Log(0, "random string", err.Error())
|
|
return ""
|
|
}
|
|
return base32.StdEncoding.EncodeToString(randombytes)[:length]
|
|
}
|
|
|
|
// StringSliceContains - sees if a string slice contains a string element
|
|
func StringSliceContains(slice []string, item string) bool {
|
|
for _, s := range slice {
|
|
if s == item {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
func SetVerbosity(logLevel int) {
|
|
var level slog.Level
|
|
switch logLevel {
|
|
|
|
case 0:
|
|
level = slog.LevelInfo
|
|
case 1:
|
|
level = slog.LevelError
|
|
case 2:
|
|
level = slog.LevelWarn
|
|
case 3:
|
|
level = slog.LevelDebug
|
|
|
|
default:
|
|
level = slog.LevelInfo
|
|
}
|
|
// Create the logger with the chosen level
|
|
handler := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
|
|
Level: level,
|
|
})
|
|
logger := slog.New(handler)
|
|
slog.SetDefault(logger)
|
|
|
|
}
|
|
|
|
// NormalizeCIDR - returns the first address of CIDR
|
|
func NormalizeCIDR(address string) (string, error) {
|
|
ip, IPNet, err := net.ParseCIDR(address)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if ip.To4() == nil {
|
|
net6 := iplib.Net6FromStr(IPNet.String())
|
|
IPNet.IP = net6.FirstAddress()
|
|
} else {
|
|
net4 := iplib.Net4FromStr(IPNet.String())
|
|
IPNet.IP = net4.NetworkAddress()
|
|
}
|
|
return IPNet.String(), nil
|
|
}
|
|
|
|
// StringDifference - returns the elements in `a` that aren't in `b`.
|
|
func StringDifference(a, b []string) []string {
|
|
mb := make(map[string]struct{}, len(b))
|
|
for _, x := range b {
|
|
mb[x] = struct{}{}
|
|
}
|
|
var diff []string
|
|
for _, x := range a {
|
|
if _, found := mb[x]; !found {
|
|
diff = append(diff, x)
|
|
}
|
|
}
|
|
return diff
|
|
}
|
|
|
|
// CheckIfFileExists - checks if file exists or not in the given path
|
|
func CheckIfFileExists(filePath string) bool {
|
|
if _, err := os.Stat(filePath); os.IsNotExist(err) {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
// RemoveStringSlice - removes an element at given index i
|
|
// from a given string slice
|
|
func RemoveStringSlice(slice []string, i int) []string {
|
|
return append(slice[:i], slice[i+1:]...)
|
|
}
|
|
|
|
// IsSlicesEqual tells whether a and b contain the same elements.
|
|
// A nil argument is equivalent to an empty slice.
|
|
func IsSlicesEqual(a, b []string) bool {
|
|
if len(a) != len(b) {
|
|
return false
|
|
}
|
|
for i, v := range a {
|
|
if v != b[i] {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// VersionLessThan checks if v1 < v2 semantically
|
|
// dev is the latest version
|
|
func VersionLessThan(v1, v2 string) (bool, error) {
|
|
if v1 == "dev" {
|
|
return false, nil
|
|
}
|
|
if v2 == "dev" {
|
|
return true, nil
|
|
}
|
|
semVer1 := strings.TrimFunc(v1, func(r rune) bool {
|
|
return !unicode.IsNumber(r)
|
|
})
|
|
semVer2 := strings.TrimFunc(v2, func(r rune) bool {
|
|
return !unicode.IsNumber(r)
|
|
})
|
|
sv1, err := semver.Parse(semVer1)
|
|
if err != nil {
|
|
return false, fmt.Errorf("failed to parse semver1 (%s): %w", semVer1, err)
|
|
}
|
|
sv2, err := semver.Parse(semVer2)
|
|
if err != nil {
|
|
return false, fmt.Errorf("failed to parse semver2 (%s): %w", semVer2, err)
|
|
}
|
|
return sv1.LT(sv2), nil
|
|
}
|
|
|
|
// Compare any two maps with any key and value types
|
|
func CompareMaps[K comparable, V any](a, b map[K]V) bool {
|
|
if len(a) != len(b) {
|
|
return false
|
|
}
|
|
|
|
for key, valA := range a {
|
|
valB, ok := b[key]
|
|
if !ok {
|
|
return false
|
|
}
|
|
|
|
if !reflect.DeepEqual(valA, valB) {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|