config.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package authority
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net"
  6. "os"
  7. "time"
  8. "github.com/smallstep/certificates/templates"
  9. "github.com/pkg/errors"
  10. "github.com/smallstep/certificates/authority/provisioner"
  11. "github.com/smallstep/certificates/db"
  12. "github.com/smallstep/cli/crypto/tlsutil"
  13. "github.com/smallstep/cli/crypto/x509util"
  14. )
  15. var (
  16. // DefaultTLSOptions represents the default TLS version as well as the cipher
  17. // suites used in the TLS certificates.
  18. DefaultTLSOptions = tlsutil.TLSOptions{
  19. CipherSuites: x509util.CipherSuites{
  20. "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305",
  21. "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
  22. "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",
  23. },
  24. MinVersion: 1.2,
  25. MaxVersion: 1.2,
  26. Renegotiation: false,
  27. }
  28. defaultDisableRenewal = false
  29. defaultEnableSSHCA = false
  30. globalProvisionerClaims = provisioner.Claims{
  31. MinTLSDur: &provisioner.Duration{Duration: 5 * time.Minute}, // TLS certs
  32. MaxTLSDur: &provisioner.Duration{Duration: 24 * time.Hour},
  33. DefaultTLSDur: &provisioner.Duration{Duration: 24 * time.Hour},
  34. DisableRenewal: &defaultDisableRenewal,
  35. MinUserSSHDur: &provisioner.Duration{Duration: 5 * time.Minute}, // User SSH certs
  36. MaxUserSSHDur: &provisioner.Duration{Duration: 24 * time.Hour},
  37. DefaultUserSSHDur: &provisioner.Duration{Duration: 16 * time.Hour},
  38. MinHostSSHDur: &provisioner.Duration{Duration: 5 * time.Minute}, // Host SSH certs
  39. MaxHostSSHDur: &provisioner.Duration{Duration: 30 * 24 * time.Hour},
  40. DefaultHostSSHDur: &provisioner.Duration{Duration: 30 * 24 * time.Hour},
  41. EnableSSHCA: &defaultEnableSSHCA,
  42. }
  43. )
  44. // Config represents the CA configuration and it's mapped to a JSON object.
  45. type Config struct {
  46. Root multiString `json:"root"`
  47. FederatedRoots []string `json:"federatedRoots"`
  48. IntermediateCert string `json:"crt"`
  49. IntermediateKey string `json:"key"`
  50. Address string `json:"address"`
  51. DNSNames []string `json:"dnsNames"`
  52. SSH *SSHConfig `json:"ssh,omitempty"`
  53. Logger json.RawMessage `json:"logger,omitempty"`
  54. DB *db.Config `json:"db,omitempty"`
  55. Monitoring json.RawMessage `json:"monitoring,omitempty"`
  56. AuthorityConfig *AuthConfig `json:"authority,omitempty"`
  57. TLS *tlsutil.TLSOptions `json:"tls,omitempty"`
  58. Password string `json:"password,omitempty"`
  59. Templates *templates.Templates `json:"templates,omitempty"`
  60. }
  61. // AuthConfig represents the configuration options for the authority.
  62. type AuthConfig struct {
  63. Provisioners provisioner.List `json:"provisioners"`
  64. Template *x509util.ASN1DN `json:"template,omitempty"`
  65. Claims *provisioner.Claims `json:"claims,omitempty"`
  66. DisableIssuedAtCheck bool `json:"disableIssuedAtCheck,omitempty"`
  67. }
  68. // Validate validates the authority configuration.
  69. func (c *AuthConfig) Validate(audiences provisioner.Audiences) error {
  70. if c == nil {
  71. return errors.New("authority cannot be undefined")
  72. }
  73. if len(c.Provisioners) == 0 {
  74. return errors.New("authority.provisioners cannot be empty")
  75. }
  76. // Merge global and configuration claims
  77. claimer, err := provisioner.NewClaimer(c.Claims, globalProvisionerClaims)
  78. if err != nil {
  79. return err
  80. }
  81. // Initialize provisioners
  82. config := provisioner.Config{
  83. Claims: claimer.Claims(),
  84. Audiences: audiences,
  85. }
  86. for _, p := range c.Provisioners {
  87. if err := p.Init(config); err != nil {
  88. return err
  89. }
  90. }
  91. if c.Template == nil {
  92. c.Template = &x509util.ASN1DN{}
  93. }
  94. return nil
  95. }
  96. // LoadConfiguration parses the given filename in JSON format and returns the
  97. // configuration struct.
  98. func LoadConfiguration(filename string) (*Config, error) {
  99. f, err := os.Open(filename)
  100. if err != nil {
  101. return nil, errors.Wrapf(err, "error opening %s", filename)
  102. }
  103. defer f.Close()
  104. var c Config
  105. if err := json.NewDecoder(f).Decode(&c); err != nil {
  106. return nil, errors.Wrapf(err, "error parsing %s", filename)
  107. }
  108. return &c, nil
  109. }
  110. // Save saves the configuration to the given filename.
  111. func (c *Config) Save(filename string) error {
  112. f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
  113. if err != nil {
  114. return errors.Wrapf(err, "error opening %s", filename)
  115. }
  116. defer f.Close()
  117. enc := json.NewEncoder(f)
  118. enc.SetIndent("", "\t")
  119. return errors.Wrapf(enc.Encode(c), "error writing %s", filename)
  120. }
  121. // Validate validates the configuration.
  122. func (c *Config) Validate() error {
  123. switch {
  124. case c.Address == "":
  125. return errors.New("address cannot be empty")
  126. case c.Root.HasEmpties():
  127. return errors.New("root cannot be empty")
  128. case c.IntermediateCert == "":
  129. return errors.New("crt cannot be empty")
  130. case c.IntermediateKey == "":
  131. return errors.New("key cannot be empty")
  132. case len(c.DNSNames) == 0:
  133. return errors.New("dnsNames cannot be empty")
  134. }
  135. // Validate address (a port is required)
  136. if _, _, err := net.SplitHostPort(c.Address); err != nil {
  137. return errors.Errorf("invalid address %s", c.Address)
  138. }
  139. if c.TLS == nil {
  140. c.TLS = &DefaultTLSOptions
  141. } else {
  142. if len(c.TLS.CipherSuites) == 0 {
  143. c.TLS.CipherSuites = DefaultTLSOptions.CipherSuites
  144. }
  145. if c.TLS.MaxVersion == 0 {
  146. c.TLS.MaxVersion = DefaultTLSOptions.MaxVersion
  147. }
  148. if c.TLS.MinVersion == 0 {
  149. c.TLS.MinVersion = c.TLS.MaxVersion
  150. }
  151. if c.TLS.MinVersion > c.TLS.MaxVersion {
  152. return errors.New("tls minVersion cannot exceed tls maxVersion")
  153. }
  154. c.TLS.Renegotiation = c.TLS.Renegotiation || DefaultTLSOptions.Renegotiation
  155. }
  156. // Validate ssh: nil is ok
  157. if err := c.SSH.Validate(); err != nil {
  158. return err
  159. }
  160. // Validate templates: nil is ok
  161. if err := c.Templates.Validate(); err != nil {
  162. return err
  163. }
  164. return c.AuthorityConfig.Validate(c.getAudiences())
  165. }
  166. // getAudiences returns the legacy and possible urls without the ports that will
  167. // be used as the default provisioner audiences. The CA might have proxies in
  168. // front so we cannot rely on the port.
  169. func (c *Config) getAudiences() provisioner.Audiences {
  170. audiences := provisioner.Audiences{
  171. Sign: []string{legacyAuthority},
  172. Revoke: []string{legacyAuthority},
  173. }
  174. for _, name := range c.DNSNames {
  175. audiences.Sign = append(audiences.Sign,
  176. fmt.Sprintf("https://%s/sign", name), fmt.Sprintf("https://%s/1.0/sign", name))
  177. audiences.Revoke = append(audiences.Revoke,
  178. fmt.Sprintf("https://%s/revoke", name), fmt.Sprintf("https://%s/1.0/revoke", name))
  179. }
  180. return audiences
  181. }