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

feat(secrets): add secret metadata #206

Merged
merged 16 commits into from
Nov 17, 2021

Conversation

ecrupper
Copy link
Contributor

Adding more metadata for secrets including the created time, user that created the secret, last updated time, and the build id of the last build where it was used

@ecrupper ecrupper requested a review from a team as a code owner October 29, 2021 16:33
@codecov
Copy link

codecov bot commented Oct 29, 2021

Codecov Report

Merging #206 (4a01d3f) into master (8e493b9) will increase coverage by 0.04%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #206      +/-   ##
==========================================
+ Coverage   96.66%   96.70%   +0.04%     
==========================================
  Files          53       53              
  Lines        5509     5589      +80     
==========================================
+ Hits         5325     5405      +80     
  Misses        137      137              
  Partials       47       47              
Impacted Files Coverage Δ
database/secret.go 100.00% <100.00%> (ø)
library/secret.go 100.00% <100.00%> (ø)

library/secret.go Outdated Show resolved Hide resolved
@@ -26,6 +26,10 @@ type Secret struct {
Images *[]string `json:"images,omitempty"`
Events *[]string `json:"events,omitempty"`
AllowCommand *bool `json:"allow_command,omitempty"`
CreateTime *string `json:"create_time,omitempty"`
CreatedBy *string `json:"created_by,omitempty"`
UpdateTime *string `json:"update_time,omitempty"`
Copy link
Contributor

Choose a reason for hiding this comment

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

Thoughts on making this UpdatedAt?

I think that is a more common naming convention for a field like this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

Copy link
Contributor Author

@ecrupper ecrupper Nov 2, 2021

Choose a reason for hiding this comment

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

Hilariously, updated_at seems to be a Postgres trigger keyword (at least in the testing DB), which causes it to override any user input and instead put the .Now() timestamp in the field. Any thoughts on a different name? CreatedTs and UpdatedTs maybe? Open to suggestions

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is actually related to the Golang library we're using for this:

https://github.com/go-gorm/gorm

I found these docs which appear to describe the issue you're referencing:

https://gorm.io/docs/models.html#Creating-Updating-Time-Unix-Milli-Nano-Seconds-Tracking

This issue might be related?

go-gorm/gorm#4827

library/secret.go Show resolved Hide resolved
Copy link
Collaborator

@wass3r wass3r left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! Few additional things:

  1. Assuming that CreatedBy and UpdatedBy will reference User IDs, we should match the type, ie. change from string to int64.
  2. I think we'll need to add these new fields to /database/secret.go as well
  3. No action for this PR, but just a reminder that we will need to have an accompanying PR that adds the fields in https://github.com/go-vela/server/tree/master/database as well

@wass3r wass3r changed the title Feat: add secret metadata feat(secrets): add secret metadata Oct 31, 2021
Copy link
Contributor

@jbrockopp jbrockopp left a comment

Choose a reason for hiding this comment

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

Thanks for updating this with the previous changes! 🎉

One more thing I noticed as I was reviewing:

database/secret_test.go Outdated Show resolved Hide resolved
@kneal
Copy link

kneal commented Nov 2, 2021

Just a clarifying question, LastBuildID is that going to be the last build to pull the secret?

@kneal kneal added the feature Indicates a new feature label Nov 2, 2021
@ecrupper
Copy link
Contributor Author

ecrupper commented Nov 2, 2021

Just a clarifying question, LastBuildID is that going to be the last build to pull the secret?

Yes that is the current plan

@kneal
Copy link

kneal commented Nov 2, 2021

Cool! Just checking, I generally recommend linking the issue in the body of the PR. Helps with that extra context and create circular deps to the PR to issue etc.

@ecrupper
Copy link
Contributor Author

ecrupper commented Nov 2, 2021

Cool! Just checking, I generally recommend linking the issue in the body of the PR. Helps with that extra context and create circular deps to the PR to issue etc.

Absolutely! It was an internal request that wasn't duplicated to the community issue board yet. Here it is for reference: https://github.com/go-vela/community/issues/428

- adding functionality for this attribute increases scope by a large margin
- will start with this smaller change (timestamps and authors)
@ecrupper
Copy link
Contributor Author

ecrupper commented Nov 4, 2021

https://github.com/go-vela/server/pull/526

Draft PR in the go-vela/server that would accompany this one

Comment on lines 57 to 60
CreatedAt sql.NullInt64 `sql:"created_at"`
CreatedBy sql.NullInt64 `sql:"created_by"`
UpdatedAt sql.NullInt64 `sql:"updated_at"`
UpdatedBy sql.NullInt64 `sql:"updated_by"`
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it safe to assume that CreatedBy and UpdatedBy are int64 types since they'll store the user's ID?

types/database/user.go

Lines 48 to 58 in 911ffbe

// User is the database representation of a user.
type User struct {
ID sql.NullInt64 `sql:"id"`
Name sql.NullString `sql:"name"`
RefreshToken sql.NullString `sql:"refresh_token"`
Token sql.NullString `sql:"token"`
Hash sql.NullString `sql:"hash"`
Favorites pq.StringArray `sql:"favorites" gorm:"type:varchar(5000)"`
Active sql.NullBool `sql:"active"`
Admin sql.NullBool `sql:"admin"`
}

IIRC, they were string types earlier which is why I'm asking.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes after taking a look at that User struct, I thought it best to change those to int64 types. Originally I thought it might be stored as a name, but I believe int64 is best.

Copy link
Contributor

@jbrockopp jbrockopp Nov 12, 2021

Choose a reason for hiding this comment

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

@ecrupper ok thanks for the extra information 👍

Out of curiosity, could you elaborate on why int64 is the approach we're choosing?

I think I tend to agree that from the data storage perspective, int64 is better than string for the database.

However, my concern with this approach has to do with data access and usage patterns.

Today, there isn't a great way to query a user by their ID in Vela.

So if I'm looking at a secret with the UI/CLI/API, I would only be able to see the user's ID who created/updated it.

To actually see what user created/updated the secret, I'd have to ask an admin "Who is user <id> in Vela?"

Perhaps one way to address this is to add a new endpoint to Vela which allows looking a user up by their ID?

Otherwise we could consider storing the user's Name rather than ID in the secret object?

What are your thoughts on this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a great point. Originally I was thinking in terms of consistency (if there's a User table with IDs as their primary key, it would make sense to have any reference to a User in another table be their ID). However, after considering the end user as you said, I agree that it should be a string. Not only would it save the UI an extra API call, but considering the CLI, running another command to find the name associated with a User ID seems tedious. I'll make that change!

Copy link
Collaborator

Choose a reason for hiding this comment

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

fwiw, i proposed user id initially to be consistent with the repo object in the repos table where we use int64 for user id. another instance where user name could be beneficial when trying to figure out who "owns" a repo.

in the future we might consider embedding the user struct in the response similar to how github does it for some of it's objects (eg. check response here: https://docs.github.com/en/rest/reference/issues#get-an-issue).

(waiting for @JordanSussman to chime in with the obligatory GraphQL commentary 😄)

thanks for the work and revisions @ecrupper !

Copy link
Collaborator

@JordanSussman JordanSussman Nov 16, 2021

Choose a reason for hiding this comment

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

(waiting for @JordanSussman to chime in with the obligatory GraphQL commentary 😄)

Don't tempt me with a good time! Would love to add a v2 API with graphql and/or parameters to allow parsed output.

graphql

library/secret.go Outdated Show resolved Hide resolved
library/secret.go Outdated Show resolved Hide resolved
Copy link
Contributor

@jbrockopp jbrockopp left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link

@kneal kneal left a comment

Choose a reason for hiding this comment

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

LGTM 🐬

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

Successfully merging this pull request may close these issues.

5 participants