/
secret.go
85 lines (70 loc) · 2.58 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package v1
import (
"net/http"
"github.com/riser-platform/riser-server/pkg/core"
"github.com/riser-platform/riser-server/pkg/environment"
"github.com/labstack/echo/v4"
"github.com/pkg/errors"
"github.com/riser-platform/riser-server/api/v1/model"
"github.com/riser-platform/riser-server/pkg/git"
"github.com/riser-platform/riser-server/pkg/secret"
"github.com/riser-platform/riser-server/pkg/state"
)
func PutSecret(c echo.Context, stateRepo git.Repo, secretService secret.Service, environmentService environment.Service) error {
unsealedSecret := &model.UnsealedSecret{}
err := c.Bind(unsealedSecret)
if err != nil {
return errors.Wrap(err, "Error binding secret")
}
err = environmentService.ValidateDeployable(unsealedSecret.Environment)
if err != nil {
return err
}
err = secretService.SealAndSave(
unsealedSecret.PlainText,
mapSecretMetaFromModel(&unsealedSecret.SecretMeta),
state.NewGitCommitter(stateRepo))
if err == core.ErrConflictNewerVersion {
return echo.NewHTTPError(http.StatusConflict, "A newer revision of the secret was saved while attempting to save this secret. This is usually caused by a race condition due to another user saving the secret at the same time.")
}
return err
}
func GetSecrets(c echo.Context, secrets core.SecretMetaRepository, environmentService environment.Service) error {
envName := c.Param("envName")
namespace := c.Param("namespace")
appName := c.Param("appName")
err := environmentService.ValidateDeployable(envName)
if err != nil {
return err
}
secretMetas, err := secrets.ListByAppInEnvironment(core.NewNamespacedName(appName, namespace), envName)
if err != nil {
return err
}
return c.JSON(http.StatusOK, mapSecretMetaStatusArrayFromDomain(secretMetas))
}
func mapSecretMetaStatusFromDomain(domain core.SecretMeta) model.SecretMetaStatus {
return model.SecretMetaStatus{
SecretMeta: model.SecretMeta{
AppName: model.AppName(domain.App.Name),
Namespace: model.NamespaceName(domain.App.Namespace),
Environment: domain.EnvironmentName,
Name: domain.Name,
},
Revision: domain.Revision,
}
}
func mapSecretMetaStatusArrayFromDomain(domainArray []core.SecretMeta) []model.SecretMetaStatus {
statuses := []model.SecretMetaStatus{}
for _, domain := range domainArray {
statuses = append(statuses, mapSecretMetaStatusFromDomain(domain))
}
return statuses
}
func mapSecretMetaFromModel(in *model.SecretMeta) *core.SecretMeta {
return &core.SecretMeta{
App: core.NewNamespacedName(string(in.AppName), string(in.Namespace)),
Name: in.Name,
EnvironmentName: in.Environment,
}
}