Skip to content

Commit

Permalink
enhance: add context to Users (#941)
Browse files Browse the repository at this point in the history
  • Loading branch information
plyr4 committed Sep 1, 2023
1 parent 38fae72 commit 91e26c4
Show file tree
Hide file tree
Showing 54 changed files with 252 additions and 124 deletions.
5 changes: 4 additions & 1 deletion api/admin/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ import (
func UpdateUser(c *gin.Context) {
logrus.Info("Admin: updating user in database")

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

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

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

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

Expand Down
8 changes: 5 additions & 3 deletions api/auth/get_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ func GetAuthToken(c *gin.Context) {
var err error

tm := c.MustGet("token-manager").(*token.Manager)
// capture middleware values
ctx := c.Request.Context()

// capture the OAuth state if present
oAuthState := c.Request.FormValue("state")
Expand Down Expand Up @@ -97,7 +99,7 @@ func GetAuthToken(c *gin.Context) {
}

// send API call to capture the user logging in
u, err := database.FromContext(c).GetUserForName(newUser.GetName())
u, err := database.FromContext(c).GetUserForName(ctx, newUser.GetName())
// create a new user account
if len(u.GetName()) == 0 || err != nil {
// create the user account
Expand All @@ -121,7 +123,7 @@ func GetAuthToken(c *gin.Context) {
u.SetRefreshToken(rt)

// send API call to create the user in the database
_, err = database.FromContext(c).CreateUser(u)
_, err = database.FromContext(c).CreateUser(ctx, u)
if err != nil {
retErr := fmt.Errorf("unable to create user %s: %w", u.GetName(), err)

Expand Down Expand Up @@ -154,7 +156,7 @@ func GetAuthToken(c *gin.Context) {
u.SetRefreshToken(rt)

// send API call to update the user in the database
_, err = database.FromContext(c).UpdateUser(u)
_, err = database.FromContext(c).UpdateUser(ctx, u)
if err != nil {
retErr := fmt.Errorf("unable to update user %s: %w", u.GetName(), err)

Expand Down
3 changes: 2 additions & 1 deletion api/auth/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func Logout(c *gin.Context) {
m := c.MustGet("metadata").(*types.Metadata)
// capture middleware values
u := user.Retrieve(c)
ctx := c.Request.Context()

// update engine logger with API metadata
//
Expand Down Expand Up @@ -75,7 +76,7 @@ func Logout(c *gin.Context) {
u.SetRefreshToken("")

// send API call to update the user in the database
_, err = database.FromContext(c).UpdateUser(u)
_, err = database.FromContext(c).UpdateUser(ctx, u)
if err != nil {
retErr := fmt.Errorf("unable to update user %s: %w", u.GetName(), err)

Expand Down
5 changes: 4 additions & 1 deletion api/auth/post_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ import (
// process a user logging in using PAT to Vela from
// the API.
func PostAuthToken(c *gin.Context) {
// capture middleware values
ctx := c.Request.Context()

// attempt to get user from source
u, err := scm.FromContext(c).AuthenticateToken(c.Request)
if err != nil {
Expand All @@ -60,7 +63,7 @@ func PostAuthToken(c *gin.Context) {
}

// check if the user exists
u, err = database.FromContext(c).GetUserForName(u.GetName())
u, err = database.FromContext(c).GetUserForName(ctx, u.GetName())
if err != nil {
retErr := fmt.Errorf("user %s not found", u.GetName())

Expand Down
2 changes: 1 addition & 1 deletion api/build/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func CreateBuild(c *gin.Context) {
}

// send API call to capture the repo owner
u, err = database.FromContext(c).GetUser(r.GetUserID())
u, err = database.FromContext(c).GetUser(ctx, r.GetUserID())
if err != nil {
retErr := fmt.Errorf("unable to get owner for %s: %w", r.GetFullName(), err)

Expand Down
2 changes: 1 addition & 1 deletion api/build/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func RestartBuild(c *gin.Context) {
logger.Infof("restarting build %s", entry)

// send API call to capture the repo owner
u, err := database.FromContext(c).GetUser(r.GetUserID())
u, err := database.FromContext(c).GetUser(ctx, r.GetUserID())
if err != nil {
retErr := fmt.Errorf("unable to get owner for %s: %w", r.GetFullName(), err)

Expand Down
2 changes: 1 addition & 1 deletion api/build/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func UpdateBuild(c *gin.Context) {
b.GetStatus() == constants.StatusKilled ||
b.GetStatus() == constants.StatusError {
// send API call to capture the repo owner
u, err := database.FromContext(c).GetUser(r.GetUserID())
u, err := database.FromContext(c).GetUser(ctx, r.GetUserID())
if err != nil {
logrus.Errorf("unable to get owner for build %s: %v", entry, err)
}
Expand Down
2 changes: 1 addition & 1 deletion api/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func recordGauges(c *gin.Context) {
// user_count
if q.UserCount {
// send API call to capture the total number of users
u, err := database.FromContext(c).CountUsers()
u, err := database.FromContext(c).CountUsers(ctx)
if err != nil {
logrus.Errorf("unable to get count of all users: %v", err)
}
Expand Down
3 changes: 2 additions & 1 deletion api/pipeline/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func GetTemplates(c *gin.Context) {
p := pipeline.Retrieve(c)
r := repo.Retrieve(c)
u := user.Retrieve(c)
ctx := c.Request.Context()

entry := fmt.Sprintf("%s/%s", r.GetFullName(), p.GetCommit())

Expand All @@ -107,7 +108,7 @@ func GetTemplates(c *gin.Context) {
}

// send API call to capture the repo owner
user, err := database.FromContext(c).GetUser(r.GetUserID())
user, err := database.FromContext(c).GetUser(ctx, r.GetUserID())
if err != nil {
util.HandleError(c, http.StatusBadRequest, fmt.Errorf("unable to get owner for %s: %w", r.GetFullName(), err))

Expand Down
2 changes: 1 addition & 1 deletion api/repo/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func UpdateRepo(c *gin.Context) {
// capture admin name for logging
admn := u.GetName()

u, err = database.FromContext(c).GetUser(r.GetUserID())
u, err = database.FromContext(c).GetUser(ctx, r.GetUserID())
if err != nil {
retErr := fmt.Errorf("unable to get repo owner of %s for platform admin webhook update: %w", r.GetFullName(), err)

Expand Down
3 changes: 2 additions & 1 deletion api/user/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import (
func CreateUser(c *gin.Context) {
// capture middleware values
u := user.Retrieve(c)
ctx := c.Request.Context()

// capture body from API request
input := new(library.User)
Expand All @@ -72,7 +73,7 @@ func CreateUser(c *gin.Context) {
}).Infof("creating new user %s", input.GetName())

// send API call to create the user
user, err := database.FromContext(c).CreateUser(input)
user, err := database.FromContext(c).CreateUser(ctx, input)
if err != nil {
retErr := fmt.Errorf("unable to create user: %w", err)

Expand Down
3 changes: 2 additions & 1 deletion api/user/create_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
func CreateToken(c *gin.Context) {
// capture middleware values
u := user.Retrieve(c)
ctx := c.Request.Context()

// update engine logger with API metadata
//
Expand All @@ -65,7 +66,7 @@ func CreateToken(c *gin.Context) {
u.SetRefreshToken(rt)

// send API call to update the user
_, err = database.FromContext(c).UpdateUser(u)
_, err = database.FromContext(c).UpdateUser(ctx, u)
if err != nil {
retErr := fmt.Errorf("unable to update user %s: %w", u.GetName(), err)

Expand Down
5 changes: 3 additions & 2 deletions api/user/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func DeleteUser(c *gin.Context) {
// capture middleware values
u := user.Retrieve(c)
user := util.PathParameter(c, "user")
ctx := c.Request.Context()

// update engine logger with API metadata
//
Expand All @@ -59,7 +60,7 @@ func DeleteUser(c *gin.Context) {
}).Infof("deleting user %s", user)

// send API call to capture the user
u, err := database.FromContext(c).GetUserForName(user)
u, err := database.FromContext(c).GetUserForName(ctx, user)
if err != nil {
retErr := fmt.Errorf("unable to get user %s: %w", user, err)

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

// send API call to remove the user
err = database.FromContext(c).DeleteUser(u)
err = database.FromContext(c).DeleteUser(ctx, u)
if err != nil {
retErr := fmt.Errorf("unable to delete user %s: %w", u.GetName(), err)

Expand Down
3 changes: 2 additions & 1 deletion api/user/delete_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
func DeleteToken(c *gin.Context) {
// capture middleware values
u := user.Retrieve(c)
ctx := c.Request.Context()

// update engine logger with API metadata
//
Expand All @@ -65,7 +66,7 @@ func DeleteToken(c *gin.Context) {
u.SetRefreshToken(rt)

// send API call to update the user
_, err = database.FromContext(c).UpdateUser(u)
_, err = database.FromContext(c).UpdateUser(ctx, u)
if err != nil {
retErr := fmt.Errorf("unable to update user %s: %w", u.GetName(), err)

Expand Down
3 changes: 2 additions & 1 deletion api/user/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func GetUser(c *gin.Context) {
// capture middleware values
u := user.Retrieve(c)
user := util.PathParameter(c, "user")
ctx := c.Request.Context()

// update engine logger with API metadata
//
Expand All @@ -55,7 +56,7 @@ func GetUser(c *gin.Context) {
}).Infof("reading user %s", user)

// send API call to capture the user
u, err := database.FromContext(c).GetUserForName(user)
u, err := database.FromContext(c).GetUserForName(ctx, user)
if err != nil {
retErr := fmt.Errorf("unable to get user %s: %w", user, err)

Expand Down
3 changes: 2 additions & 1 deletion api/user/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import (
func ListUsers(c *gin.Context) {
// capture middleware values
u := user.Retrieve(c)
ctx := c.Request.Context()

// update engine logger with API metadata
//
Expand Down Expand Up @@ -98,7 +99,7 @@ func ListUsers(c *gin.Context) {
perPage = util.MaxInt(1, util.MinInt(100, perPage))

// send API call to capture the list of users
users, t, err := database.FromContext(c).ListLiteUsers(page, perPage)
users, t, err := database.FromContext(c).ListLiteUsers(ctx, page, perPage)
if err != nil {
retErr := fmt.Errorf("unable to get users: %w", err)

Expand Down
5 changes: 3 additions & 2 deletions api/user/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func UpdateUser(c *gin.Context) {
// capture middleware values
u := user.Retrieve(c)
user := util.PathParameter(c, "user")
ctx := c.Request.Context()

// update engine logger with API metadata
//
Expand All @@ -82,7 +83,7 @@ func UpdateUser(c *gin.Context) {
}

// send API call to capture the user
u, err = database.FromContext(c).GetUserForName(user)
u, err = database.FromContext(c).GetUserForName(ctx, user)
if err != nil {
retErr := fmt.Errorf("unable to get user %s: %w", user, err)

Expand All @@ -108,7 +109,7 @@ func UpdateUser(c *gin.Context) {
}

// send API call to update the user
u, err = database.FromContext(c).UpdateUser(u)
u, err = database.FromContext(c).UpdateUser(ctx, u)
if err != nil {
retErr := fmt.Errorf("unable to update user %s: %w", user, err)

Expand Down
3 changes: 2 additions & 1 deletion api/user/update_current.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import (
func UpdateCurrentUser(c *gin.Context) {
// capture middleware values
u := user.Retrieve(c)
ctx := c.Request.Context()

// update engine logger with API metadata
//
Expand Down Expand Up @@ -82,7 +83,7 @@ func UpdateCurrentUser(c *gin.Context) {
}

// send API call to update the user
u, err = database.FromContext(c).UpdateUser(u)
u, err = database.FromContext(c).UpdateUser(ctx, u)
if err != nil {
retErr := fmt.Errorf("unable to update user %s: %w", u.GetName(), err)

Expand Down
2 changes: 1 addition & 1 deletion api/webhook/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ func PostWebhook(c *gin.Context) {
// send API call to capture repo owner
logrus.Debugf("capturing owner of repository %s", repo.GetFullName())

u, err := database.FromContext(c).GetUser(repo.GetUserID())
u, err := database.FromContext(c).GetUser(ctx, repo.GetUserID())
if err != nil {
retErr := fmt.Errorf("%s: failed to get owner for %s: %w", baseErr, repo.GetFullName(), err)
util.HandleError(c, http.StatusBadRequest, retErr)
Expand Down
2 changes: 1 addition & 1 deletion cmd/vela-server/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func processSchedule(ctx context.Context, s *library.Schedule, compiler compiler
}

// send API call to capture the owner for the repo
u, err := database.GetUser(r.GetUserID())
u, err := database.GetUser(ctx, r.GetUserID())
if err != nil {
return fmt.Errorf("unable to get owner for repo %s: %w", r.GetFullName(), err)
}
Expand Down
14 changes: 7 additions & 7 deletions database/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1656,15 +1656,15 @@ func testUsers(t *testing.T, db Interface, resources *Resources) {

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

// count the users
count, err := db.CountUsers()
count, err := db.CountUsers(context.TODO())
if err != nil {
t.Errorf("unable to count users: %v", err)
}
Expand All @@ -1674,7 +1674,7 @@ func testUsers(t *testing.T, db Interface, resources *Resources) {
methods["CountUsers"] = true

// list the users
list, err := db.ListUsers()
list, err := db.ListUsers(context.TODO())
if err != nil {
t.Errorf("unable to list users: %v", err)
}
Expand All @@ -1684,7 +1684,7 @@ func testUsers(t *testing.T, db Interface, resources *Resources) {
methods["ListUsers"] = true

// lite list the users
list, count, err = db.ListLiteUsers(1, 10)
list, count, err = db.ListLiteUsers(context.TODO(), 1, 10)
if err != nil {
t.Errorf("unable to list lite users: %v", err)
}
Expand All @@ -1698,7 +1698,7 @@ func testUsers(t *testing.T, db Interface, resources *Resources) {

// lookup the users by name
for _, user := range resources.Users {
got, err := db.GetUserForName(user.GetName())
got, err := db.GetUserForName(context.TODO(), user.GetName())
if err != nil {
t.Errorf("unable to get user %d by name: %v", user.GetID(), err)
}
Expand All @@ -1711,7 +1711,7 @@ func testUsers(t *testing.T, db Interface, resources *Resources) {
// update the users
for _, user := range resources.Users {
user.SetActive(false)
got, err := db.UpdateUser(user)
got, err := db.UpdateUser(context.TODO(), user)
if err != nil {
t.Errorf("unable to update user %d: %v", user.GetID(), err)
}
Expand All @@ -1725,7 +1725,7 @@ func testUsers(t *testing.T, db Interface, resources *Resources) {

// delete the users
for _, user := range resources.Users {
err = db.DeleteUser(user)
err = db.DeleteUser(context.TODO(), user)
if err != nil {
t.Errorf("unable to delete user %d: %v", user.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 @@ -141,6 +141,7 @@ func (e *engine) NewResources(ctx context.Context) error {

// create the database agnostic engine for users
e.UserInterface, err = user.New(
user.WithContext(e.ctx),
user.WithClient(e.client),
user.WithEncryptionKey(e.config.EncryptionKey),
user.WithLogger(e.logger),
Expand Down
Loading

0 comments on commit 91e26c4

Please sign in to comment.