linker.go 5.9 KB

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