-
Notifications
You must be signed in to change notification settings - Fork 28
/
create.go
96 lines (80 loc) · 2.7 KB
/
create.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// SPDX-License-Identifier: Apache-2.0
package vault
import (
"context"
"fmt"
"strings"
"github.com/sirupsen/logrus"
api "github.com/go-vela/server/api/types"
"github.com/go-vela/server/constants"
database "github.com/go-vela/server/database/types"
)
// Create creates a new secret.
func (c *client) Create(ctx context.Context, sType, org, name string, s *api.Secret) (*api.Secret, error) {
// create log fields from secret metadata
fields := logrus.Fields{
"org": org,
"repo": name,
"secret": s.GetName(),
"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": s.GetName(),
"type": sType,
}
}
c.Logger.WithFields(fields).Tracef("creating vault %s secret %s for %s/%s", sType, s.GetName(), org, name)
// validate the secret
err := database.SecretFromAPI(s).Validate()
if err != nil {
return nil, err
}
// convert our secret to a Vault secret
vault := vaultFromSecret(s)
// create the secret for the Vault service
switch sType {
case constants.SecretOrg:
return c.createOrg(org, s.GetName(), vault.Data)
case constants.SecretRepo:
return c.createRepo(org, name, s.GetName(), vault.Data)
case constants.SecretShared:
return c.createShared(org, name, s.GetName(), vault.Data)
default:
return nil, fmt.Errorf("invalid secret type: %v", sType)
}
}
// createOrg is a helper function to create
// the org secret for the provided path.
func (c *client) createOrg(org, path string, data map[string]interface{}) (*api.Secret, error) {
return c.create(fmt.Sprintf("%s/org/%s/%s", c.config.Prefix, org, path), data)
}
// createRepo is a helper function to create
// the repo secret for the provided path.
func (c *client) createRepo(org, repo, path string, data map[string]interface{}) (*api.Secret, error) {
return c.create(fmt.Sprintf("%s/repo/%s/%s/%s", c.config.Prefix, org, repo, path), data)
}
// createShared is a helper function to create
// the shared secret for the provided path.
func (c *client) createShared(org, team, path string, data map[string]interface{}) (*api.Secret, error) {
return c.create(fmt.Sprintf("%s/shared/%s/%s/%s", c.config.Prefix, org, team, path), data)
}
// create is a helper function to create
// the secret for the provided path.
func (c *client) create(path string, data map[string]interface{}) (*api.Secret, error) {
if strings.HasPrefix("secret/data", c.config.Prefix) {
data = map[string]interface{}{
"data": data,
}
}
// send API call to create the secret
s, err := c.Vault.Logical().Write(path, data)
if err != nil {
return nil, err
}
return secretFromVault(s), nil
}