claims.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. return c.global.MinTLSDur.Duration
  67. }
  68. return c.claims.MinTLSDur.Duration
  69. }
  70. // MaxTLSCertDuration returns the maximum TLS cert duration for the provisioner.
  71. // If the maximum is not set within the provisioner, then the global
  72. // maximum from the authority configuration will be used.
  73. func (c *Claimer) MaxTLSCertDuration() time.Duration {
  74. if c.claims == nil || c.claims.MaxTLSDur == nil {
  75. return c.global.MaxTLSDur.Duration
  76. }
  77. return c.claims.MaxTLSDur.Duration
  78. }
  79. // IsDisableRenewal returns if the renewal flow is disabled for the
  80. // provisioner. If the property is not set within the provisioner, then the
  81. // global value from the authority configuration will be used.
  82. func (c *Claimer) IsDisableRenewal() bool {
  83. if c.claims == nil || c.claims.DisableRenewal == nil {
  84. return *c.global.DisableRenewal
  85. }
  86. return *c.claims.DisableRenewal
  87. }
  88. // DefaultSSHCertDuration returns the default SSH certificate duration for the
  89. // given certificate type.
  90. func (c *Claimer) DefaultSSHCertDuration(certType uint32) (time.Duration, error) {
  91. switch certType {
  92. case ssh.UserCert:
  93. return c.DefaultUserSSHCertDuration(), nil
  94. case ssh.HostCert:
  95. return c.DefaultHostSSHCertDuration(), nil
  96. case 0:
  97. return 0, errors.New("ssh certificate type has not been set")
  98. default:
  99. return 0, errors.Errorf("ssh certificate has an unknown type: %d", certType)
  100. }
  101. }
  102. // DefaultUserSSHCertDuration returns the default SSH user cert duration for the
  103. // provisioner. If the default is not set within the provisioner, then the
  104. // global default from the authority configuration will be used.
  105. func (c *Claimer) DefaultUserSSHCertDuration() time.Duration {
  106. if c.claims == nil || c.claims.DefaultUserSSHDur == nil {
  107. return c.global.DefaultUserSSHDur.Duration
  108. }
  109. return c.claims.DefaultUserSSHDur.Duration
  110. }
  111. // MinUserSSHCertDuration returns the minimum SSH user cert duration for the
  112. // provisioner. If the minimum is not set within the provisioner, then the
  113. // global minimum from the authority configuration will be used.
  114. func (c *Claimer) MinUserSSHCertDuration() time.Duration {
  115. if c.claims == nil || c.claims.MinUserSSHDur == nil {
  116. return c.global.MinUserSSHDur.Duration
  117. }
  118. return c.claims.MinUserSSHDur.Duration
  119. }
  120. // MaxUserSSHCertDuration returns the maximum SSH user cert duration for the
  121. // provisioner. If the maximum is not set within the provisioner, then the
  122. // global maximum from the authority configuration will be used.
  123. func (c *Claimer) MaxUserSSHCertDuration() time.Duration {
  124. if c.claims == nil || c.claims.MaxUserSSHDur == nil {
  125. return c.global.MaxUserSSHDur.Duration
  126. }
  127. return c.claims.MaxUserSSHDur.Duration
  128. }
  129. // DefaultHostSSHCertDuration returns the default SSH host cert duration for the
  130. // provisioner. If the default is not set within the provisioner, then the
  131. // global default from the authority configuration will be used.
  132. func (c *Claimer) DefaultHostSSHCertDuration() time.Duration {
  133. if c.claims == nil || c.claims.DefaultHostSSHDur == nil {
  134. return c.global.DefaultHostSSHDur.Duration
  135. }
  136. return c.claims.DefaultHostSSHDur.Duration
  137. }
  138. // MinHostSSHCertDuration returns the minimum SSH host cert duration for the
  139. // provisioner. If the minimum is not set within the provisioner, then the
  140. // global minimum from the authority configuration will be used.
  141. func (c *Claimer) MinHostSSHCertDuration() time.Duration {
  142. if c.claims == nil || c.claims.MinHostSSHDur == nil {
  143. return c.global.MinHostSSHDur.Duration
  144. }
  145. return c.claims.MinHostSSHDur.Duration
  146. }
  147. // MaxHostSSHCertDuration returns the maximum SSH Host cert duration for the
  148. // provisioner. If the maximum is not set within the provisioner, then the
  149. // global maximum from the authority configuration will be used.
  150. func (c *Claimer) MaxHostSSHCertDuration() time.Duration {
  151. if c.claims == nil || c.claims.MaxHostSSHDur == nil {
  152. return c.global.MaxHostSSHDur.Duration
  153. }
  154. return c.claims.MaxHostSSHDur.Duration
  155. }
  156. // IsSSHCAEnabled returns if the SSH CA is enabled for the provisioner. If the
  157. // property is not set within the provisioner, then the global value from the
  158. // authority configuration will be used.
  159. func (c *Claimer) IsSSHCAEnabled() bool {
  160. if c.claims == nil || c.claims.EnableSSHCA == nil {
  161. return *c.global.EnableSSHCA
  162. }
  163. return *c.claims.EnableSSHCA
  164. }
  165. // Validate validates and modifies the Claims with default values.
  166. func (c *Claimer) Validate() error {
  167. var (
  168. min = c.MinTLSCertDuration()
  169. max = c.MaxTLSCertDuration()
  170. def = c.DefaultTLSCertDuration()
  171. )
  172. switch {
  173. case min <= 0:
  174. return errors.Errorf("claims: MinTLSCertDuration must be greater than 0")
  175. case max <= 0:
  176. return errors.Errorf("claims: MaxTLSCertDuration must be greater than 0")
  177. case def <= 0:
  178. return errors.Errorf("claims: DefaultTLSCertDuration must be greater than 0")
  179. case max < min:
  180. return errors.Errorf("claims: MaxCertDuration cannot be less "+
  181. "than MinCertDuration: MaxCertDuration - %v, MinCertDuration - %v", max, min)
  182. case def < min:
  183. return errors.Errorf("claims: DefaultCertDuration cannot be less than MinCertDuration: DefaultCertDuration - %v, MinCertDuration - %v", def, min)
  184. case max < def:
  185. return errors.Errorf("claims: MaxCertDuration cannot be less than DefaultCertDuration: MaxCertDuration - %v, DefaultCertDuration - %v", max, def)
  186. default:
  187. return nil
  188. }
  189. }