vault.go 2.4 KB

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