vault.go 7.9 KB

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