config.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. if c.Template == nil {
  77. c.Template = &x509util.ASN1DN{}
  78. }
  79. return nil
  80. }
  81. // LoadConfiguration parses the given filename in JSON format and returns the
  82. // configuration struct.
  83. func LoadConfiguration(filename string) (*Config, error) {
  84. f, err := os.Open(filename)
  85. if err != nil {
  86. return nil, errors.Wrapf(err, "error opening %s", filename)
  87. }
  88. defer f.Close()
  89. var c Config
  90. if err := json.NewDecoder(f).Decode(&c); err != nil {
  91. return nil, errors.Wrapf(err, "error parsing %s", filename)
  92. }
  93. return &c, nil
  94. }
  95. // Save saves the configuration to the given filename.
  96. func (c *Config) Save(filename string) error {
  97. f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
  98. if err != nil {
  99. return errors.Wrapf(err, "error opening %s", filename)
  100. }
  101. defer f.Close()
  102. enc := json.NewEncoder(f)
  103. enc.SetIndent("", "\t")
  104. return errors.Wrapf(enc.Encode(c), "error writing %s", filename)
  105. }
  106. // Validate validates the configuration.
  107. func (c *Config) Validate() error {
  108. switch {
  109. case c.Address == "":
  110. return errors.New("address cannot be empty")
  111. case c.Root.HasEmpties():
  112. return errors.New("root cannot be empty")
  113. case c.IntermediateCert == "":
  114. return errors.New("crt cannot be empty")
  115. case c.IntermediateKey == "":
  116. return errors.New("key cannot be empty")
  117. case len(c.DNSNames) == 0:
  118. return errors.New("dnsNames cannot be empty")
  119. }
  120. // Validate address (a port is required)
  121. if _, _, err := net.SplitHostPort(c.Address); err != nil {
  122. return errors.Errorf("invalid address %s", c.Address)
  123. }
  124. if c.TLS == nil {
  125. c.TLS = &DefaultTLSOptions
  126. } else {
  127. if len(c.TLS.CipherSuites) == 0 {
  128. c.TLS.CipherSuites = DefaultTLSOptions.CipherSuites
  129. }
  130. if c.TLS.MaxVersion == 0 {
  131. c.TLS.MaxVersion = DefaultTLSOptions.MaxVersion
  132. }
  133. if c.TLS.MinVersion == 0 {
  134. c.TLS.MinVersion = c.TLS.MaxVersion
  135. }
  136. if c.TLS.MinVersion > c.TLS.MaxVersion {
  137. return errors.New("tls minVersion cannot exceed tls maxVersion")
  138. }
  139. c.TLS.Renegotiation = c.TLS.Renegotiation || DefaultTLSOptions.Renegotiation
  140. }
  141. // Validate ssh: nil is ok
  142. if err := c.SSH.Validate(); err != nil {
  143. return err
  144. }
  145. // Validate templates: nil is ok
  146. if err := c.Templates.Validate(); err != nil {
  147. return err
  148. }
  149. return c.AuthorityConfig.Validate(c.getAudiences())
  150. }
  151. // getAudiences returns the legacy and possible urls without the ports that will
  152. // be used as the default provisioner audiences. The CA might have proxies in
  153. // front so we cannot rely on the port.
  154. func (c *Config) getAudiences() provisioner.Audiences {
  155. audiences := provisioner.Audiences{
  156. Sign: []string{legacyAuthority},
  157. Revoke: []string{legacyAuthority},
  158. SSHSign: []string{},
  159. SSHRevoke: []string{},
  160. SSHRenew: []string{},
  161. }
  162. for _, name := range c.DNSNames {
  163. audiences.Sign = append(audiences.Sign,
  164. fmt.Sprintf("https://%s/sign", name), fmt.Sprintf("https://%s/1.0/sign", name))
  165. audiences.Revoke = append(audiences.Revoke,
  166. fmt.Sprintf("https://%s/revoke", name), fmt.Sprintf("https://%s/1.0/revoke", name))
  167. audiences.SSHSign = append(audiences.SSHSign,
  168. fmt.Sprintf("https://%s/ssh/sign", name), fmt.Sprintf("https://%s/1.0/ssh/sign", name))
  169. audiences.SSHRevoke = append(audiences.SSHRevoke,
  170. fmt.Sprintf("https://%s/ssh/revoke", name), fmt.Sprintf("https://%s/1.0/ssh/revoke", name))
  171. audiences.SSHRenew = append(audiences.SSHRenew,
  172. fmt.Sprintf("https://%s/ssh/renew", name), fmt.Sprintf("https://%s/1.0/ssh/renew", name))
  173. audiences.SSHRekey = append(audiences.SSHRekey,
  174. fmt.Sprintf("https://%s/ssh/rekey", name), fmt.Sprintf("https://%s/1.0/ssh/rekey", name))
  175. }
  176. return audiences
  177. }