vault.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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]any) (*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. Namespace() string
  40. SetNamespace(namespace string)
  41. AddHeader(key, value string)
  42. }
  43. type VaultClient struct {
  44. SetTokenFunc func(v string)
  45. TokenFunc func() string
  46. ClearTokenFunc func()
  47. AuthField Auth
  48. LogicalField Logical
  49. AuthTokenField Token
  50. NamespaceFunc func() string
  51. SetNamespaceFunc func(namespace string)
  52. AddHeaderFunc func(key, value string)
  53. }
  54. func (v VaultClient) AddHeader(key, value string) {
  55. v.AddHeaderFunc(key, value)
  56. }
  57. func (v VaultClient) Namespace() string {
  58. return v.NamespaceFunc()
  59. }
  60. func (v VaultClient) SetNamespace(namespace string) {
  61. v.SetNamespaceFunc(namespace)
  62. }
  63. func (v VaultClient) ClearToken() {
  64. v.ClearTokenFunc()
  65. }
  66. func (v VaultClient) Token() string {
  67. return v.TokenFunc()
  68. }
  69. func (v VaultClient) SetToken(token string) {
  70. v.SetTokenFunc(token)
  71. }
  72. func (v VaultClient) Auth() Auth {
  73. return v.AuthField
  74. }
  75. func (v VaultClient) AuthToken() Token {
  76. return v.AuthTokenField
  77. }
  78. func (v VaultClient) Logical() Logical {
  79. return v.LogicalField
  80. }