enrollment_key.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package models
  2. import (
  3. "errors"
  4. "fmt"
  5. "time"
  6. "github.com/google/uuid"
  7. )
  8. const (
  9. Undefined KeyType = iota
  10. TimeExpiration
  11. Uses
  12. Unlimited
  13. )
  14. var (
  15. ErrNilEnrollmentKey = errors.New("enrollment key is nil")
  16. ErrNilNetworksEnrollmentKey = errors.New("enrollment key networks is nil")
  17. ErrNilTagsEnrollmentKey = errors.New("enrollment key tags is nil")
  18. ErrInvalidEnrollmentKey = errors.New("enrollment key is not valid")
  19. ErrInvalidEnrollmentKeyValue = errors.New("enrollment key value is not valid")
  20. )
  21. // KeyType - the type of enrollment key
  22. type KeyType int
  23. // String - returns the string representation of a KeyType
  24. func (k KeyType) String() string {
  25. return [...]string{"Undefined", "TimeExpiration", "Uses", "Unlimited"}[k]
  26. }
  27. // EnrollmentToken - the tokenized version of an enrollmentkey;
  28. // to be used for host registration
  29. type EnrollmentToken struct {
  30. Server string `json:"server"`
  31. Value string `json:"value"`
  32. }
  33. // EnrollmentKeyLength - the length of an enrollment key - 62^16 unique possibilities
  34. const EnrollmentKeyLength = 32
  35. // EnrollmentKey - the key used to register hosts and join them to specific networks
  36. type EnrollmentKey struct {
  37. Expiration time.Time `json:"expiration"`
  38. UsesRemaining int `json:"uses_remaining"`
  39. Value string `json:"value"`
  40. Networks []string `json:"networks"`
  41. Unlimited bool `json:"unlimited"`
  42. Tags []string `json:"tags"`
  43. Token string `json:"token,omitempty"` // B64 value of EnrollmentToken
  44. Type KeyType `json:"type"`
  45. Relay uuid.UUID `json:"relay"`
  46. }
  47. // APIEnrollmentKey - used to create enrollment keys via API
  48. type APIEnrollmentKey struct {
  49. Expiration int64 `json:"expiration"`
  50. UsesRemaining int `json:"uses_remaining"`
  51. Networks []string `json:"networks"`
  52. Unlimited bool `json:"unlimited"`
  53. Tags []string `json:"tags" validate:"required,dive,min=3,max=32"`
  54. Type KeyType `json:"type"`
  55. Relay string `json:"relay"`
  56. }
  57. // RegisterResponse - the response to a successful enrollment register
  58. type RegisterResponse struct {
  59. ServerConf ServerConfig `json:"server_config"`
  60. RequestedHost Host `json:"requested_host"`
  61. }
  62. // EnrollmentKey.IsValid - checks if the key is still valid to use
  63. func (k *EnrollmentKey) IsValid() bool {
  64. if k == nil {
  65. return false
  66. }
  67. if k.UsesRemaining > 0 {
  68. return true
  69. }
  70. if !k.Expiration.IsZero() && time.Now().Before(k.Expiration) {
  71. return true
  72. }
  73. if k.Type == Undefined {
  74. return false
  75. }
  76. return k.Unlimited
  77. }
  78. // EnrollmentKey.Validate - validate's an EnrollmentKey
  79. // should be used during creation
  80. func (k *EnrollmentKey) Validate() error {
  81. if k == nil {
  82. return ErrNilEnrollmentKey
  83. }
  84. if k.Tags == nil {
  85. return ErrNilTagsEnrollmentKey
  86. }
  87. if len(k.Value) != EnrollmentKeyLength {
  88. return fmt.Errorf("%w: length not %d characters", ErrInvalidEnrollmentKeyValue, EnrollmentKeyLength)
  89. }
  90. if !k.IsValid() {
  91. return fmt.Errorf("%w: uses remaining: %d, expiration: %s, unlimited: %t", ErrInvalidEnrollmentKey, k.UsesRemaining, k.Expiration, k.Unlimited)
  92. }
  93. return nil
  94. }