engine.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package policy
  2. import (
  3. "crypto/x509"
  4. "errors"
  5. "fmt"
  6. "golang.org/x/crypto/ssh"
  7. )
  8. // Engine is a container for multiple policies.
  9. type Engine struct {
  10. x509Policy X509Policy
  11. sshUserPolicy UserPolicy
  12. sshHostPolicy HostPolicy
  13. }
  14. // New returns a new Engine using Options.
  15. func New(options *Options) (*Engine, error) {
  16. // if no options provided, return early
  17. if options == nil {
  18. //nolint:nilnil // legacy
  19. return nil, nil
  20. }
  21. var (
  22. x509Policy X509Policy
  23. sshHostPolicy HostPolicy
  24. sshUserPolicy UserPolicy
  25. err error
  26. )
  27. // initialize the x509 allow/deny policy engine
  28. if x509Policy, err = NewX509PolicyEngine(options.GetX509Options()); err != nil {
  29. return nil, err
  30. }
  31. // initialize the SSH allow/deny policy engine for host certificates
  32. if sshHostPolicy, err = NewSSHHostPolicyEngine(options.GetSSHOptions()); err != nil {
  33. return nil, err
  34. }
  35. // initialize the SSH allow/deny policy engine for user certificates
  36. if sshUserPolicy, err = NewSSHUserPolicyEngine(options.GetSSHOptions()); err != nil {
  37. return nil, err
  38. }
  39. return &Engine{
  40. x509Policy: x509Policy,
  41. sshHostPolicy: sshHostPolicy,
  42. sshUserPolicy: sshUserPolicy,
  43. }, nil
  44. }
  45. // IsX509CertificateAllowed evaluates an X.509 certificate against
  46. // the X.509 policy (if available) and returns an error if one of the
  47. // names in the certificate is not allowed.
  48. func (e *Engine) IsX509CertificateAllowed(cert *x509.Certificate) error {
  49. // return early if there's no policy to evaluate
  50. if e == nil || e.x509Policy == nil {
  51. return nil
  52. }
  53. // return result of X.509 policy evaluation
  54. return e.x509Policy.IsX509CertificateAllowed(cert)
  55. }
  56. // AreSANsAllowed evaluates the slice of SANs against the X.509 policy
  57. // (if available) and returns an error if one of the SANs is not allowed.
  58. func (e *Engine) AreSANsAllowed(sans []string) error {
  59. // return early if there's no policy to evaluate
  60. if e == nil || e.x509Policy == nil {
  61. return nil
  62. }
  63. // return result of X.509 policy evaluation
  64. return e.x509Policy.AreSANsAllowed(sans)
  65. }
  66. // IsSSHCertificateAllowed evaluates an SSH certificate against the
  67. // user or host policy (if configured) and returns an error if one of the
  68. // principals in the certificate is not allowed.
  69. func (e *Engine) IsSSHCertificateAllowed(cert *ssh.Certificate) error {
  70. // return early if there's no policy to evaluate
  71. if e == nil || (e.sshHostPolicy == nil && e.sshUserPolicy == nil) {
  72. return nil
  73. }
  74. switch cert.CertType {
  75. case ssh.HostCert:
  76. // when no host policy engine is configured, but a user policy engine is
  77. // configured, the host certificate is denied.
  78. if e.sshHostPolicy == nil && e.sshUserPolicy != nil {
  79. return errors.New("authority not allowed to sign SSH host certificates when SSH user certificate policy is active")
  80. }
  81. // return result of SSH host policy evaluation
  82. return e.sshHostPolicy.IsSSHCertificateAllowed(cert)
  83. case ssh.UserCert:
  84. // when no user policy engine is configured, but a host policy engine is
  85. // configured, the user certificate is denied.
  86. if e.sshUserPolicy == nil && e.sshHostPolicy != nil {
  87. return errors.New("authority not allowed to sign SSH user certificates when SSH host certificate policy is active")
  88. }
  89. // return result of SSH user policy evaluation
  90. return e.sshUserPolicy.IsSSHCertificateAllowed(cert)
  91. default:
  92. return fmt.Errorf("unexpected SSH certificate type %q", cert.CertType)
  93. }
  94. }