config.go 5.5 KB

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