group.go 3.0 KB

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