vault.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. util "github.com/external-secrets/external-secrets/pkg/provider/vault/util"
  17. )
  18. type LoginFn func(ctx context.Context, authMethod vault.AuthMethod) (*vault.Secret, error)
  19. type Auth struct {
  20. LoginFn LoginFn
  21. }
  22. func (f Auth) Login(ctx context.Context, authMethod vault.AuthMethod) (*vault.Secret, error) {
  23. return f.LoginFn(ctx, authMethod)
  24. }
  25. type ReadWithDataWithContextFn func(ctx context.Context, path string, data map[string][]string) (*vault.Secret, error)
  26. type ListWithContextFn func(ctx context.Context, path string) (*vault.Secret, error)
  27. type WriteWithContextFn func(ctx context.Context, path string, data map[string]interface{}) (*vault.Secret, error)
  28. type DeleteWithContextFn func(ctx context.Context, path string) (*vault.Secret, error)
  29. type Logical struct {
  30. ReadWithDataWithContextFn ReadWithDataWithContextFn
  31. ListWithContextFn ListWithContextFn
  32. WriteWithContextFn WriteWithContextFn
  33. DeleteWithContextFn DeleteWithContextFn
  34. }
  35. func (f Logical) DeleteWithContext(ctx context.Context, path string) (*vault.Secret, error) {
  36. return f.DeleteWithContextFn(ctx, path)
  37. }
  38. func NewDeleteWithContextFn(secret map[string]interface{}, err error) DeleteWithContextFn {
  39. return func(ctx context.Context, path string) (*vault.Secret, error) {
  40. vault := &vault.Secret{
  41. Data: secret,
  42. }
  43. return vault, err
  44. }
  45. }
  46. func NewReadWithContextFn(secret map[string]interface{}, err error) ReadWithDataWithContextFn {
  47. return func(ctx context.Context, path string, data map[string][]string) (*vault.Secret, error) {
  48. if secret == nil {
  49. return nil, err
  50. }
  51. vault := &vault.Secret{
  52. Data: secret,
  53. }
  54. return vault, err
  55. }
  56. }
  57. func NewReadMetadataWithContextFn(secret map[string]interface{}, err error) ReadWithDataWithContextFn {
  58. return func(ctx context.Context, path string, data map[string][]string) (*vault.Secret, error) {
  59. if secret == nil {
  60. return nil, err
  61. }
  62. metadata := make(map[string]interface{})
  63. metadata["custom_metadata"] = secret
  64. vault := &vault.Secret{
  65. Data: metadata,
  66. }
  67. return vault, err
  68. }
  69. }
  70. func NewWriteWithContextFn(secret map[string]interface{}, err error) WriteWithContextFn {
  71. return func(ctx context.Context, path string, data map[string]interface{}) (*vault.Secret, error) {
  72. vault := &vault.Secret{
  73. Data: secret,
  74. }
  75. return vault, err
  76. }
  77. }
  78. func WriteChangingReadContext(secret map[string]interface{}, l Logical) WriteWithContextFn {
  79. v := &vault.Secret{
  80. Data: secret,
  81. }
  82. return func(ctx context.Context, path string, data map[string]interface{}) (*vault.Secret, error) {
  83. l.ReadWithDataWithContextFn = func(ctx context.Context, path string, data map[string][]string) (*vault.Secret, error) {
  84. return v, nil
  85. }
  86. return v, nil
  87. }
  88. }
  89. func (f Logical) ReadWithDataWithContext(ctx context.Context, path string, data map[string][]string) (*vault.Secret, error) {
  90. return f.ReadWithDataWithContextFn(ctx, path, data)
  91. }
  92. func (f Logical) ListWithContext(ctx context.Context, path string) (*vault.Secret, error) {
  93. return f.ListWithContextFn(ctx, path)
  94. }
  95. func (f Logical) WriteWithContext(ctx context.Context, path string, data map[string]interface{}) (*vault.Secret, error) {
  96. return f.WriteWithContextFn(ctx, path, data)
  97. }
  98. type RevokeSelfWithContextFn func(ctx context.Context, token string) error
  99. type LookupSelfWithContextFn func(ctx context.Context) (*vault.Secret, error)
  100. type Token struct {
  101. RevokeSelfWithContextFn RevokeSelfWithContextFn
  102. LookupSelfWithContextFn LookupSelfWithContextFn
  103. }
  104. func (f Token) RevokeSelfWithContext(ctx context.Context, token string) error {
  105. return f.RevokeSelfWithContextFn(ctx, token)
  106. }
  107. func (f Token) LookupSelfWithContext(ctx context.Context) (*vault.Secret, error) {
  108. return f.LookupSelfWithContextFn(ctx)
  109. }
  110. type MockSetTokenFn func(v string)
  111. type MockTokenFn func() string
  112. type MockClearTokenFn func()
  113. type MockSetNamespaceFn func(namespace string)
  114. type MockAddHeaderFn func(key, value string)
  115. type VaultListResponse struct {
  116. Metadata *vault.Response
  117. Data *vault.Response
  118. }
  119. func NewAuthTokenFn() Token {
  120. return Token{nil, func(ctx context.Context) (*vault.Secret, error) {
  121. return &(vault.Secret{}), nil
  122. }}
  123. }
  124. func NewSetTokenFn(ofn ...func(v string)) MockSetTokenFn {
  125. return func(v string) {
  126. for _, fn := range ofn {
  127. fn(v)
  128. }
  129. }
  130. }
  131. func NewTokenFn(v string) MockTokenFn {
  132. return func() string {
  133. return v
  134. }
  135. }
  136. func NewClearTokenFn() MockClearTokenFn {
  137. return func() {}
  138. }
  139. func NewSetNamespaceFn() MockSetNamespaceFn {
  140. return func(namespace string) {}
  141. }
  142. func NewAddHeaderFn() MockAddHeaderFn {
  143. return func(key, value string) {}
  144. }
  145. type VaultClient struct {
  146. MockLogical Logical
  147. MockAuth Auth
  148. MockAuthToken Token
  149. MockSetToken MockSetTokenFn
  150. MockToken MockTokenFn
  151. MockClearToken MockClearTokenFn
  152. MockSetNamespace MockSetNamespaceFn
  153. MockAddHeader MockAddHeaderFn
  154. }
  155. func (c *VaultClient) Logical() Logical {
  156. return c.MockLogical
  157. }
  158. func NewVaultLogical() Logical {
  159. logical := Logical{
  160. ReadWithDataWithContextFn: func(ctx context.Context, path string, data map[string][]string) (*vault.Secret, error) {
  161. return nil, nil
  162. },
  163. ListWithContextFn: func(ctx context.Context, path string) (*vault.Secret, error) {
  164. return nil, nil
  165. },
  166. WriteWithContextFn: func(ctx context.Context, path string, data map[string]interface{}) (*vault.Secret, error) {
  167. return nil, nil
  168. },
  169. }
  170. return logical
  171. }
  172. func (c *VaultClient) Auth() Auth {
  173. return c.MockAuth
  174. }
  175. func NewVaultAuth() Auth {
  176. auth := Auth{
  177. LoginFn: func(ctx context.Context, authMethod vault.AuthMethod) (*vault.Secret, error) {
  178. return nil, nil
  179. },
  180. }
  181. return auth
  182. }
  183. func (c *VaultClient) AuthToken() Token {
  184. return c.MockAuthToken
  185. }
  186. func (c *VaultClient) SetToken(v string) {
  187. c.MockSetToken(v)
  188. }
  189. func (c *VaultClient) Token() string {
  190. return c.MockToken()
  191. }
  192. func (c *VaultClient) ClearToken() {
  193. c.MockClearToken()
  194. }
  195. func (c *VaultClient) SetNamespace(namespace string) {
  196. c.MockSetNamespace(namespace)
  197. }
  198. func (c *VaultClient) AddHeader(key, value string) {
  199. c.MockAddHeader(key, value)
  200. }
  201. func ClientWithLoginMock(c *vault.Config) (util.Client, error) {
  202. cl := VaultClient{
  203. MockAuthToken: NewAuthTokenFn(),
  204. MockSetToken: NewSetTokenFn(),
  205. MockToken: NewTokenFn(""),
  206. MockAuth: NewVaultAuth(),
  207. MockLogical: NewVaultLogical(),
  208. }
  209. auth := cl.Auth()
  210. token := cl.AuthToken()
  211. logical := cl.Logical()
  212. out := util.VClient{
  213. SetTokenFunc: cl.SetToken,
  214. TokenFunc: cl.Token,
  215. ClearTokenFunc: cl.ClearToken,
  216. AuthField: auth,
  217. AuthTokenField: token,
  218. LogicalField: logical,
  219. SetNamespaceFunc: cl.SetNamespace,
  220. AddHeaderFunc: cl.AddHeader,
  221. }
  222. return out, nil
  223. }