config.go 5.1 KB

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