netmaker/models/enrollment_key.go

109 lines
3.1 KiB
Go
Raw Normal View History

2023-02-15 06:21:51 +08:00
package models
2023-02-16 04:27:26 +08:00
import (
"errors"
"fmt"
2023-02-16 04:27:26 +08:00
"time"
"github.com/google/uuid"
2023-02-16 04:27:26 +08:00
)
2023-02-15 06:21:51 +08:00
2023-05-05 23:03:59 +08:00
const (
Undefined KeyType = iota
TimeExpiration
Uses
Unlimited
)
var (
ErrNilEnrollmentKey = errors.New("enrollment key is nil")
ErrNilNetworksEnrollmentKey = errors.New("enrollment key networks is nil")
ErrNilTagsEnrollmentKey = errors.New("enrollment key tags is nil")
ErrInvalidEnrollmentKey = errors.New("enrollment key is not valid")
ErrInvalidEnrollmentKeyValue = errors.New("enrollment key value is not valid")
)
2023-05-09 01:44:32 +08:00
// KeyType - the type of enrollment key
2023-05-05 23:03:59 +08:00
type KeyType int
2023-05-09 01:44:32 +08:00
// String - returns the string representation of a KeyType
2023-05-05 23:03:59 +08:00
func (k KeyType) String() string {
return [...]string{"Undefined", "TimeExpiration", "Uses", "Unlimited"}[k]
}
2023-02-16 05:32:16 +08:00
// EnrollmentToken - the tokenized version of an enrollmentkey;
// to be used for host registration
type EnrollmentToken struct {
2023-02-16 23:56:13 +08:00
Server string `json:"server"`
2023-02-16 05:32:16 +08:00
Value string `json:"value"`
}
2023-02-17 03:27:57 +08:00
// EnrollmentKeyLength - the length of an enrollment key - 62^16 unique possibilities
2023-02-15 06:21:51 +08:00
const EnrollmentKeyLength = 32
2023-02-17 03:27:57 +08:00
// EnrollmentKey - the key used to register hosts and join them to specific networks
2023-02-15 06:21:51 +08:00
type EnrollmentKey struct {
Expiration time.Time `json:"expiration"`
UsesRemaining int `json:"uses_remaining"`
Value string `json:"value"`
Networks []string `json:"networks"`
Unlimited bool `json:"unlimited"`
Tags []string `json:"tags"`
2023-02-16 05:32:16 +08:00
Token string `json:"token,omitempty"` // B64 value of EnrollmentToken
2023-05-05 23:03:59 +08:00
Type KeyType `json:"type"`
Relay uuid.UUID `json:"relay"`
2023-02-15 06:21:51 +08:00
}
2023-02-17 03:27:57 +08:00
// APIEnrollmentKey - used to create enrollment keys via API
type APIEnrollmentKey struct {
Expiration int64 `json:"expiration"`
UsesRemaining int `json:"uses_remaining"`
Networks []string `json:"networks"`
Unlimited bool `json:"unlimited"`
Tags []string `json:"tags" validate:"required,dive,min=3,max=32"`
2023-05-08 18:30:55 +08:00
Type KeyType `json:"type"`
Relay string `json:"relay"`
2023-02-17 03:27:57 +08:00
}
// RegisterResponse - the response to a successful enrollment register
type RegisterResponse struct {
ServerConf ServerConfig `json:"server_config"`
RequestedHost Host `json:"requested_host"`
}
2023-02-15 06:21:51 +08:00
// EnrollmentKey.IsValid - checks if the key is still valid to use
func (k *EnrollmentKey) IsValid() bool {
if k == nil {
return false
}
if k.UsesRemaining > 0 {
return true
}
2023-02-16 04:27:26 +08:00
if !k.Expiration.IsZero() && time.Now().Before(k.Expiration) {
2023-02-15 06:21:51 +08:00
return true
}
2023-05-05 23:03:59 +08:00
if k.Type == Undefined {
return false
}
2023-02-15 06:21:51 +08:00
return k.Unlimited
}
// EnrollmentKey.Validate - validate's an EnrollmentKey
// should be used during creation
func (k *EnrollmentKey) Validate() error {
if k == nil {
return ErrNilEnrollmentKey
}
if k.Tags == nil {
return ErrNilTagsEnrollmentKey
}
if len(k.Value) != EnrollmentKeyLength {
return fmt.Errorf("%w: length not %d characters", ErrInvalidEnrollmentKeyValue, EnrollmentKeyLength)
}
if !k.IsValid() {
return fmt.Errorf("%w: uses remaining: %d, expiration: %s, unlimited: %t", ErrInvalidEnrollmentKey, k.UsesRemaining, k.Expiration, k.Unlimited)
}
return nil
2023-02-15 06:21:51 +08:00
}