|
| 1 | +package vault |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + _ "embed" |
| 6 | + "encoding/json" |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + "net/url" |
| 10 | + "strings" |
| 11 | + |
| 12 | + _ "github.com/redpanda-data/benthos/v4/public/components/pure" |
| 13 | + |
| 14 | + "github.com/hashicorp/vault-client-go" |
| 15 | + "github.com/hashicorp/vault-client-go/schema" |
| 16 | + "github.com/redpanda-data/benthos/v4/public/bloblang" |
| 17 | + "github.com/redpanda-data/benthos/v4/public/service" |
| 18 | +) |
| 19 | + |
| 20 | +var ( |
| 21 | + _ service.Processor = (*processor)(nil) |
| 22 | + spec *service.ConfigSpec |
| 23 | + errLoginResponseEmpty = errors.New("login responded with unexpected empty response") |
| 24 | + errLoginResponseMissingAuth = errors.New("login responded with unexpected missing auth") |
| 25 | + errLoginResponseEmptyClientToken = errors.New("login responded with unexpected missing or empty client_token") |
| 26 | +) |
| 27 | + |
| 28 | +func init() { |
| 29 | + spec = service.NewConfigSpec(). |
| 30 | + Beta(). |
| 31 | + Summary("Fetches a Value for a Key from Hashicorp Vault"). |
| 32 | + Description(` |
| 33 | +The fields `+"`mount_path`"+`, `+"`path`"+` and `+"`version`"+` support |
| 34 | +xref:configuration:interpolation.adoc#bloblang-queries[interpolation functions], allowing |
| 35 | +you to create a unique `+"`mount_path`"+`, `+"`path`"+` and/or `+"`version`"+` for each message. |
| 36 | +
|
| 37 | +`). |
| 38 | + Fields( |
| 39 | + service.NewStringField("url"). |
| 40 | + Description("The base URL of the Vault server."), |
| 41 | + service.NewObjectField( |
| 42 | + "auth", |
| 43 | + service.NewStringField("mount_path"). |
| 44 | + Optional(), |
| 45 | + service.NewObjectField( |
| 46 | + "app_role", |
| 47 | + service.NewStringField("role_id"). |
| 48 | + Description("Unique identifier of the Role"). |
| 49 | + Secret(), |
| 50 | + service.NewStringField("secret_id"). |
| 51 | + Description("SecretID belong to the App role"). |
| 52 | + Secret(), |
| 53 | + ), |
| 54 | + ), |
| 55 | + service.NewBloblangField("mount_path"). |
| 56 | + Description(`Supports xref:configuration:interpolation.adoc#bloblang-queries[interpolation functions]. |
| 57 | +`). |
| 58 | + Optional(), |
| 59 | + service.NewBloblangField("path"). |
| 60 | + Description(`The key path to fetch from Vault. |
| 61 | +Supports xref:configuration:interpolation.adoc#bloblang-queries[interpolation functions]. |
| 62 | +If root gets deleted no message gets produced. |
| 63 | +`), |
| 64 | + service.NewInterpolatedStringField("version"). |
| 65 | + Description(`The specific key version to fetch from Vault. |
| 66 | +Supports xref:configuration:interpolation.adoc#bloblang-queries[interpolation functions]. |
| 67 | +`). |
| 68 | + Optional(), |
| 69 | + ) |
| 70 | + err := service.RegisterProcessor("vault_key", spec, ctor) |
| 71 | + if err != nil { |
| 72 | + panic(err) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func ctor(conf *service.ParsedConfig, mgr *service.Resources) (service.Processor, error) { |
| 77 | + |
| 78 | + url, err := conf.FieldString("url") |
| 79 | + if err != nil { |
| 80 | + return nil, fmt.Errorf("missing url for Vault: %w", err) |
| 81 | + } |
| 82 | + if strings.TrimSpace(url) == "" { |
| 83 | + return nil, fmt.Errorf("unexpected empty url for Vault AppRole login: %w", err) |
| 84 | + } |
| 85 | + |
| 86 | + roleId, err := conf.FieldString("auth", "app_role", "role_id") |
| 87 | + if err != nil { |
| 88 | + return nil, fmt.Errorf("missing role_id for Vault AppRole login: %w", err) |
| 89 | + } |
| 90 | + if strings.TrimSpace(roleId) == "" { |
| 91 | + return nil, fmt.Errorf("unexpected empty role_id for Vault AppRole login: %w", err) |
| 92 | + } |
| 93 | + |
| 94 | + secretId, err := conf.FieldString("auth", "app_role", "secret_id") |
| 95 | + if err != nil { |
| 96 | + return nil, fmt.Errorf("missing secret_id for Vault AppRole login: %w", err) |
| 97 | + } |
| 98 | + if strings.TrimSpace(secretId) == "" { |
| 99 | + return nil, fmt.Errorf("unexpected empty secret_id for Vault AppRole login: %w", err) |
| 100 | + } |
| 101 | + |
| 102 | + client, err := vault.New( |
| 103 | + vault.WithAddress(url), |
| 104 | + ) |
| 105 | + if err != nil { |
| 106 | + return nil, fmt.Errorf("failed creating Vault client: %w", err) |
| 107 | + } |
| 108 | + |
| 109 | + var authOptions []vault.RequestOption |
| 110 | + authMountPath := "" |
| 111 | + if conf.Contains("auth", "mount_path") { |
| 112 | + authMountPath, err = conf.FieldString("auth", "mount_path") |
| 113 | + if err != nil { |
| 114 | + return nil, err |
| 115 | + } |
| 116 | + authOptions = append(authOptions, vault.WithMountPath(authMountPath)) |
| 117 | + } |
| 118 | + |
| 119 | + ctx := context.Background() |
| 120 | + resp, err := client.Auth.AppRoleLogin(ctx, schema.AppRoleLoginRequest{ |
| 121 | + RoleId: roleId, |
| 122 | + SecretId: secretId, |
| 123 | + }, authOptions...) |
| 124 | + if err != nil { |
| 125 | + return nil, fmt.Errorf("failed to login via Vault client: %w", err) |
| 126 | + } |
| 127 | + if resp == nil { |
| 128 | + return nil, errLoginResponseEmpty |
| 129 | + } |
| 130 | + |
| 131 | + var mountPath *service.InterpolatedString |
| 132 | + if conf.Contains("mount_path") { |
| 133 | + mountPath, err = conf.FieldInterpolatedString("mount_path") |
| 134 | + if err != nil { |
| 135 | + return nil, err |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + var path *bloblang.Executor |
| 140 | + path, err = conf.FieldBloblang("path") |
| 141 | + if err != nil { |
| 142 | + return nil, fmt.Errorf("missing key path for Vault fetch: %w", err) |
| 143 | + } |
| 144 | + |
| 145 | + var version *service.InterpolatedString |
| 146 | + if conf.Contains("version") { |
| 147 | + version, err = conf.FieldInterpolatedString("version") |
| 148 | + if err != nil { |
| 149 | + return nil, err |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + if resp.Auth == nil { |
| 154 | + return nil, errLoginResponseMissingAuth |
| 155 | + } |
| 156 | + |
| 157 | + if resp.Auth.ClientToken == "" { |
| 158 | + return nil, errLoginResponseEmptyClientToken |
| 159 | + } |
| 160 | + |
| 161 | + clientToken := resp.Auth.ClientToken |
| 162 | + |
| 163 | + return &processor{ |
| 164 | + client: client, |
| 165 | + clientToken: clientToken, |
| 166 | + logger: mgr.Logger(), |
| 167 | + metrics: mgr.Metrics(), |
| 168 | + mountPath: mountPath, |
| 169 | + path: path, |
| 170 | + version: version, |
| 171 | + }, nil |
| 172 | +} |
| 173 | + |
| 174 | +type processor struct { |
| 175 | + client *vault.Client |
| 176 | + clientToken string |
| 177 | + logger *service.Logger |
| 178 | + metrics *service.Metrics |
| 179 | + mountPath *service.InterpolatedString |
| 180 | + path *bloblang.Executor |
| 181 | + version *service.InterpolatedString |
| 182 | +} |
| 183 | + |
| 184 | +func (p *processor) Process(ctx context.Context, message *service.Message) (service.MessageBatch, error) { |
| 185 | + |
| 186 | + opts := []vault.RequestOption{ |
| 187 | + vault.WithToken(p.clientToken), |
| 188 | + } |
| 189 | + |
| 190 | + mountPath := "" |
| 191 | + if p.mountPath != nil { |
| 192 | + var err error |
| 193 | + mountPath, err = p.mountPath.TryString(message) |
| 194 | + if err != nil { |
| 195 | + return nil, err |
| 196 | + } |
| 197 | + if mountPath != "" { |
| 198 | + opts = append(opts, vault.WithMountPath(mountPath)) |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + output, err := p.path.Query(message) |
| 203 | + if errors.Is(err, bloblang.ErrRootDeleted) { |
| 204 | + // Take this as an indicator to not produce a message |
| 205 | + return nil, nil |
| 206 | + } |
| 207 | + path := output.(string) |
| 208 | + if path == "" { |
| 209 | + return nil, fmt.Errorf("empty key path") |
| 210 | + } |
| 211 | + |
| 212 | + version := "" |
| 213 | + if p.version != nil { |
| 214 | + version, err := p.version.TryString(message) |
| 215 | + if err != nil { |
| 216 | + return nil, err |
| 217 | + } |
| 218 | + if version != "" { |
| 219 | + opts = append(opts, vault.WithQueryParameters(url.Values{ |
| 220 | + "version": []string{version}, |
| 221 | + })) |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + p.logger.Tracef("Reading key value from Vault (mount_path: %s, path: %s, version: %s)", mountPath, path, version) |
| 226 | + kv, err := p.client.Secrets.KvV2Read(ctx, path, opts...) |
| 227 | + if err != nil { |
| 228 | + outMsg := message.Copy() |
| 229 | + outMsg.SetError(err) |
| 230 | + return service.MessageBatch{outMsg}, nil |
| 231 | + } |
| 232 | + |
| 233 | + bs, err := json.Marshal(kv.Data.Data) |
| 234 | + if err != nil { |
| 235 | + return nil, fmt.Errorf("failed to marshal Vault response: %w", err) |
| 236 | + } |
| 237 | + |
| 238 | + outMsg := message.Copy() |
| 239 | + outMsg.SetBytes(bs) |
| 240 | + for k, v := range kv.Data.Metadata { |
| 241 | + outMsg.MetaSetMut(k, v) |
| 242 | + } |
| 243 | + |
| 244 | + return service.MessageBatch{ |
| 245 | + outMsg, |
| 246 | + }, nil |
| 247 | +} |
| 248 | + |
| 249 | +func (p *processor) Close(ctx context.Context) error { |
| 250 | + return nil |
| 251 | +} |
0 commit comments