vault.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 LoginFn func(ctx context.Context, authMethod vault.AuthMethod) (*vault.Secret, error)
  18. type Auth struct {
  19. LoginFn LoginFn
  20. }
  21. func (f Auth) Login(ctx context.Context, authMethod vault.AuthMethod) (*vault.Secret, error) {
  22. return f.LoginFn(ctx, authMethod)
  23. }
  24. type ReadWithDataWithContextFn func(ctx context.Context, path string, data map[string][]string) (*vault.Secret, error)
  25. type ListWithContextFn func(ctx context.Context, path string) (*vault.Secret, error)
  26. type WriteWithContextFn func(ctx context.Context, path string, data map[string]interface{}) (*vault.Secret, error)
  27. type Logical struct {
  28. ReadWithDataWithContextFn ReadWithDataWithContextFn
  29. ListWithContextFn ListWithContextFn
  30. WriteWithContextFn WriteWithContextFn
  31. }
  32. func NewReadWithContextFn(secret map[string]interface{}, err error) ReadWithDataWithContextFn {
  33. return func(ctx context.Context, path string, data map[string][]string) (*vault.Secret, error) {
  34. vault := &vault.Secret{
  35. Data: secret,
  36. }
  37. return vault, err
  38. }
  39. }
  40. func (f Logical) ReadWithDataWithContext(ctx context.Context, path string, data map[string][]string) (*vault.Secret, error) {
  41. return f.ReadWithDataWithContextFn(ctx, path, data)
  42. }
  43. func (f Logical) ListWithContext(ctx context.Context, path string) (*vault.Secret, error) {
  44. return f.ListWithContextFn(ctx, path)
  45. }
  46. func (f Logical) WriteWithContext(ctx context.Context, path string, data map[string]interface{}) (*vault.Secret, error) {
  47. return f.WriteWithContextFn(ctx, path, data)
  48. }
  49. type RevokeSelfWithContextFn func(ctx context.Context, token string) error
  50. type LookupSelfWithContextFn func(ctx context.Context) (*vault.Secret, error)
  51. type Token struct {
  52. RevokeSelfWithContextFn RevokeSelfWithContextFn
  53. LookupSelfWithContextFn LookupSelfWithContextFn
  54. }
  55. func (f Token) RevokeSelfWithContext(ctx context.Context, token string) error {
  56. return f.RevokeSelfWithContextFn(ctx, token)
  57. }
  58. func (f Token) LookupSelfWithContext(ctx context.Context) (*vault.Secret, error) {
  59. return f.LookupSelfWithContextFn(ctx)
  60. }
  61. type MockSetTokenFn func(v string)
  62. type MockTokenFn func() string
  63. type MockClearTokenFn func()
  64. type MockSetNamespaceFn func(namespace string)
  65. type MockAddHeaderFn func(key, value string)
  66. type VaultListResponse struct {
  67. Metadata *vault.Response
  68. Data *vault.Response
  69. }
  70. func NewSetTokenFn(ofn ...func(v string)) MockSetTokenFn {
  71. return func(v string) {
  72. for _, fn := range ofn {
  73. fn(v)
  74. }
  75. }
  76. }
  77. func NewTokenFn(v string) MockTokenFn {
  78. return func() string {
  79. return v
  80. }
  81. }
  82. func NewClearTokenFn() MockClearTokenFn {
  83. return func() {}
  84. }
  85. func NewSetNamespaceFn() MockSetNamespaceFn {
  86. return func(namespace string) {}
  87. }
  88. func NewAddHeaderFn() MockAddHeaderFn {
  89. return func(key, value string) {}
  90. }
  91. type VaultClient struct {
  92. MockLogical Logical
  93. MockAuth Auth
  94. MockAuthToken Token
  95. MockSetToken MockSetTokenFn
  96. MockToken MockTokenFn
  97. MockClearToken MockClearTokenFn
  98. MockSetNamespace MockSetNamespaceFn
  99. MockAddHeader MockAddHeaderFn
  100. }
  101. func (c *VaultClient) Logical() Logical {
  102. return c.MockLogical
  103. }
  104. func NewVaultLogical() Logical {
  105. logical := Logical{
  106. ReadWithDataWithContextFn: func(ctx context.Context, path string, data map[string][]string) (*vault.Secret, error) {
  107. return nil, nil
  108. },
  109. ListWithContextFn: func(ctx context.Context, path string) (*vault.Secret, error) {
  110. return nil, nil
  111. },
  112. WriteWithContextFn: func(ctx context.Context, path string, data map[string]interface{}) (*vault.Secret, error) {
  113. return nil, nil
  114. },
  115. }
  116. return logical
  117. }
  118. func (c *VaultClient) Auth() Auth {
  119. return c.MockAuth
  120. }
  121. func NewVaultAuth() Auth {
  122. auth := Auth{
  123. LoginFn: func(ctx context.Context, authMethod vault.AuthMethod) (*vault.Secret, error) {
  124. return nil, nil
  125. },
  126. }
  127. return auth
  128. }
  129. func (c *VaultClient) AuthToken() Token {
  130. return c.MockAuthToken
  131. }
  132. func (c *VaultClient) SetToken(v string) {
  133. c.MockSetToken(v)
  134. }
  135. func (c *VaultClient) Token() string {
  136. return c.MockToken()
  137. }
  138. func (c *VaultClient) ClearToken() {
  139. c.MockClearToken()
  140. }
  141. func (c *VaultClient) SetNamespace(namespace string) {
  142. c.MockSetNamespace(namespace)
  143. }
  144. func (c *VaultClient) AddHeader(key, value string) {
  145. c.MockAddHeader(key, value)
  146. }