policy.go 7.4 KB

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