2023-02-15 06:21:51 +08:00
|
|
|
package models
|
|
|
|
|
2023-02-16 04:27:26 +08:00
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
2023-02-15 06:21:51 +08:00
|
|
|
|
2023-05-05 23:03:59 +08:00
|
|
|
const (
|
|
|
|
Undefined KeyType = iota
|
|
|
|
TimeExpiration
|
|
|
|
Uses
|
|
|
|
Unlimited
|
|
|
|
)
|
|
|
|
|
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"`
|
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"`
|
2023-05-08 18:30:55 +08:00
|
|
|
Type KeyType `json:"type"`
|
2023-02-17 03:27:57 +08:00
|
|
|
}
|
|
|
|
|
2023-03-11 00:18:29 +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() bool {
|
|
|
|
return k.Networks != nil &&
|
|
|
|
k.Tags != nil &&
|
|
|
|
len(k.Value) == EnrollmentKeyLength &&
|
|
|
|
k.IsValid()
|
|
|
|
}
|