claims.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package provisioner
  2. import (
  3. "time"
  4. "github.com/pkg/errors"
  5. )
  6. // Claims so that individual provisioners can override global claims.
  7. type Claims struct {
  8. // TLS CA properties
  9. MinTLSDur *Duration `json:"minTLSCertDuration,omitempty"`
  10. MaxTLSDur *Duration `json:"maxTLSCertDuration,omitempty"`
  11. DefaultTLSDur *Duration `json:"defaultTLSCertDuration,omitempty"`
  12. DisableRenewal *bool `json:"disableRenewal,omitempty"`
  13. // SSH CA properties
  14. MinUserSSHDur *Duration `json:"minUserSSHCertDuration,omitempty"`
  15. MaxUserSSHDur *Duration `json:"maxUserSSHCertDuration,omitempty"`
  16. DefaultUserSSHDur *Duration `json:"defaultUserSSHCertDuration,omitempty"`
  17. MinHostSSHDur *Duration `json:"minHostSSHCertDuration,omitempty"`
  18. MaxHostSSHDur *Duration `json:"maxHostSSHCertDuration,omitempty"`
  19. DefaultHostSSHDur *Duration `json:"defaultHostSSHCertDuration,omitempty"`
  20. EnableSSHCA *bool `json:"enableSSHCA,omitempty"`
  21. }
  22. // Claimer is the type that controls claims. It provides an interface around the
  23. // current claim and the global one.
  24. type Claimer struct {
  25. global Claims
  26. claims *Claims
  27. }
  28. // NewClaimer initializes a new claimer with the given claims.
  29. func NewClaimer(claims *Claims, global Claims) (*Claimer, error) {
  30. c := &Claimer{global: global, claims: claims}
  31. return c, c.Validate()
  32. }
  33. // Claims returns the merge of the inner and global claims.
  34. func (c *Claimer) Claims() Claims {
  35. disableRenewal := c.IsDisableRenewal()
  36. enableSSHCA := c.IsSSHCAEnabled()
  37. return Claims{
  38. MinTLSDur: &Duration{c.MinTLSCertDuration()},
  39. MaxTLSDur: &Duration{c.MaxTLSCertDuration()},
  40. DefaultTLSDur: &Duration{c.DefaultTLSCertDuration()},
  41. DisableRenewal: &disableRenewal,
  42. MinUserSSHDur: &Duration{c.MinUserSSHCertDuration()},
  43. MaxUserSSHDur: &Duration{c.MaxUserSSHCertDuration()},
  44. DefaultUserSSHDur: &Duration{c.DefaultUserSSHCertDuration()},
  45. MinHostSSHDur: &Duration{c.MinHostSSHCertDuration()},
  46. MaxHostSSHDur: &Duration{c.MaxHostSSHCertDuration()},
  47. DefaultHostSSHDur: &Duration{c.DefaultHostSSHCertDuration()},
  48. EnableSSHCA: &enableSSHCA,
  49. }
  50. }
  51. // DefaultTLSCertDuration returns the default TLS cert duration for the
  52. // provisioner. If the default is not set within the provisioner, then the global
  53. // default from the authority configuration will be used.
  54. func (c *Claimer) DefaultTLSCertDuration() time.Duration {
  55. if c.claims == nil || c.claims.DefaultTLSDur == nil {
  56. return c.global.DefaultTLSDur.Duration
  57. }
  58. return c.claims.DefaultTLSDur.Duration
  59. }
  60. // MinTLSCertDuration returns the minimum TLS cert duration for the provisioner.
  61. // If the minimum is not set within the provisioner, then the global
  62. // minimum from the authority configuration will be used.
  63. func (c *Claimer) MinTLSCertDuration() time.Duration {
  64. if c.claims == nil || c.claims.MinTLSDur == nil {
  65. return c.global.MinTLSDur.Duration
  66. }
  67. return c.claims.MinTLSDur.Duration
  68. }
  69. // MaxTLSCertDuration returns the maximum TLS cert duration for the provisioner.
  70. // If the maximum is not set within the provisioner, then the global
  71. // maximum from the authority configuration will be used.
  72. func (c *Claimer) MaxTLSCertDuration() time.Duration {
  73. if c.claims == nil || c.claims.MaxTLSDur == nil {
  74. return c.global.MaxTLSDur.Duration
  75. }
  76. return c.claims.MaxTLSDur.Duration
  77. }
  78. // IsDisableRenewal returns if the renewal flow is disabled for the
  79. // provisioner. If the property is not set within the provisioner, then the
  80. // global value from the authority configuration will be used.
  81. func (c *Claimer) IsDisableRenewal() bool {
  82. if c.claims == nil || c.claims.DisableRenewal == nil {
  83. return *c.global.DisableRenewal
  84. }
  85. return *c.claims.DisableRenewal
  86. }
  87. // DefaultUserSSHCertDuration returns the default SSH user cert duration for the
  88. // provisioner. If the default is not set within the provisioner, then the
  89. // global default from the authority configuration will be used.
  90. func (c *Claimer) DefaultUserSSHCertDuration() time.Duration {
  91. if c.claims == nil || c.claims.DefaultUserSSHDur == nil {
  92. return c.global.DefaultUserSSHDur.Duration
  93. }
  94. return c.claims.DefaultUserSSHDur.Duration
  95. }
  96. // MinUserSSHCertDuration returns the minimum SSH user cert duration for the
  97. // provisioner. If the minimum is not set within the provisioner, then the
  98. // global minimum from the authority configuration will be used.
  99. func (c *Claimer) MinUserSSHCertDuration() time.Duration {
  100. if c.claims == nil || c.claims.MinUserSSHDur == nil {
  101. return c.global.MinUserSSHDur.Duration
  102. }
  103. return c.claims.MinUserSSHDur.Duration
  104. }
  105. // MaxUserSSHCertDuration returns the maximum SSH user cert duration for the
  106. // provisioner. If the maximum is not set within the provisioner, then the
  107. // global maximum from the authority configuration will be used.
  108. func (c *Claimer) MaxUserSSHCertDuration() time.Duration {
  109. if c.claims == nil || c.claims.MaxUserSSHDur == nil {
  110. return c.global.MaxUserSSHDur.Duration
  111. }
  112. return c.claims.MaxUserSSHDur.Duration
  113. }
  114. // DefaultHostSSHCertDuration returns the default SSH host cert duration for the
  115. // provisioner. If the default is not set within the provisioner, then the
  116. // global default from the authority configuration will be used.
  117. func (c *Claimer) DefaultHostSSHCertDuration() time.Duration {
  118. if c.claims == nil || c.claims.DefaultHostSSHDur == nil {
  119. return c.global.DefaultHostSSHDur.Duration
  120. }
  121. return c.claims.DefaultHostSSHDur.Duration
  122. }
  123. // MinHostSSHCertDuration returns the minimum SSH host cert duration for the
  124. // provisioner. If the minimum is not set within the provisioner, then the
  125. // global minimum from the authority configuration will be used.
  126. func (c *Claimer) MinHostSSHCertDuration() time.Duration {
  127. if c.claims == nil || c.claims.MinHostSSHDur == nil {
  128. return c.global.MinHostSSHDur.Duration
  129. }
  130. return c.claims.MinHostSSHDur.Duration
  131. }
  132. // MaxHostSSHCertDuration returns the maximum SSH Host cert duration for the
  133. // provisioner. If the maximum is not set within the provisioner, then the
  134. // global maximum from the authority configuration will be used.
  135. func (c *Claimer) MaxHostSSHCertDuration() time.Duration {
  136. if c.claims == nil || c.claims.MaxHostSSHDur == nil {
  137. return c.global.MaxHostSSHDur.Duration
  138. }
  139. return c.claims.MaxHostSSHDur.Duration
  140. }
  141. // IsSSHCAEnabled returns if the SSH CA is enabled for the provisioner. If the
  142. // property is not set within the provisioner, then the global value from the
  143. // authority configuration will be used.
  144. func (c *Claimer) IsSSHCAEnabled() bool {
  145. if c.claims == nil || c.claims.EnableSSHCA == nil {
  146. return *c.global.EnableSSHCA
  147. }
  148. return *c.claims.EnableSSHCA
  149. }
  150. // Validate validates and modifies the Claims with default values.
  151. func (c *Claimer) Validate() error {
  152. var (
  153. min = c.MinTLSCertDuration()
  154. max = c.MaxTLSCertDuration()
  155. def = c.DefaultTLSCertDuration()
  156. )
  157. switch {
  158. case min <= 0:
  159. return errors.Errorf("claims: MinTLSCertDuration must be greater than 0")
  160. case max <= 0:
  161. return errors.Errorf("claims: MaxTLSCertDuration must be greater than 0")
  162. case def <= 0:
  163. return errors.Errorf("claims: DefaultTLSCertDuration must be greater than 0")
  164. case max < min:
  165. return errors.Errorf("claims: MaxCertDuration cannot be less "+
  166. "than MinCertDuration: MaxCertDuration - %v, MinCertDuration - %v", max, min)
  167. case def < min:
  168. return errors.Errorf("claims: DefaultCertDuration cannot be less than MinCertDuration: DefaultCertDuration - %v, MinCertDuration - %v", def, min)
  169. case max < def:
  170. return errors.Errorf("claims: MaxCertDuration cannot be less than DefaultCertDuration: MaxCertDuration - %v, DefaultCertDuration - %v", max, def)
  171. default:
  172. return nil
  173. }
  174. }