-
Notifications
You must be signed in to change notification settings - Fork 28
/
delete.go
79 lines (66 loc) · 2 KB
/
delete.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
75
76
77
78
79
// SPDX-License-Identifier: Apache-2.0
package vault
import (
"context"
"fmt"
"strings"
"github.com/sirupsen/logrus"
"github.com/go-vela/server/constants"
)
// Delete deletes a secret.
func (c *client) Delete(ctx context.Context, sType, org, name, path string) error {
// create log fields from secret metadata
fields := logrus.Fields{
"org": org,
"repo": name,
"secret": path,
"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,
"secret": path,
"type": sType,
}
}
c.Logger.WithFields(fields).Tracef("deleting vault %s secret %s for %s/%s", sType, path, org, name)
// delete the secret from the Vault service
switch sType {
case constants.SecretOrg:
return c.deleteOrg(org, path)
case constants.SecretRepo:
return c.deleteRepo(org, name, path)
case constants.SecretShared:
return c.deleteShared(org, name, path)
default:
return fmt.Errorf("invalid secret type: %v", sType)
}
}
// deleteOrg is a helper function to delete
// the org secret for the provided path.
func (c *client) deleteOrg(org, path string) error {
return c.delete(fmt.Sprintf("%s/org/%s/%s", c.config.Prefix, org, path))
}
// deleteRepo is a helper function to delete
// the repo secret for the provided path.
func (c *client) deleteRepo(org, repo, path string) error {
return c.delete(fmt.Sprintf("%s/repo/%s/%s/%s", c.config.Prefix, org, repo, path))
}
// deleteShared is a helper function to delete
// the shared secret for the provided path.
func (c *client) deleteShared(org, team, path string) error {
return c.delete(fmt.Sprintf("%s/shared/%s/%s/%s", c.config.Prefix, org, team, path))
}
// delete is a helper function to delete
// the secret for the provided path.
func (c *client) delete(path string) error {
// send API call to delete the secret
_, err := c.Vault.Logical().Delete(path)
if err != nil {
return err
}
return nil
}