policy.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. package nosql
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/smallstep/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(_ 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. //nolint:nilnil // legacy
  72. return nil, nil
  73. }
  74. var dba = new(dbAuthorityPolicy)
  75. if err := json.Unmarshal(data, dba); err != nil {
  76. return nil, fmt.Errorf("error unmarshaling policy bytes into dbAuthorityPolicy: %w", err)
  77. }
  78. return dba, nil
  79. }
  80. func (db *DB) getDBAuthorityPolicy(ctx context.Context, authorityID string) (*dbAuthorityPolicy, error) {
  81. data, err := db.getDBAuthorityPolicyBytes(ctx, authorityID)
  82. if err != nil {
  83. return nil, err
  84. }
  85. dbap, err := db.unmarshalDBAuthorityPolicy(data)
  86. if err != nil {
  87. return nil, err
  88. }
  89. if dbap == nil {
  90. //nolint:nilnil // legacy
  91. return nil, nil
  92. }
  93. if dbap.AuthorityID != authorityID {
  94. return nil, admin.NewError(admin.ErrorAuthorityMismatchType,
  95. "authority policy is not owned by authority %s", authorityID)
  96. }
  97. return dbap, nil
  98. }
  99. func (db *DB) CreateAuthorityPolicy(ctx context.Context, policy *linkedca.Policy) error {
  100. dbap := &dbAuthorityPolicy{
  101. ID: db.authorityID,
  102. AuthorityID: db.authorityID,
  103. Policy: linkedToDB(policy),
  104. }
  105. if err := db.save(ctx, dbap.ID, dbap, nil, "authority_policy", authorityPoliciesTable); err != nil {
  106. return admin.WrapErrorISE(err, "error creating authority policy")
  107. }
  108. return nil
  109. }
  110. func (db *DB) GetAuthorityPolicy(ctx context.Context) (*linkedca.Policy, error) {
  111. dbap, err := db.getDBAuthorityPolicy(ctx, db.authorityID)
  112. if err != nil {
  113. return nil, err
  114. }
  115. return dbap.convert(), nil
  116. }
  117. func (db *DB) UpdateAuthorityPolicy(ctx context.Context, policy *linkedca.Policy) error {
  118. old, err := db.getDBAuthorityPolicy(ctx, db.authorityID)
  119. if err != nil {
  120. return err
  121. }
  122. dbap := &dbAuthorityPolicy{
  123. ID: db.authorityID,
  124. AuthorityID: db.authorityID,
  125. Policy: linkedToDB(policy),
  126. }
  127. if err := db.save(ctx, dbap.ID, dbap, old, "authority_policy", authorityPoliciesTable); err != nil {
  128. return admin.WrapErrorISE(err, "error updating authority policy")
  129. }
  130. return nil
  131. }
  132. func (db *DB) DeleteAuthorityPolicy(ctx context.Context) error {
  133. old, err := db.getDBAuthorityPolicy(ctx, db.authorityID)
  134. if err != nil {
  135. return err
  136. }
  137. if err := db.save(ctx, old.ID, nil, old, "authority_policy", authorityPoliciesTable); err != nil {
  138. return admin.WrapErrorISE(err, "error deleting authority policy")
  139. }
  140. return nil
  141. }
  142. func dbToLinked(p *dbPolicy) *linkedca.Policy {
  143. if p == nil {
  144. return nil
  145. }
  146. r := &linkedca.Policy{}
  147. if x509 := p.X509; x509 != nil {
  148. r.X509 = &linkedca.X509Policy{}
  149. if allow := x509.Allow; allow != nil {
  150. r.X509.Allow = &linkedca.X509Names{}
  151. r.X509.Allow.Dns = allow.DNSDomains
  152. r.X509.Allow.Emails = allow.EmailAddresses
  153. r.X509.Allow.Ips = allow.IPRanges
  154. r.X509.Allow.Uris = allow.URIDomains
  155. r.X509.Allow.CommonNames = allow.CommonNames
  156. }
  157. if deny := x509.Deny; deny != nil {
  158. r.X509.Deny = &linkedca.X509Names{}
  159. r.X509.Deny.Dns = deny.DNSDomains
  160. r.X509.Deny.Emails = deny.EmailAddresses
  161. r.X509.Deny.Ips = deny.IPRanges
  162. r.X509.Deny.Uris = deny.URIDomains
  163. r.X509.Deny.CommonNames = deny.CommonNames
  164. }
  165. r.X509.AllowWildcardNames = x509.AllowWildcardNames
  166. }
  167. if ssh := p.SSH; ssh != nil {
  168. r.Ssh = &linkedca.SSHPolicy{}
  169. if host := ssh.Host; host != nil {
  170. r.Ssh.Host = &linkedca.SSHHostPolicy{}
  171. if allow := host.Allow; allow != nil {
  172. r.Ssh.Host.Allow = &linkedca.SSHHostNames{}
  173. r.Ssh.Host.Allow.Dns = allow.DNSDomains
  174. r.Ssh.Host.Allow.Ips = allow.IPRanges
  175. r.Ssh.Host.Allow.Principals = allow.Principals
  176. }
  177. if deny := host.Deny; deny != nil {
  178. r.Ssh.Host.Deny = &linkedca.SSHHostNames{}
  179. r.Ssh.Host.Deny.Dns = deny.DNSDomains
  180. r.Ssh.Host.Deny.Ips = deny.IPRanges
  181. r.Ssh.Host.Deny.Principals = deny.Principals
  182. }
  183. }
  184. if user := ssh.User; user != nil {
  185. r.Ssh.User = &linkedca.SSHUserPolicy{}
  186. if allow := user.Allow; allow != nil {
  187. r.Ssh.User.Allow = &linkedca.SSHUserNames{}
  188. r.Ssh.User.Allow.Emails = allow.EmailAddresses
  189. r.Ssh.User.Allow.Principals = allow.Principals
  190. }
  191. if deny := user.Deny; deny != nil {
  192. r.Ssh.User.Deny = &linkedca.SSHUserNames{}
  193. r.Ssh.User.Deny.Emails = deny.EmailAddresses
  194. r.Ssh.User.Deny.Principals = deny.Principals
  195. }
  196. }
  197. }
  198. return r
  199. }
  200. func linkedToDB(p *linkedca.Policy) *dbPolicy {
  201. if p == nil {
  202. return nil
  203. }
  204. // return early if x509 nor SSH is set
  205. if p.GetX509() == nil && p.GetSsh() == nil {
  206. return nil
  207. }
  208. r := &dbPolicy{}
  209. // fill x509 policy configuration
  210. if x509 := p.GetX509(); x509 != nil {
  211. r.X509 = &dbX509Policy{}
  212. if allow := x509.GetAllow(); allow != nil {
  213. r.X509.Allow = &dbX509Names{}
  214. if allow.Dns != nil {
  215. r.X509.Allow.DNSDomains = allow.Dns
  216. }
  217. if allow.Ips != nil {
  218. r.X509.Allow.IPRanges = allow.Ips
  219. }
  220. if allow.Emails != nil {
  221. r.X509.Allow.EmailAddresses = allow.Emails
  222. }
  223. if allow.Uris != nil {
  224. r.X509.Allow.URIDomains = allow.Uris
  225. }
  226. if allow.CommonNames != nil {
  227. r.X509.Allow.CommonNames = allow.CommonNames
  228. }
  229. }
  230. if deny := x509.GetDeny(); deny != nil {
  231. r.X509.Deny = &dbX509Names{}
  232. if deny.Dns != nil {
  233. r.X509.Deny.DNSDomains = deny.Dns
  234. }
  235. if deny.Ips != nil {
  236. r.X509.Deny.IPRanges = deny.Ips
  237. }
  238. if deny.Emails != nil {
  239. r.X509.Deny.EmailAddresses = deny.Emails
  240. }
  241. if deny.Uris != nil {
  242. r.X509.Deny.URIDomains = deny.Uris
  243. }
  244. if deny.CommonNames != nil {
  245. r.X509.Deny.CommonNames = deny.CommonNames
  246. }
  247. }
  248. r.X509.AllowWildcardNames = x509.GetAllowWildcardNames()
  249. }
  250. // fill ssh policy configuration
  251. if ssh := p.GetSsh(); ssh != nil {
  252. r.SSH = &dbSSHPolicy{}
  253. if host := ssh.GetHost(); host != nil {
  254. r.SSH.Host = &dbSSHHostPolicy{}
  255. if allow := host.GetAllow(); allow != nil {
  256. r.SSH.Host.Allow = &dbSSHHostNames{}
  257. if allow.Dns != nil {
  258. r.SSH.Host.Allow.DNSDomains = allow.Dns
  259. }
  260. if allow.Ips != nil {
  261. r.SSH.Host.Allow.IPRanges = allow.Ips
  262. }
  263. if allow.Principals != nil {
  264. r.SSH.Host.Allow.Principals = allow.Principals
  265. }
  266. }
  267. if deny := host.GetDeny(); deny != nil {
  268. r.SSH.Host.Deny = &dbSSHHostNames{}
  269. if deny.Dns != nil {
  270. r.SSH.Host.Deny.DNSDomains = deny.Dns
  271. }
  272. if deny.Ips != nil {
  273. r.SSH.Host.Deny.IPRanges = deny.Ips
  274. }
  275. if deny.Principals != nil {
  276. r.SSH.Host.Deny.Principals = deny.Principals
  277. }
  278. }
  279. }
  280. if user := ssh.GetUser(); user != nil {
  281. r.SSH.User = &dbSSHUserPolicy{}
  282. if allow := user.GetAllow(); allow != nil {
  283. r.SSH.User.Allow = &dbSSHUserNames{}
  284. if allow.Emails != nil {
  285. r.SSH.User.Allow.EmailAddresses = allow.Emails
  286. }
  287. if allow.Principals != nil {
  288. r.SSH.User.Allow.Principals = allow.Principals
  289. }
  290. }
  291. if deny := user.GetDeny(); deny != nil {
  292. r.SSH.User.Deny = &dbSSHUserNames{}
  293. if deny.Emails != nil {
  294. r.SSH.User.Deny.EmailAddresses = deny.Emails
  295. }
  296. if deny.Principals != nil {
  297. r.SSH.User.Deny.Principals = deny.Principals
  298. }
  299. }
  300. }
  301. }
  302. return r
  303. }