vault.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. Copyright © 2025 ESO Maintainer Team
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package util
  14. import (
  15. "context"
  16. "github.com/aws/aws-sdk-go/aws/credentials"
  17. vault "github.com/hashicorp/vault/api"
  18. )
  19. type JwtProviderFactory func(name, namespace, roleArn string, aud []string, region string) (credentials.Provider, error)
  20. type Auth interface {
  21. Login(ctx context.Context, authMethod vault.AuthMethod) (*vault.Secret, error)
  22. }
  23. type Token interface {
  24. RevokeSelfWithContext(ctx context.Context, token string) error
  25. LookupSelfWithContext(ctx context.Context) (*vault.Secret, error)
  26. }
  27. type Logical interface {
  28. ReadWithDataWithContext(ctx context.Context, path string, data map[string][]string) (*vault.Secret, error)
  29. ListWithContext(ctx context.Context, path string) (*vault.Secret, error)
  30. WriteWithContext(ctx context.Context, path string, data map[string]any) (*vault.Secret, error)
  31. DeleteWithContext(ctx context.Context, path string) (*vault.Secret, error)
  32. }
  33. type Client interface {
  34. SetToken(v string)
  35. Token() string
  36. ClearToken()
  37. Auth() Auth
  38. Logical() Logical
  39. AuthToken() Token
  40. Namespace() string
  41. SetNamespace(namespace string)
  42. AddHeader(key, value string)
  43. }
  44. type VaultClient struct {
  45. SetTokenFunc func(v string)
  46. TokenFunc func() string
  47. ClearTokenFunc func()
  48. AuthField Auth
  49. LogicalField Logical
  50. AuthTokenField Token
  51. NamespaceFunc func() string
  52. SetNamespaceFunc func(namespace string)
  53. AddHeaderFunc func(key, value string)
  54. }
  55. func (v VaultClient) AddHeader(key, value string) {
  56. v.AddHeaderFunc(key, value)
  57. }
  58. func (v VaultClient) Namespace() string {
  59. return v.NamespaceFunc()
  60. }
  61. func (v VaultClient) SetNamespace(namespace string) {
  62. v.SetNamespaceFunc(namespace)
  63. }
  64. func (v VaultClient) ClearToken() {
  65. v.ClearTokenFunc()
  66. }
  67. func (v VaultClient) Token() string {
  68. return v.TokenFunc()
  69. }
  70. func (v VaultClient) SetToken(token string) {
  71. v.SetTokenFunc(token)
  72. }
  73. func (v VaultClient) Auth() Auth {
  74. return v.AuthField
  75. }
  76. func (v VaultClient) AuthToken() Token {
  77. return v.AuthTokenField
  78. }
  79. func (v VaultClient) Logical() Logical {
  80. return v.LogicalField
  81. }