config.go 7.2 KB

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