ServiceAccounts: Add Service Account Token last used at date (#51446)
* ServiceAccounts Add api key last used at * ServiceAccounts: LastUpdateAt tests
This commit is contained in:
@@ -163,3 +163,15 @@ func (ss *SQLStore) GetAPIKeyByHash(ctx context.Context, hash string) (*models.A
|
||||
|
||||
return &apikey, err
|
||||
}
|
||||
|
||||
// UpdateAPIKeyLastUsedDate updates the last used date of the API key to current time.
|
||||
func (ss *SQLStore) UpdateAPIKeyLastUsedDate(ctx context.Context, tokenID int64) error {
|
||||
now := timeNow()
|
||||
return ss.WithDbSession(ctx, func(sess *DBSession) error {
|
||||
if _, err := sess.Table("api_key").ID(tokenID).Cols("last_used_at").Update(&models.ApiKey{LastUsedAt: &now}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
@@ -76,6 +76,24 @@ func TestIntegrationApiKeyDataAccess(t *testing.T) {
|
||||
assert.Equal(t, *query.Result.Expires, expected)
|
||||
})
|
||||
|
||||
t.Run("Last Used At datetime update", func(t *testing.T) {
|
||||
// expires in one hour
|
||||
cmd := models.AddApiKeyCommand{OrgId: 1, Name: "last-update-at", Key: "asd3", SecondsToLive: 3600}
|
||||
err := ss.AddAPIKey(context.Background(), &cmd)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Nil(t, cmd.Result.LastUsedAt)
|
||||
|
||||
err = ss.UpdateAPIKeyLastUsedDate(context.Background(), cmd.Result.Id)
|
||||
require.NoError(t, err)
|
||||
|
||||
query := models.GetApiKeyByNameQuery{KeyName: "last-update-at", OrgId: 1}
|
||||
err = ss.GetApiKeyByName(context.Background(), &query)
|
||||
assert.Nil(t, err)
|
||||
|
||||
assert.NotNil(t, query.Result.LastUsedAt)
|
||||
})
|
||||
|
||||
t.Run("Add a key with negative lifespan", func(t *testing.T) {
|
||||
// expires in one day
|
||||
cmd := models.AddApiKeyCommand{OrgId: 1, Name: "key-with-negative-lifespan", Key: "asd3", SecondsToLive: -3600}
|
||||
|
||||
@@ -91,4 +91,8 @@ func addApiKeyMigrations(mg *Migrator) {
|
||||
|
||||
mg.AddMigration("set service account foreign key to nil if 0", NewRawSQLMigration(
|
||||
"UPDATE api_key SET service_account_id = NULL WHERE service_account_id = 0;"))
|
||||
|
||||
mg.AddMigration("Add last_used_at to api_key table", NewAddColumnMigration(apiKeyV2, &Column{
|
||||
Name: "last_used_at", Type: DB_DateTime, Nullable: true,
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -543,6 +543,10 @@ func (m *SQLStoreMock) GetApiKeyByName(ctx context.Context, query *models.GetApi
|
||||
return m.ExpectedError
|
||||
}
|
||||
|
||||
func (m *SQLStoreMock) UpdateAPIKeyLastUsedDate(ctx context.Context, tokenID int64) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *SQLStoreMock) UpdateTempUserStatus(ctx context.Context, cmd *models.UpdateTempUserStatusCommand) error {
|
||||
return m.ExpectedError
|
||||
}
|
||||
|
||||
@@ -121,6 +121,7 @@ type Store interface {
|
||||
GetApiKeyById(ctx context.Context, query *models.GetApiKeyByIdQuery) error
|
||||
GetApiKeyByName(ctx context.Context, query *models.GetApiKeyByNameQuery) error
|
||||
GetAPIKeyByHash(ctx context.Context, hash string) (*models.ApiKey, error)
|
||||
UpdateAPIKeyLastUsedDate(ctx context.Context, tokenID int64) error
|
||||
UpdateTempUserStatus(ctx context.Context, cmd *models.UpdateTempUserStatusCommand) error
|
||||
CreateTempUser(ctx context.Context, cmd *models.CreateTempUserCommand) error
|
||||
UpdateTempUserWithEmailSent(ctx context.Context, cmd *models.UpdateTempUserWithEmailSentCommand) error
|
||||
|
||||
Reference in New Issue
Block a user