vault.go 7.9 KB

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