certificates/authority/provisioner/policy.go

70 lines
2.2 KiB
Go
Raw Normal View History

package provisioner
import (
2022-01-17 22:36:13 +00:00
"github.com/smallstep/certificates/policy"
)
// newX509PolicyEngine creates a new x509 name policy engine
2022-01-17 22:36:13 +00:00
func newX509PolicyEngine(x509Opts *X509Options) (policy.X509NamePolicyEngine, error) {
if x509Opts == nil {
return nil, nil
}
2022-01-17 22:36:13 +00:00
options := []policy.NamePolicyOption{
policy.WithSubjectCommonNameVerification(), // enable x509 Subject Common Name validation by default
2022-01-03 14:32:58 +00:00
}
allowed := x509Opts.GetAllowedNameOptions()
if allowed != nil && allowed.HasNames() {
options = append(options,
2022-01-17 22:36:13 +00:00
policy.WithPermittedDNSDomains(allowed.DNSDomains),
policy.WithPermittedIPsOrCIDRs(allowed.IPRanges),
2022-01-17 22:36:13 +00:00
policy.WithPermittedEmailAddresses(allowed.EmailAddresses),
policy.WithPermittedURIDomains(allowed.URIDomains),
)
}
denied := x509Opts.GetDeniedNameOptions()
if denied != nil && denied.HasNames() {
options = append(options,
2022-01-17 22:36:13 +00:00
policy.WithExcludedDNSDomains(denied.DNSDomains),
policy.WithExcludedIPsOrCIDRs(denied.IPRanges),
2022-01-17 22:36:13 +00:00
policy.WithExcludedEmailAddresses(denied.EmailAddresses),
policy.WithExcludedURIDomains(denied.URIDomains),
)
}
2022-01-17 22:36:13 +00:00
return policy.New(options...)
}
// newSSHPolicyEngine creates a new SSH name policy engine
2022-01-17 22:36:13 +00:00
func newSSHPolicyEngine(sshOpts *SSHOptions) (policy.SSHNamePolicyEngine, error) {
if sshOpts == nil {
return nil, nil
}
2022-01-17 22:36:13 +00:00
options := []policy.NamePolicyOption{}
allowed := sshOpts.GetAllowedNameOptions()
if allowed != nil && allowed.HasNames() {
options = append(options,
2022-01-17 22:36:13 +00:00
policy.WithPermittedDNSDomains(allowed.DNSDomains), // TODO(hs): be a bit more lenient w.r.t. the format of domains? I.e. allow "*.localhost" instead of the ".localhost", which is what Name Constraints do.
policy.WithPermittedEmailAddresses(allowed.EmailAddresses),
policy.WithPermittedPrincipals(allowed.Principals),
)
}
denied := sshOpts.GetDeniedNameOptions()
if denied != nil && denied.HasNames() {
options = append(options,
2022-01-17 22:36:13 +00:00
policy.WithExcludedDNSDomains(denied.DNSDomains), // TODO(hs): be a bit more lenient w.r.t. the format of domains? I.e. allow "*.localhost" instead of the ".localhost", which is what Name Constraints do.
policy.WithExcludedEmailAddresses(denied.EmailAddresses),
policy.WithExcludedPrincipals(denied.Principals),
)
}
2022-01-17 22:36:13 +00:00
return policy.New(options...)
}