4
0

linker_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. package api
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "testing"
  7. "github.com/smallstep/assert"
  8. "github.com/smallstep/certificates/acme"
  9. )
  10. func TestLinker_GetUnescapedPathSuffix(t *testing.T) {
  11. dns := "ca.smallstep.com"
  12. prefix := "acme"
  13. linker := NewLinker(dns, prefix)
  14. getPath := linker.GetUnescapedPathSuffix
  15. assert.Equals(t, getPath(NewNonceLinkType, "{provisionerID}"), "/{provisionerID}/new-nonce")
  16. assert.Equals(t, getPath(DirectoryLinkType, "{provisionerID}"), "/{provisionerID}/directory")
  17. assert.Equals(t, getPath(NewAccountLinkType, "{provisionerID}"), "/{provisionerID}/new-account")
  18. assert.Equals(t, getPath(AccountLinkType, "{provisionerID}", "{accID}"), "/{provisionerID}/account/{accID}")
  19. assert.Equals(t, getPath(KeyChangeLinkType, "{provisionerID}"), "/{provisionerID}/key-change")
  20. assert.Equals(t, getPath(NewOrderLinkType, "{provisionerID}"), "/{provisionerID}/new-order")
  21. assert.Equals(t, getPath(OrderLinkType, "{provisionerID}", "{ordID}"), "/{provisionerID}/order/{ordID}")
  22. assert.Equals(t, getPath(OrdersByAccountLinkType, "{provisionerID}", "{accID}"), "/{provisionerID}/account/{accID}/orders")
  23. assert.Equals(t, getPath(FinalizeLinkType, "{provisionerID}", "{ordID}"), "/{provisionerID}/order/{ordID}/finalize")
  24. assert.Equals(t, getPath(AuthzLinkType, "{provisionerID}", "{authzID}"), "/{provisionerID}/authz/{authzID}")
  25. assert.Equals(t, getPath(ChallengeLinkType, "{provisionerID}", "{authzID}", "{chID}"), "/{provisionerID}/challenge/{authzID}/{chID}")
  26. assert.Equals(t, getPath(CertificateLinkType, "{provisionerID}", "{certID}"), "/{provisionerID}/certificate/{certID}")
  27. }
  28. func TestLinker_GetLink(t *testing.T) {
  29. dns := "ca.smallstep.com"
  30. prefix := "acme"
  31. linker := NewLinker(dns, prefix)
  32. id := "1234"
  33. prov := newProv()
  34. escProvName := url.PathEscape(prov.GetName())
  35. baseURL := &url.URL{Scheme: "https", Host: "test.ca.smallstep.com"}
  36. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  37. ctx = context.WithValue(ctx, baseURLContextKey, baseURL)
  38. // No provisioner and no BaseURL from request
  39. assert.Equals(t, linker.GetLink(context.Background(), NewNonceLinkType), fmt.Sprintf("%s/acme/%s/new-nonce", "https://ca.smallstep.com", ""))
  40. // Provisioner: yes, BaseURL: no
  41. assert.Equals(t, linker.GetLink(context.WithValue(context.Background(), provisionerContextKey, prov), NewNonceLinkType), fmt.Sprintf("%s/acme/%s/new-nonce", "https://ca.smallstep.com", escProvName))
  42. // Provisioner: no, BaseURL: yes
  43. assert.Equals(t, linker.GetLink(context.WithValue(context.Background(), baseURLContextKey, baseURL), NewNonceLinkType), fmt.Sprintf("%s/acme/%s/new-nonce", "https://test.ca.smallstep.com", ""))
  44. assert.Equals(t, linker.GetLink(ctx, NewNonceLinkType), fmt.Sprintf("%s/acme/%s/new-nonce", baseURL, escProvName))
  45. assert.Equals(t, linker.GetLink(ctx, NewNonceLinkType), fmt.Sprintf("%s/acme/%s/new-nonce", baseURL, escProvName))
  46. assert.Equals(t, linker.GetLink(ctx, NewAccountLinkType), fmt.Sprintf("%s/acme/%s/new-account", baseURL, escProvName))
  47. assert.Equals(t, linker.GetLink(ctx, AccountLinkType, id), fmt.Sprintf("%s/acme/%s/account/1234", baseURL, escProvName))
  48. assert.Equals(t, linker.GetLink(ctx, NewOrderLinkType), fmt.Sprintf("%s/acme/%s/new-order", baseURL, escProvName))
  49. assert.Equals(t, linker.GetLink(ctx, OrderLinkType, id), fmt.Sprintf("%s/acme/%s/order/1234", baseURL, escProvName))
  50. assert.Equals(t, linker.GetLink(ctx, OrdersByAccountLinkType, id), fmt.Sprintf("%s/acme/%s/account/1234/orders", baseURL, escProvName))
  51. assert.Equals(t, linker.GetLink(ctx, FinalizeLinkType, id), fmt.Sprintf("%s/acme/%s/order/1234/finalize", baseURL, escProvName))
  52. assert.Equals(t, linker.GetLink(ctx, NewAuthzLinkType), fmt.Sprintf("%s/acme/%s/new-authz", baseURL, escProvName))
  53. assert.Equals(t, linker.GetLink(ctx, AuthzLinkType, id), fmt.Sprintf("%s/acme/%s/authz/1234", baseURL, escProvName))
  54. assert.Equals(t, linker.GetLink(ctx, DirectoryLinkType), fmt.Sprintf("%s/acme/%s/directory", baseURL, escProvName))
  55. assert.Equals(t, linker.GetLink(ctx, RevokeCertLinkType, id), fmt.Sprintf("%s/acme/%s/revoke-cert", baseURL, escProvName))
  56. assert.Equals(t, linker.GetLink(ctx, KeyChangeLinkType), fmt.Sprintf("%s/acme/%s/key-change", baseURL, escProvName))
  57. assert.Equals(t, linker.GetLink(ctx, ChallengeLinkType, id, id), fmt.Sprintf("%s/acme/%s/challenge/%s/%s", baseURL, escProvName, id, id))
  58. assert.Equals(t, linker.GetLink(ctx, CertificateLinkType, id), fmt.Sprintf("%s/acme/%s/certificate/1234", baseURL, escProvName))
  59. }
  60. func TestLinker_LinkOrder(t *testing.T) {
  61. baseURL := &url.URL{Scheme: "https", Host: "test.ca.smallstep.com"}
  62. prov := newProv()
  63. provName := url.PathEscape(prov.GetName())
  64. ctx := context.WithValue(context.Background(), baseURLContextKey, baseURL)
  65. ctx = context.WithValue(ctx, provisionerContextKey, prov)
  66. oid := "orderID"
  67. certID := "certID"
  68. linkerPrefix := "acme"
  69. l := NewLinker("dns", linkerPrefix)
  70. type test struct {
  71. o *acme.Order
  72. validate func(o *acme.Order)
  73. }
  74. var tests = map[string]test{
  75. "no-authz-and-no-cert": {
  76. o: &acme.Order{
  77. ID: oid,
  78. },
  79. validate: func(o *acme.Order) {
  80. assert.Equals(t, o.FinalizeURL, fmt.Sprintf("%s/%s/%s/order/%s/finalize", baseURL, linkerPrefix, provName, oid))
  81. assert.Equals(t, o.AuthorizationURLs, []string{})
  82. assert.Equals(t, o.CertificateURL, "")
  83. },
  84. },
  85. "one-authz-and-cert": {
  86. o: &acme.Order{
  87. ID: oid,
  88. CertificateID: certID,
  89. AuthorizationIDs: []string{"foo"},
  90. },
  91. validate: func(o *acme.Order) {
  92. assert.Equals(t, o.FinalizeURL, fmt.Sprintf("%s/%s/%s/order/%s/finalize", baseURL, linkerPrefix, provName, oid))
  93. assert.Equals(t, o.AuthorizationURLs, []string{
  94. fmt.Sprintf("%s/%s/%s/authz/%s", baseURL, linkerPrefix, provName, "foo"),
  95. })
  96. assert.Equals(t, o.CertificateURL, fmt.Sprintf("%s/%s/%s/certificate/%s", baseURL, linkerPrefix, provName, certID))
  97. },
  98. },
  99. "many-authz": {
  100. o: &acme.Order{
  101. ID: oid,
  102. CertificateID: certID,
  103. AuthorizationIDs: []string{"foo", "bar", "zap"},
  104. },
  105. validate: func(o *acme.Order) {
  106. assert.Equals(t, o.FinalizeURL, fmt.Sprintf("%s/%s/%s/order/%s/finalize", baseURL, linkerPrefix, provName, oid))
  107. assert.Equals(t, o.AuthorizationURLs, []string{
  108. fmt.Sprintf("%s/%s/%s/authz/%s", baseURL, linkerPrefix, provName, "foo"),
  109. fmt.Sprintf("%s/%s/%s/authz/%s", baseURL, linkerPrefix, provName, "bar"),
  110. fmt.Sprintf("%s/%s/%s/authz/%s", baseURL, linkerPrefix, provName, "zap"),
  111. })
  112. assert.Equals(t, o.CertificateURL, fmt.Sprintf("%s/%s/%s/certificate/%s", baseURL, linkerPrefix, provName, certID))
  113. },
  114. },
  115. }
  116. for name, tc := range tests {
  117. t.Run(name, func(t *testing.T) {
  118. l.LinkOrder(ctx, tc.o)
  119. tc.validate(tc.o)
  120. })
  121. }
  122. }
  123. func TestLinker_LinkAccount(t *testing.T) {
  124. baseURL := &url.URL{Scheme: "https", Host: "test.ca.smallstep.com"}
  125. prov := newProv()
  126. provName := url.PathEscape(prov.GetName())
  127. ctx := context.WithValue(context.Background(), baseURLContextKey, baseURL)
  128. ctx = context.WithValue(ctx, provisionerContextKey, prov)
  129. accID := "accountID"
  130. linkerPrefix := "acme"
  131. l := NewLinker("dns", linkerPrefix)
  132. type test struct {
  133. a *acme.Account
  134. validate func(o *acme.Account)
  135. }
  136. var tests = map[string]test{
  137. "ok": {
  138. a: &acme.Account{
  139. ID: accID,
  140. },
  141. validate: func(a *acme.Account) {
  142. assert.Equals(t, a.OrdersURL, fmt.Sprintf("%s/%s/%s/account/%s/orders", baseURL, linkerPrefix, provName, accID))
  143. },
  144. },
  145. }
  146. for name, tc := range tests {
  147. t.Run(name, func(t *testing.T) {
  148. l.LinkAccount(ctx, tc.a)
  149. tc.validate(tc.a)
  150. })
  151. }
  152. }
  153. func TestLinker_LinkChallenge(t *testing.T) {
  154. baseURL := &url.URL{Scheme: "https", Host: "test.ca.smallstep.com"}
  155. prov := newProv()
  156. provName := url.PathEscape(prov.GetName())
  157. ctx := context.WithValue(context.Background(), baseURLContextKey, baseURL)
  158. ctx = context.WithValue(ctx, provisionerContextKey, prov)
  159. chID := "chID"
  160. azID := "azID"
  161. linkerPrefix := "acme"
  162. l := NewLinker("dns", linkerPrefix)
  163. type test struct {
  164. ch *acme.Challenge
  165. validate func(o *acme.Challenge)
  166. }
  167. var tests = map[string]test{
  168. "ok": {
  169. ch: &acme.Challenge{
  170. ID: chID,
  171. },
  172. validate: func(ch *acme.Challenge) {
  173. assert.Equals(t, ch.URL, fmt.Sprintf("%s/%s/%s/challenge/%s/%s", baseURL, linkerPrefix, provName, azID, ch.ID))
  174. },
  175. },
  176. }
  177. for name, tc := range tests {
  178. t.Run(name, func(t *testing.T) {
  179. l.LinkChallenge(ctx, tc.ch, azID)
  180. tc.validate(tc.ch)
  181. })
  182. }
  183. }
  184. func TestLinker_LinkAuthorization(t *testing.T) {
  185. baseURL := &url.URL{Scheme: "https", Host: "test.ca.smallstep.com"}
  186. prov := newProv()
  187. provName := url.PathEscape(prov.GetName())
  188. ctx := context.WithValue(context.Background(), baseURLContextKey, baseURL)
  189. ctx = context.WithValue(ctx, provisionerContextKey, prov)
  190. chID0 := "chID-0"
  191. chID1 := "chID-1"
  192. chID2 := "chID-2"
  193. azID := "azID"
  194. linkerPrefix := "acme"
  195. l := NewLinker("dns", linkerPrefix)
  196. type test struct {
  197. az *acme.Authorization
  198. validate func(o *acme.Authorization)
  199. }
  200. var tests = map[string]test{
  201. "ok": {
  202. az: &acme.Authorization{
  203. ID: azID,
  204. Challenges: []*acme.Challenge{
  205. {ID: chID0},
  206. {ID: chID1},
  207. {ID: chID2},
  208. },
  209. },
  210. validate: func(az *acme.Authorization) {
  211. assert.Equals(t, az.Challenges[0].URL, fmt.Sprintf("%s/%s/%s/challenge/%s/%s", baseURL, linkerPrefix, provName, az.ID, chID0))
  212. assert.Equals(t, az.Challenges[1].URL, fmt.Sprintf("%s/%s/%s/challenge/%s/%s", baseURL, linkerPrefix, provName, az.ID, chID1))
  213. assert.Equals(t, az.Challenges[2].URL, fmt.Sprintf("%s/%s/%s/challenge/%s/%s", baseURL, linkerPrefix, provName, az.ID, chID2))
  214. },
  215. },
  216. }
  217. for name, tc := range tests {
  218. t.Run(name, func(t *testing.T) {
  219. l.LinkAuthorization(ctx, tc.az)
  220. tc.validate(tc.az)
  221. })
  222. }
  223. }
  224. func TestLinker_LinkOrdersByAccountID(t *testing.T) {
  225. baseURL := &url.URL{Scheme: "https", Host: "test.ca.smallstep.com"}
  226. prov := newProv()
  227. provName := url.PathEscape(prov.GetName())
  228. ctx := context.WithValue(context.Background(), baseURLContextKey, baseURL)
  229. ctx = context.WithValue(ctx, provisionerContextKey, prov)
  230. linkerPrefix := "acme"
  231. l := NewLinker("dns", linkerPrefix)
  232. type test struct {
  233. oids []string
  234. }
  235. var tests = map[string]test{
  236. "ok": {
  237. oids: []string{"foo", "bar", "baz"},
  238. },
  239. }
  240. for name, tc := range tests {
  241. t.Run(name, func(t *testing.T) {
  242. l.LinkOrdersByAccountID(ctx, tc.oids)
  243. assert.Equals(t, tc.oids, []string{
  244. fmt.Sprintf("%s/%s/%s/order/%s", baseURL, linkerPrefix, provName, "foo"),
  245. fmt.Sprintf("%s/%s/%s/order/%s", baseURL, linkerPrefix, provName, "bar"),
  246. fmt.Sprintf("%s/%s/%s/order/%s", baseURL, linkerPrefix, provName, "baz"),
  247. })
  248. })
  249. }
  250. }