config.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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/certificates/templates"
  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. defaultBackdate = time.Minute
  29. defaultDisableRenewal = false
  30. defaultEnableSSHCA = false
  31. globalProvisionerClaims = provisioner.Claims{
  32. MinTLSDur: &provisioner.Duration{Duration: 5 * time.Minute}, // TLS certs
  33. MaxTLSDur: &provisioner.Duration{Duration: 24 * time.Hour},
  34. DefaultTLSDur: &provisioner.Duration{Duration: 24 * time.Hour},
  35. DisableRenewal: &defaultDisableRenewal,
  36. MinUserSSHDur: &provisioner.Duration{Duration: 5 * time.Minute}, // User SSH certs
  37. MaxUserSSHDur: &provisioner.Duration{Duration: 24 * time.Hour},
  38. DefaultUserSSHDur: &provisioner.Duration{Duration: 16 * time.Hour},
  39. MinHostSSHDur: &provisioner.Duration{Duration: 5 * time.Minute}, // Host SSH certs
  40. MaxHostSSHDur: &provisioner.Duration{Duration: 30 * 24 * time.Hour},
  41. DefaultHostSSHDur: &provisioner.Duration{Duration: 30 * 24 * time.Hour},
  42. EnableSSHCA: &defaultEnableSSHCA,
  43. }
  44. )
  45. // Config represents the CA configuration and it's mapped to a JSON object.
  46. type Config struct {
  47. Root multiString `json:"root"`
  48. FederatedRoots []string `json:"federatedRoots"`
  49. IntermediateCert string `json:"crt"`
  50. IntermediateKey string `json:"key"`
  51. Address string `json:"address"`
  52. DNSNames []string `json:"dnsNames"`
  53. SSH *SSHConfig `json:"ssh,omitempty"`
  54. Logger json.RawMessage `json:"logger,omitempty"`
  55. DB *db.Config `json:"db,omitempty"`
  56. Monitoring json.RawMessage `json:"monitoring,omitempty"`
  57. AuthorityConfig *AuthConfig `json:"authority,omitempty"`
  58. TLS *tlsutil.TLSOptions `json:"tls,omitempty"`
  59. Password string `json:"password,omitempty"`
  60. Templates *templates.Templates `json:"templates,omitempty"`
  61. }
  62. // AuthConfig represents the configuration options for the authority.
  63. type AuthConfig struct {
  64. Provisioners provisioner.List `json:"provisioners"`
  65. Template *x509util.ASN1DN `json:"template,omitempty"`
  66. Claims *provisioner.Claims `json:"claims,omitempty"`
  67. DisableIssuedAtCheck bool `json:"disableIssuedAtCheck,omitempty"`
  68. Backdate *provisioner.Duration `json:"backdate,omitempty"`
  69. }
  70. // Validate validates the authority configuration.
  71. func (c *AuthConfig) Validate(audiences provisioner.Audiences) error {
  72. if c == nil {
  73. return errors.New("authority cannot be undefined")
  74. }
  75. // Check that only one K8sSA is enabled
  76. var k8sCount int
  77. for _, p := range c.Provisioners {
  78. if p.GetType() == provisioner.TypeK8sSA {
  79. k8sCount++
  80. }
  81. }
  82. if k8sCount > 1 {
  83. return errors.New("cannot have more than one kubernetes service account provisioner")
  84. }
  85. if c.Template == nil {
  86. c.Template = &x509util.ASN1DN{}
  87. }
  88. if c.Backdate != nil {
  89. if c.Backdate.Duration < 0 {
  90. return errors.New("authority.backdate cannot be less than 0")
  91. }
  92. } else {
  93. c.Backdate = &provisioner.Duration{
  94. Duration: defaultBackdate,
  95. }
  96. }
  97. return nil
  98. }
  99. // LoadConfiguration parses the given filename in JSON format and returns the
  100. // configuration struct.
  101. func LoadConfiguration(filename string) (*Config, error) {
  102. f, err := os.Open(filename)
  103. if err != nil {
  104. return nil, errors.Wrapf(err, "error opening %s", filename)
  105. }
  106. defer f.Close()
  107. var c Config
  108. if err := json.NewDecoder(f).Decode(&c); err != nil {
  109. return nil, errors.Wrapf(err, "error parsing %s", filename)
  110. }
  111. return &c, nil
  112. }
  113. // Save saves the configuration to the given filename.
  114. func (c *Config) Save(filename string) error {
  115. f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
  116. if err != nil {
  117. return errors.Wrapf(err, "error opening %s", filename)
  118. }
  119. defer f.Close()
  120. enc := json.NewEncoder(f)
  121. enc.SetIndent("", "\t")
  122. return errors.Wrapf(enc.Encode(c), "error writing %s", filename)
  123. }
  124. // Validate validates the configuration.
  125. func (c *Config) Validate() error {
  126. switch {
  127. case c.Address == "":
  128. return errors.New("address cannot be empty")
  129. case c.Root.HasEmpties():
  130. return errors.New("root cannot be empty")
  131. case c.IntermediateCert == "":
  132. return errors.New("crt cannot be empty")
  133. case c.IntermediateKey == "":
  134. return errors.New("key cannot be empty")
  135. case len(c.DNSNames) == 0:
  136. return errors.New("dnsNames cannot be empty")
  137. }
  138. // Validate address (a port is required)
  139. if _, _, err := net.SplitHostPort(c.Address); err != nil {
  140. return errors.Errorf("invalid address %s", c.Address)
  141. }
  142. if c.TLS == nil {
  143. c.TLS = &DefaultTLSOptions
  144. } else {
  145. if len(c.TLS.CipherSuites) == 0 {
  146. c.TLS.CipherSuites = DefaultTLSOptions.CipherSuites
  147. }
  148. if c.TLS.MaxVersion == 0 {
  149. c.TLS.MaxVersion = DefaultTLSOptions.MaxVersion
  150. }
  151. if c.TLS.MinVersion == 0 {
  152. c.TLS.MinVersion = c.TLS.MaxVersion
  153. }
  154. if c.TLS.MinVersion > c.TLS.MaxVersion {
  155. return errors.New("tls minVersion cannot exceed tls maxVersion")
  156. }
  157. c.TLS.Renegotiation = c.TLS.Renegotiation || DefaultTLSOptions.Renegotiation
  158. }
  159. // Validate ssh: nil is ok
  160. if err := c.SSH.Validate(); err != nil {
  161. return err
  162. }
  163. // Validate templates: nil is ok
  164. if err := c.Templates.Validate(); err != nil {
  165. return err
  166. }
  167. return c.AuthorityConfig.Validate(c.getAudiences())
  168. }
  169. // getAudiences returns the legacy and possible urls without the ports that will
  170. // be used as the default provisioner audiences. The CA might have proxies in
  171. // front so we cannot rely on the port.
  172. func (c *Config) getAudiences() provisioner.Audiences {
  173. audiences := provisioner.Audiences{
  174. Sign: []string{legacyAuthority},
  175. Revoke: []string{legacyAuthority},
  176. SSHSign: []string{},
  177. SSHRevoke: []string{},
  178. SSHRenew: []string{},
  179. }
  180. for _, name := range c.DNSNames {
  181. audiences.Sign = append(audiences.Sign,
  182. fmt.Sprintf("https://%s/1.0/sign", name),
  183. fmt.Sprintf("https://%s/sign", name),
  184. fmt.Sprintf("https://%s/1.0/ssh/sign", name),
  185. fmt.Sprintf("https://%s/ssh/sign", name))
  186. audiences.Revoke = append(audiences.Revoke,
  187. fmt.Sprintf("https://%s/1.0/revoke", name),
  188. fmt.Sprintf("https://%s/revoke", name))
  189. audiences.SSHSign = append(audiences.SSHSign,
  190. fmt.Sprintf("https://%s/1.0/ssh/sign", name),
  191. fmt.Sprintf("https://%s/ssh/sign", name),
  192. fmt.Sprintf("https://%s/1.0/sign", name),
  193. fmt.Sprintf("https://%s/sign", name))
  194. audiences.SSHRevoke = append(audiences.SSHRevoke,
  195. fmt.Sprintf("https://%s/1.0/ssh/revoke", name),
  196. fmt.Sprintf("https://%s/ssh/revoke", name))
  197. audiences.SSHRenew = append(audiences.SSHRenew,
  198. fmt.Sprintf("https://%s/1.0/ssh/renew", name),
  199. fmt.Sprintf("https://%s/ssh/renew", name))
  200. audiences.SSHRekey = append(audiences.SSHRekey,
  201. fmt.Sprintf("https://%s/1.0/ssh/rekey", name),
  202. fmt.Sprintf("https://%s/ssh/rekey", name))
  203. }
  204. return audiences
  205. }