vault.go 6.0 KB

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