vault.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 DeleteWithContextFn func(ctx context.Context, path string) (*vault.Secret, error)
  28. type Logical struct {
  29. ReadWithDataWithContextFn ReadWithDataWithContextFn
  30. ListWithContextFn ListWithContextFn
  31. WriteWithContextFn WriteWithContextFn
  32. DeleteWithContextFn DeleteWithContextFn
  33. }
  34. func (f Logical) DeleteWithContext(ctx context.Context, path string) (*vault.Secret, error) {
  35. return f.DeleteWithContextFn(ctx, path)
  36. }
  37. func NewDeleteWithContextFn(secret map[string]interface{}, err error) DeleteWithContextFn {
  38. return func(ctx context.Context, path string) (*vault.Secret, error) {
  39. vault := &vault.Secret{
  40. Data: secret,
  41. }
  42. return vault, err
  43. }
  44. }
  45. func NewReadWithContextFn(secret map[string]interface{}, err error) ReadWithDataWithContextFn {
  46. return func(ctx context.Context, path string, data map[string][]string) (*vault.Secret, error) {
  47. vault := &vault.Secret{
  48. Data: secret,
  49. }
  50. return vault, err
  51. }
  52. }
  53. func NewWriteWithContextFn(secret map[string]interface{}, err error) WriteWithContextFn {
  54. return func(ctx context.Context, path string, data map[string]interface{}) (*vault.Secret, error) {
  55. vault := &vault.Secret{
  56. Data: secret,
  57. }
  58. return vault, err
  59. }
  60. }
  61. func WriteChangingReadContext(secret map[string]interface{}, l Logical) WriteWithContextFn {
  62. v := &vault.Secret{
  63. Data: secret,
  64. }
  65. return func(ctx context.Context, path string, data map[string]interface{}) (*vault.Secret, error) {
  66. l.ReadWithDataWithContextFn = func(ctx context.Context, path string, data map[string][]string) (*vault.Secret, error) {
  67. return v, nil
  68. }
  69. return v, nil
  70. }
  71. }
  72. func (f Logical) ReadWithDataWithContext(ctx context.Context, path string, data map[string][]string) (*vault.Secret, error) {
  73. return f.ReadWithDataWithContextFn(ctx, path, data)
  74. }
  75. func (f Logical) ListWithContext(ctx context.Context, path string) (*vault.Secret, error) {
  76. return f.ListWithContextFn(ctx, path)
  77. }
  78. func (f Logical) WriteWithContext(ctx context.Context, path string, data map[string]interface{}) (*vault.Secret, error) {
  79. return f.WriteWithContextFn(ctx, path, data)
  80. }
  81. type RevokeSelfWithContextFn func(ctx context.Context, token string) error
  82. type LookupSelfWithContextFn func(ctx context.Context) (*vault.Secret, error)
  83. type Token struct {
  84. RevokeSelfWithContextFn RevokeSelfWithContextFn
  85. LookupSelfWithContextFn LookupSelfWithContextFn
  86. }
  87. func (f Token) RevokeSelfWithContext(ctx context.Context, token string) error {
  88. return f.RevokeSelfWithContextFn(ctx, token)
  89. }
  90. func (f Token) LookupSelfWithContext(ctx context.Context) (*vault.Secret, error) {
  91. return f.LookupSelfWithContextFn(ctx)
  92. }
  93. type MockSetTokenFn func(v string)
  94. type MockTokenFn func() string
  95. type MockClearTokenFn func()
  96. type MockSetNamespaceFn func(namespace string)
  97. type MockAddHeaderFn func(key, value string)
  98. type VaultListResponse struct {
  99. Metadata *vault.Response
  100. Data *vault.Response
  101. }
  102. func NewAuthTokenFn() Token {
  103. return Token{nil, func(ctx context.Context) (*vault.Secret, error) {
  104. return &(vault.Secret{}), nil
  105. }}
  106. }
  107. func NewSetTokenFn(ofn ...func(v string)) MockSetTokenFn {
  108. return func(v string) {
  109. for _, fn := range ofn {
  110. fn(v)
  111. }
  112. }
  113. }
  114. func NewTokenFn(v string) MockTokenFn {
  115. return func() string {
  116. return v
  117. }
  118. }
  119. func NewClearTokenFn() MockClearTokenFn {
  120. return func() {}
  121. }
  122. func NewSetNamespaceFn() MockSetNamespaceFn {
  123. return func(namespace string) {}
  124. }
  125. func NewAddHeaderFn() MockAddHeaderFn {
  126. return func(key, value string) {}
  127. }
  128. type VaultClient struct {
  129. MockLogical Logical
  130. MockAuth Auth
  131. MockAuthToken Token
  132. MockSetToken MockSetTokenFn
  133. MockToken MockTokenFn
  134. MockClearToken MockClearTokenFn
  135. MockSetNamespace MockSetNamespaceFn
  136. MockAddHeader MockAddHeaderFn
  137. }
  138. func (c *VaultClient) Logical() Logical {
  139. return c.MockLogical
  140. }
  141. func NewVaultLogical() Logical {
  142. logical := Logical{
  143. ReadWithDataWithContextFn: func(ctx context.Context, path string, data map[string][]string) (*vault.Secret, error) {
  144. return nil, nil
  145. },
  146. ListWithContextFn: func(ctx context.Context, path string) (*vault.Secret, error) {
  147. return nil, nil
  148. },
  149. WriteWithContextFn: func(ctx context.Context, path string, data map[string]interface{}) (*vault.Secret, error) {
  150. return nil, nil
  151. },
  152. }
  153. return logical
  154. }
  155. func (c *VaultClient) Auth() Auth {
  156. return c.MockAuth
  157. }
  158. func NewVaultAuth() Auth {
  159. auth := Auth{
  160. LoginFn: func(ctx context.Context, authMethod vault.AuthMethod) (*vault.Secret, error) {
  161. return nil, nil
  162. },
  163. }
  164. return auth
  165. }
  166. func (c *VaultClient) AuthToken() Token {
  167. return c.MockAuthToken
  168. }
  169. func (c *VaultClient) SetToken(v string) {
  170. c.MockSetToken(v)
  171. }
  172. func (c *VaultClient) Token() string {
  173. return c.MockToken()
  174. }
  175. func (c *VaultClient) ClearToken() {
  176. c.MockClearToken()
  177. }
  178. func (c *VaultClient) SetNamespace(namespace string) {
  179. c.MockSetNamespace(namespace)
  180. }
  181. func (c *VaultClient) AddHeader(key, value string) {
  182. c.MockAddHeader(key, value)
  183. }