-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathsecret.go
65 lines (55 loc) · 1.6 KB
/
secret.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package bulk
import (
"context"
"encoding/json"
"net/http"
"github.com/elastic/go-elasticsearch/v8"
"go.elastic.co/apm/v2"
)
type ExtendedClient struct {
*elasticsearch.Client
Custom *ExtendedAPI
}
type ExtendedAPI struct {
*elasticsearch.Client
}
// Read secret values with custom ES API added in Fleet ES plugin, there is no direct access to secrets index
// GET /_fleet/secret/secretId
func (c *ExtendedAPI) Read(ctx context.Context, secretID string) (*SecretResponse, error) {
req, err := http.NewRequestWithContext(ctx, "GET", "/_fleet/secret/"+secretID, nil)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
if err != nil {
return nil, err
}
res, err := c.Perform(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
var secretResp SecretResponse
err = json.NewDecoder(res.Body).Decode(&secretResp)
if err != nil {
return nil, err
}
return &secretResp, nil
}
type SecretResponse struct {
Value string
}
func ReadSecret(ctx context.Context, client *elasticsearch.Client, secretID string) (string, error) {
span, ctx := apm.StartSpan(ctx, "readSecret", "elasticsearch")
defer span.End()
es := ExtendedClient{Client: client, Custom: &ExtendedAPI{client}}
res, err := es.Custom.Read(ctx, secretID)
if err != nil {
return "", err
}
if res == nil {
return "", nil
}
return (*res).Value, err
}