enrollment_key.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package models
  2. import (
  3. "time"
  4. )
  5. // EnrollmentToken - the tokenized version of an enrollmentkey;
  6. // to be used for host registration
  7. type EnrollmentToken struct {
  8. Server string `json:"server"`
  9. Value string `json:"value"`
  10. }
  11. // EnrollmentKeyLength - the length of an enrollment key - 62^16 unique possibilities
  12. const EnrollmentKeyLength = 32
  13. // EnrollmentKey - the key used to register hosts and join them to specific networks
  14. type EnrollmentKey struct {
  15. Expiration time.Time `json:"expiration"`
  16. UsesRemaining int `json:"uses_remaining"`
  17. Value string `json:"value"`
  18. Networks []string `json:"networks"`
  19. Unlimited bool `json:"unlimited"`
  20. Tags []string `json:"tags"`
  21. Token string `json:"token,omitempty"` // B64 value of EnrollmentToken
  22. }
  23. // APIEnrollmentKey - used to create enrollment keys via API
  24. type APIEnrollmentKey struct {
  25. Expiration int64 `json:"expiration"`
  26. UsesRemaining int `json:"uses_remaining"`
  27. Networks []string `json:"networks"`
  28. Unlimited bool `json:"unlimited"`
  29. Tags []string `json:"tags"`
  30. }
  31. // RegisterResponse - the response to a successful enrollment register
  32. type RegisterResponse struct {
  33. ServerConf ServerConfig `json:"server_config"`
  34. RequestedHost Host `json:"requested_host"`
  35. }
  36. // EnrollmentKey.IsValid - checks if the key is still valid to use
  37. func (k *EnrollmentKey) IsValid() bool {
  38. if k == nil {
  39. return false
  40. }
  41. if k.UsesRemaining > 0 {
  42. return true
  43. }
  44. if !k.Expiration.IsZero() && time.Now().Before(k.Expiration) {
  45. return true
  46. }
  47. return k.Unlimited
  48. }
  49. // EnrollmentKey.Validate - validate's an EnrollmentKey
  50. // should be used during creation
  51. func (k *EnrollmentKey) Validate() bool {
  52. return k.Networks != nil &&
  53. k.Tags != nil &&
  54. len(k.Value) == EnrollmentKeyLength &&
  55. k.IsValid()
  56. }