policy.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. package policy
  2. import (
  3. "fmt"
  4. "go.step.sm/linkedca"
  5. "github.com/smallstep/certificates/policy"
  6. )
  7. // X509Policy is an alias for policy.X509NamePolicyEngine
  8. type X509Policy policy.X509NamePolicyEngine
  9. // UserPolicy is an alias for policy.SSHNamePolicyEngine
  10. type UserPolicy policy.SSHNamePolicyEngine
  11. // HostPolicy is an alias for policy.SSHNamePolicyEngine
  12. type HostPolicy policy.SSHNamePolicyEngine
  13. // NewX509PolicyEngine creates a new x509 name policy engine
  14. func NewX509PolicyEngine(policyOptions X509PolicyOptionsInterface) (X509Policy, error) {
  15. // return early if no policy engine options to configure
  16. if policyOptions == nil {
  17. return nil, nil
  18. }
  19. options := []policy.NamePolicyOption{}
  20. allowed := policyOptions.GetAllowedNameOptions()
  21. if allowed != nil && allowed.HasNames() {
  22. options = append(options,
  23. policy.WithPermittedCommonNames(allowed.CommonNames...),
  24. policy.WithPermittedDNSDomains(allowed.DNSDomains...),
  25. policy.WithPermittedIPsOrCIDRs(allowed.IPRanges...),
  26. policy.WithPermittedEmailAddresses(allowed.EmailAddresses...),
  27. policy.WithPermittedURIDomains(allowed.URIDomains...),
  28. )
  29. }
  30. denied := policyOptions.GetDeniedNameOptions()
  31. if denied != nil && denied.HasNames() {
  32. options = append(options,
  33. policy.WithExcludedCommonNames(denied.CommonNames...),
  34. policy.WithExcludedDNSDomains(denied.DNSDomains...),
  35. policy.WithExcludedIPsOrCIDRs(denied.IPRanges...),
  36. policy.WithExcludedEmailAddresses(denied.EmailAddresses...),
  37. policy.WithExcludedURIDomains(denied.URIDomains...),
  38. )
  39. }
  40. // ensure no policy engine is returned when no name options were provided
  41. if len(options) == 0 {
  42. return nil, nil
  43. }
  44. // check if configuration specifies that wildcard names are allowed
  45. if policyOptions.AreWildcardNamesAllowed() {
  46. options = append(options, policy.WithAllowLiteralWildcardNames())
  47. }
  48. // enable subject common name verification by default
  49. options = append(options, policy.WithSubjectCommonNameVerification())
  50. return policy.New(options...)
  51. }
  52. type sshPolicyEngineType string
  53. const (
  54. UserPolicyEngineType sshPolicyEngineType = "user"
  55. HostPolicyEngineType sshPolicyEngineType = "host"
  56. )
  57. // newSSHUserPolicyEngine creates a new SSH user certificate policy engine
  58. func NewSSHUserPolicyEngine(policyOptions SSHPolicyOptionsInterface) (UserPolicy, error) {
  59. policyEngine, err := newSSHPolicyEngine(policyOptions, UserPolicyEngineType)
  60. if err != nil {
  61. return nil, err
  62. }
  63. return policyEngine, nil
  64. }
  65. // newSSHHostPolicyEngine create a new SSH host certificate policy engine
  66. func NewSSHHostPolicyEngine(policyOptions SSHPolicyOptionsInterface) (HostPolicy, error) {
  67. policyEngine, err := newSSHPolicyEngine(policyOptions, HostPolicyEngineType)
  68. if err != nil {
  69. return nil, err
  70. }
  71. return policyEngine, nil
  72. }
  73. // newSSHPolicyEngine creates a new SSH name policy engine
  74. func newSSHPolicyEngine(policyOptions SSHPolicyOptionsInterface, typ sshPolicyEngineType) (policy.SSHNamePolicyEngine, error) {
  75. // return early if no policy engine options to configure
  76. if policyOptions == nil {
  77. return nil, nil
  78. }
  79. var (
  80. allowed *SSHNameOptions
  81. denied *SSHNameOptions
  82. )
  83. switch typ {
  84. case UserPolicyEngineType:
  85. allowed = policyOptions.GetAllowedUserNameOptions()
  86. denied = policyOptions.GetDeniedUserNameOptions()
  87. case HostPolicyEngineType:
  88. allowed = policyOptions.GetAllowedHostNameOptions()
  89. denied = policyOptions.GetDeniedHostNameOptions()
  90. default:
  91. return nil, fmt.Errorf("unknown SSH policy engine type %s provided", typ)
  92. }
  93. options := []policy.NamePolicyOption{}
  94. if allowed != nil && allowed.HasNames() {
  95. options = append(options,
  96. policy.WithPermittedDNSDomains(allowed.DNSDomains...),
  97. policy.WithPermittedIPsOrCIDRs(allowed.IPRanges...),
  98. policy.WithPermittedEmailAddresses(allowed.EmailAddresses...),
  99. policy.WithPermittedPrincipals(allowed.Principals...),
  100. )
  101. }
  102. if denied != nil && denied.HasNames() {
  103. options = append(options,
  104. policy.WithExcludedDNSDomains(denied.DNSDomains...),
  105. policy.WithExcludedIPsOrCIDRs(denied.IPRanges...),
  106. policy.WithExcludedEmailAddresses(denied.EmailAddresses...),
  107. policy.WithExcludedPrincipals(denied.Principals...),
  108. )
  109. }
  110. // ensure no policy engine is returned when no name options were provided
  111. if len(options) == 0 {
  112. return nil, nil
  113. }
  114. return policy.New(options...)
  115. }
  116. func LinkedToCertificates(p *linkedca.Policy) *Options {
  117. // return early
  118. if p == nil {
  119. return nil
  120. }
  121. // return early if x509 nor SSH is set
  122. if p.GetX509() == nil && p.GetSsh() == nil {
  123. return nil
  124. }
  125. opts := &Options{}
  126. // fill x509 policy configuration
  127. if x509 := p.GetX509(); x509 != nil {
  128. opts.X509 = &X509PolicyOptions{}
  129. if allow := x509.GetAllow(); allow != nil {
  130. opts.X509.AllowedNames = &X509NameOptions{}
  131. if allow.Dns != nil {
  132. opts.X509.AllowedNames.DNSDomains = allow.Dns
  133. }
  134. if allow.Ips != nil {
  135. opts.X509.AllowedNames.IPRanges = allow.Ips
  136. }
  137. if allow.Emails != nil {
  138. opts.X509.AllowedNames.EmailAddresses = allow.Emails
  139. }
  140. if allow.Uris != nil {
  141. opts.X509.AllowedNames.URIDomains = allow.Uris
  142. }
  143. if allow.CommonNames != nil {
  144. opts.X509.AllowedNames.CommonNames = allow.CommonNames
  145. }
  146. }
  147. if deny := x509.GetDeny(); deny != nil {
  148. opts.X509.DeniedNames = &X509NameOptions{}
  149. if deny.Dns != nil {
  150. opts.X509.DeniedNames.DNSDomains = deny.Dns
  151. }
  152. if deny.Ips != nil {
  153. opts.X509.DeniedNames.IPRanges = deny.Ips
  154. }
  155. if deny.Emails != nil {
  156. opts.X509.DeniedNames.EmailAddresses = deny.Emails
  157. }
  158. if deny.Uris != nil {
  159. opts.X509.DeniedNames.URIDomains = deny.Uris
  160. }
  161. if deny.CommonNames != nil {
  162. opts.X509.DeniedNames.CommonNames = deny.CommonNames
  163. }
  164. }
  165. opts.X509.AllowWildcardNames = x509.GetAllowWildcardNames()
  166. }
  167. // fill ssh policy configuration
  168. if ssh := p.GetSsh(); ssh != nil {
  169. opts.SSH = &SSHPolicyOptions{}
  170. if host := ssh.GetHost(); host != nil {
  171. opts.SSH.Host = &SSHHostCertificateOptions{}
  172. if allow := host.GetAllow(); allow != nil {
  173. opts.SSH.Host.AllowedNames = &SSHNameOptions{}
  174. if allow.Dns != nil {
  175. opts.SSH.Host.AllowedNames.DNSDomains = allow.Dns
  176. }
  177. if allow.Ips != nil {
  178. opts.SSH.Host.AllowedNames.IPRanges = allow.Ips
  179. }
  180. if allow.Principals != nil {
  181. opts.SSH.Host.AllowedNames.Principals = allow.Principals
  182. }
  183. }
  184. if deny := host.GetDeny(); deny != nil {
  185. opts.SSH.Host.DeniedNames = &SSHNameOptions{}
  186. if deny.Dns != nil {
  187. opts.SSH.Host.DeniedNames.DNSDomains = deny.Dns
  188. }
  189. if deny.Ips != nil {
  190. opts.SSH.Host.DeniedNames.IPRanges = deny.Ips
  191. }
  192. if deny.Principals != nil {
  193. opts.SSH.Host.DeniedNames.Principals = deny.Principals
  194. }
  195. }
  196. }
  197. if user := ssh.GetUser(); user != nil {
  198. opts.SSH.User = &SSHUserCertificateOptions{}
  199. if allow := user.GetAllow(); allow != nil {
  200. opts.SSH.User.AllowedNames = &SSHNameOptions{}
  201. if allow.Emails != nil {
  202. opts.SSH.User.AllowedNames.EmailAddresses = allow.Emails
  203. }
  204. if allow.Principals != nil {
  205. opts.SSH.User.AllowedNames.Principals = allow.Principals
  206. }
  207. }
  208. if deny := user.GetDeny(); deny != nil {
  209. opts.SSH.User.DeniedNames = &SSHNameOptions{}
  210. if deny.Emails != nil {
  211. opts.SSH.User.DeniedNames.EmailAddresses = deny.Emails
  212. }
  213. if deny.Principals != nil {
  214. opts.SSH.User.DeniedNames.Principals = deny.Principals
  215. }
  216. }
  217. }
  218. }
  219. return opts
  220. }