claims.go 9.0 KB

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