engine.go 3.2 KB

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