2018-10-05 21:48:36 +00:00
|
|
|
package authority
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2019-03-07 02:32:56 +00:00
|
|
|
"fmt"
|
2018-12-21 23:27:22 +00:00
|
|
|
"net"
|
2018-10-05 21:48:36 +00:00
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2019-03-06 22:49:25 +00:00
|
|
|
"github.com/smallstep/certificates/authority/provisioner"
|
2020-09-09 02:26:32 +00:00
|
|
|
cas "github.com/smallstep/certificates/cas/apiv1"
|
2019-03-05 08:07:13 +00:00
|
|
|
"github.com/smallstep/certificates/db"
|
2020-01-10 02:42:26 +00:00
|
|
|
kms "github.com/smallstep/certificates/kms/apiv1"
|
2019-12-10 00:54:48 +00:00
|
|
|
"github.com/smallstep/certificates/templates"
|
2018-10-05 21:48:36 +00:00
|
|
|
)
|
|
|
|
|
2018-10-19 05:26:39 +00:00
|
|
|
var (
|
|
|
|
// DefaultTLSOptions represents the default TLS version as well as the cipher
|
|
|
|
// suites used in the TLS certificates.
|
2020-08-10 22:29:18 +00:00
|
|
|
DefaultTLSOptions = TLSOptions{
|
|
|
|
CipherSuites: CipherSuites{
|
2018-10-19 05:26:39 +00:00
|
|
|
"TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305",
|
|
|
|
"TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
|
|
|
|
"TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",
|
|
|
|
},
|
|
|
|
MinVersion: 1.2,
|
|
|
|
MaxVersion: 1.2,
|
|
|
|
Renegotiation: false,
|
|
|
|
}
|
2020-01-03 01:48:28 +00:00
|
|
|
defaultBackdate = time.Minute
|
2018-11-01 22:43:24 +00:00
|
|
|
defaultDisableRenewal = false
|
2019-08-05 18:40:27 +00:00
|
|
|
defaultEnableSSHCA = false
|
2019-03-06 22:49:25 +00:00
|
|
|
globalProvisionerClaims = provisioner.Claims{
|
2019-07-24 01:46:43 +00:00
|
|
|
MinTLSDur: &provisioner.Duration{Duration: 5 * time.Minute}, // TLS certs
|
|
|
|
MaxTLSDur: &provisioner.Duration{Duration: 24 * time.Hour},
|
|
|
|
DefaultTLSDur: &provisioner.Duration{Duration: 24 * time.Hour},
|
|
|
|
DisableRenewal: &defaultDisableRenewal,
|
|
|
|
MinUserSSHDur: &provisioner.Duration{Duration: 5 * time.Minute}, // User SSH certs
|
|
|
|
MaxUserSSHDur: &provisioner.Duration{Duration: 24 * time.Hour},
|
2019-10-25 19:23:52 +00:00
|
|
|
DefaultUserSSHDur: &provisioner.Duration{Duration: 16 * time.Hour},
|
2019-07-24 01:46:43 +00:00
|
|
|
MinHostSSHDur: &provisioner.Duration{Duration: 5 * time.Minute}, // Host SSH certs
|
|
|
|
MaxHostSSHDur: &provisioner.Duration{Duration: 30 * 24 * time.Hour},
|
|
|
|
DefaultHostSSHDur: &provisioner.Duration{Duration: 30 * 24 * time.Hour},
|
2019-08-05 18:40:27 +00:00
|
|
|
EnableSSHCA: &defaultEnableSSHCA,
|
2018-10-19 05:26:39 +00:00
|
|
|
}
|
2018-10-05 21:48:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config represents the CA configuration and it's mapped to a JSON object.
|
|
|
|
type Config struct {
|
2019-10-04 02:03:38 +00:00
|
|
|
Root multiString `json:"root"`
|
|
|
|
FederatedRoots []string `json:"federatedRoots"`
|
|
|
|
IntermediateCert string `json:"crt"`
|
|
|
|
IntermediateKey string `json:"key"`
|
|
|
|
Address string `json:"address"`
|
|
|
|
DNSNames []string `json:"dnsNames"`
|
2020-01-10 02:42:26 +00:00
|
|
|
KMS *kms.Options `json:"kms,omitempty"`
|
2019-10-04 02:03:38 +00:00
|
|
|
SSH *SSHConfig `json:"ssh,omitempty"`
|
|
|
|
Logger json.RawMessage `json:"logger,omitempty"`
|
|
|
|
DB *db.Config `json:"db,omitempty"`
|
|
|
|
Monitoring json.RawMessage `json:"monitoring,omitempty"`
|
|
|
|
AuthorityConfig *AuthConfig `json:"authority,omitempty"`
|
2020-08-10 22:29:18 +00:00
|
|
|
TLS *TLSOptions `json:"tls,omitempty"`
|
2019-10-04 02:03:38 +00:00
|
|
|
Password string `json:"password,omitempty"`
|
|
|
|
Templates *templates.Templates `json:"templates,omitempty"`
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
|
|
|
|
2020-08-10 22:29:18 +00:00
|
|
|
// ASN1DN contains ASN1.DN attributes that are used in Subject and Issuer
|
|
|
|
// x509 Certificate blocks.
|
|
|
|
type ASN1DN struct {
|
|
|
|
Country string `json:"country,omitempty" step:"country"`
|
|
|
|
Organization string `json:"organization,omitempty" step:"organization"`
|
|
|
|
OrganizationalUnit string `json:"organizationalUnit,omitempty" step:"organizationalUnit"`
|
|
|
|
Locality string `json:"locality,omitempty" step:"locality"`
|
|
|
|
Province string `json:"province,omitempty" step:"province"`
|
|
|
|
StreetAddress string `json:"streetAddress,omitempty" step:"streetAddress"`
|
|
|
|
CommonName string `json:"commonName,omitempty" step:"commonName"`
|
|
|
|
}
|
|
|
|
|
2020-10-20 01:08:51 +00:00
|
|
|
// AuthConfig represents the configuration options for the authority. An
|
|
|
|
// underlaying registration authority can also be configured using the
|
|
|
|
// cas.Options.
|
2018-10-05 21:48:36 +00:00
|
|
|
type AuthConfig struct {
|
2020-10-20 01:08:51 +00:00
|
|
|
*cas.Options
|
2020-01-03 01:48:28 +00:00
|
|
|
Provisioners provisioner.List `json:"provisioners"`
|
2020-08-10 22:29:18 +00:00
|
|
|
Template *ASN1DN `json:"template,omitempty"`
|
2020-01-03 01:48:28 +00:00
|
|
|
Claims *provisioner.Claims `json:"claims,omitempty"`
|
|
|
|
DisableIssuedAtCheck bool `json:"disableIssuedAtCheck,omitempty"`
|
|
|
|
Backdate *provisioner.Duration `json:"backdate,omitempty"`
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 20:00:42 +00:00
|
|
|
// init initializes the required fields in the AuthConfig if they are not
|
|
|
|
// provided.
|
|
|
|
func (c *AuthConfig) init() {
|
|
|
|
if c.Provisioners == nil {
|
|
|
|
c.Provisioners = provisioner.List{}
|
|
|
|
}
|
|
|
|
if c.Template == nil {
|
2020-08-10 22:29:18 +00:00
|
|
|
c.Template = &ASN1DN{}
|
2020-05-06 20:00:42 +00:00
|
|
|
}
|
|
|
|
if c.Backdate == nil {
|
|
|
|
c.Backdate = &provisioner.Duration{
|
|
|
|
Duration: defaultBackdate,
|
|
|
|
}
|
|
|
|
}
|
2020-05-05 01:52:18 +00:00
|
|
|
}
|
|
|
|
|
2018-10-05 21:48:36 +00:00
|
|
|
// Validate validates the authority configuration.
|
2019-03-05 08:07:13 +00:00
|
|
|
func (c *AuthConfig) Validate(audiences provisioner.Audiences) error {
|
2018-10-09 06:25:18 +00:00
|
|
|
if c == nil {
|
2018-10-05 21:48:36 +00:00
|
|
|
return errors.New("authority cannot be undefined")
|
2018-10-09 06:25:18 +00:00
|
|
|
}
|
2018-10-19 05:26:39 +00:00
|
|
|
|
2020-05-06 20:00:42 +00:00
|
|
|
// Initialize required fields.
|
|
|
|
c.init()
|
|
|
|
|
2019-11-09 01:43:54 +00:00
|
|
|
// Check that only one K8sSA is enabled
|
|
|
|
var k8sCount int
|
|
|
|
for _, p := range c.Provisioners {
|
|
|
|
if p.GetType() == provisioner.TypeK8sSA {
|
|
|
|
k8sCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if k8sCount > 1 {
|
|
|
|
return errors.New("cannot have more than one kubernetes service account provisioner")
|
|
|
|
}
|
|
|
|
|
2020-05-06 20:00:42 +00:00
|
|
|
if c.Backdate.Duration < 0 {
|
|
|
|
return errors.New("authority.backdate cannot be less than 0")
|
2020-01-03 01:48:28 +00:00
|
|
|
}
|
|
|
|
|
2018-10-09 06:25:18 +00:00
|
|
|
return nil
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LoadConfiguration parses the given filename in JSON format and returns the
|
|
|
|
// configuration struct.
|
|
|
|
func LoadConfiguration(filename string) (*Config, error) {
|
|
|
|
f, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error opening %s", filename)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
var c Config
|
|
|
|
if err := json.NewDecoder(f).Decode(&c); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error parsing %s", filename)
|
|
|
|
}
|
|
|
|
|
2021-05-04 21:45:11 +00:00
|
|
|
c.init()
|
|
|
|
|
2018-10-05 21:48:36 +00:00
|
|
|
return &c, nil
|
|
|
|
}
|
|
|
|
|
2020-05-06 20:00:42 +00:00
|
|
|
// initializes the minimal configuration required to create an authority. This
|
|
|
|
// is mainly used on embedded authorities.
|
|
|
|
func (c *Config) init() {
|
|
|
|
if c.DNSNames == nil {
|
|
|
|
c.DNSNames = []string{"localhost", "127.0.0.1", "::1"}
|
|
|
|
}
|
|
|
|
if c.TLS == nil {
|
|
|
|
c.TLS = &DefaultTLSOptions
|
|
|
|
}
|
|
|
|
if c.AuthorityConfig == nil {
|
|
|
|
c.AuthorityConfig = &AuthConfig{}
|
|
|
|
}
|
|
|
|
c.AuthorityConfig.init()
|
|
|
|
}
|
|
|
|
|
2018-10-05 21:48:36 +00:00
|
|
|
// Save saves the configuration to the given filename.
|
|
|
|
func (c *Config) Save(filename string) error {
|
|
|
|
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "error opening %s", filename)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
enc := json.NewEncoder(f)
|
|
|
|
enc.SetIndent("", "\t")
|
|
|
|
return errors.Wrapf(enc.Encode(c), "error writing %s", filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate validates the configuration.
|
|
|
|
func (c *Config) Validate() error {
|
|
|
|
switch {
|
|
|
|
case c.Address == "":
|
|
|
|
return errors.New("address cannot be empty")
|
|
|
|
|
|
|
|
case len(c.DNSNames) == 0:
|
|
|
|
return errors.New("dnsNames cannot be empty")
|
|
|
|
}
|
|
|
|
|
2020-10-20 01:08:51 +00:00
|
|
|
// Options holds the RA/CAS configuration.
|
|
|
|
ra := c.AuthorityConfig.Options
|
|
|
|
// The default RA/CAS requires root, crt and key.
|
|
|
|
if ra.Is(cas.SoftCAS) {
|
2020-09-21 22:27:20 +00:00
|
|
|
switch {
|
|
|
|
case c.Root.HasEmpties():
|
|
|
|
return errors.New("root cannot be empty")
|
|
|
|
case c.IntermediateCert == "":
|
|
|
|
return errors.New("crt cannot be empty")
|
|
|
|
case c.IntermediateKey == "":
|
|
|
|
return errors.New("key cannot be empty")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-21 23:27:22 +00:00
|
|
|
// Validate address (a port is required)
|
|
|
|
if _, _, err := net.SplitHostPort(c.Address); err != nil {
|
|
|
|
return errors.Errorf("invalid address %s", c.Address)
|
|
|
|
}
|
|
|
|
|
2018-10-05 21:48:36 +00:00
|
|
|
if c.TLS == nil {
|
|
|
|
c.TLS = &DefaultTLSOptions
|
|
|
|
} else {
|
|
|
|
if len(c.TLS.CipherSuites) == 0 {
|
|
|
|
c.TLS.CipherSuites = DefaultTLSOptions.CipherSuites
|
|
|
|
}
|
|
|
|
if c.TLS.MaxVersion == 0 {
|
|
|
|
c.TLS.MaxVersion = DefaultTLSOptions.MaxVersion
|
|
|
|
}
|
|
|
|
if c.TLS.MinVersion == 0 {
|
|
|
|
c.TLS.MinVersion = c.TLS.MaxVersion
|
|
|
|
}
|
|
|
|
if c.TLS.MinVersion > c.TLS.MaxVersion {
|
|
|
|
return errors.New("tls minVersion cannot exceed tls maxVersion")
|
|
|
|
}
|
|
|
|
c.TLS.Renegotiation = c.TLS.Renegotiation || DefaultTLSOptions.Renegotiation
|
|
|
|
}
|
|
|
|
|
2020-01-10 02:42:26 +00:00
|
|
|
// Validate KMS options, nil is ok.
|
|
|
|
if err := c.KMS.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-20 01:08:51 +00:00
|
|
|
// Validate RA/CAS options, nil is ok.
|
|
|
|
if err := ra.Validate(); err != nil {
|
2020-09-09 02:26:32 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-09 01:09:41 +00:00
|
|
|
// Validate ssh: nil is ok
|
|
|
|
if err := c.SSH.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-04 02:03:38 +00:00
|
|
|
// Validate templates: nil is ok
|
|
|
|
if err := c.Templates.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-07 02:32:56 +00:00
|
|
|
return c.AuthorityConfig.Validate(c.getAudiences())
|
|
|
|
}
|
|
|
|
|
|
|
|
// getAudiences returns the legacy and possible urls without the ports that will
|
|
|
|
// be used as the default provisioner audiences. The CA might have proxies in
|
|
|
|
// front so we cannot rely on the port.
|
2019-03-05 08:07:13 +00:00
|
|
|
func (c *Config) getAudiences() provisioner.Audiences {
|
|
|
|
audiences := provisioner.Audiences{
|
2019-10-28 18:50:43 +00:00
|
|
|
Sign: []string{legacyAuthority},
|
|
|
|
Revoke: []string{legacyAuthority},
|
|
|
|
SSHSign: []string{},
|
|
|
|
SSHRevoke: []string{},
|
|
|
|
SSHRenew: []string{},
|
2019-03-05 08:07:13 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 02:32:56 +00:00
|
|
|
for _, name := range c.DNSNames {
|
2019-03-05 08:07:13 +00:00
|
|
|
audiences.Sign = append(audiences.Sign,
|
2019-11-14 23:18:49 +00:00
|
|
|
fmt.Sprintf("https://%s/1.0/sign", name),
|
2019-11-20 19:51:25 +00:00
|
|
|
fmt.Sprintf("https://%s/sign", name),
|
|
|
|
fmt.Sprintf("https://%s/1.0/ssh/sign", name),
|
|
|
|
fmt.Sprintf("https://%s/ssh/sign", name))
|
2019-03-05 08:07:13 +00:00
|
|
|
audiences.Revoke = append(audiences.Revoke,
|
2019-11-14 23:18:49 +00:00
|
|
|
fmt.Sprintf("https://%s/1.0/revoke", name),
|
|
|
|
fmt.Sprintf("https://%s/revoke", name))
|
2019-10-28 18:50:43 +00:00
|
|
|
audiences.SSHSign = append(audiences.SSHSign,
|
2019-11-14 23:18:49 +00:00
|
|
|
fmt.Sprintf("https://%s/1.0/ssh/sign", name),
|
|
|
|
fmt.Sprintf("https://%s/ssh/sign", name),
|
|
|
|
fmt.Sprintf("https://%s/1.0/sign", name),
|
|
|
|
fmt.Sprintf("https://%s/sign", name))
|
2019-10-28 18:50:43 +00:00
|
|
|
audiences.SSHRevoke = append(audiences.SSHRevoke,
|
2019-11-14 23:18:49 +00:00
|
|
|
fmt.Sprintf("https://%s/1.0/ssh/revoke", name),
|
|
|
|
fmt.Sprintf("https://%s/ssh/revoke", name))
|
2019-10-28 18:50:43 +00:00
|
|
|
audiences.SSHRenew = append(audiences.SSHRenew,
|
2019-11-14 23:18:49 +00:00
|
|
|
fmt.Sprintf("https://%s/1.0/ssh/renew", name),
|
|
|
|
fmt.Sprintf("https://%s/ssh/renew", name))
|
2019-10-28 18:50:43 +00:00
|
|
|
audiences.SSHRekey = append(audiences.SSHRekey,
|
2019-11-14 23:18:49 +00:00
|
|
|
fmt.Sprintf("https://%s/1.0/ssh/rekey", name),
|
|
|
|
fmt.Sprintf("https://%s/ssh/rekey", name))
|
2019-03-07 02:32:56 +00:00
|
|
|
}
|
2019-03-05 08:07:13 +00:00
|
|
|
|
2019-03-07 02:32:56 +00:00
|
|
|
return audiences
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|