enrollment_key.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. Groups []TagID `json:"groups"`
  47. Default bool `json:"default"`
  48. AutoEgress bool `json:"auto_egress"`
  49. }
  50. // APIEnrollmentKey - used to create enrollment keys via API
  51. type APIEnrollmentKey struct {
  52. Expiration int64 `json:"expiration" swaggertype:"primitive,integer" format:"int64"`
  53. UsesRemaining int `json:"uses_remaining"`
  54. Networks []string `json:"networks"`
  55. Unlimited bool `json:"unlimited"`
  56. Tags []string `json:"tags" validate:"required,dive,min=3,max=32"`
  57. Type KeyType `json:"type"`
  58. Relay string `json:"relay"`
  59. Groups []TagID `json:"groups"`
  60. AutoEgress bool `json:"auto_egress"`
  61. }
  62. // RegisterResponse - the response to a successful enrollment register
  63. type RegisterResponse struct {
  64. ServerConf ServerConfig `json:"server_config"`
  65. RequestedHost Host `json:"requested_host"`
  66. }
  67. // EnrollmentKey.IsValid - checks if the key is still valid to use
  68. func (k *EnrollmentKey) IsValid() bool {
  69. if k == nil {
  70. return false
  71. }
  72. if k.UsesRemaining > 0 {
  73. return true
  74. }
  75. if !k.Expiration.IsZero() && time.Now().Before(k.Expiration) {
  76. return true
  77. }
  78. if k.Type == Undefined {
  79. return false
  80. }
  81. return k.Unlimited
  82. }
  83. // EnrollmentKey.Validate - validate's an EnrollmentKey
  84. // should be used during creation
  85. func (k *EnrollmentKey) Validate() error {
  86. if k == nil {
  87. return ErrNilEnrollmentKey
  88. }
  89. if k.Tags == nil {
  90. return ErrNilTagsEnrollmentKey
  91. }
  92. if len(k.Value) != EnrollmentKeyLength {
  93. return fmt.Errorf("%w: length not %d characters", ErrInvalidEnrollmentKeyValue, EnrollmentKeyLength)
  94. }
  95. if !k.IsValid() {
  96. return fmt.Errorf("%w: uses remaining: %d, expiration: %s, unlimited: %t", ErrInvalidEnrollmentKey, k.UsesRemaining, k.Expiration, k.Unlimited)
  97. }
  98. return nil
  99. }