group.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package client
  2. import (
  3. "crypto/tls"
  4. "errors"
  5. "net/http"
  6. "strings"
  7. "time"
  8. "github.com/cloudflare/cfssl/auth"
  9. "github.com/cloudflare/cfssl/info"
  10. )
  11. // Strategy is the means by which the server to use as a remote should
  12. // be selected.
  13. type Strategy int
  14. const (
  15. // StrategyInvalid indicates any strategy that is unsupported
  16. // or returned when no strategy is applicable.
  17. StrategyInvalid = iota
  18. // StrategyOrderedList is a sequential list of servers: if the
  19. // first server cannot be reached, the next is used. The
  20. // client will proceed in this manner until the list of
  21. // servers is exhausted, and then an error is returned.
  22. StrategyOrderedList
  23. )
  24. var strategyStrings = map[string]Strategy{
  25. "ordered_list": StrategyOrderedList,
  26. }
  27. // StrategyFromString takes a string describing a
  28. func StrategyFromString(s string) Strategy {
  29. s = strings.TrimSpace(strings.ToLower(s))
  30. strategy, ok := strategyStrings[s]
  31. if !ok {
  32. return StrategyInvalid
  33. }
  34. return strategy
  35. }
  36. // NewGroup will use the collection of remotes specified with the
  37. // given strategy.
  38. func NewGroup(remotes []string, tlsConfig *tls.Config, strategy Strategy) (Remote, error) {
  39. var servers = make([]*server, len(remotes))
  40. for i := range remotes {
  41. u, err := normalizeURL(remotes[i])
  42. if err != nil {
  43. return nil, err
  44. }
  45. servers[i] = newServer(u, tlsConfig)
  46. }
  47. switch strategy {
  48. case StrategyOrderedList:
  49. return newOrdererdListGroup(servers)
  50. default:
  51. return nil, errors.New("unrecognised strategy")
  52. }
  53. }
  54. type orderedListGroup struct {
  55. remotes []*server
  56. }
  57. func (g *orderedListGroup) Hosts() []string {
  58. var hosts = make([]string, 0, len(g.remotes))
  59. for _, srv := range g.remotes {
  60. srvHosts := srv.Hosts()
  61. hosts = append(hosts, srvHosts[0])
  62. }
  63. return hosts
  64. }
  65. func (g *orderedListGroup) SetRequestTimeout(timeout time.Duration) {
  66. for _, srv := range g.remotes {
  67. srv.SetRequestTimeout(timeout)
  68. }
  69. }
  70. func newOrdererdListGroup(remotes []*server) (Remote, error) {
  71. return &orderedListGroup{
  72. remotes: remotes,
  73. }, nil
  74. }
  75. func (g *orderedListGroup) AuthSign(req, id []byte, provider auth.Provider) (resp []byte, err error) {
  76. for i := range g.remotes {
  77. resp, err = g.remotes[i].AuthSign(req, id, provider)
  78. if err == nil {
  79. return resp, nil
  80. }
  81. }
  82. return nil, err
  83. }
  84. func (g *orderedListGroup) Sign(jsonData []byte) (resp []byte, err error) {
  85. for i := range g.remotes {
  86. resp, err = g.remotes[i].Sign(jsonData)
  87. if err == nil {
  88. return resp, nil
  89. }
  90. }
  91. return nil, err
  92. }
  93. func (g *orderedListGroup) Info(jsonData []byte) (resp *info.Resp, err error) {
  94. for i := range g.remotes {
  95. resp, err = g.remotes[i].Info(jsonData)
  96. if err == nil {
  97. return resp, nil
  98. }
  99. }
  100. return nil, err
  101. }
  102. // SetReqModifier does nothing because there is no request modifier for group
  103. func (g *orderedListGroup) SetReqModifier(mod func(*http.Request, []byte)) {
  104. // noop
  105. }