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(database): move secret logic into separate package #782

Merged
merged 92 commits into from
Apr 10, 2023

Conversation

jbrockopp
Copy link
Contributor

@jbrockopp jbrockopp commented Mar 4, 2023

Based off of #574, #663, #687, #692 #721 and #722

This change continues the refactor efforts initially introduced in the above PRs.

This adds a new secret package to the github.com/go-vela/server/database package.

This contains a SecretService interface declaring all functions necessary for worker based interactions with the database:

// SecretService represents the Vela interface for secret
// functions with the supported Database backends.
//
//nolint:revive // ignore name stutter
type SecretService interface {
// Secret Data Definition Language Functions
//
// https://en.wikipedia.org/wiki/Data_definition_language
// CreateSecretIndexes defines a function that creates the indexes for the secrets table.
CreateSecretIndexes() error
// CreateSecretTable defines a function that creates the secrets table.
CreateSecretTable(string) error
// Secret Data Manipulation Language Functions
//
// https://en.wikipedia.org/wiki/Data_manipulation_language
// CountSecrets defines a function that gets the count of all secrets.
CountSecrets() (int64, error)
// CountSecretsForOrg defines a function that gets the count of secrets by org name.
CountSecretsForOrg(string, map[string]interface{}) (int64, error)
// CountSecretsForRepo defines a function that gets the count of secrets by org and repo name.
CountSecretsForRepo(*library.Repo, map[string]interface{}) (int64, error)
// CountSecretsForTeam defines a function that gets the count of secrets by org and team name.
CountSecretsForTeam(string, string, map[string]interface{}) (int64, error)
// 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
// DeleteSecret defines a function that deletes an existing secret.
DeleteSecret(*library.Secret) error
// GetSecret defines a function that gets a secret by ID.
GetSecret(int64) (*library.Secret, error)
// GetSecretForOrg defines a function that gets a secret by org name.
GetSecretForOrg(string, string) (*library.Secret, error)
// GetSecretForRepo defines a function that gets a secret by org and repo name.
GetSecretForRepo(string, *library.Repo) (*library.Secret, error)
// GetSecretForTeam defines a function that gets a secret by org and team name.
GetSecretForTeam(string, string, string) (*library.Secret, error)
// ListSecrets defines a function that gets a list of all secrets.
ListSecrets() ([]*library.Secret, error)
// ListSecretsForOrg defines a function that gets a list of secrets by org name.
ListSecretsForOrg(string, map[string]interface{}, int, int) ([]*library.Secret, int64, error)
// ListSecretsForRepo defines a function that gets a list of secrets by org and repo name.
ListSecretsForRepo(*library.Repo, map[string]interface{}, int, int) ([]*library.Secret, int64, error)
// ListSecretsForTeam defines a function that gets a list of secrets by org and team name.
ListSecretsForTeam(string, string, map[string]interface{}, int, int) ([]*library.Secret, int64, error)
// 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
}

This package also contains the engine which implements the above service interface:

// engine represents the secret functionality that implements the SecretService interface.
engine struct {
// engine configuration settings used in secret functions
config *config
// gorm.io/gorm database client used in secret functions
//
// https://pkg.go.dev/gorm.io/gorm#DB
client *gorm.DB
// sirupsen/logrus logger used in secret functions
//
// https://pkg.go.dev/github.com/sirupsen/logrus#Entry
logger *logrus.Entry
}

This engine contains no raw SQL queries for integrating with the secrets table.

Instead, we leverage our DB library's (https://gorm.io/) agnostic abstraction for integrating with that table.

jbrockopp and others added 30 commits February 23, 2022 13:45
Copy link
Member

@cognifloyd cognifloyd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is so much cleaner! Thanks for working on this.

I started reviewing and have a few comments/requests about handling all secret types.

database/secret/create.go Show resolved Hide resolved
database/secret/delete.go Show resolved Hide resolved
database/secret/update.go Show resolved Hide resolved
Copy link
Member

@cognifloyd cognifloyd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the count methods, the filters arg looks like a new feature. Where/when might this be used?

Also, where are we planning to use the count all secrets (database CountSecrets) method? I don't see anything that uses it in the native secrets engine.

edit: Oh. I see CountSecrets is used by the ListSecrets method. And that is probably useful for migrations or to rotate the encryption key or similar.

database/secret/count_team.go Show resolved Hide resolved
database/secret/count_team.go Show resolved Hide resolved
Copy link
Member

@cognifloyd cognifloyd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I finished my first pass through this PR. This feels a lot cleaner.

I just noticed a couple bits of copy pasta to clean up.

database/secret/service.go Outdated Show resolved Hide resolved
database/secret/service.go Outdated Show resolved Hide resolved
cognifloyd
cognifloyd previously approved these changes Mar 28, 2023
Copy link
Member

@cognifloyd cognifloyd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome. Thank you!

plyr4
plyr4 previously requested changes Mar 30, 2023
Copy link
Contributor

@plyr4 plyr4 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extremely minor comment typos

database/secret/opts.go Outdated Show resolved Hide resolved
database/secret/secret_test.go Outdated Show resolved Hide resolved
database/secret/secret_test.go Outdated Show resolved Hide resolved
@jbrockopp jbrockopp added the enhancement Indicates an improvement to a feature label Apr 8, 2023
Copy link
Contributor

@ecrupper ecrupper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice — LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Indicates an improvement to a feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants