vault.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 NewAuthTokenFn() Token {
  71. return Token{nil, func(ctx context.Context) (*vault.Secret, error) {
  72. return &(vault.Secret{}), nil
  73. }}
  74. }
  75. func NewSetTokenFn(ofn ...func(v string)) MockSetTokenFn {
  76. return func(v string) {
  77. for _, fn := range ofn {
  78. fn(v)
  79. }
  80. }
  81. }
  82. func NewTokenFn(v string) MockTokenFn {
  83. return func() string {
  84. return v
  85. }
  86. }
  87. func NewClearTokenFn() MockClearTokenFn {
  88. return func() {}
  89. }
  90. func NewSetNamespaceFn() MockSetNamespaceFn {
  91. return func(namespace string) {}
  92. }
  93. func NewAddHeaderFn() MockAddHeaderFn {
  94. return func(key, value string) {}
  95. }
  96. type VaultClient struct {
  97. MockLogical Logical
  98. MockAuth Auth
  99. MockAuthToken Token
  100. MockSetToken MockSetTokenFn
  101. MockToken MockTokenFn
  102. MockClearToken MockClearTokenFn
  103. MockSetNamespace MockSetNamespaceFn
  104. MockAddHeader MockAddHeaderFn
  105. }
  106. func (c *VaultClient) Logical() Logical {
  107. return c.MockLogical
  108. }
  109. func NewVaultLogical() Logical {
  110. logical := Logical{
  111. ReadWithDataWithContextFn: func(ctx context.Context, path string, data map[string][]string) (*vault.Secret, error) {
  112. return nil, nil
  113. },
  114. ListWithContextFn: func(ctx context.Context, path string) (*vault.Secret, error) {
  115. return nil, nil
  116. },
  117. WriteWithContextFn: func(ctx context.Context, path string, data map[string]interface{}) (*vault.Secret, error) {
  118. return nil, nil
  119. },
  120. }
  121. return logical
  122. }
  123. func (c *VaultClient) Auth() Auth {
  124. return c.MockAuth
  125. }
  126. func NewVaultAuth() Auth {
  127. auth := Auth{
  128. LoginFn: func(ctx context.Context, authMethod vault.AuthMethod) (*vault.Secret, error) {
  129. return nil, nil
  130. },
  131. }
  132. return auth
  133. }
  134. func (c *VaultClient) AuthToken() Token {
  135. return c.MockAuthToken
  136. }
  137. func (c *VaultClient) SetToken(v string) {
  138. c.MockSetToken(v)
  139. }
  140. func (c *VaultClient) Token() string {
  141. return c.MockToken()
  142. }
  143. func (c *VaultClient) ClearToken() {
  144. c.MockClearToken()
  145. }
  146. func (c *VaultClient) SetNamespace(namespace string) {
  147. c.MockSetNamespace(namespace)
  148. }
  149. func (c *VaultClient) AddHeader(key, value string) {
  150. c.MockAddHeader(key, value)
  151. }