claims.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. // 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. // Renewal properties
  22. DisableRenewal *bool `json:"disableRenewal,omitempty"`
  23. AllowRenewalAfterExpiry *bool `json:"allowRenewalAfterExpiry,omitempty"`
  24. // Other properties
  25. DisableSmallstepExtensions *bool `json:"disableSmallstepExtensions,omitempty"`
  26. }
  27. // Claimer is the type that controls claims. It provides an interface around the
  28. // current claim and the global one.
  29. type Claimer struct {
  30. global Claims
  31. claims *Claims
  32. }
  33. // NewClaimer initializes a new claimer with the given claims.
  34. func NewClaimer(claims *Claims, global Claims) (*Claimer, error) {
  35. c := &Claimer{global: global, claims: claims}
  36. err := c.Validate()
  37. return c, err
  38. }
  39. // Claims returns the merge of the inner and global claims.
  40. func (c *Claimer) Claims() Claims {
  41. disableRenewal := c.IsDisableRenewal()
  42. allowRenewalAfterExpiry := c.AllowRenewalAfterExpiry()
  43. enableSSHCA := c.IsSSHCAEnabled()
  44. disableSmallstepExtensions := c.IsDisableSmallstepExtensions()
  45. return Claims{
  46. MinTLSDur: &Duration{c.MinTLSCertDuration()},
  47. MaxTLSDur: &Duration{c.MaxTLSCertDuration()},
  48. DefaultTLSDur: &Duration{c.DefaultTLSCertDuration()},
  49. MinUserSSHDur: &Duration{c.MinUserSSHCertDuration()},
  50. MaxUserSSHDur: &Duration{c.MaxUserSSHCertDuration()},
  51. DefaultUserSSHDur: &Duration{c.DefaultUserSSHCertDuration()},
  52. MinHostSSHDur: &Duration{c.MinHostSSHCertDuration()},
  53. MaxHostSSHDur: &Duration{c.MaxHostSSHCertDuration()},
  54. DefaultHostSSHDur: &Duration{c.DefaultHostSSHCertDuration()},
  55. EnableSSHCA: &enableSSHCA,
  56. DisableRenewal: &disableRenewal,
  57. AllowRenewalAfterExpiry: &allowRenewalAfterExpiry,
  58. DisableSmallstepExtensions: &disableSmallstepExtensions,
  59. }
  60. }
  61. // DefaultTLSCertDuration returns the default TLS cert duration for the
  62. // provisioner. If the default is not set within the provisioner, then the global
  63. // default from the authority configuration will be used.
  64. func (c *Claimer) DefaultTLSCertDuration() time.Duration {
  65. if c.claims == nil || c.claims.DefaultTLSDur == nil {
  66. return c.global.DefaultTLSDur.Duration
  67. }
  68. return c.claims.DefaultTLSDur.Duration
  69. }
  70. // MinTLSCertDuration returns the minimum TLS cert duration for the provisioner.
  71. // If the minimum is not set within the provisioner, then the global
  72. // minimum from the authority configuration will be used.
  73. func (c *Claimer) MinTLSCertDuration() time.Duration {
  74. if c.claims == nil || c.claims.MinTLSDur == nil {
  75. if c.claims != nil && c.claims.DefaultTLSDur != nil && c.claims.DefaultTLSDur.Duration < c.global.MinTLSDur.Duration {
  76. return c.claims.DefaultTLSDur.Duration
  77. }
  78. return c.global.MinTLSDur.Duration
  79. }
  80. return c.claims.MinTLSDur.Duration
  81. }
  82. // MaxTLSCertDuration returns the maximum TLS cert duration for the provisioner.
  83. // If the maximum is not set within the provisioner, then the global
  84. // maximum from the authority configuration will be used.
  85. func (c *Claimer) MaxTLSCertDuration() time.Duration {
  86. if c.claims == nil || c.claims.MaxTLSDur == nil {
  87. if c.claims != nil && c.claims.DefaultTLSDur != nil && c.claims.DefaultTLSDur.Duration > c.global.MaxTLSDur.Duration {
  88. return c.claims.DefaultTLSDur.Duration
  89. }
  90. return c.global.MaxTLSDur.Duration
  91. }
  92. return c.claims.MaxTLSDur.Duration
  93. }
  94. // IsDisableRenewal returns if the renewal flow is disabled for the
  95. // provisioner. If the property is not set within the provisioner, then the
  96. // global value from the authority configuration will be used.
  97. func (c *Claimer) IsDisableRenewal() bool {
  98. if c.claims == nil || c.claims.DisableRenewal == nil {
  99. return *c.global.DisableRenewal
  100. }
  101. return *c.claims.DisableRenewal
  102. }
  103. // IsDisableSmallstepExtensions returns whether Smallstep extensions, such as
  104. // the provisioner extension, should be excluded from the certificate.
  105. func (c *Claimer) IsDisableSmallstepExtensions() bool {
  106. if c.claims == nil || c.claims.DisableSmallstepExtensions == nil {
  107. return *c.global.DisableSmallstepExtensions
  108. }
  109. return *c.claims.DisableSmallstepExtensions
  110. }
  111. // AllowRenewalAfterExpiry returns if the renewal flow is authorized if the
  112. // certificate is expired. If the property is not set within the provisioner
  113. // then the global value from the authority configuration will be used.
  114. func (c *Claimer) AllowRenewalAfterExpiry() bool {
  115. if c.claims == nil || c.claims.AllowRenewalAfterExpiry == nil {
  116. return *c.global.AllowRenewalAfterExpiry
  117. }
  118. return *c.claims.AllowRenewalAfterExpiry
  119. }
  120. // DefaultSSHCertDuration returns the default SSH certificate duration for the
  121. // given certificate type.
  122. func (c *Claimer) DefaultSSHCertDuration(certType uint32) (time.Duration, error) {
  123. switch certType {
  124. case ssh.UserCert:
  125. return c.DefaultUserSSHCertDuration(), nil
  126. case ssh.HostCert:
  127. return c.DefaultHostSSHCertDuration(), nil
  128. case 0:
  129. return 0, errors.New("ssh certificate type has not been set")
  130. default:
  131. return 0, errors.Errorf("ssh certificate has an unknown type: %d", certType)
  132. }
  133. }
  134. // DefaultUserSSHCertDuration returns the default SSH user cert duration for the
  135. // provisioner. If the default is not set within the provisioner, then the
  136. // global default from the authority configuration will be used.
  137. func (c *Claimer) DefaultUserSSHCertDuration() time.Duration {
  138. if c.claims == nil || c.claims.DefaultUserSSHDur == nil {
  139. return c.global.DefaultUserSSHDur.Duration
  140. }
  141. return c.claims.DefaultUserSSHDur.Duration
  142. }
  143. // MinUserSSHCertDuration returns the minimum SSH user cert duration for the
  144. // provisioner. If the minimum is not set within the provisioner, then the
  145. // global minimum from the authority configuration will be used.
  146. func (c *Claimer) MinUserSSHCertDuration() time.Duration {
  147. if c.claims == nil || c.claims.MinUserSSHDur == nil {
  148. if c.claims != nil && c.claims.DefaultUserSSHDur != nil && c.claims.DefaultUserSSHDur.Duration < c.global.MinUserSSHDur.Duration {
  149. return c.claims.DefaultUserSSHDur.Duration
  150. }
  151. return c.global.MinUserSSHDur.Duration
  152. }
  153. return c.claims.MinUserSSHDur.Duration
  154. }
  155. // MaxUserSSHCertDuration returns the maximum SSH user cert duration for the
  156. // provisioner. If the maximum is not set within the provisioner, then the
  157. // global maximum from the authority configuration will be used.
  158. func (c *Claimer) MaxUserSSHCertDuration() time.Duration {
  159. if c.claims == nil || c.claims.MaxUserSSHDur == nil {
  160. if c.claims != nil && c.claims.DefaultUserSSHDur != nil && c.claims.DefaultUserSSHDur.Duration > c.global.MaxUserSSHDur.Duration {
  161. return c.claims.DefaultUserSSHDur.Duration
  162. }
  163. return c.global.MaxUserSSHDur.Duration
  164. }
  165. return c.claims.MaxUserSSHDur.Duration
  166. }
  167. // DefaultHostSSHCertDuration returns the default SSH host cert duration for the
  168. // provisioner. If the default is not set within the provisioner, then the
  169. // global default from the authority configuration will be used.
  170. func (c *Claimer) DefaultHostSSHCertDuration() time.Duration {
  171. if c.claims == nil || c.claims.DefaultHostSSHDur == nil {
  172. return c.global.DefaultHostSSHDur.Duration
  173. }
  174. return c.claims.DefaultHostSSHDur.Duration
  175. }
  176. // MinHostSSHCertDuration returns the minimum SSH host cert duration for the
  177. // provisioner. If the minimum is not set within the provisioner, then the
  178. // global minimum from the authority configuration will be used.
  179. func (c *Claimer) MinHostSSHCertDuration() time.Duration {
  180. if c.claims == nil || c.claims.MinHostSSHDur == nil {
  181. if c.claims != nil && c.claims.DefaultHostSSHDur != nil && c.claims.DefaultHostSSHDur.Duration < c.global.MinHostSSHDur.Duration {
  182. return c.claims.DefaultHostSSHDur.Duration
  183. }
  184. return c.global.MinHostSSHDur.Duration
  185. }
  186. return c.claims.MinHostSSHDur.Duration
  187. }
  188. // MaxHostSSHCertDuration returns the maximum SSH Host cert duration for the
  189. // provisioner. If the maximum is not set within the provisioner, then the
  190. // global maximum from the authority configuration will be used.
  191. func (c *Claimer) MaxHostSSHCertDuration() time.Duration {
  192. if c.claims == nil || c.claims.MaxHostSSHDur == nil {
  193. if c.claims != nil && c.claims.DefaultHostSSHDur != nil && c.claims.DefaultHostSSHDur.Duration > c.global.MaxHostSSHDur.Duration {
  194. return c.claims.DefaultHostSSHDur.Duration
  195. }
  196. return c.global.MaxHostSSHDur.Duration
  197. }
  198. return c.claims.MaxHostSSHDur.Duration
  199. }
  200. // IsSSHCAEnabled returns if the SSH CA is enabled for the provisioner. If the
  201. // property is not set within the provisioner, then the global value from the
  202. // authority configuration will be used.
  203. func (c *Claimer) IsSSHCAEnabled() bool {
  204. if c.claims == nil || c.claims.EnableSSHCA == nil {
  205. return *c.global.EnableSSHCA
  206. }
  207. return *c.claims.EnableSSHCA
  208. }
  209. // Validate validates and modifies the Claims with default values.
  210. func (c *Claimer) Validate() error {
  211. var (
  212. minDur = c.MinTLSCertDuration()
  213. maxDur = c.MaxTLSCertDuration()
  214. defDur = c.DefaultTLSCertDuration()
  215. )
  216. switch {
  217. case minDur <= 0:
  218. return errors.Errorf("claims: MinTLSCertDuration must be greater than 0")
  219. case maxDur <= 0:
  220. return errors.Errorf("claims: MaxTLSCertDuration must be greater than 0")
  221. case defDur <= 0:
  222. return errors.Errorf("claims: DefaultTLSCertDuration must be greater than 0")
  223. case maxDur < minDur:
  224. return errors.Errorf("claims: MaxCertDuration cannot be less "+
  225. "than MinCertDuration: MaxCertDuration - %v, MinCertDuration - %v", maxDur, minDur)
  226. case defDur < minDur:
  227. return errors.Errorf("claims: DefaultCertDuration cannot be less than MinCertDuration: DefaultCertDuration - %v, MinCertDuration - %v", defDur, minDur)
  228. case maxDur < defDur:
  229. return errors.Errorf("claims: MaxCertDuration cannot be less than DefaultCertDuration: MaxCertDuration - %v, DefaultCertDuration - %v", maxDur, defDur)
  230. default:
  231. return nil
  232. }
  233. }