forked from datastax/vault-plugin-secrets-datastax-astra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath_credentials_list.go
50 lines (46 loc) · 1.42 KB
/
path_credentials_list.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
package datastax_astra
import (
"context"
"errors"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
func pathCredentialsList(b *datastaxAstraBackend) *framework.Path {
return &framework.Path{
Pattern: credsListPath,
Operations: map[logical.Operation]framework.OperationHandler{
logical.ListOperation: &framework.PathOperation{
Callback: b.pathCredsList,
Summary: "List all tokens.",
},
},
}
}
func (b *datastaxAstraBackend) pathCredsList(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
credsList, err := listCreds(ctx, req.Storage)
if err != nil {
return nil, errors.New("could not find tokens. error: " + err.Error())
}
if len(credsList) == 0 {
return nil, errors.New("no tokens found")
}
keyInfo := make(map[string]interface{})
for i := 0; i < len(credsList); i++ {
token, err := readToken(ctx, req.Storage, credsList[i])
if err != nil {
return nil, errors.New("could not list tokens. error: " + err.Error())
}
keyInfo[token.ClientID] = token.toResponseData()
}
return logical.ListResponseWithInfo(credsList, keyInfo), nil
}
func listCreds(ctx context.Context, s logical.Storage) ([]string, error) {
objList, err := s.List(ctx, "token/")
if err != nil {
return nil, errors.New("failed to load tokens list")
}
if len(objList) == 0 {
return nil, errors.New("no tokens found")
}
return objList, nil
}