vault.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. vault "github.com/hashicorp/vault/api"
  16. )
  17. type MockNewRequestFn func(method, requestPath string) *vault.Request
  18. type MockRawRequestWithContextFn func(ctx context.Context, r *vault.Request) (*vault.Response, error)
  19. type MockSetTokenFn func(v string)
  20. type MockTokenFn func() string
  21. type MockClearTokenFn func()
  22. type MockSetNamespaceFn func(namespace string)
  23. func NewMockNewRequestFn(req *vault.Request) MockNewRequestFn {
  24. return func(method, requestPath string) *vault.Request {
  25. return req
  26. }
  27. }
  28. // An RequestFn operates on the supplied Request. You might use an RequestFn to
  29. // test or update the contents of an Request.
  30. type RequestFn func(req *vault.Request) error
  31. func NewMockRawRequestWithContextFn(res *vault.Response, err error, ofn ...RequestFn) MockRawRequestWithContextFn {
  32. return func(_ context.Context, r *vault.Request) (*vault.Response, error) {
  33. for _, fn := range ofn {
  34. if err := fn(r); err != nil {
  35. return res, err
  36. }
  37. }
  38. return res, err
  39. }
  40. }
  41. func NewSetTokenFn(ofn ...func(v string)) MockSetTokenFn {
  42. return func(v string) {
  43. for _, fn := range ofn {
  44. fn(v)
  45. }
  46. }
  47. }
  48. func NewTokenFn(v string) MockTokenFn {
  49. return func() string {
  50. return v
  51. }
  52. }
  53. func NewClearTokenFn() MockClearTokenFn {
  54. return func() {}
  55. }
  56. func NewSetNamespaceFn() MockSetNamespaceFn {
  57. return func(namespace string) {}
  58. }
  59. type VaultClient struct {
  60. MockNewRequest MockNewRequestFn
  61. MockRawRequestWithContext MockRawRequestWithContextFn
  62. MockSetToken MockSetTokenFn
  63. MockToken MockTokenFn
  64. MockClearToken MockClearTokenFn
  65. MockSetNamespace MockSetNamespaceFn
  66. }
  67. func (c *VaultClient) NewRequest(method, requestPath string) *vault.Request {
  68. return c.MockNewRequest(method, requestPath)
  69. }
  70. func (c *VaultClient) RawRequestWithContext(ctx context.Context, r *vault.Request) (*vault.Response, error) {
  71. return c.MockRawRequestWithContext(ctx, r)
  72. }
  73. func (c *VaultClient) SetToken(v string) {
  74. c.MockSetToken(v)
  75. }
  76. func (c *VaultClient) Token() string {
  77. return c.MockToken()
  78. }
  79. func (c *VaultClient) ClearToken() {
  80. c.MockClearToken()
  81. }
  82. func (c *VaultClient) SetNamespace(namespace string) {
  83. c.MockSetNamespace(namespace)
  84. }