config.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. cas "github.com/smallstep/certificates/cas/apiv1"
  11. "github.com/smallstep/certificates/db"
  12. kms "github.com/smallstep/certificates/kms/apiv1"
  13. "github.com/smallstep/certificates/templates"
  14. )
  15. var (
  16. // DefaultTLSOptions represents the default TLS version as well as the cipher
  17. // suites used in the TLS certificates.
  18. DefaultTLSOptions = TLSOptions{
  19. CipherSuites: 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. KMS *kms.Options `json:"kms,omitempty"`
  54. SSH *SSHConfig `json:"ssh,omitempty"`
  55. Logger json.RawMessage `json:"logger,omitempty"`
  56. DB *db.Config `json:"db,omitempty"`
  57. Monitoring json.RawMessage `json:"monitoring,omitempty"`
  58. AuthorityConfig *AuthConfig `json:"authority,omitempty"`
  59. TLS *TLSOptions `json:"tls,omitempty"`
  60. Password string `json:"password,omitempty"`
  61. Templates *templates.Templates `json:"templates,omitempty"`
  62. }
  63. // ASN1DN contains ASN1.DN attributes that are used in Subject and Issuer
  64. // x509 Certificate blocks.
  65. type ASN1DN struct {
  66. Country string `json:"country,omitempty" step:"country"`
  67. Organization string `json:"organization,omitempty" step:"organization"`
  68. OrganizationalUnit string `json:"organizationalUnit,omitempty" step:"organizationalUnit"`
  69. Locality string `json:"locality,omitempty" step:"locality"`
  70. Province string `json:"province,omitempty" step:"province"`
  71. StreetAddress string `json:"streetAddress,omitempty" step:"streetAddress"`
  72. CommonName string `json:"commonName,omitempty" step:"commonName"`
  73. }
  74. // AuthConfig represents the configuration options for the authority. An
  75. // underlaying registration authority can also be configured using the
  76. // cas.Options.
  77. type AuthConfig struct {
  78. *cas.Options
  79. Provisioners provisioner.List `json:"provisioners"`
  80. Template *ASN1DN `json:"template,omitempty"`
  81. Claims *provisioner.Claims `json:"claims,omitempty"`
  82. DisableIssuedAtCheck bool `json:"disableIssuedAtCheck,omitempty"`
  83. Backdate *provisioner.Duration `json:"backdate,omitempty"`
  84. }
  85. // init initializes the required fields in the AuthConfig if they are not
  86. // provided.
  87. func (c *AuthConfig) init() {
  88. if c.Provisioners == nil {
  89. c.Provisioners = provisioner.List{}
  90. }
  91. if c.Template == nil {
  92. c.Template = &ASN1DN{}
  93. }
  94. if c.Backdate == nil {
  95. c.Backdate = &provisioner.Duration{
  96. Duration: defaultBackdate,
  97. }
  98. }
  99. }
  100. // Validate validates the authority configuration.
  101. func (c *AuthConfig) Validate(audiences provisioner.Audiences) error {
  102. if c == nil {
  103. return errors.New("authority cannot be undefined")
  104. }
  105. // Initialize required fields.
  106. c.init()
  107. // Check that only one K8sSA is enabled
  108. var k8sCount int
  109. for _, p := range c.Provisioners {
  110. if p.GetType() == provisioner.TypeK8sSA {
  111. k8sCount++
  112. }
  113. }
  114. if k8sCount > 1 {
  115. return errors.New("cannot have more than one kubernetes service account provisioner")
  116. }
  117. if c.Backdate.Duration < 0 {
  118. return errors.New("authority.backdate cannot be less than 0")
  119. }
  120. return nil
  121. }
  122. // LoadConfiguration parses the given filename in JSON format and returns the
  123. // configuration struct.
  124. func LoadConfiguration(filename string) (*Config, error) {
  125. f, err := os.Open(filename)
  126. if err != nil {
  127. return nil, errors.Wrapf(err, "error opening %s", filename)
  128. }
  129. defer f.Close()
  130. var c Config
  131. if err := json.NewDecoder(f).Decode(&c); err != nil {
  132. return nil, errors.Wrapf(err, "error parsing %s", filename)
  133. }
  134. return &c, nil
  135. }
  136. // initializes the minimal configuration required to create an authority. This
  137. // is mainly used on embedded authorities.
  138. func (c *Config) init() {
  139. if c.DNSNames == nil {
  140. c.DNSNames = []string{"localhost", "127.0.0.1", "::1"}
  141. }
  142. if c.TLS == nil {
  143. c.TLS = &DefaultTLSOptions
  144. }
  145. if c.AuthorityConfig == nil {
  146. c.AuthorityConfig = &AuthConfig{}
  147. }
  148. c.AuthorityConfig.init()
  149. }
  150. // Save saves the configuration to the given filename.
  151. func (c *Config) Save(filename string) error {
  152. f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
  153. if err != nil {
  154. return errors.Wrapf(err, "error opening %s", filename)
  155. }
  156. defer f.Close()
  157. enc := json.NewEncoder(f)
  158. enc.SetIndent("", "\t")
  159. return errors.Wrapf(enc.Encode(c), "error writing %s", filename)
  160. }
  161. // Validate validates the configuration.
  162. func (c *Config) Validate() error {
  163. switch {
  164. case c.Address == "":
  165. return errors.New("address cannot be empty")
  166. case len(c.DNSNames) == 0:
  167. return errors.New("dnsNames cannot be empty")
  168. }
  169. // Options holds the RA/CAS configuration.
  170. ra := c.AuthorityConfig.Options
  171. // The default RA/CAS requires root, crt and key.
  172. if ra.Is(cas.SoftCAS) {
  173. switch {
  174. case c.Root.HasEmpties():
  175. return errors.New("root cannot be empty")
  176. case c.IntermediateCert == "":
  177. return errors.New("crt cannot be empty")
  178. case c.IntermediateKey == "":
  179. return errors.New("key cannot be empty")
  180. }
  181. }
  182. // Validate address (a port is required)
  183. if _, _, err := net.SplitHostPort(c.Address); err != nil {
  184. return errors.Errorf("invalid address %s", c.Address)
  185. }
  186. if c.TLS == nil {
  187. c.TLS = &DefaultTLSOptions
  188. } else {
  189. if len(c.TLS.CipherSuites) == 0 {
  190. c.TLS.CipherSuites = DefaultTLSOptions.CipherSuites
  191. }
  192. if c.TLS.MaxVersion == 0 {
  193. c.TLS.MaxVersion = DefaultTLSOptions.MaxVersion
  194. }
  195. if c.TLS.MinVersion == 0 {
  196. c.TLS.MinVersion = c.TLS.MaxVersion
  197. }
  198. if c.TLS.MinVersion > c.TLS.MaxVersion {
  199. return errors.New("tls minVersion cannot exceed tls maxVersion")
  200. }
  201. c.TLS.Renegotiation = c.TLS.Renegotiation || DefaultTLSOptions.Renegotiation
  202. }
  203. // Validate KMS options, nil is ok.
  204. if err := c.KMS.Validate(); err != nil {
  205. return err
  206. }
  207. // Validate RA/CAS options, nil is ok.
  208. if err := ra.Validate(); err != nil {
  209. return err
  210. }
  211. // Validate ssh: nil is ok
  212. if err := c.SSH.Validate(); err != nil {
  213. return err
  214. }
  215. // Validate templates: nil is ok
  216. if err := c.Templates.Validate(); err != nil {
  217. return err
  218. }
  219. return c.AuthorityConfig.Validate(c.getAudiences())
  220. }
  221. // getAudiences returns the legacy and possible urls without the ports that will
  222. // be used as the default provisioner audiences. The CA might have proxies in
  223. // front so we cannot rely on the port.
  224. func (c *Config) getAudiences() provisioner.Audiences {
  225. audiences := provisioner.Audiences{
  226. Sign: []string{legacyAuthority},
  227. Revoke: []string{legacyAuthority},
  228. SSHSign: []string{},
  229. SSHRevoke: []string{},
  230. SSHRenew: []string{},
  231. }
  232. for _, name := range c.DNSNames {
  233. audiences.Sign = append(audiences.Sign,
  234. fmt.Sprintf("https://%s/1.0/sign", name),
  235. fmt.Sprintf("https://%s/sign", name),
  236. fmt.Sprintf("https://%s/1.0/ssh/sign", name),
  237. fmt.Sprintf("https://%s/ssh/sign", name))
  238. audiences.Revoke = append(audiences.Revoke,
  239. fmt.Sprintf("https://%s/1.0/revoke", name),
  240. fmt.Sprintf("https://%s/revoke", name))
  241. audiences.SSHSign = append(audiences.SSHSign,
  242. fmt.Sprintf("https://%s/1.0/ssh/sign", name),
  243. fmt.Sprintf("https://%s/ssh/sign", name),
  244. fmt.Sprintf("https://%s/1.0/sign", name),
  245. fmt.Sprintf("https://%s/sign", name))
  246. audiences.SSHRevoke = append(audiences.SSHRevoke,
  247. fmt.Sprintf("https://%s/1.0/ssh/revoke", name),
  248. fmt.Sprintf("https://%s/ssh/revoke", name))
  249. audiences.SSHRenew = append(audiences.SSHRenew,
  250. fmt.Sprintf("https://%s/1.0/ssh/renew", name),
  251. fmt.Sprintf("https://%s/ssh/renew", name))
  252. audiences.SSHRekey = append(audiences.SSHRekey,
  253. fmt.Sprintf("https://%s/1.0/ssh/rekey", name),
  254. fmt.Sprintf("https://%s/ssh/rekey", name))
  255. }
  256. return audiences
  257. }