linker_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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_DNS(t *testing.T) {
  29. prov := newProv()
  30. escProvName := url.PathEscape(prov.GetName())
  31. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  32. type test struct {
  33. name string
  34. dns string
  35. prefix string
  36. expectedDirectoryLink string
  37. }
  38. tests := []test{
  39. {
  40. name: "domain",
  41. dns: "ca.smallstep.com",
  42. prefix: "acme",
  43. expectedDirectoryLink: fmt.Sprintf("https://ca.smallstep.com/acme/%s/directory", escProvName),
  44. },
  45. {
  46. name: "domain-port",
  47. dns: "ca.smallstep.com:8443",
  48. prefix: "acme",
  49. expectedDirectoryLink: fmt.Sprintf("https://ca.smallstep.com:8443/acme/%s/directory", escProvName),
  50. },
  51. {
  52. name: "ipv4",
  53. dns: "127.0.0.1",
  54. prefix: "acme",
  55. expectedDirectoryLink: fmt.Sprintf("https://127.0.0.1/acme/%s/directory", escProvName),
  56. },
  57. {
  58. name: "ipv4-port",
  59. dns: "127.0.0.1:8443",
  60. prefix: "acme",
  61. expectedDirectoryLink: fmt.Sprintf("https://127.0.0.1:8443/acme/%s/directory", escProvName),
  62. },
  63. {
  64. name: "ipv6",
  65. dns: "[::1]",
  66. prefix: "acme",
  67. expectedDirectoryLink: fmt.Sprintf("https://[::1]/acme/%s/directory", escProvName),
  68. },
  69. {
  70. name: "ipv6-port",
  71. dns: "[::1]:8443",
  72. prefix: "acme",
  73. expectedDirectoryLink: fmt.Sprintf("https://[::1]:8443/acme/%s/directory", escProvName),
  74. },
  75. {
  76. name: "ipv6-no-brackets",
  77. dns: "::1",
  78. prefix: "acme",
  79. expectedDirectoryLink: fmt.Sprintf("https://[::1]/acme/%s/directory", escProvName),
  80. },
  81. {
  82. name: "ipv6-port-no-brackets",
  83. dns: "::1:8443",
  84. prefix: "acme",
  85. expectedDirectoryLink: fmt.Sprintf("https://[::1]:8443/acme/%s/directory", escProvName),
  86. },
  87. {
  88. name: "ipv6-long-no-brackets",
  89. dns: "2001:0db8:85a3:0000:0000:8a2e:0370:7334",
  90. prefix: "acme",
  91. expectedDirectoryLink: fmt.Sprintf("https://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]/acme/%s/directory", escProvName),
  92. },
  93. {
  94. name: "ipv6-long-port-no-brackets",
  95. dns: "2001:0db8:85a3:0000:0000:8a2e:0370:7334:8443",
  96. prefix: "acme",
  97. expectedDirectoryLink: fmt.Sprintf("https://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:8443/acme/%s/directory", escProvName),
  98. },
  99. }
  100. for _, tt := range tests {
  101. t.Run(tt.name, func(t *testing.T) {
  102. linker := NewLinker(tt.dns, tt.prefix)
  103. assert.Equals(t, tt.expectedDirectoryLink, linker.GetLink(ctx, DirectoryLinkType))
  104. })
  105. }
  106. }
  107. func TestLinker_GetLink(t *testing.T) {
  108. dns := "ca.smallstep.com"
  109. prefix := "acme"
  110. linker := NewLinker(dns, prefix)
  111. id := "1234"
  112. prov := newProv()
  113. escProvName := url.PathEscape(prov.GetName())
  114. baseURL := &url.URL{Scheme: "https", Host: "test.ca.smallstep.com"}
  115. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  116. ctx = context.WithValue(ctx, baseURLContextKey, baseURL)
  117. // No provisioner and no BaseURL from request
  118. assert.Equals(t, linker.GetLink(context.Background(), NewNonceLinkType), fmt.Sprintf("%s/acme/%s/new-nonce", "https://ca.smallstep.com", ""))
  119. // Provisioner: yes, BaseURL: no
  120. assert.Equals(t, linker.GetLink(context.WithValue(context.Background(), provisionerContextKey, prov), NewNonceLinkType), fmt.Sprintf("%s/acme/%s/new-nonce", "https://ca.smallstep.com", escProvName))
  121. // Provisioner: no, BaseURL: yes
  122. assert.Equals(t, linker.GetLink(context.WithValue(context.Background(), baseURLContextKey, baseURL), NewNonceLinkType), fmt.Sprintf("%s/acme/%s/new-nonce", "https://test.ca.smallstep.com", ""))
  123. assert.Equals(t, linker.GetLink(ctx, NewNonceLinkType), fmt.Sprintf("%s/acme/%s/new-nonce", baseURL, escProvName))
  124. assert.Equals(t, linker.GetLink(ctx, NewNonceLinkType), fmt.Sprintf("%s/acme/%s/new-nonce", baseURL, escProvName))
  125. assert.Equals(t, linker.GetLink(ctx, NewAccountLinkType), fmt.Sprintf("%s/acme/%s/new-account", baseURL, escProvName))
  126. assert.Equals(t, linker.GetLink(ctx, AccountLinkType, id), fmt.Sprintf("%s/acme/%s/account/1234", baseURL, escProvName))
  127. assert.Equals(t, linker.GetLink(ctx, NewOrderLinkType), fmt.Sprintf("%s/acme/%s/new-order", baseURL, escProvName))
  128. assert.Equals(t, linker.GetLink(ctx, OrderLinkType, id), fmt.Sprintf("%s/acme/%s/order/1234", baseURL, escProvName))
  129. assert.Equals(t, linker.GetLink(ctx, OrdersByAccountLinkType, id), fmt.Sprintf("%s/acme/%s/account/1234/orders", baseURL, escProvName))
  130. assert.Equals(t, linker.GetLink(ctx, FinalizeLinkType, id), fmt.Sprintf("%s/acme/%s/order/1234/finalize", baseURL, escProvName))
  131. assert.Equals(t, linker.GetLink(ctx, NewAuthzLinkType), fmt.Sprintf("%s/acme/%s/new-authz", baseURL, escProvName))
  132. assert.Equals(t, linker.GetLink(ctx, AuthzLinkType, id), fmt.Sprintf("%s/acme/%s/authz/1234", baseURL, escProvName))
  133. assert.Equals(t, linker.GetLink(ctx, DirectoryLinkType), fmt.Sprintf("%s/acme/%s/directory", baseURL, escProvName))
  134. assert.Equals(t, linker.GetLink(ctx, RevokeCertLinkType, id), fmt.Sprintf("%s/acme/%s/revoke-cert", baseURL, escProvName))
  135. assert.Equals(t, linker.GetLink(ctx, KeyChangeLinkType), fmt.Sprintf("%s/acme/%s/key-change", baseURL, escProvName))
  136. assert.Equals(t, linker.GetLink(ctx, ChallengeLinkType, id, id), fmt.Sprintf("%s/acme/%s/challenge/%s/%s", baseURL, escProvName, id, id))
  137. assert.Equals(t, linker.GetLink(ctx, CertificateLinkType, id), fmt.Sprintf("%s/acme/%s/certificate/1234", baseURL, escProvName))
  138. }
  139. func TestLinker_LinkOrder(t *testing.T) {
  140. baseURL := &url.URL{Scheme: "https", Host: "test.ca.smallstep.com"}
  141. prov := newProv()
  142. provName := url.PathEscape(prov.GetName())
  143. ctx := context.WithValue(context.Background(), baseURLContextKey, baseURL)
  144. ctx = context.WithValue(ctx, provisionerContextKey, prov)
  145. oid := "orderID"
  146. certID := "certID"
  147. linkerPrefix := "acme"
  148. l := NewLinker("dns", linkerPrefix)
  149. type test struct {
  150. o *acme.Order
  151. validate func(o *acme.Order)
  152. }
  153. var tests = map[string]test{
  154. "no-authz-and-no-cert": {
  155. o: &acme.Order{
  156. ID: oid,
  157. },
  158. validate: func(o *acme.Order) {
  159. assert.Equals(t, o.FinalizeURL, fmt.Sprintf("%s/%s/%s/order/%s/finalize", baseURL, linkerPrefix, provName, oid))
  160. assert.Equals(t, o.AuthorizationURLs, []string{})
  161. assert.Equals(t, o.CertificateURL, "")
  162. },
  163. },
  164. "one-authz-and-cert": {
  165. o: &acme.Order{
  166. ID: oid,
  167. CertificateID: certID,
  168. AuthorizationIDs: []string{"foo"},
  169. },
  170. validate: func(o *acme.Order) {
  171. assert.Equals(t, o.FinalizeURL, fmt.Sprintf("%s/%s/%s/order/%s/finalize", baseURL, linkerPrefix, provName, oid))
  172. assert.Equals(t, o.AuthorizationURLs, []string{
  173. fmt.Sprintf("%s/%s/%s/authz/%s", baseURL, linkerPrefix, provName, "foo"),
  174. })
  175. assert.Equals(t, o.CertificateURL, fmt.Sprintf("%s/%s/%s/certificate/%s", baseURL, linkerPrefix, provName, certID))
  176. },
  177. },
  178. "many-authz": {
  179. o: &acme.Order{
  180. ID: oid,
  181. CertificateID: certID,
  182. AuthorizationIDs: []string{"foo", "bar", "zap"},
  183. },
  184. validate: func(o *acme.Order) {
  185. assert.Equals(t, o.FinalizeURL, fmt.Sprintf("%s/%s/%s/order/%s/finalize", baseURL, linkerPrefix, provName, oid))
  186. assert.Equals(t, o.AuthorizationURLs, []string{
  187. fmt.Sprintf("%s/%s/%s/authz/%s", baseURL, linkerPrefix, provName, "foo"),
  188. fmt.Sprintf("%s/%s/%s/authz/%s", baseURL, linkerPrefix, provName, "bar"),
  189. fmt.Sprintf("%s/%s/%s/authz/%s", baseURL, linkerPrefix, provName, "zap"),
  190. })
  191. assert.Equals(t, o.CertificateURL, fmt.Sprintf("%s/%s/%s/certificate/%s", baseURL, linkerPrefix, provName, certID))
  192. },
  193. },
  194. }
  195. for name, tc := range tests {
  196. t.Run(name, func(t *testing.T) {
  197. l.LinkOrder(ctx, tc.o)
  198. tc.validate(tc.o)
  199. })
  200. }
  201. }
  202. func TestLinker_LinkAccount(t *testing.T) {
  203. baseURL := &url.URL{Scheme: "https", Host: "test.ca.smallstep.com"}
  204. prov := newProv()
  205. provName := url.PathEscape(prov.GetName())
  206. ctx := context.WithValue(context.Background(), baseURLContextKey, baseURL)
  207. ctx = context.WithValue(ctx, provisionerContextKey, prov)
  208. accID := "accountID"
  209. linkerPrefix := "acme"
  210. l := NewLinker("dns", linkerPrefix)
  211. type test struct {
  212. a *acme.Account
  213. validate func(o *acme.Account)
  214. }
  215. var tests = map[string]test{
  216. "ok": {
  217. a: &acme.Account{
  218. ID: accID,
  219. },
  220. validate: func(a *acme.Account) {
  221. assert.Equals(t, a.OrdersURL, fmt.Sprintf("%s/%s/%s/account/%s/orders", baseURL, linkerPrefix, provName, accID))
  222. },
  223. },
  224. }
  225. for name, tc := range tests {
  226. t.Run(name, func(t *testing.T) {
  227. l.LinkAccount(ctx, tc.a)
  228. tc.validate(tc.a)
  229. })
  230. }
  231. }
  232. func TestLinker_LinkChallenge(t *testing.T) {
  233. baseURL := &url.URL{Scheme: "https", Host: "test.ca.smallstep.com"}
  234. prov := newProv()
  235. provName := url.PathEscape(prov.GetName())
  236. ctx := context.WithValue(context.Background(), baseURLContextKey, baseURL)
  237. ctx = context.WithValue(ctx, provisionerContextKey, prov)
  238. chID := "chID"
  239. azID := "azID"
  240. linkerPrefix := "acme"
  241. l := NewLinker("dns", linkerPrefix)
  242. type test struct {
  243. ch *acme.Challenge
  244. validate func(o *acme.Challenge)
  245. }
  246. var tests = map[string]test{
  247. "ok": {
  248. ch: &acme.Challenge{
  249. ID: chID,
  250. },
  251. validate: func(ch *acme.Challenge) {
  252. assert.Equals(t, ch.URL, fmt.Sprintf("%s/%s/%s/challenge/%s/%s", baseURL, linkerPrefix, provName, azID, ch.ID))
  253. },
  254. },
  255. }
  256. for name, tc := range tests {
  257. t.Run(name, func(t *testing.T) {
  258. l.LinkChallenge(ctx, tc.ch, azID)
  259. tc.validate(tc.ch)
  260. })
  261. }
  262. }
  263. func TestLinker_LinkAuthorization(t *testing.T) {
  264. baseURL := &url.URL{Scheme: "https", Host: "test.ca.smallstep.com"}
  265. prov := newProv()
  266. provName := url.PathEscape(prov.GetName())
  267. ctx := context.WithValue(context.Background(), baseURLContextKey, baseURL)
  268. ctx = context.WithValue(ctx, provisionerContextKey, prov)
  269. chID0 := "chID-0"
  270. chID1 := "chID-1"
  271. chID2 := "chID-2"
  272. azID := "azID"
  273. linkerPrefix := "acme"
  274. l := NewLinker("dns", linkerPrefix)
  275. type test struct {
  276. az *acme.Authorization
  277. validate func(o *acme.Authorization)
  278. }
  279. var tests = map[string]test{
  280. "ok": {
  281. az: &acme.Authorization{
  282. ID: azID,
  283. Challenges: []*acme.Challenge{
  284. {ID: chID0},
  285. {ID: chID1},
  286. {ID: chID2},
  287. },
  288. },
  289. validate: func(az *acme.Authorization) {
  290. assert.Equals(t, az.Challenges[0].URL, fmt.Sprintf("%s/%s/%s/challenge/%s/%s", baseURL, linkerPrefix, provName, az.ID, chID0))
  291. assert.Equals(t, az.Challenges[1].URL, fmt.Sprintf("%s/%s/%s/challenge/%s/%s", baseURL, linkerPrefix, provName, az.ID, chID1))
  292. assert.Equals(t, az.Challenges[2].URL, fmt.Sprintf("%s/%s/%s/challenge/%s/%s", baseURL, linkerPrefix, provName, az.ID, chID2))
  293. },
  294. },
  295. }
  296. for name, tc := range tests {
  297. t.Run(name, func(t *testing.T) {
  298. l.LinkAuthorization(ctx, tc.az)
  299. tc.validate(tc.az)
  300. })
  301. }
  302. }
  303. func TestLinker_LinkOrdersByAccountID(t *testing.T) {
  304. baseURL := &url.URL{Scheme: "https", Host: "test.ca.smallstep.com"}
  305. prov := newProv()
  306. provName := url.PathEscape(prov.GetName())
  307. ctx := context.WithValue(context.Background(), baseURLContextKey, baseURL)
  308. ctx = context.WithValue(ctx, provisionerContextKey, prov)
  309. linkerPrefix := "acme"
  310. l := NewLinker("dns", linkerPrefix)
  311. type test struct {
  312. oids []string
  313. }
  314. var tests = map[string]test{
  315. "ok": {
  316. oids: []string{"foo", "bar", "baz"},
  317. },
  318. }
  319. for name, tc := range tests {
  320. t.Run(name, func(t *testing.T) {
  321. l.LinkOrdersByAccountID(ctx, tc.oids)
  322. assert.Equals(t, tc.oids, []string{
  323. fmt.Sprintf("%s/%s/%s/order/%s", baseURL, linkerPrefix, provName, "foo"),
  324. fmt.Sprintf("%s/%s/%s/order/%s", baseURL, linkerPrefix, provName, "bar"),
  325. fmt.Sprintf("%s/%s/%s/order/%s", baseURL, linkerPrefix, provName, "baz"),
  326. })
  327. })
  328. }
  329. }