vault.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 vaultutil provides utility types and functions for interacting with HashiCorp Vault.
  14. package vaultutil
  15. import (
  16. "context"
  17. "github.com/aws/aws-sdk-go/aws/credentials"
  18. vault "github.com/hashicorp/vault/api"
  19. )
  20. // JwtProviderFactory is a function type that creates a JWT credentials provider.
  21. type JwtProviderFactory func(name, namespace, roleArn string, aud []string, region string) (credentials.Provider, error)
  22. // Auth defines the interface for Vault authentication.
  23. type Auth interface {
  24. Login(ctx context.Context, authMethod vault.AuthMethod) (*vault.Secret, error)
  25. }
  26. // Token defines the interface for Vault token operations.
  27. type Token interface {
  28. RevokeSelfWithContext(ctx context.Context, token string) error
  29. LookupSelfWithContext(ctx context.Context) (*vault.Secret, error)
  30. }
  31. // Logical defines the interface for Vault's logical operations.
  32. type Logical interface {
  33. ReadWithDataWithContext(ctx context.Context, path string, data map[string][]string) (*vault.Secret, error)
  34. ListWithContext(ctx context.Context, path string) (*vault.Secret, error)
  35. WriteWithContext(ctx context.Context, path string, data map[string]any) (*vault.Secret, error)
  36. DeleteWithContext(ctx context.Context, path string) (*vault.Secret, error)
  37. }
  38. // Client defines the interface for a Vault client with methods for token management,
  39. // authentication, and secret operations.
  40. type Client interface {
  41. SetToken(v string)
  42. Token() string
  43. ClearToken()
  44. Auth() Auth
  45. Logical() Logical
  46. AuthToken() Token
  47. Namespace() string
  48. SetNamespace(namespace string)
  49. AddHeader(key, value string)
  50. }
  51. // VaultClient is a wrapper around the HashiCorp Vault API client that provides
  52. // methods for authentication, token management, and secret operations.
  53. type VaultClient struct {
  54. SetTokenFunc func(v string)
  55. TokenFunc func() string
  56. ClearTokenFunc func()
  57. AuthField Auth
  58. LogicalField Logical
  59. AuthTokenField Token
  60. NamespaceFunc func() string
  61. SetNamespaceFunc func(namespace string)
  62. AddHeaderFunc func(key, value string)
  63. }
  64. // AddHeader adds a header to all requests using the provided key, value pair.
  65. func (v VaultClient) AddHeader(key, value string) {
  66. v.AddHeaderFunc(key, value)
  67. }
  68. // Namespace returns the current Vault namespace.
  69. func (v VaultClient) Namespace() string {
  70. return v.NamespaceFunc()
  71. }
  72. // SetNamespace sets the Vault namespace to use for requests.
  73. func (v VaultClient) SetNamespace(namespace string) {
  74. v.SetNamespaceFunc(namespace)
  75. }
  76. // ClearToken clears the Vault token.
  77. func (v VaultClient) ClearToken() {
  78. v.ClearTokenFunc()
  79. }
  80. // Token returns the current Vault token.
  81. func (v VaultClient) Token() string {
  82. return v.TokenFunc()
  83. }
  84. // SetToken sets the Vault token to use for requests.
  85. func (v VaultClient) SetToken(token string) {
  86. v.SetTokenFunc(token)
  87. }
  88. // Auth returns the Auth interface for authentication operations.
  89. func (v VaultClient) Auth() Auth {
  90. return v.AuthField
  91. }
  92. // AuthToken returns the Token interface for token operations.
  93. func (v VaultClient) AuthToken() Token {
  94. return v.AuthTokenField
  95. }
  96. // Logical returns the Logical interface for secret operations.
  97. func (v VaultClient) Logical() Logical {
  98. return v.LogicalField
  99. }