vault.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package fake
  13. import (
  14. "context"
  15. "net/url"
  16. "strings"
  17. vault "github.com/hashicorp/vault/api"
  18. )
  19. type MockNewRequestFn func(method, requestPath string) *vault.Request
  20. type MockRawRequestWithContextFn func(ctx context.Context, r *vault.Request) (*vault.Response, error)
  21. type MockSetTokenFn func(v string)
  22. type MockTokenFn func() string
  23. type MockClearTokenFn func()
  24. type MockSetNamespaceFn func(namespace string)
  25. type MockAddHeaderFn func(key, value string)
  26. func NewMockNewRequestFn(req *vault.Request) MockNewRequestFn {
  27. return func(method, requestPath string) *vault.Request {
  28. return req
  29. }
  30. }
  31. func NewMockNewRequestListFn(req *vault.Request) MockNewRequestFn {
  32. return func(method, requestPath string) *vault.Request {
  33. urlPath := url.URL{
  34. Path: requestPath,
  35. }
  36. req.URL = &urlPath
  37. req.Params = make(url.Values)
  38. return req
  39. }
  40. }
  41. // An RequestFn operates on the supplied Request. You might use an RequestFn to
  42. // test or update the contents of an Request.
  43. type RequestFn func(req *vault.Request) error
  44. func NewMockRawRequestWithContextFn(res *vault.Response, err error, ofn ...RequestFn) MockRawRequestWithContextFn {
  45. return func(_ context.Context, r *vault.Request) (*vault.Response, error) {
  46. for _, fn := range ofn {
  47. if err := fn(r); err != nil {
  48. return res, err
  49. }
  50. }
  51. return res, err
  52. }
  53. }
  54. type VaultListResponse struct {
  55. Metadata *vault.Response
  56. Data *vault.Response
  57. }
  58. func NewMockRawRequestListWithContextFn(res map[string]VaultListResponse, err error) MockRawRequestWithContextFn {
  59. return func(_ context.Context, r *vault.Request) (*vault.Response, error) {
  60. pathList := strings.Split(r.URL.Path, "/")
  61. path := "default"
  62. if pathList[4] != "" {
  63. path = strings.Join(pathList[4:], "/")
  64. }
  65. if strings.Contains(r.URL.Path, "metadata") {
  66. resp := res[path].Metadata
  67. return resp, err
  68. }
  69. resp := res[path].Data
  70. return resp, err
  71. }
  72. }
  73. func NewSetTokenFn(ofn ...func(v string)) MockSetTokenFn {
  74. return func(v string) {
  75. for _, fn := range ofn {
  76. fn(v)
  77. }
  78. }
  79. }
  80. func NewTokenFn(v string) MockTokenFn {
  81. return func() string {
  82. return v
  83. }
  84. }
  85. func NewClearTokenFn() MockClearTokenFn {
  86. return func() {}
  87. }
  88. func NewSetNamespaceFn() MockSetNamespaceFn {
  89. return func(namespace string) {}
  90. }
  91. func NewAddHeaderFn() MockAddHeaderFn {
  92. return func(key, value string) {}
  93. }
  94. type VaultClient struct {
  95. MockNewRequest MockNewRequestFn
  96. MockRawRequestWithContext MockRawRequestWithContextFn
  97. MockSetToken MockSetTokenFn
  98. MockToken MockTokenFn
  99. MockClearToken MockClearTokenFn
  100. MockSetNamespace MockSetNamespaceFn
  101. MockAddHeader MockAddHeaderFn
  102. }
  103. func (c *VaultClient) NewRequest(method, requestPath string) *vault.Request {
  104. return c.MockNewRequest(method, requestPath)
  105. }
  106. func (c *VaultClient) RawRequestWithContext(ctx context.Context, r *vault.Request) (*vault.Response, error) {
  107. return c.MockRawRequestWithContext(ctx, r)
  108. }
  109. func (c *VaultClient) SetToken(v string) {
  110. c.MockSetToken(v)
  111. }
  112. func (c *VaultClient) Token() string {
  113. return c.MockToken()
  114. }
  115. func (c *VaultClient) ClearToken() {
  116. c.MockClearToken()
  117. }
  118. func (c *VaultClient) SetNamespace(namespace string) {
  119. c.MockSetNamespace(namespace)
  120. }
  121. func (c *VaultClient) AddHeader(key, value string) {
  122. c.MockAddHeader(key, value)
  123. }