policy.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. package nosql
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "go.step.sm/linkedca"
  7. "github.com/smallstep/certificates/authority/admin"
  8. "github.com/smallstep/nosql"
  9. )
  10. type dbX509Policy struct {
  11. Allow *dbX509Names `json:"allow,omitempty"`
  12. Deny *dbX509Names `json:"deny,omitempty"`
  13. AllowWildcardNames bool `json:"allow_wildcard_names,omitempty"`
  14. }
  15. type dbX509Names struct {
  16. CommonNames []string `json:"cn,omitempty"`
  17. DNSDomains []string `json:"dns,omitempty"`
  18. IPRanges []string `json:"ip,omitempty"`
  19. EmailAddresses []string `json:"email,omitempty"`
  20. URIDomains []string `json:"uri,omitempty"`
  21. }
  22. type dbSSHPolicy struct {
  23. // User contains SSH user certificate options.
  24. User *dbSSHUserPolicy `json:"user,omitempty"`
  25. // Host contains SSH host certificate options.
  26. Host *dbSSHHostPolicy `json:"host,omitempty"`
  27. }
  28. type dbSSHHostPolicy struct {
  29. Allow *dbSSHHostNames `json:"allow,omitempty"`
  30. Deny *dbSSHHostNames `json:"deny,omitempty"`
  31. }
  32. type dbSSHHostNames struct {
  33. DNSDomains []string `json:"dns,omitempty"`
  34. IPRanges []string `json:"ip,omitempty"`
  35. Principals []string `json:"principal,omitempty"`
  36. }
  37. type dbSSHUserPolicy struct {
  38. Allow *dbSSHUserNames `json:"allow,omitempty"`
  39. Deny *dbSSHUserNames `json:"deny,omitempty"`
  40. }
  41. type dbSSHUserNames struct {
  42. EmailAddresses []string `json:"email,omitempty"`
  43. Principals []string `json:"principal,omitempty"`
  44. }
  45. type dbPolicy struct {
  46. X509 *dbX509Policy `json:"x509,omitempty"`
  47. SSH *dbSSHPolicy `json:"ssh,omitempty"`
  48. }
  49. type dbAuthorityPolicy struct {
  50. ID string `json:"id"`
  51. AuthorityID string `json:"authorityID"`
  52. Policy *dbPolicy `json:"policy,omitempty"`
  53. }
  54. func (dbap *dbAuthorityPolicy) convert() *linkedca.Policy {
  55. if dbap == nil {
  56. return nil
  57. }
  58. return dbToLinked(dbap.Policy)
  59. }
  60. func (db *DB) getDBAuthorityPolicyBytes(ctx context.Context, authorityID string) ([]byte, error) {
  61. data, err := db.db.Get(authorityPoliciesTable, []byte(authorityID))
  62. if nosql.IsErrNotFound(err) {
  63. return nil, admin.NewError(admin.ErrorNotFoundType, "authority policy not found")
  64. } else if err != nil {
  65. return nil, fmt.Errorf("error loading authority policy: %w", err)
  66. }
  67. return data, nil
  68. }
  69. func (db *DB) unmarshalDBAuthorityPolicy(data []byte) (*dbAuthorityPolicy, error) {
  70. if len(data) == 0 {
  71. return nil, nil
  72. }
  73. var dba = new(dbAuthorityPolicy)
  74. if err := json.Unmarshal(data, dba); err != nil {
  75. return nil, fmt.Errorf("error unmarshaling policy bytes into dbAuthorityPolicy: %w", err)
  76. }
  77. return dba, nil
  78. }
  79. func (db *DB) getDBAuthorityPolicy(ctx context.Context, authorityID string) (*dbAuthorityPolicy, error) {
  80. data, err := db.getDBAuthorityPolicyBytes(ctx, authorityID)
  81. if err != nil {
  82. return nil, err
  83. }
  84. dbap, err := db.unmarshalDBAuthorityPolicy(data)
  85. if err != nil {
  86. return nil, err
  87. }
  88. if dbap == nil {
  89. return nil, nil
  90. }
  91. if dbap.AuthorityID != authorityID {
  92. return nil, admin.NewError(admin.ErrorAuthorityMismatchType,
  93. "authority policy is not owned by authority %s", authorityID)
  94. }
  95. return dbap, nil
  96. }
  97. func (db *DB) CreateAuthorityPolicy(ctx context.Context, policy *linkedca.Policy) error {
  98. dbap := &dbAuthorityPolicy{
  99. ID: db.authorityID,
  100. AuthorityID: db.authorityID,
  101. Policy: linkedToDB(policy),
  102. }
  103. if err := db.save(ctx, dbap.ID, dbap, nil, "authority_policy", authorityPoliciesTable); err != nil {
  104. return admin.WrapErrorISE(err, "error creating authority policy")
  105. }
  106. return nil
  107. }
  108. func (db *DB) GetAuthorityPolicy(ctx context.Context) (*linkedca.Policy, error) {
  109. dbap, err := db.getDBAuthorityPolicy(ctx, db.authorityID)
  110. if err != nil {
  111. return nil, err
  112. }
  113. return dbap.convert(), nil
  114. }
  115. func (db *DB) UpdateAuthorityPolicy(ctx context.Context, policy *linkedca.Policy) error {
  116. old, err := db.getDBAuthorityPolicy(ctx, db.authorityID)
  117. if err != nil {
  118. return err
  119. }
  120. dbap := &dbAuthorityPolicy{
  121. ID: db.authorityID,
  122. AuthorityID: db.authorityID,
  123. Policy: linkedToDB(policy),
  124. }
  125. if err := db.save(ctx, dbap.ID, dbap, old, "authority_policy", authorityPoliciesTable); err != nil {
  126. return admin.WrapErrorISE(err, "error updating authority policy")
  127. }
  128. return nil
  129. }
  130. func (db *DB) DeleteAuthorityPolicy(ctx context.Context) error {
  131. old, err := db.getDBAuthorityPolicy(ctx, db.authorityID)
  132. if err != nil {
  133. return err
  134. }
  135. if err := db.save(ctx, old.ID, nil, old, "authority_policy", authorityPoliciesTable); err != nil {
  136. return admin.WrapErrorISE(err, "error deleting authority policy")
  137. }
  138. return nil
  139. }
  140. func dbToLinked(p *dbPolicy) *linkedca.Policy {
  141. if p == nil {
  142. return nil
  143. }
  144. r := &linkedca.Policy{}
  145. if x509 := p.X509; x509 != nil {
  146. r.X509 = &linkedca.X509Policy{}
  147. if allow := x509.Allow; allow != nil {
  148. r.X509.Allow = &linkedca.X509Names{}
  149. r.X509.Allow.Dns = allow.DNSDomains
  150. r.X509.Allow.Emails = allow.EmailAddresses
  151. r.X509.Allow.Ips = allow.IPRanges
  152. r.X509.Allow.Uris = allow.URIDomains
  153. r.X509.Allow.CommonNames = allow.CommonNames
  154. }
  155. if deny := x509.Deny; deny != nil {
  156. r.X509.Deny = &linkedca.X509Names{}
  157. r.X509.Deny.Dns = deny.DNSDomains
  158. r.X509.Deny.Emails = deny.EmailAddresses
  159. r.X509.Deny.Ips = deny.IPRanges
  160. r.X509.Deny.Uris = deny.URIDomains
  161. r.X509.Deny.CommonNames = deny.CommonNames
  162. }
  163. r.X509.AllowWildcardNames = x509.AllowWildcardNames
  164. }
  165. if ssh := p.SSH; ssh != nil {
  166. r.Ssh = &linkedca.SSHPolicy{}
  167. if host := ssh.Host; host != nil {
  168. r.Ssh.Host = &linkedca.SSHHostPolicy{}
  169. if allow := host.Allow; allow != nil {
  170. r.Ssh.Host.Allow = &linkedca.SSHHostNames{}
  171. r.Ssh.Host.Allow.Dns = allow.DNSDomains
  172. r.Ssh.Host.Allow.Ips = allow.IPRanges
  173. r.Ssh.Host.Allow.Principals = allow.Principals
  174. }
  175. if deny := host.Deny; deny != nil {
  176. r.Ssh.Host.Deny = &linkedca.SSHHostNames{}
  177. r.Ssh.Host.Deny.Dns = deny.DNSDomains
  178. r.Ssh.Host.Deny.Ips = deny.IPRanges
  179. r.Ssh.Host.Deny.Principals = deny.Principals
  180. }
  181. }
  182. if user := ssh.User; user != nil {
  183. r.Ssh.User = &linkedca.SSHUserPolicy{}
  184. if allow := user.Allow; allow != nil {
  185. r.Ssh.User.Allow = &linkedca.SSHUserNames{}
  186. r.Ssh.User.Allow.Emails = allow.EmailAddresses
  187. r.Ssh.User.Allow.Principals = allow.Principals
  188. }
  189. if deny := user.Deny; deny != nil {
  190. r.Ssh.User.Deny = &linkedca.SSHUserNames{}
  191. r.Ssh.User.Deny.Emails = deny.EmailAddresses
  192. r.Ssh.User.Deny.Principals = deny.Principals
  193. }
  194. }
  195. }
  196. return r
  197. }
  198. func linkedToDB(p *linkedca.Policy) *dbPolicy {
  199. if p == nil {
  200. return nil
  201. }
  202. // return early if x509 nor SSH is set
  203. if p.GetX509() == nil && p.GetSsh() == nil {
  204. return nil
  205. }
  206. r := &dbPolicy{}
  207. // fill x509 policy configuration
  208. if x509 := p.GetX509(); x509 != nil {
  209. r.X509 = &dbX509Policy{}
  210. if allow := x509.GetAllow(); allow != nil {
  211. r.X509.Allow = &dbX509Names{}
  212. if allow.Dns != nil {
  213. r.X509.Allow.DNSDomains = allow.Dns
  214. }
  215. if allow.Ips != nil {
  216. r.X509.Allow.IPRanges = allow.Ips
  217. }
  218. if allow.Emails != nil {
  219. r.X509.Allow.EmailAddresses = allow.Emails
  220. }
  221. if allow.Uris != nil {
  222. r.X509.Allow.URIDomains = allow.Uris
  223. }
  224. if allow.CommonNames != nil {
  225. r.X509.Allow.CommonNames = allow.CommonNames
  226. }
  227. }
  228. if deny := x509.GetDeny(); deny != nil {
  229. r.X509.Deny = &dbX509Names{}
  230. if deny.Dns != nil {
  231. r.X509.Deny.DNSDomains = deny.Dns
  232. }
  233. if deny.Ips != nil {
  234. r.X509.Deny.IPRanges = deny.Ips
  235. }
  236. if deny.Emails != nil {
  237. r.X509.Deny.EmailAddresses = deny.Emails
  238. }
  239. if deny.Uris != nil {
  240. r.X509.Deny.URIDomains = deny.Uris
  241. }
  242. if deny.CommonNames != nil {
  243. r.X509.Deny.CommonNames = deny.CommonNames
  244. }
  245. }
  246. r.X509.AllowWildcardNames = x509.GetAllowWildcardNames()
  247. }
  248. // fill ssh policy configuration
  249. if ssh := p.GetSsh(); ssh != nil {
  250. r.SSH = &dbSSHPolicy{}
  251. if host := ssh.GetHost(); host != nil {
  252. r.SSH.Host = &dbSSHHostPolicy{}
  253. if allow := host.GetAllow(); allow != nil {
  254. r.SSH.Host.Allow = &dbSSHHostNames{}
  255. if allow.Dns != nil {
  256. r.SSH.Host.Allow.DNSDomains = allow.Dns
  257. }
  258. if allow.Ips != nil {
  259. r.SSH.Host.Allow.IPRanges = allow.Ips
  260. }
  261. if allow.Principals != nil {
  262. r.SSH.Host.Allow.Principals = allow.Principals
  263. }
  264. }
  265. if deny := host.GetDeny(); deny != nil {
  266. r.SSH.Host.Deny = &dbSSHHostNames{}
  267. if deny.Dns != nil {
  268. r.SSH.Host.Deny.DNSDomains = deny.Dns
  269. }
  270. if deny.Ips != nil {
  271. r.SSH.Host.Deny.IPRanges = deny.Ips
  272. }
  273. if deny.Principals != nil {
  274. r.SSH.Host.Deny.Principals = deny.Principals
  275. }
  276. }
  277. }
  278. if user := ssh.GetUser(); user != nil {
  279. r.SSH.User = &dbSSHUserPolicy{}
  280. if allow := user.GetAllow(); allow != nil {
  281. r.SSH.User.Allow = &dbSSHUserNames{}
  282. if allow.Emails != nil {
  283. r.SSH.User.Allow.EmailAddresses = allow.Emails
  284. }
  285. if allow.Principals != nil {
  286. r.SSH.User.Allow.Principals = allow.Principals
  287. }
  288. }
  289. if deny := user.GetDeny(); deny != nil {
  290. r.SSH.User.Deny = &dbSSHUserNames{}
  291. if deny.Emails != nil {
  292. r.SSH.User.Deny.EmailAddresses = deny.Emails
  293. }
  294. if deny.Principals != nil {
  295. r.SSH.User.Deny.Principals = deny.Principals
  296. }
  297. }
  298. }
  299. }
  300. return r
  301. }