Skip to content

Commit

Permalink
enhance: add context to Secrets (#951)
Browse files Browse the repository at this point in the history
Co-authored-by: David May <[email protected]>
  • Loading branch information
plyr4 and wass3rw3rk authored Sep 7, 2023
1 parent ccc46bf commit 0464cb5
Show file tree
Hide file tree
Showing 72 changed files with 384 additions and 232 deletions.
5 changes: 4 additions & 1 deletion api/admin/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ import (
func UpdateSecret(c *gin.Context) {
logrus.Info("Admin: updating secret in database")

// capture middleware values
ctx := c.Request.Context()

// capture body from API request
input := new(library.Secret)

Expand All @@ -66,7 +69,7 @@ func UpdateSecret(c *gin.Context) {
}

// send API call to update the secret
s, err := database.FromContext(c).UpdateSecret(input)
s, err := database.FromContext(c).UpdateSecret(ctx, input)
if err != nil {
retErr := fmt.Errorf("unable to update secret %d: %w", input.GetID(), err)

Expand Down
3 changes: 2 additions & 1 deletion api/secret/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func CreateSecret(c *gin.Context) {
t := util.PathParameter(c, "type")
o := util.PathParameter(c, "org")
n := util.PathParameter(c, "name")
ctx := c.Request.Context()

entry := fmt.Sprintf("%s/%s/%s", t, o, n)

Expand Down Expand Up @@ -229,7 +230,7 @@ func CreateSecret(c *gin.Context) {
}

// send API call to create the secret
s, err := secret.FromContext(c, e).Create(t, o, n, input)
s, err := secret.FromContext(c, e).Create(ctx, t, o, n, input)
if err != nil {
retErr := fmt.Errorf("unable to create secret %s for %s service: %w", entry, e, err)

Expand Down
3 changes: 2 additions & 1 deletion api/secret/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func DeleteSecret(c *gin.Context) {
o := util.PathParameter(c, "org")
n := util.PathParameter(c, "name")
s := strings.TrimPrefix(util.PathParameter(c, "secret"), "/")
ctx := c.Request.Context()

entry := fmt.Sprintf("%s/%s/%s/%s", t, o, n, s)

Expand Down Expand Up @@ -108,7 +109,7 @@ func DeleteSecret(c *gin.Context) {
logrus.WithFields(fields).Infof("deleting secret %s from %s service", entry, e)

// send API call to remove the secret
err := secret.FromContext(c, e).Delete(t, o, n, s)
err := secret.FromContext(c, e).Delete(ctx, t, o, n, s)
if err != nil {
retErr := fmt.Errorf("unable to delete secret %s from %s service: %w", entry, e, err)

Expand Down
3 changes: 2 additions & 1 deletion api/secret/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func GetSecret(c *gin.Context) {
o := util.PathParameter(c, "org")
n := util.PathParameter(c, "name")
s := strings.TrimPrefix(util.PathParameter(c, "secret"), "/")
ctx := c.Request.Context()

entry := fmt.Sprintf("%s/%s/%s/%s", t, o, n, s)

Expand Down Expand Up @@ -110,7 +111,7 @@ func GetSecret(c *gin.Context) {
logrus.WithFields(fields).Infof("reading secret %s from %s service", entry, e)

// send API call to capture the secret
secret, err := secret.FromContext(c, e).Get(t, o, n, s)
secret, err := secret.FromContext(c, e).Get(ctx, t, o, n, s)
if err != nil {
retErr := fmt.Errorf("unable to get secret %s from %s service: %w", entry, e, err)

Expand Down
5 changes: 3 additions & 2 deletions api/secret/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func ListSecrets(c *gin.Context) {
t := util.PathParameter(c, "type")
o := util.PathParameter(c, "org")
n := util.PathParameter(c, "name")
ctx := c.Request.Context()

var teams []string
// get list of user's teams if type is shared secret and team is '*'
Expand Down Expand Up @@ -164,7 +165,7 @@ func ListSecrets(c *gin.Context) {
}

// send API call to capture the total number of secrets
total, err := secret.FromContext(c, e).Count(t, o, n, teams)
total, err := secret.FromContext(c, e).Count(ctx, t, o, n, teams)
if err != nil {
retErr := fmt.Errorf("unable to get secret count for %s from %s service: %w", entry, e, err)

Expand All @@ -177,7 +178,7 @@ func ListSecrets(c *gin.Context) {
perPage = util.MaxInt(1, util.MinInt(100, perPage))

// send API call to capture the list of secrets
s, err := secret.FromContext(c, e).List(t, o, n, page, perPage, teams)
s, err := secret.FromContext(c, e).List(ctx, t, o, n, page, perPage, teams)
if err != nil {
retErr := fmt.Errorf("unable to list secrets for %s from %s service: %w", entry, e, err)

Expand Down
3 changes: 2 additions & 1 deletion api/secret/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func UpdateSecret(c *gin.Context) {
o := util.PathParameter(c, "org")
n := util.PathParameter(c, "name")
s := strings.TrimPrefix(util.PathParameter(c, "secret"), "/")
ctx := c.Request.Context()

entry := fmt.Sprintf("%s/%s/%s/%s", t, o, n, s)

Expand Down Expand Up @@ -161,7 +162,7 @@ func UpdateSecret(c *gin.Context) {
}

// send API call to update the secret
secret, err := secret.FromContext(c, e).Update(t, o, n, input)
secret, err := secret.FromContext(c, e).Update(ctx, t, o, n, input)
if err != nil {
retErr := fmt.Errorf("unable to update secret %s for %s service: %w", entry, e, err)

Expand Down
6 changes: 3 additions & 3 deletions api/webhook/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ func renameRepository(ctx context.Context, h *library.Hook, r *library.Repo, c *
}

// get total number of secrets associated with repository
t, err := database.FromContext(c).CountSecretsForRepo(dbR, map[string]interface{}{})
t, err := database.FromContext(c).CountSecretsForRepo(ctx, dbR, map[string]interface{}{})
if err != nil {
return nil, fmt.Errorf("unable to get secret count for repo %s/%s: %w", dbR.GetOrg(), dbR.GetName(), err)
}
Expand All @@ -822,7 +822,7 @@ func renameRepository(ctx context.Context, h *library.Hook, r *library.Repo, c *
page := 1
// capture all secrets belonging to certain repo in database
for repoSecrets := int64(0); repoSecrets < t; repoSecrets += 100 {
s, _, err := database.FromContext(c).ListSecretsForRepo(dbR, map[string]interface{}{}, page, 100)
s, _, err := database.FromContext(c).ListSecretsForRepo(ctx, dbR, map[string]interface{}{}, page, 100)
if err != nil {
return nil, fmt.Errorf("unable to get secret list for repo %s/%s: %w", dbR.GetOrg(), dbR.GetName(), err)
}
Expand All @@ -837,7 +837,7 @@ func renameRepository(ctx context.Context, h *library.Hook, r *library.Repo, c *
secret.SetOrg(r.GetOrg())
secret.SetRepo(r.GetName())

_, err = database.FromContext(c).UpdateSecret(secret)
_, err = database.FromContext(c).UpdateSecret(ctx, secret)
if err != nil {
return nil, fmt.Errorf("unable to update secret for repo %s/%s: %w", dbR.GetOrg(), dbR.GetName(), err)
}
Expand Down
32 changes: 16 additions & 16 deletions database/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1115,15 +1115,15 @@ func testSecrets(t *testing.T, db Interface, resources *Resources) {

// create the secrets
for _, secret := range resources.Secrets {
_, err := db.CreateSecret(secret)
_, err := db.CreateSecret(context.TODO(), secret)
if err != nil {
t.Errorf("unable to create secret %d: %v", secret.GetID(), err)
}
}
methods["CreateSecret"] = true

// count the secrets
count, err := db.CountSecrets()
count, err := db.CountSecrets(context.TODO())
if err != nil {
t.Errorf("unable to count secrets: %v", err)
}
Expand All @@ -1136,7 +1136,7 @@ func testSecrets(t *testing.T, db Interface, resources *Resources) {
switch secret.GetType() {
case constants.SecretOrg:
// count the secrets for an org
count, err = db.CountSecretsForOrg(secret.GetOrg(), nil)
count, err = db.CountSecretsForOrg(context.TODO(), secret.GetOrg(), nil)
if err != nil {
t.Errorf("unable to count secrets for org %s: %v", secret.GetOrg(), err)
}
Expand All @@ -1146,7 +1146,7 @@ func testSecrets(t *testing.T, db Interface, resources *Resources) {
methods["CountSecretsForOrg"] = true
case constants.SecretRepo:
// count the secrets for a repo
count, err = db.CountSecretsForRepo(resources.Repos[0], nil)
count, err = db.CountSecretsForRepo(context.TODO(), resources.Repos[0], nil)
if err != nil {
t.Errorf("unable to count secrets for repo %d: %v", resources.Repos[0].GetID(), err)
}
Expand All @@ -1156,7 +1156,7 @@ func testSecrets(t *testing.T, db Interface, resources *Resources) {
methods["CountSecretsForRepo"] = true
case constants.SecretShared:
// count the secrets for a team
count, err = db.CountSecretsForTeam(secret.GetOrg(), secret.GetTeam(), nil)
count, err = db.CountSecretsForTeam(context.TODO(), secret.GetOrg(), secret.GetTeam(), nil)
if err != nil {
t.Errorf("unable to count secrets for team %s: %v", secret.GetTeam(), err)
}
Expand All @@ -1166,7 +1166,7 @@ func testSecrets(t *testing.T, db Interface, resources *Resources) {
methods["CountSecretsForTeam"] = true

// count the secrets for a list of teams
count, err = db.CountSecretsForTeams(secret.GetOrg(), []string{secret.GetTeam()}, nil)
count, err = db.CountSecretsForTeams(context.TODO(), secret.GetOrg(), []string{secret.GetTeam()}, nil)
if err != nil {
t.Errorf("unable to count secrets for teams %s: %v", []string{secret.GetTeam()}, err)
}
Expand All @@ -1180,7 +1180,7 @@ func testSecrets(t *testing.T, db Interface, resources *Resources) {
}

// list the secrets
list, err := db.ListSecrets()
list, err := db.ListSecrets(context.TODO())
if err != nil {
t.Errorf("unable to list secrets: %v", err)
}
Expand All @@ -1193,7 +1193,7 @@ func testSecrets(t *testing.T, db Interface, resources *Resources) {
switch secret.GetType() {
case constants.SecretOrg:
// list the secrets for an org
list, count, err = db.ListSecretsForOrg(secret.GetOrg(), nil, 1, 10)
list, count, err = db.ListSecretsForOrg(context.TODO(), secret.GetOrg(), nil, 1, 10)
if err != nil {
t.Errorf("unable to list secrets for org %s: %v", secret.GetOrg(), err)
}
Expand All @@ -1206,7 +1206,7 @@ func testSecrets(t *testing.T, db Interface, resources *Resources) {
methods["ListSecretsForOrg"] = true
case constants.SecretRepo:
// list the secrets for a repo
list, count, err = db.ListSecretsForRepo(resources.Repos[0], nil, 1, 10)
list, count, err = db.ListSecretsForRepo(context.TODO(), resources.Repos[0], nil, 1, 10)
if err != nil {
t.Errorf("unable to list secrets for repo %d: %v", resources.Repos[0].GetID(), err)
}
Expand All @@ -1219,7 +1219,7 @@ func testSecrets(t *testing.T, db Interface, resources *Resources) {
methods["ListSecretsForRepo"] = true
case constants.SecretShared:
// list the secrets for a team
list, count, err = db.ListSecretsForTeam(secret.GetOrg(), secret.GetTeam(), nil, 1, 10)
list, count, err = db.ListSecretsForTeam(context.TODO(), secret.GetOrg(), secret.GetTeam(), nil, 1, 10)
if err != nil {
t.Errorf("unable to list secrets for team %s: %v", secret.GetTeam(), err)
}
Expand All @@ -1232,7 +1232,7 @@ func testSecrets(t *testing.T, db Interface, resources *Resources) {
methods["ListSecretsForTeam"] = true

// list the secrets for a list of teams
list, count, err = db.ListSecretsForTeams(secret.GetOrg(), []string{secret.GetTeam()}, nil, 1, 10)
list, count, err = db.ListSecretsForTeams(context.TODO(), secret.GetOrg(), []string{secret.GetTeam()}, nil, 1, 10)
if err != nil {
t.Errorf("unable to list secrets for teams %s: %v", []string{secret.GetTeam()}, err)
}
Expand All @@ -1252,7 +1252,7 @@ func testSecrets(t *testing.T, db Interface, resources *Resources) {
switch secret.GetType() {
case constants.SecretOrg:
// lookup the secret by org
got, err := db.GetSecretForOrg(secret.GetOrg(), secret.GetName())
got, err := db.GetSecretForOrg(context.TODO(), secret.GetOrg(), secret.GetName())
if err != nil {
t.Errorf("unable to get secret %d for org %s: %v", secret.GetID(), secret.GetOrg(), err)
}
Expand All @@ -1262,7 +1262,7 @@ func testSecrets(t *testing.T, db Interface, resources *Resources) {
methods["GetSecretForOrg"] = true
case constants.SecretRepo:
// lookup the secret by repo
got, err := db.GetSecretForRepo(secret.GetName(), resources.Repos[0])
got, err := db.GetSecretForRepo(context.TODO(), secret.GetName(), resources.Repos[0])
if err != nil {
t.Errorf("unable to get secret %d for repo %d: %v", secret.GetID(), resources.Repos[0].GetID(), err)
}
Expand All @@ -1272,7 +1272,7 @@ func testSecrets(t *testing.T, db Interface, resources *Resources) {
methods["GetSecretForRepo"] = true
case constants.SecretShared:
// lookup the secret by team
got, err := db.GetSecretForTeam(secret.GetOrg(), secret.GetTeam(), secret.GetName())
got, err := db.GetSecretForTeam(context.TODO(), secret.GetOrg(), secret.GetTeam(), secret.GetName())
if err != nil {
t.Errorf("unable to get secret %d for team %s: %v", secret.GetID(), secret.GetTeam(), err)
}
Expand All @@ -1288,7 +1288,7 @@ func testSecrets(t *testing.T, db Interface, resources *Resources) {
// update the secrets
for _, secret := range resources.Secrets {
secret.SetUpdatedAt(time.Now().UTC().Unix())
got, err := db.UpdateSecret(secret)
got, err := db.UpdateSecret(context.TODO(), secret)
if err != nil {
t.Errorf("unable to update secret %d: %v", secret.GetID(), err)
}
Expand All @@ -1302,7 +1302,7 @@ func testSecrets(t *testing.T, db Interface, resources *Resources) {

// delete the secrets
for _, secret := range resources.Secrets {
err = db.DeleteSecret(secret)
err = db.DeleteSecret(context.TODO(), secret)
if err != nil {
t.Errorf("unable to delete secret %d: %v", secret.GetID(), err)
}
Expand Down
1 change: 1 addition & 0 deletions database/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ func (e *engine) NewResources(ctx context.Context) error {
//
// https://pkg.go.dev/github.com/go-vela/server/database/secret#New
e.SecretInterface, err = secret.New(
secret.WithContext(e.ctx),
secret.WithClient(e.client),
secret.WithEncryptionKey(e.config.EncryptionKey),
secret.WithLogger(e.logger),
Expand Down
4 changes: 3 additions & 1 deletion database/secret/count.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
package secret

import (
"context"

"github.com/go-vela/types/constants"
)

// CountSecrets gets the count of all secrets from the database.
func (e *engine) CountSecrets() (int64, error) {
func (e *engine) CountSecrets(ctx context.Context) (int64, error) {
e.logger.Tracef("getting count of all secrets from the database")

// variable to store query results
Expand Down
4 changes: 3 additions & 1 deletion database/secret/count_org.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
package secret

import (
"context"

"github.com/go-vela/types/constants"
"github.com/sirupsen/logrus"
)

// CountSecretsForOrg gets the count of secrets by org name from the database.
func (e *engine) CountSecretsForOrg(org string, filters map[string]interface{}) (int64, error) {
func (e *engine) CountSecretsForOrg(ctx context.Context, org string, filters map[string]interface{}) (int64, error) {
e.logger.WithFields(logrus.Fields{
"org": org,
"type": constants.SecretOrg,
Expand Down
7 changes: 4 additions & 3 deletions database/secret/count_org_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package secret

import (
"context"
"reflect"
"testing"

Expand Down Expand Up @@ -51,12 +52,12 @@ func TestSecret_Engine_CountSecretsForOrg(t *testing.T) {
_sqlite := testSqlite(t)
defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }()

_, err := _sqlite.CreateSecret(_secretOne)
_, err := _sqlite.CreateSecret(context.TODO(), _secretOne)
if err != nil {
t.Errorf("unable to create test secret for sqlite: %v", err)
}

_, err = _sqlite.CreateSecret(_secretTwo)
_, err = _sqlite.CreateSecret(context.TODO(), _secretTwo)
if err != nil {
t.Errorf("unable to create test secret for sqlite: %v", err)
}
Expand Down Expand Up @@ -87,7 +88,7 @@ func TestSecret_Engine_CountSecretsForOrg(t *testing.T) {
// run tests
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := test.database.CountSecretsForOrg("foo", filters)
got, err := test.database.CountSecretsForOrg(context.TODO(), "foo", filters)

if test.failure {
if err == nil {
Expand Down
4 changes: 3 additions & 1 deletion database/secret/count_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
package secret

import (
"context"

"github.com/go-vela/types/constants"
"github.com/go-vela/types/library"
"github.com/sirupsen/logrus"
)

// CountSecretsForRepo gets the count of secrets by org and repo name from the database.
func (e *engine) CountSecretsForRepo(r *library.Repo, filters map[string]interface{}) (int64, error) {
func (e *engine) CountSecretsForRepo(ctx context.Context, r *library.Repo, filters map[string]interface{}) (int64, error) {
e.logger.WithFields(logrus.Fields{
"org": r.GetOrg(),
"repo": r.GetName(),
Expand Down
Loading

0 comments on commit 0464cb5

Please sign in to comment.