2022-03-15 14:51:45 +00:00
|
|
|
package authority
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-03-24 13:55:40 +00:00
|
|
|
"errors"
|
2022-03-21 14:53:59 +00:00
|
|
|
"fmt"
|
|
|
|
|
2022-03-15 14:51:45 +00:00
|
|
|
"go.step.sm/linkedca"
|
2022-03-21 14:53:59 +00:00
|
|
|
|
|
|
|
"github.com/smallstep/certificates/authority/admin"
|
|
|
|
authPolicy "github.com/smallstep/certificates/authority/policy"
|
|
|
|
policy "github.com/smallstep/certificates/policy"
|
2022-03-15 14:51:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (a *Authority) GetAuthorityPolicy(ctx context.Context) (*linkedca.Policy, error) {
|
|
|
|
a.adminMutex.Lock()
|
|
|
|
defer a.adminMutex.Unlock()
|
|
|
|
|
2022-03-24 12:10:49 +00:00
|
|
|
p, err := a.adminDB.GetAuthorityPolicy(ctx)
|
2022-03-15 14:51:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-24 12:10:49 +00:00
|
|
|
return p, nil
|
2022-03-15 14:51:45 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 12:10:49 +00:00
|
|
|
func (a *Authority) CreateAuthorityPolicy(ctx context.Context, adm *linkedca.Admin, p *linkedca.Policy) (*linkedca.Policy, error) {
|
2022-03-15 14:51:45 +00:00
|
|
|
a.adminMutex.Lock()
|
|
|
|
defer a.adminMutex.Unlock()
|
|
|
|
|
2022-03-24 12:10:49 +00:00
|
|
|
if err := a.checkPolicy(ctx, adm, p); err != nil {
|
2022-03-24 09:54:45 +00:00
|
|
|
return nil, err
|
2022-03-21 14:53:59 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 12:10:49 +00:00
|
|
|
if err := a.adminDB.CreateAuthorityPolicy(ctx, p); err != nil {
|
2022-03-24 09:54:45 +00:00
|
|
|
return nil, err
|
2022-03-15 14:51:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := a.reloadPolicyEngines(ctx); err != nil {
|
2022-03-24 09:54:45 +00:00
|
|
|
return nil, admin.WrapErrorISE(err, "error reloading policy engines when creating authority policy")
|
2022-03-15 14:51:45 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 12:10:49 +00:00
|
|
|
return p, nil // TODO: return the newly stored policy
|
2022-03-15 14:51:45 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 12:10:49 +00:00
|
|
|
func (a *Authority) UpdateAuthorityPolicy(ctx context.Context, adm *linkedca.Admin, p *linkedca.Policy) (*linkedca.Policy, error) {
|
2022-03-15 14:51:45 +00:00
|
|
|
a.adminMutex.Lock()
|
|
|
|
defer a.adminMutex.Unlock()
|
|
|
|
|
2022-03-24 12:10:49 +00:00
|
|
|
if err := a.checkPolicy(ctx, adm, p); err != nil {
|
2022-03-24 09:54:45 +00:00
|
|
|
return nil, err
|
2022-03-21 14:53:59 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 12:10:49 +00:00
|
|
|
if err := a.adminDB.UpdateAuthorityPolicy(ctx, p); err != nil {
|
2022-03-24 09:54:45 +00:00
|
|
|
return nil, err
|
2022-03-15 14:51:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := a.reloadPolicyEngines(ctx); err != nil {
|
2022-03-24 09:54:45 +00:00
|
|
|
return nil, admin.WrapErrorISE(err, "error reloading policy engines when updating authority policy")
|
2022-03-15 14:51:45 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 12:10:49 +00:00
|
|
|
return p, nil // TODO: return the updated stored policy
|
2022-03-15 14:51:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Authority) RemoveAuthorityPolicy(ctx context.Context) error {
|
|
|
|
a.adminMutex.Lock()
|
|
|
|
defer a.adminMutex.Unlock()
|
|
|
|
|
|
|
|
if err := a.adminDB.DeleteAuthorityPolicy(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := a.reloadPolicyEngines(ctx); err != nil {
|
2022-03-21 14:53:59 +00:00
|
|
|
return admin.WrapErrorISE(err, "error reloading policy engines when deleting authority policy")
|
2022-03-15 14:51:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-30 12:21:39 +00:00
|
|
|
// checkPolicy checks if a new or updated policy configuration results in the user
|
|
|
|
// locking themselves or other admins out of the CA.
|
2022-03-21 14:53:59 +00:00
|
|
|
func (a *Authority) checkPolicy(ctx context.Context, adm *linkedca.Admin, p *linkedca.Policy) error {
|
|
|
|
|
|
|
|
// convert the policy; return early if nil
|
|
|
|
policyOptions := policyToCertificates(p)
|
|
|
|
if policyOptions == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
engine, err := authPolicy.NewX509PolicyEngine(policyOptions.GetX509Options())
|
|
|
|
if err != nil {
|
|
|
|
return admin.WrapErrorISE(err, "error creating temporary policy engine")
|
|
|
|
}
|
|
|
|
|
2022-03-30 12:21:39 +00:00
|
|
|
// when an empty policy is provided, the resulting engine is nil
|
|
|
|
// and there's no policy to evaluate.
|
|
|
|
if engine == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-21 14:53:59 +00:00
|
|
|
// TODO(hs): Provide option to force the policy, even when the admin subject would be locked out?
|
|
|
|
|
2022-03-30 13:39:03 +00:00
|
|
|
// check if the admin user that instructed the authority policy to be
|
|
|
|
// created or updated, would still be allowed when the provided policy
|
|
|
|
// would be applied to the authority.
|
2022-03-30 12:21:39 +00:00
|
|
|
sans := []string{adm.GetSubject()}
|
2022-03-21 14:53:59 +00:00
|
|
|
if err := isAllowed(engine, sans); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-30 13:39:03 +00:00
|
|
|
// get all current admins from the database
|
|
|
|
admins, err := a.adminDB.GetAdmins(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// loop through admins to verify that none of them would be
|
|
|
|
// locked out when the new policy were to be applied. Returns
|
|
|
|
// an error with a message that includes the admin subject that
|
|
|
|
// would be locked out
|
|
|
|
for _, adm := range admins {
|
|
|
|
sans = []string{adm.GetSubject()}
|
|
|
|
if err := isAllowed(engine, sans); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(hs): mask the error message for non-super admins?
|
2022-03-21 14:53:59 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func isAllowed(engine authPolicy.X509Policy, sans []string) error {
|
|
|
|
var (
|
|
|
|
allowed bool
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if allowed, err = engine.AreSANsAllowed(sans); err != nil {
|
|
|
|
var policyErr *policy.NamePolicyError
|
2022-03-24 12:10:49 +00:00
|
|
|
if isPolicyErr := errors.As(err, &policyErr); isPolicyErr && policyErr.Reason == policy.NotAuthorizedForThisName {
|
2022-03-21 14:53:59 +00:00
|
|
|
return fmt.Errorf("the provided policy would lock out %s from the CA. Please update your policy to include %s as an allowed name", sans, sans)
|
|
|
|
}
|
2022-03-24 12:10:49 +00:00
|
|
|
return err
|
2022-03-21 14:53:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !allowed {
|
|
|
|
return fmt.Errorf("the provided policy would lock out %s from the CA. Please update your policy to include %s as an allowed name", sans, sans)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func policyToCertificates(p *linkedca.Policy) *authPolicy.Options {
|
|
|
|
|
2022-03-15 14:51:45 +00:00
|
|
|
// return early
|
|
|
|
if p == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2022-03-21 14:53:59 +00:00
|
|
|
|
2022-03-15 14:51:45 +00:00
|
|
|
// prepare full policy struct
|
2022-03-21 14:53:59 +00:00
|
|
|
opts := &authPolicy.Options{
|
|
|
|
X509: &authPolicy.X509PolicyOptions{
|
|
|
|
AllowedNames: &authPolicy.X509NameOptions{},
|
|
|
|
DeniedNames: &authPolicy.X509NameOptions{},
|
2022-03-15 14:51:45 +00:00
|
|
|
},
|
2022-03-21 14:53:59 +00:00
|
|
|
SSH: &authPolicy.SSHPolicyOptions{
|
|
|
|
Host: &authPolicy.SSHHostCertificateOptions{
|
|
|
|
AllowedNames: &authPolicy.SSHNameOptions{},
|
|
|
|
DeniedNames: &authPolicy.SSHNameOptions{},
|
2022-03-15 14:51:45 +00:00
|
|
|
},
|
2022-03-21 14:53:59 +00:00
|
|
|
User: &authPolicy.SSHUserCertificateOptions{
|
|
|
|
AllowedNames: &authPolicy.SSHNameOptions{},
|
|
|
|
DeniedNames: &authPolicy.SSHNameOptions{},
|
2022-03-15 14:51:45 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2022-03-21 14:53:59 +00:00
|
|
|
|
2022-03-15 14:51:45 +00:00
|
|
|
// fill x509 policy configuration
|
|
|
|
if p.X509 != nil {
|
|
|
|
if p.X509.Allow != nil {
|
2022-03-30 12:21:39 +00:00
|
|
|
if p.X509.Allow.Dns != nil {
|
|
|
|
opts.X509.AllowedNames.DNSDomains = p.X509.Allow.Dns
|
|
|
|
}
|
|
|
|
if p.X509.Allow.Ips != nil {
|
|
|
|
opts.X509.AllowedNames.IPRanges = p.X509.Allow.Ips
|
|
|
|
}
|
|
|
|
if p.X509.Allow.Emails != nil {
|
|
|
|
opts.X509.AllowedNames.EmailAddresses = p.X509.Allow.Emails
|
|
|
|
}
|
|
|
|
if p.X509.Allow.Uris != nil {
|
|
|
|
opts.X509.AllowedNames.URIDomains = p.X509.Allow.Uris
|
|
|
|
}
|
2022-03-15 14:51:45 +00:00
|
|
|
}
|
|
|
|
if p.X509.Deny != nil {
|
2022-03-30 12:21:39 +00:00
|
|
|
if p.X509.Deny.Dns != nil {
|
|
|
|
opts.X509.DeniedNames.DNSDomains = p.X509.Deny.Dns
|
|
|
|
}
|
|
|
|
if p.X509.Deny.Ips != nil {
|
|
|
|
opts.X509.DeniedNames.IPRanges = p.X509.Deny.Ips
|
|
|
|
}
|
|
|
|
if p.X509.Deny.Emails != nil {
|
|
|
|
opts.X509.DeniedNames.EmailAddresses = p.X509.Deny.Emails
|
|
|
|
}
|
|
|
|
if p.X509.Deny.Uris != nil {
|
|
|
|
opts.X509.DeniedNames.URIDomains = p.X509.Deny.Uris
|
|
|
|
}
|
2022-03-15 14:51:45 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-21 14:53:59 +00:00
|
|
|
|
2022-03-15 14:51:45 +00:00
|
|
|
// fill ssh policy configuration
|
|
|
|
if p.Ssh != nil {
|
|
|
|
if p.Ssh.Host != nil {
|
|
|
|
if p.Ssh.Host.Allow != nil {
|
2022-03-30 12:21:39 +00:00
|
|
|
if p.Ssh.Host.Allow.Dns != nil {
|
|
|
|
opts.SSH.Host.AllowedNames.DNSDomains = p.Ssh.Host.Allow.Dns
|
|
|
|
}
|
|
|
|
if p.Ssh.Host.Allow.Ips != nil {
|
|
|
|
opts.SSH.Host.AllowedNames.IPRanges = p.Ssh.Host.Allow.Ips
|
|
|
|
}
|
|
|
|
if p.Ssh.Host.Allow.Principals != nil {
|
|
|
|
opts.SSH.Host.AllowedNames.Principals = p.Ssh.Host.Allow.Principals
|
|
|
|
}
|
2022-03-15 14:51:45 +00:00
|
|
|
}
|
|
|
|
if p.Ssh.Host.Deny != nil {
|
2022-03-30 12:21:39 +00:00
|
|
|
if p.Ssh.Host.Deny.Dns != nil {
|
|
|
|
opts.SSH.Host.DeniedNames.DNSDomains = p.Ssh.Host.Deny.Dns
|
|
|
|
}
|
|
|
|
if p.Ssh.Host.Deny.Ips != nil {
|
|
|
|
opts.SSH.Host.DeniedNames.IPRanges = p.Ssh.Host.Deny.Ips
|
|
|
|
}
|
|
|
|
if p.Ssh.Host.Deny.Principals != nil {
|
|
|
|
opts.SSH.Host.DeniedNames.Principals = p.Ssh.Host.Deny.Principals
|
|
|
|
}
|
2022-03-15 14:51:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if p.Ssh.User != nil {
|
|
|
|
if p.Ssh.User.Allow != nil {
|
2022-03-30 12:21:39 +00:00
|
|
|
if p.Ssh.User.Allow.Emails != nil {
|
|
|
|
opts.SSH.User.AllowedNames.EmailAddresses = p.Ssh.User.Allow.Emails
|
|
|
|
}
|
|
|
|
if p.Ssh.User.Allow.Principals != nil {
|
|
|
|
opts.SSH.User.AllowedNames.Principals = p.Ssh.User.Allow.Principals
|
|
|
|
}
|
2022-03-15 14:51:45 +00:00
|
|
|
}
|
|
|
|
if p.Ssh.User.Deny != nil {
|
2022-03-30 12:21:39 +00:00
|
|
|
if p.Ssh.User.Deny.Emails != nil {
|
|
|
|
opts.SSH.User.DeniedNames.EmailAddresses = p.Ssh.User.Deny.Emails
|
|
|
|
}
|
|
|
|
if p.Ssh.User.Deny.Principals != nil {
|
|
|
|
opts.SSH.User.DeniedNames.Principals = p.Ssh.User.Deny.Principals
|
|
|
|
}
|
2022-03-15 14:51:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return opts
|
|
|
|
}
|