-
Notifications
You must be signed in to change notification settings - Fork 28
/
count.go
74 lines (60 loc) · 1.61 KB
/
count.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
66
67
68
69
70
71
72
73
74
// SPDX-License-Identifier: Apache-2.0
package vault
import (
"context"
"fmt"
"strings"
"github.com/hashicorp/vault/api"
"github.com/sirupsen/logrus"
"github.com/go-vela/server/constants"
)
// Count counts a list of secrets.
func (c *client) Count(ctx context.Context, sType, org, name string, _ []string) (i int64, err error) {
// create log fields from secret metadata
fields := logrus.Fields{
"org": org,
"repo": name,
"type": sType,
}
// check if secret is a shared secret
if strings.EqualFold(sType, constants.SecretShared) {
// update log fields from secret metadata
fields = logrus.Fields{
"org": org,
"team": name,
"type": sType,
}
}
c.Logger.WithFields(fields).Tracef("counting vault %s secrets for %s/%s", sType, org, name)
vault := new(api.Secret)
count := 0
// capture the list of secrets from the Vault service
switch sType {
case constants.SecretOrg:
vault, err = c.listOrg(org)
case constants.SecretRepo:
vault, err = c.listRepo(org, name)
case constants.SecretShared:
vault, err = c.listShared(org, name)
default:
return 0, fmt.Errorf("invalid secret type: %v", sType)
}
if err != nil {
return 0, err
}
// cast the list of secrets to the expected type
keys, ok := vault.Data["keys"].([]interface{})
if !ok {
return 0, fmt.Errorf("not a valid list of secrets from Vault")
}
// iterate through each element in the list of secrets
for _, element := range keys {
// cast the secret to the expected type
_, ok := element.(string)
if !ok {
return 0, fmt.Errorf("not a valid list of secrets from Vault")
}
count++
}
return int64(count), nil
}