4
0

linker.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package api
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "github.com/smallstep/certificates/acme"
  7. )
  8. // NewLinker returns a new Directory type.
  9. func NewLinker(dns, prefix string) Linker {
  10. return &linker{prefix: prefix, dns: dns}
  11. }
  12. // Linker interface for generating links for ACME resources.
  13. type Linker interface {
  14. GetLink(ctx context.Context, typ LinkType, inputs ...string) string
  15. GetUnescapedPathSuffix(typ LinkType, provName string, inputs ...string) string
  16. LinkOrder(ctx context.Context, o *acme.Order)
  17. LinkAccount(ctx context.Context, o *acme.Account)
  18. LinkChallenge(ctx context.Context, o *acme.Challenge, azID string)
  19. LinkAuthorization(ctx context.Context, o *acme.Authorization)
  20. LinkOrdersByAccountID(ctx context.Context, orders []string)
  21. }
  22. // linker generates ACME links.
  23. type linker struct {
  24. prefix string
  25. dns string
  26. }
  27. func (l *linker) GetUnescapedPathSuffix(typ LinkType, provisionerName string, inputs ...string) string {
  28. switch typ {
  29. case NewNonceLinkType, NewAccountLinkType, NewOrderLinkType, NewAuthzLinkType, DirectoryLinkType, KeyChangeLinkType, RevokeCertLinkType:
  30. return fmt.Sprintf("/%s/%s", provisionerName, typ)
  31. case AccountLinkType, OrderLinkType, AuthzLinkType, CertificateLinkType:
  32. return fmt.Sprintf("/%s/%s/%s", provisionerName, typ, inputs[0])
  33. case ChallengeLinkType:
  34. return fmt.Sprintf("/%s/%s/%s/%s", provisionerName, typ, inputs[0], inputs[1])
  35. case OrdersByAccountLinkType:
  36. return fmt.Sprintf("/%s/%s/%s/orders", provisionerName, AccountLinkType, inputs[0])
  37. case FinalizeLinkType:
  38. return fmt.Sprintf("/%s/%s/%s/finalize", provisionerName, OrderLinkType, inputs[0])
  39. default:
  40. return ""
  41. }
  42. }
  43. // GetLink is a helper for GetLinkExplicit
  44. func (l *linker) GetLink(ctx context.Context, typ LinkType, inputs ...string) string {
  45. var (
  46. provName string
  47. baseURL = baseURLFromContext(ctx)
  48. u = url.URL{}
  49. )
  50. if p, err := provisionerFromContext(ctx); err == nil && p != nil {
  51. provName = p.GetName()
  52. }
  53. // Copy the baseURL value from the pointer. https://github.com/golang/go/issues/38351
  54. if baseURL != nil {
  55. u = *baseURL
  56. }
  57. u.Path = l.GetUnescapedPathSuffix(typ, provName, inputs...)
  58. // If no Scheme is set, then default to https.
  59. if u.Scheme == "" {
  60. u.Scheme = "https"
  61. }
  62. // If no Host is set, then use the default (first DNS attr in the ca.json).
  63. if u.Host == "" {
  64. u.Host = l.dns
  65. }
  66. u.Path = l.prefix + u.Path
  67. return u.String()
  68. }
  69. // LinkType captures the link type.
  70. type LinkType int
  71. const (
  72. // NewNonceLinkType new-nonce
  73. NewNonceLinkType LinkType = iota
  74. // NewAccountLinkType new-account
  75. NewAccountLinkType
  76. // AccountLinkType account
  77. AccountLinkType
  78. // OrderLinkType order
  79. OrderLinkType
  80. // NewOrderLinkType new-order
  81. NewOrderLinkType
  82. // OrdersByAccountLinkType list of orders owned by account
  83. OrdersByAccountLinkType
  84. // FinalizeLinkType finalize order
  85. FinalizeLinkType
  86. // NewAuthzLinkType authz
  87. NewAuthzLinkType
  88. // AuthzLinkType new-authz
  89. AuthzLinkType
  90. // ChallengeLinkType challenge
  91. ChallengeLinkType
  92. // CertificateLinkType certificate
  93. CertificateLinkType
  94. // DirectoryLinkType directory
  95. DirectoryLinkType
  96. // RevokeCertLinkType revoke certificate
  97. RevokeCertLinkType
  98. // KeyChangeLinkType key rollover
  99. KeyChangeLinkType
  100. )
  101. func (l LinkType) String() string {
  102. switch l {
  103. case NewNonceLinkType:
  104. return "new-nonce"
  105. case NewAccountLinkType:
  106. return "new-account"
  107. case AccountLinkType:
  108. return "account"
  109. case NewOrderLinkType:
  110. return "new-order"
  111. case OrderLinkType:
  112. return "order"
  113. case NewAuthzLinkType:
  114. return "new-authz"
  115. case AuthzLinkType:
  116. return "authz"
  117. case ChallengeLinkType:
  118. return "challenge"
  119. case CertificateLinkType:
  120. return "certificate"
  121. case DirectoryLinkType:
  122. return "directory"
  123. case RevokeCertLinkType:
  124. return "revoke-cert"
  125. case KeyChangeLinkType:
  126. return "key-change"
  127. default:
  128. return fmt.Sprintf("unexpected LinkType '%d'", int(l))
  129. }
  130. }
  131. // LinkOrder sets the ACME links required by an ACME order.
  132. func (l *linker) LinkOrder(ctx context.Context, o *acme.Order) {
  133. o.AuthorizationURLs = make([]string, len(o.AuthorizationIDs))
  134. for i, azID := range o.AuthorizationIDs {
  135. o.AuthorizationURLs[i] = l.GetLink(ctx, AuthzLinkType, azID)
  136. }
  137. o.FinalizeURL = l.GetLink(ctx, FinalizeLinkType, o.ID)
  138. if o.CertificateID != "" {
  139. o.CertificateURL = l.GetLink(ctx, CertificateLinkType, o.CertificateID)
  140. }
  141. }
  142. // LinkAccount sets the ACME links required by an ACME account.
  143. func (l *linker) LinkAccount(ctx context.Context, acc *acme.Account) {
  144. acc.OrdersURL = l.GetLink(ctx, OrdersByAccountLinkType, acc.ID)
  145. }
  146. // LinkChallenge sets the ACME links required by an ACME challenge.
  147. func (l *linker) LinkChallenge(ctx context.Context, ch *acme.Challenge, azID string) {
  148. ch.URL = l.GetLink(ctx, ChallengeLinkType, azID, ch.ID)
  149. }
  150. // LinkAuthorization sets the ACME links required by an ACME authorization.
  151. func (l *linker) LinkAuthorization(ctx context.Context, az *acme.Authorization) {
  152. for _, ch := range az.Challenges {
  153. l.LinkChallenge(ctx, ch, az.ID)
  154. }
  155. }
  156. // LinkOrdersByAccountID converts each order ID to an ACME link.
  157. func (l *linker) LinkOrdersByAccountID(ctx context.Context, orders []string) {
  158. for i, id := range orders {
  159. orders[i] = l.GetLink(ctx, OrderLinkType, id)
  160. }
  161. }