- 
                Notifications
    You must be signed in to change notification settings 
- Fork 23
feat: implement api key last used at #2175
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
base: main
Are you sure you want to change the base?
Changes from all commits
aec2d36
              f70a444
              f94a9f9
              2f26a4e
              3fc722e
              4496bf2
              7221e41
              1a7f486
              a356847
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ALTER TABLE api_key ADD COLUMN last_used_at bigint DEFAULT 0; | 
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -773,3 +773,62 @@ func (s *AccountService) UpdateAPIKey( | |
|  | ||
| return &proto.UpdateAPIKeyResponse{}, nil | ||
| } | ||
|  | ||
| func (s *AccountService) UpdateAPIKeyLastUsedAt( | ||
| ctx context.Context, | ||
| req *proto.UpdateAPIKeyLastUsedAtRequest, | ||
| ) (*proto.UpdateAPIKeyLastUsedAtResponse, error) { | ||
| // No need to check roles since this is only allowed to be called internally by other services. | ||
| err := validateUpdateAPIKeyLastUsedAtRequest(req) | ||
| if err != nil { | ||
| s.logger.Error("Invalid UpdateAPIKeyLastUsedAt request", | ||
| log.FieldsFromIncomingContext(ctx).AddFields( | ||
| zap.Error(err), | ||
| zap.String("apiKeyId", req.ApiKeyId), | ||
| zap.Int64("lastUsedAt", req.LastUsedAt), | ||
| )...) | ||
| return nil, err | ||
| } | ||
|  | ||
| err = s.mysqlClient.RunInTransactionV2(ctx, func(contextWithTx context.Context, _ mysql.Transaction) error { | ||
| envAPIKey, err := s.accountStorage.GetEnvironmentAPIKey(contextWithTx, req.ApiKeyId) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| apiKey := &domain.APIKey{ | ||
| APIKey: envAPIKey.ApiKey, | ||
| } | ||
| err = apiKey.SetUsedAt(req.LastUsedAt) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return s.accountStorage.UpdateAPIKey(contextWithTx, apiKey, envAPIKey.Environment.Id) | ||
| }) | ||
| if err != nil { | ||
| if errors.Is(err, v2as.ErrAPIKeyNotFound) { | ||
| return nil, statusNotFound.Err() | ||
| } | ||
| s.logger.Error( | ||
| "Failed to update api key last used at", | ||
| log.FieldsFromIncomingContext(ctx).AddFields( | ||
| zap.Error(err), | ||
| zap.String("id", req.ApiKeyId), | ||
| )..., | ||
| ) | ||
| return nil, api.NewGRPCStatus(err).Err() | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is an inconsistency here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, let's return the grpc status error only, updated https://github.com/bucketeer-io/bucketeer/pull/2175/files#diff-1236610833a324b8af6d6273437bdeb2fb35012b5ae3b3374760927a8427c204R809 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Additionally, by passing the  Since we already have the  Reference: https://github.com/bucketeer-io/bucketeer/blob/main/pkg/account/storage/v2/api_key.go#L94-L103 | ||
| } | ||
| return &proto.UpdateAPIKeyLastUsedAtResponse{}, nil | ||
| } | ||
|  | ||
| func validateUpdateAPIKeyLastUsedAtRequest( | ||
|         
                  hvn2k1 marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| req *proto.UpdateAPIKeyLastUsedAtRequest, | ||
| ) error { | ||
| if req.ApiKeyId == "" { | ||
| return statusMissingAPIKeyID.Err() | ||
| } | ||
|  | ||
| if req.LastUsedAt <= 0 { | ||
| return statusInvalidLastUsedAt.Err() | ||
| } | ||
| return nil | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -7,7 +7,8 @@ SELECT | |
| updated_at, | ||
| description, | ||
| api_key, | ||
| maintainer | ||
| maintainer, | ||
| last_used_at | ||
| FROM | ||
| api_key | ||
| WHERE | ||
|  | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -7,7 +7,8 @@ SELECT | |
| updated_at, | ||
| description, | ||
| api_key, | ||
| maintainer | ||
| maintainer, | ||
| last_used_at | ||
| FROM | ||
| api_key | ||
| WHERE | ||
|  | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
statusNotFound.Err()is anaccount not founderror, not api key. This could be confusing when debugging.Please add a new one
statusAPIKeyNotFound.Reference: https://github.com/bucketeer-io/bucketeer/blob/main/pkg/account/api/error.go#L41
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at the code, the storage interface is already formatting the not found error, so we just need to use this:
return nil, api.NewGRPCStatus(err).Err()Reference: https://github.com/bucketeer-io/bucketeer/blob/main/pkg/account/storage/v2/api_key.go#L31C2-L31C19
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I looked at the GetAPIKey, not the Update interface. In the end, we need to implement it in the storage interface, the not-found check.
Reference:
https://github.com/bucketeer-io/bucketeer/blob/main/pkg/account/storage/v2/api_key.go#L94-L103