uri.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package uri
  2. import (
  3. "bytes"
  4. "encoding/hex"
  5. "net/url"
  6. "os"
  7. "strings"
  8. "unicode"
  9. "github.com/pkg/errors"
  10. )
  11. // URI implements a parser for a URI format based on the the PKCS #11 URI Scheme
  12. // defined in https://tools.ietf.org/html/rfc7512
  13. //
  14. // These URIs will be used to define the key names in a KMS.
  15. type URI struct {
  16. *url.URL
  17. Values url.Values
  18. }
  19. // New creates a new URI from a scheme and key-value pairs.
  20. func New(scheme string, values url.Values) *URI {
  21. return &URI{
  22. URL: &url.URL{
  23. Scheme: scheme,
  24. Opaque: strings.ReplaceAll(values.Encode(), "&", ";"),
  25. },
  26. Values: values,
  27. }
  28. }
  29. // NewFile creates an uri for a file.
  30. func NewFile(path string) *URI {
  31. return &URI{
  32. URL: &url.URL{
  33. Scheme: "file",
  34. Path: path,
  35. },
  36. }
  37. }
  38. // HasScheme returns true if the given uri has the given scheme, false otherwise.
  39. func HasScheme(scheme, rawuri string) bool {
  40. u, err := url.Parse(rawuri)
  41. if err != nil {
  42. return false
  43. }
  44. return strings.EqualFold(u.Scheme, scheme)
  45. }
  46. // Parse returns the URI for the given string or an error.
  47. func Parse(rawuri string) (*URI, error) {
  48. u, err := url.Parse(rawuri)
  49. if err != nil {
  50. return nil, errors.Wrapf(err, "error parsing %s", rawuri)
  51. }
  52. if u.Scheme == "" {
  53. return nil, errors.Errorf("error parsing %s: scheme is missing", rawuri)
  54. }
  55. // Starting with Go 1.17 url.ParseQuery returns an error using semicolon as
  56. // separator.
  57. v, err := url.ParseQuery(strings.ReplaceAll(u.Opaque, ";", "&"))
  58. if err != nil {
  59. return nil, errors.Wrapf(err, "error parsing %s", rawuri)
  60. }
  61. return &URI{
  62. URL: u,
  63. Values: v,
  64. }, nil
  65. }
  66. // ParseWithScheme returns the URI for the given string only if it has the given
  67. // scheme.
  68. func ParseWithScheme(scheme, rawuri string) (*URI, error) {
  69. u, err := Parse(rawuri)
  70. if err != nil {
  71. return nil, err
  72. }
  73. if !strings.EqualFold(u.Scheme, scheme) {
  74. return nil, errors.Errorf("error parsing %s: scheme not expected", rawuri)
  75. }
  76. return u, nil
  77. }
  78. // Get returns the first value in the uri with the given key, it will return
  79. // empty string if that field is not present.
  80. func (u *URI) Get(key string) string {
  81. v := u.Values.Get(key)
  82. if v == "" {
  83. v = u.URL.Query().Get(key)
  84. }
  85. return v
  86. }
  87. // GetBool returns true if a given key has the value "true". It returns false
  88. // otherwise.
  89. func (u *URI) GetBool(key string) bool {
  90. v := u.Values.Get(key)
  91. if v == "" {
  92. v = u.URL.Query().Get(key)
  93. }
  94. return strings.EqualFold(v, "true")
  95. }
  96. // GetEncoded returns the first value in the uri with the given key, it will
  97. // return empty nil if that field is not present or is empty. If the return
  98. // value is hex encoded it will decode it and return it.
  99. func (u *URI) GetEncoded(key string) []byte {
  100. v := u.Get(key)
  101. if v == "" {
  102. return nil
  103. }
  104. if len(v)%2 == 0 {
  105. if b, err := hex.DecodeString(v); err == nil {
  106. return b
  107. }
  108. }
  109. return []byte(v)
  110. }
  111. // Pin returns the pin encoded in the url. It will read the pin from the
  112. // pin-value or the pin-source attributes.
  113. func (u *URI) Pin() string {
  114. if value := u.Get("pin-value"); value != "" {
  115. return value
  116. }
  117. if path := u.Get("pin-source"); path != "" {
  118. if b, err := readFile(path); err == nil {
  119. return string(bytes.TrimRightFunc(b, unicode.IsSpace))
  120. }
  121. }
  122. return ""
  123. }
  124. func readFile(path string) ([]byte, error) {
  125. u, err := url.Parse(path)
  126. if err == nil && (u.Scheme == "" || u.Scheme == "file") && u.Path != "" {
  127. path = u.Path
  128. }
  129. b, err := os.ReadFile(path)
  130. if err != nil {
  131. return nil, errors.Wrapf(err, "error reading %s", path)
  132. }
  133. return b, nil
  134. }