config.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. package config
  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. "go.step.sm/linkedca"
  15. )
  16. const (
  17. legacyAuthority = "step-certificate-authority"
  18. )
  19. var (
  20. // DefaultBackdate length of time to backdate certificates to avoid
  21. // clock skew validation issues.
  22. DefaultBackdate = time.Minute
  23. // DefaultDisableRenewal disables renewals per provisioner.
  24. DefaultDisableRenewal = false
  25. // DefaultEnableSSHCA enable SSH CA features per provisioner or globally
  26. // for all provisioners.
  27. DefaultEnableSSHCA = false
  28. // GlobalProvisionerClaims default claims for the Authority. Can be overridden
  29. // by provisioner specific claims.
  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. InsecureAddress string `json:"insecureAddress"`
  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"`
  67. Organization string `json:"organization,omitempty"`
  68. OrganizationalUnit string `json:"organizationalUnit,omitempty"`
  69. Locality string `json:"locality,omitempty"`
  70. Province string `json:"province,omitempty"`
  71. StreetAddress string `json:"streetAddress,omitempty"`
  72. SerialNumber string `json:"serialNumber,omitempty"`
  73. CommonName string `json:"commonName,omitempty"`
  74. }
  75. // AuthConfig represents the configuration options for the authority. An
  76. // underlaying registration authority can also be configured using the
  77. // cas.Options.
  78. type AuthConfig struct {
  79. *cas.Options
  80. AuthorityID string `json:"authorityId,omitempty"`
  81. DeploymentType string `json:"deploymentType,omitempty"`
  82. Provisioners provisioner.List `json:"provisioners,omitempty"`
  83. Admins []*linkedca.Admin `json:"-"`
  84. Template *ASN1DN `json:"template,omitempty"`
  85. Claims *provisioner.Claims `json:"claims,omitempty"`
  86. DisableIssuedAtCheck bool `json:"disableIssuedAtCheck,omitempty"`
  87. Backdate *provisioner.Duration `json:"backdate,omitempty"`
  88. EnableAdmin bool `json:"enableAdmin,omitempty"`
  89. }
  90. // init initializes the required fields in the AuthConfig if they are not
  91. // provided.
  92. func (c *AuthConfig) init() {
  93. if c.Provisioners == nil {
  94. c.Provisioners = provisioner.List{}
  95. }
  96. if c.Template == nil {
  97. c.Template = &ASN1DN{}
  98. }
  99. if c.Backdate == nil {
  100. c.Backdate = &provisioner.Duration{
  101. Duration: DefaultBackdate,
  102. }
  103. }
  104. }
  105. // Validate validates the authority configuration.
  106. func (c *AuthConfig) Validate(audiences provisioner.Audiences) error {
  107. if c == nil {
  108. return errors.New("authority cannot be undefined")
  109. }
  110. // Initialize required fields.
  111. c.init()
  112. // Check that only one K8sSA is enabled
  113. var k8sCount int
  114. for _, p := range c.Provisioners {
  115. if p.GetType() == provisioner.TypeK8sSA {
  116. k8sCount++
  117. }
  118. }
  119. if k8sCount > 1 {
  120. return errors.New("cannot have more than one kubernetes service account provisioner")
  121. }
  122. if c.Backdate.Duration < 0 {
  123. return errors.New("authority.backdate cannot be less than 0")
  124. }
  125. return nil
  126. }
  127. // LoadConfiguration parses the given filename in JSON format and returns the
  128. // configuration struct.
  129. func LoadConfiguration(filename string) (*Config, error) {
  130. f, err := os.Open(filename)
  131. if err != nil {
  132. return nil, errors.Wrapf(err, "error opening %s", filename)
  133. }
  134. defer f.Close()
  135. var c Config
  136. if err := json.NewDecoder(f).Decode(&c); err != nil {
  137. return nil, errors.Wrapf(err, "error parsing %s", filename)
  138. }
  139. c.Init()
  140. return &c, nil
  141. }
  142. // Init initializes the minimal configuration required to create an authority. This
  143. // is mainly used on embedded authorities.
  144. func (c *Config) Init() {
  145. if c.DNSNames == nil {
  146. c.DNSNames = []string{"localhost", "127.0.0.1", "::1"}
  147. }
  148. if c.TLS == nil {
  149. c.TLS = &DefaultTLSOptions
  150. }
  151. if c.AuthorityConfig == nil {
  152. c.AuthorityConfig = &AuthConfig{}
  153. }
  154. c.AuthorityConfig.init()
  155. }
  156. // Save saves the configuration to the given filename.
  157. func (c *Config) Save(filename string) error {
  158. f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
  159. if err != nil {
  160. return errors.Wrapf(err, "error opening %s", filename)
  161. }
  162. defer f.Close()
  163. enc := json.NewEncoder(f)
  164. enc.SetIndent("", "\t")
  165. return errors.Wrapf(enc.Encode(c), "error writing %s", filename)
  166. }
  167. // Validate validates the configuration.
  168. func (c *Config) Validate() error {
  169. switch {
  170. case c.Address == "":
  171. return errors.New("address cannot be empty")
  172. case len(c.DNSNames) == 0:
  173. return errors.New("dnsNames cannot be empty")
  174. case c.AuthorityConfig == nil:
  175. return errors.New("authority cannot be nil")
  176. }
  177. // Options holds the RA/CAS configuration.
  178. ra := c.AuthorityConfig.Options
  179. // The default RA/CAS requires root, crt and key.
  180. if ra.Is(cas.SoftCAS) {
  181. switch {
  182. case c.Root.HasEmpties():
  183. return errors.New("root cannot be empty")
  184. case c.IntermediateCert == "":
  185. return errors.New("crt cannot be empty")
  186. case c.IntermediateKey == "":
  187. return errors.New("key cannot be empty")
  188. }
  189. }
  190. // Validate address (a port is required)
  191. if _, _, err := net.SplitHostPort(c.Address); err != nil {
  192. return errors.Errorf("invalid address %s", c.Address)
  193. }
  194. if c.TLS == nil {
  195. c.TLS = &DefaultTLSOptions
  196. } else {
  197. if len(c.TLS.CipherSuites) == 0 {
  198. c.TLS.CipherSuites = DefaultTLSOptions.CipherSuites
  199. }
  200. if c.TLS.MaxVersion == 0 {
  201. c.TLS.MaxVersion = DefaultTLSOptions.MaxVersion
  202. }
  203. if c.TLS.MinVersion == 0 {
  204. c.TLS.MinVersion = DefaultTLSOptions.MinVersion
  205. }
  206. if c.TLS.MinVersion > c.TLS.MaxVersion {
  207. return errors.New("tls minVersion cannot exceed tls maxVersion")
  208. }
  209. c.TLS.Renegotiation = c.TLS.Renegotiation || DefaultTLSOptions.Renegotiation
  210. }
  211. // Validate KMS options, nil is ok.
  212. if err := c.KMS.Validate(); err != nil {
  213. return err
  214. }
  215. // Validate RA/CAS options, nil is ok.
  216. if err := ra.Validate(); err != nil {
  217. return err
  218. }
  219. // Validate ssh: nil is ok
  220. if err := c.SSH.Validate(); err != nil {
  221. return err
  222. }
  223. // Validate templates: nil is ok
  224. if err := c.Templates.Validate(); err != nil {
  225. return err
  226. }
  227. return c.AuthorityConfig.Validate(c.GetAudiences())
  228. }
  229. // GetAudiences returns the legacy and possible urls without the ports that will
  230. // be used as the default provisioner audiences. The CA might have proxies in
  231. // front so we cannot rely on the port.
  232. func (c *Config) GetAudiences() provisioner.Audiences {
  233. audiences := provisioner.Audiences{
  234. Sign: []string{legacyAuthority},
  235. Revoke: []string{legacyAuthority},
  236. SSHSign: []string{},
  237. SSHRevoke: []string{},
  238. SSHRenew: []string{},
  239. }
  240. for _, name := range c.DNSNames {
  241. audiences.Sign = append(audiences.Sign,
  242. fmt.Sprintf("https://%s/1.0/sign", name),
  243. fmt.Sprintf("https://%s/sign", name),
  244. fmt.Sprintf("https://%s/1.0/ssh/sign", name),
  245. fmt.Sprintf("https://%s/ssh/sign", name))
  246. audiences.Revoke = append(audiences.Revoke,
  247. fmt.Sprintf("https://%s/1.0/revoke", name),
  248. fmt.Sprintf("https://%s/revoke", name))
  249. audiences.SSHSign = append(audiences.SSHSign,
  250. fmt.Sprintf("https://%s/1.0/ssh/sign", name),
  251. fmt.Sprintf("https://%s/ssh/sign", name),
  252. fmt.Sprintf("https://%s/1.0/sign", name),
  253. fmt.Sprintf("https://%s/sign", name))
  254. audiences.SSHRevoke = append(audiences.SSHRevoke,
  255. fmt.Sprintf("https://%s/1.0/ssh/revoke", name),
  256. fmt.Sprintf("https://%s/ssh/revoke", name))
  257. audiences.SSHRenew = append(audiences.SSHRenew,
  258. fmt.Sprintf("https://%s/1.0/ssh/renew", name),
  259. fmt.Sprintf("https://%s/ssh/renew", name))
  260. audiences.SSHRekey = append(audiences.SSHRekey,
  261. fmt.Sprintf("https://%s/1.0/ssh/rekey", name),
  262. fmt.Sprintf("https://%s/ssh/rekey", name))
  263. }
  264. return audiences
  265. }