Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(db): return secret on created / updated #928

Merged
merged 1 commit into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions api/admin/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func UpdateSecret(c *gin.Context) {
}

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

Expand All @@ -75,5 +75,5 @@ func UpdateSecret(c *gin.Context) {
return
}

c.JSON(http.StatusOK, input)
c.JSON(http.StatusOK, s)
}
4 changes: 1 addition & 3 deletions api/secret/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func CreateSecret(c *gin.Context) {
}

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

Expand All @@ -238,7 +238,5 @@ func CreateSecret(c *gin.Context) {
return
}

s, _ := secret.FromContext(c, e).Get(t, o, n, input.GetName())

c.JSON(http.StatusOK, s.Sanitize())
}
5 changes: 1 addition & 4 deletions api/secret/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func UpdateSecret(c *gin.Context) {
}

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

Expand All @@ -170,8 +170,5 @@ func UpdateSecret(c *gin.Context) {
return
}

// send API call to capture the updated secret
secret, _ := secret.FromContext(c, e).Get(t, o, n, input.GetName())

c.JSON(http.StatusOK, secret.Sanitize())
}
2 changes: 1 addition & 1 deletion api/webhook/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ func renameRepository(h *library.Hook, r *library.Repo, c *gin.Context, m *types
secret.SetOrg(r.GetOrg())
secret.SetRepo(r.GetName())

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

// create the secrets
for _, secret := range resources.Secrets {
err := db.CreateSecret(secret)
_, err := db.CreateSecret(secret)
if err != nil {
t.Errorf("unable to create secret %d: %v", secret.GetID(), err)
}
Expand Down Expand Up @@ -1226,16 +1226,11 @@ func testSecrets(t *testing.T, db Interface, resources *Resources) {
// update the secrets
for _, secret := range resources.Secrets {
secret.SetUpdatedAt(time.Now().UTC().Unix())
err = db.UpdateSecret(secret)
got, err := db.UpdateSecret(secret)
if err != nil {
t.Errorf("unable to update secret %d: %v", secret.GetID(), err)
}

// lookup the secret by ID
got, err := db.GetSecret(secret.GetID())
if err != nil {
t.Errorf("unable to get secret %d by ID: %v", secret.GetID(), err)
}
if !reflect.DeepEqual(got, secret) {
t.Errorf("GetSecret() is %v, want %v", got, secret)
}
Expand Down
4 changes: 2 additions & 2 deletions database/secret/count_org_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,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(_secretOne)
if err != nil {
t.Errorf("unable to create test secret for sqlite: %v", err)
}

err = _sqlite.CreateSecret(_secretTwo)
_, err = _sqlite.CreateSecret(_secretTwo)
if err != nil {
t.Errorf("unable to create test secret for sqlite: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions database/secret/count_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ func TestSecret_Engine_CountSecretsForRepo(t *testing.T) {
_sqlite := testSqlite(t)
defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }()

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

err = _sqlite.CreateSecret(_secretTwo)
_, err = _sqlite.CreateSecret(_secretTwo)
if err != nil {
t.Errorf("unable to create test secret for sqlite: %v", err)
}
Expand Down
8 changes: 4 additions & 4 deletions database/secret/count_team_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ func TestSecret_Engine_CountSecretsForTeam(t *testing.T) {
_sqlite := testSqlite(t)
defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }()

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

err = _sqlite.CreateSecret(_secretTwo)
_, err = _sqlite.CreateSecret(_secretTwo)
if err != nil {
t.Errorf("unable to create test secret for sqlite: %v", err)
}
Expand Down Expand Up @@ -158,12 +158,12 @@ func TestSecret_Engine_CountSecretsForTeams(t *testing.T) {
_sqlite := testSqlite(t)
defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }()

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

err = _sqlite.CreateSecret(_secretTwo)
_, err = _sqlite.CreateSecret(_secretTwo)
if err != nil {
t.Errorf("unable to create test secret for sqlite: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions database/secret/count_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ func TestSecret_Engine_CountSecrets(t *testing.T) {
_sqlite := testSqlite(t)
defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }()

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

err = _sqlite.CreateSecret(_secretTwo)
_, err = _sqlite.CreateSecret(_secretTwo)
if err != nil {
t.Errorf("unable to create test repo for sqlite: %v", err)
}
Expand Down
32 changes: 23 additions & 9 deletions database/secret/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

// CreateSecret creates a new secret in the database.
func (e *engine) CreateSecret(s *library.Secret) error {
func (e *engine) CreateSecret(s *library.Secret) (*library.Secret, error) {
// handle the secret based off the type
switch s.GetType() {
case constants.SecretShared:
Expand Down Expand Up @@ -44,7 +44,7 @@ func (e *engine) CreateSecret(s *library.Secret) error {
// https://pkg.go.dev/github.com/go-vela/types/database#Secret.Validate
err := secret.Validate()
if err != nil {
return err
return nil, err
}

// encrypt the fields for the secret
Expand All @@ -54,15 +54,29 @@ func (e *engine) CreateSecret(s *library.Secret) error {
if err != nil {
switch s.GetType() {
case constants.SecretShared:
return fmt.Errorf("unable to encrypt secret %s/%s/%s/%s: %w", s.GetType(), s.GetOrg(), s.GetTeam(), s.GetName(), err)
return nil, fmt.Errorf("unable to encrypt secret %s/%s/%s/%s: %w", s.GetType(), s.GetOrg(), s.GetTeam(), s.GetName(), err)
default:
return fmt.Errorf("unable to encrypt secret %s/%s/%s/%s: %w", s.GetType(), s.GetOrg(), s.GetRepo(), s.GetName(), err)
return nil, fmt.Errorf("unable to encrypt secret %s/%s/%s/%s: %w", s.GetType(), s.GetOrg(), s.GetRepo(), s.GetName(), err)
}
}

// send query to the database
return e.client.
Table(constants.TableSecret).
Create(secret.Nullify()).
Error
// create secret record
result := e.client.Table(constants.TableSecret).Create(secret.Nullify())

if result.Error != nil {
return nil, result.Error
}

// decrypt the fields for the secret to return
err = secret.Decrypt(e.config.EncryptionKey)
if err != nil {
switch s.GetType() {
case constants.SecretShared:
return nil, fmt.Errorf("unable to decrypt secret %s/%s/%s/%s: %w", s.GetType(), s.GetOrg(), s.GetTeam(), s.GetName(), err)
default:
return nil, fmt.Errorf("unable to decrypt secret %s/%s/%s/%s: %w", s.GetType(), s.GetOrg(), s.GetRepo(), s.GetName(), err)
}
}

return secret.ToLibrary(), nil
}
7 changes: 6 additions & 1 deletion database/secret/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package secret

import (
"reflect"
"testing"

"github.com/DATA-DOG/go-sqlmock"
Expand Down Expand Up @@ -127,7 +128,7 @@ VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14) RETURNING "id"`).
// run tests
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := test.database.CreateSecret(test.secret)
got, err := test.database.CreateSecret(test.secret)

if test.failure {
if err == nil {
Expand All @@ -140,6 +141,10 @@ VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14) RETURNING "id"`).
if err != nil {
t.Errorf("CreateSecret for %s returned err: %v", test.name, err)
}

if !reflect.DeepEqual(got, test.secret) {
t.Errorf("CreateSecret is %s, want %s", got, test.secret)
}
})
}
}
6 changes: 3 additions & 3 deletions database/secret/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,17 @@ func TestSecret_Engine_DeleteSecret(t *testing.T) {
_sqlite := testSqlite(t)
defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }()

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

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

err = _sqlite.CreateSecret(_secretShared)
_, err = _sqlite.CreateSecret(_secretShared)
if err != nil {
t.Errorf("unable to create test shared secret for sqlite: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion database/secret/get_org_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestSecret_Engine_GetSecretForOrg(t *testing.T) {
_sqlite := testSqlite(t)
defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }()

err := _sqlite.CreateSecret(_secret)
_, err := _sqlite.CreateSecret(_secret)
if err != nil {
t.Errorf("unable to create test secret for sqlite: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion database/secret/get_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestSecret_Engine_GetSecretForRepo(t *testing.T) {
_sqlite := testSqlite(t)
defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }()

err := _sqlite.CreateSecret(_secret)
_, err := _sqlite.CreateSecret(_secret)
if err != nil {
t.Errorf("unable to create test secret for sqlite: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion database/secret/get_team_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestSecret_Engine_GetSecretForTeam(t *testing.T) {
_sqlite := testSqlite(t)
defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }()

err := _sqlite.CreateSecret(_secret)
_, err := _sqlite.CreateSecret(_secret)
if err != nil {
t.Errorf("unable to create test secret for sqlite: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion database/secret/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestSecret_Engine_GetSecret(t *testing.T) {
_sqlite := testSqlite(t)
defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }()

err := _sqlite.CreateSecret(_secret)
_, err := _sqlite.CreateSecret(_secret)
if err != nil {
t.Errorf("unable to create test secret for sqlite: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions database/secret/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type SecretInterface interface {
// CountSecretsForTeams defines a function that gets the count of secrets by teams within an org.
CountSecretsForTeams(string, []string, map[string]interface{}) (int64, error)
// CreateSecret defines a function that creates a new secret.
CreateSecret(*library.Secret) error
CreateSecret(*library.Secret) (*library.Secret, error)
// DeleteSecret defines a function that deletes an existing secret.
DeleteSecret(*library.Secret) error
// GetSecret defines a function that gets a secret by ID.
Expand All @@ -59,5 +59,5 @@ type SecretInterface interface {
// ListSecretsForTeams defines a function that gets a list of secrets by teams within an org.
ListSecretsForTeams(string, []string, map[string]interface{}, int, int) ([]*library.Secret, int64, error)
// UpdateSecret defines a function that updates an existing secret.
UpdateSecret(*library.Secret) error
UpdateSecret(*library.Secret) (*library.Secret, error)
}
4 changes: 2 additions & 2 deletions database/secret/list_org_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ func TestSecret_Engine_ListSecretsForOrg(t *testing.T) {
_sqlite := testSqlite(t)
defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }()

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

err = _sqlite.CreateSecret(_secretTwo)
_, err = _sqlite.CreateSecret(_secretTwo)
if err != nil {
t.Errorf("unable to create test secret for sqlite: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions database/secret/list_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ func TestSecret_Engine_ListSecretsForRepo(t *testing.T) {
_sqlite := testSqlite(t)
defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }()

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

err = _sqlite.CreateSecret(_secretTwo)
_, err = _sqlite.CreateSecret(_secretTwo)
if err != nil {
t.Errorf("unable to create test secret for sqlite: %v", err)
}
Expand Down
8 changes: 4 additions & 4 deletions database/secret/list_team_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ func TestSecret_Engine_ListSecretsForTeam(t *testing.T) {
_sqlite := testSqlite(t)
defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }()

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

err = _sqlite.CreateSecret(_secretTwo)
_, err = _sqlite.CreateSecret(_secretTwo)
if err != nil {
t.Errorf("unable to create test secret for sqlite: %v", err)
}
Expand Down Expand Up @@ -169,12 +169,12 @@ func TestSecret_Engine_ListSecretsForTeams(t *testing.T) {
_sqlite := testSqlite(t)
defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }()

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

err = _sqlite.CreateSecret(_secretTwo)
_, err = _sqlite.CreateSecret(_secretTwo)
if err != nil {
t.Errorf("unable to create test secret for sqlite: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions database/secret/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ func TestSecret_Engine_ListSecrets(t *testing.T) {
_sqlite := testSqlite(t)
defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }()

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

err = _sqlite.CreateSecret(_secretTwo)
_, err = _sqlite.CreateSecret(_secretTwo)
if err != nil {
t.Errorf("unable to create test secret for sqlite: %v", err)
}
Expand Down
Loading