vault.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 util
  13. import (
  14. "context"
  15. vault "github.com/hashicorp/vault/api"
  16. )
  17. type Auth interface {
  18. Login(ctx context.Context, authMethod vault.AuthMethod) (*vault.Secret, error)
  19. }
  20. type Token interface {
  21. RevokeSelfWithContext(ctx context.Context, token string) error
  22. LookupSelfWithContext(ctx context.Context) (*vault.Secret, error)
  23. }
  24. type Logical interface {
  25. ReadWithDataWithContext(ctx context.Context, path string, data map[string][]string) (*vault.Secret, error)
  26. ListWithContext(ctx context.Context, path string) (*vault.Secret, error)
  27. WriteWithContext(ctx context.Context, path string, data map[string]interface{}) (*vault.Secret, error)
  28. DeleteWithContext(ctx context.Context, path string) (*vault.Secret, error)
  29. }
  30. type Client interface {
  31. SetToken(v string)
  32. Token() string
  33. ClearToken()
  34. Auth() Auth
  35. Logical() Logical
  36. AuthToken() Token
  37. SetNamespace(namespace string)
  38. AddHeader(key, value string)
  39. }
  40. type VClient struct {
  41. SetTokenFunc func(v string)
  42. TokenFunc func() string
  43. ClearTokenFunc func()
  44. AuthField Auth
  45. LogicalField Logical
  46. AuthTokenField Token
  47. SetNamespaceFunc func(namespace string)
  48. AddHeaderFunc func(key, value string)
  49. }
  50. func (v VClient) AddHeader(key, value string) {
  51. v.AddHeaderFunc(key, value)
  52. }
  53. func (v VClient) SetNamespace(namespace string) {
  54. v.SetNamespaceFunc(namespace)
  55. }
  56. func (v VClient) ClearToken() {
  57. v.ClearTokenFunc()
  58. }
  59. func (v VClient) Token() string {
  60. return v.TokenFunc()
  61. }
  62. func (v VClient) SetToken(token string) {
  63. v.SetTokenFunc(token)
  64. }
  65. func (v VClient) Auth() Auth {
  66. return v.AuthField
  67. }
  68. func (v VClient) AuthToken() Token {
  69. return v.AuthTokenField
  70. }
  71. func (v VClient) Logical() Logical {
  72. return v.LogicalField
  73. }