provisioners.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package authority
  2. import (
  3. "crypto/sha1"
  4. "encoding/binary"
  5. "encoding/hex"
  6. "encoding/json"
  7. "fmt"
  8. "math"
  9. "net/http"
  10. "sort"
  11. "strings"
  12. "github.com/pkg/errors"
  13. )
  14. // DefaultProvisionersLimit is the default limit for listing provisioners.
  15. const DefaultProvisionersLimit = 20
  16. // DefaultProvisionersMax is the maximum limit for listing provisioners.
  17. const DefaultProvisionersMax = 100
  18. // GetEncryptedKey returns the JWE key corresponding to the given kid argument.
  19. func (a *Authority) GetEncryptedKey(kid string) (string, error) {
  20. val, ok := a.encryptedKeyIndex.Load(kid)
  21. if !ok {
  22. return "", &apiError{errors.Errorf("encrypted key with kid %s was not found", kid),
  23. http.StatusNotFound, context{}}
  24. }
  25. key, ok := val.(string)
  26. if !ok {
  27. return "", &apiError{errors.Errorf("stored value is not a string"),
  28. http.StatusInternalServerError, context{}}
  29. }
  30. return key, nil
  31. }
  32. // GetProvisioners returns a map listing each provisioner and the JWK Key Set
  33. // with their public keys.
  34. func (a *Authority) GetProvisioners(cursor string, limit int) ([]*Provisioner, string, error) {
  35. provisioners, nextCursor := a.sortedProvisioners.Find(cursor, limit)
  36. return provisioners, nextCursor, nil
  37. }
  38. type uidProvisioner struct {
  39. provisioner *Provisioner
  40. uid string
  41. }
  42. func newSortedProvisioners(provisioners []*Provisioner) (provisionerSlice, error) {
  43. if len(provisioners) > math.MaxInt32 {
  44. return nil, errors.New("too many provisioners")
  45. }
  46. var slice provisionerSlice
  47. bi := make([]byte, 4)
  48. for i, p := range provisioners {
  49. sum, err := provisionerSum(p)
  50. if err != nil {
  51. return nil, err
  52. }
  53. // Use the first 4 bytes (32bit) of the sum to insert the order
  54. // Using big endian format to get the strings sorted:
  55. // 0x00000000, 0x00000001, 0x00000002, ...
  56. binary.BigEndian.PutUint32(bi, uint32(i))
  57. sum[0], sum[1], sum[2], sum[3] = bi[0], bi[1], bi[2], bi[3]
  58. bi[0], bi[1], bi[2], bi[3] = 0, 0, 0, 0
  59. slice = append(slice, uidProvisioner{
  60. provisioner: p,
  61. uid: hex.EncodeToString(sum),
  62. })
  63. }
  64. sort.Sort(slice)
  65. return slice, nil
  66. }
  67. type provisionerSlice []uidProvisioner
  68. func (p provisionerSlice) Len() int { return len(p) }
  69. func (p provisionerSlice) Less(i, j int) bool { return p[i].uid < p[j].uid }
  70. func (p provisionerSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  71. func (p provisionerSlice) Find(cursor string, limit int) ([]*Provisioner, string) {
  72. switch {
  73. case limit <= 0:
  74. limit = DefaultProvisionersLimit
  75. case limit > DefaultProvisionersMax:
  76. limit = DefaultProvisionersMax
  77. }
  78. n := len(p)
  79. cursor = fmt.Sprintf("%040s", cursor)
  80. i := sort.Search(n, func(i int) bool { return p[i].uid >= cursor })
  81. var slice []*Provisioner
  82. for ; i < n && len(slice) < limit; i++ {
  83. slice = append(slice, p[i].provisioner)
  84. }
  85. if i < n {
  86. return slice, strings.TrimLeft(p[i].uid, "0")
  87. }
  88. return slice, ""
  89. }
  90. // provisionerSum returns the SHA1 of the json representation of the
  91. // provisioner. From this we will create the unique and sorted id.
  92. func provisionerSum(p *Provisioner) ([]byte, error) {
  93. b, err := json.Marshal(p.Key)
  94. if err != nil {
  95. return nil, errors.Wrap(err, "error marshalling provisioner")
  96. }
  97. sum := sha1.Sum(b)
  98. return sum[:], nil
  99. }