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

[82969] MAP STS token caching #16801

Merged
merged 9 commits into from
Jun 3, 2024
Merged

[82969] MAP STS token caching #16801

merged 9 commits into from
Jun 3, 2024

Conversation

bramleyjl
Copy link
Contributor

@bramleyjl bramleyjl commented May 17, 2024

z## Summary

  • This PR adds Redis caching to MAP STS tokens requested via vets-api. Token information is stored in via Rails cache with map_sts_token_<application>_icn as its key.
  • Cache time is 5 minutes.
  • MAP STS Service updated to include cache param, defaulting to true. If cache is false the service will force a query for a new token. Tokens are always cached for future retrieval, regardless of cache param.
  • Using the MapServicesController to request a token will always call for a new token - cache is always false.

Related issue(s)

Testing done

  • To test, you will need to create an STS token to validate with:
# create private key from fixture
pem = File.read('spec/fixtures/sign_in/sts_client.pem')
private_key = OpenSSL::PKey::RSA.new(pem)

# build token payload & encode
current_time = Time.now.to_i
token = {
  'iss' => 'http://localhost:3978/api/messages',
  'sub' => 'vets.gov.user+0@gmail.com',
  'aud' => 'http://127.0.0.1:3000/v0/sign_in/token',
  'iat' => current_time,
  'exp' => current_time + 300,
  'scopes' => ['http://localhost:3000/v0/map_services/chatbot/token'],
  'service_account_id' => '88a6d94a3182fd63279ea5565f26bcb4',
  'jti' => '2ed8a21d207adf50eb935e32d25a41ff',
  'user_attributes' => { 'icn' => '1012667122V019349' }
}
JWT.encode(token, private_key, 'RS256')
  • Submit a request to /v0/map_services/chatbot/token:
curl --location --request POST 'http://localhost:3000/v0/map_services/chatbot/token' \
--header 'Authorization: Bearer <sts token>
  • You should receive a token, and can find a token request log in your Rails console with cached_response => false:
Rails -- [MAP][SecurityToken][Service] token request -- { :application => :chatbot, :icn => "1012667122V019349" }
Rails -- [MAP][SecurityToken][Service] token success -- { :application => :chatbot, :icn => "1012667122V019349", :cached_response => false }
  • Change the MapServicesController to use caching when requesting a token:
# app/controllers/v0/map_services_controller.rb
result = MAP::SecurityToken::Service.new.token(application: params[:application].to_sym, icn:, cache: true)
  • Repeat your query, you should immediately receive a cached token instead of the result of a new query:
Rails -- [MAP][SecurityToken][Service] token success -- { :application => :chatbot, :icn => "1012667122V019349", :cached_response => false }

What areas of the site does it impact?

MAP STS token requests

Acceptance criteria

  • I fixed|updated|added unit tests and integration tests for each feature (if applicable).
  • No error nor warning in the console.
  • Events are being sent to the appropriate logging solution
  • No sensitive information (i.e. PII/credentials/internal URLs/etc.) is captured in logging, hardcoded, or specs
  • Feature/bug has a monitor built into Datadog or Grafana (if applicable)
  • If app impacted requires authentication, did you login to a local build and verify all authenticated routes work as expected

@va-vfs-bot va-vfs-bot temporarily deployed to 82969_map_token_cache/main/main May 17, 2024 22:32 Inactive
@bramleyjl bramleyjl force-pushed the 82969_map_token_cache branch 2 times, most recently from b38b1d9 to f0c48c5 Compare May 20, 2024 17:13
@bramleyjl bramleyjl added identity identity-backend Identity team backend label labels May 20, 2024
@bramleyjl bramleyjl marked this pull request as ready for review May 20, 2024 17:24
@bramleyjl bramleyjl requested review from a team as code owners May 20, 2024 17:24
@va-vfs-bot va-vfs-bot temporarily deployed to 82969_map_token_cache/main/main May 20, 2024 19:43 Inactive
@va-vfs-bot va-vfs-bot temporarily deployed to 82969_map_token_cache/main/main May 21, 2024 19:19 Inactive
@va-vfs-bot va-vfs-bot temporarily deployed to 82969_map_token_cache/main/main May 22, 2024 01:04 Inactive
ericboehs
ericboehs previously approved these changes May 22, 2024
ericboehs
ericboehs previously approved these changes May 23, 2024
@va-vfs-bot va-vfs-bot temporarily deployed to 82969_map_token_cache/main/main May 23, 2024 23:34 Inactive
@va-vfs-bot va-vfs-bot temporarily deployed to 82969_map_token_cache/main/main May 24, 2024 01:38 Inactive
@bramleyjl bramleyjl requested a review from bosawt May 28, 2024 20:02
@va-vfs-bot va-vfs-bot temporarily deployed to 82969_map_token_cache/main/main May 28, 2024 20:45 Inactive
@va-vfs-bot va-vfs-bot temporarily deployed to 82969_map_token_cache/main/main May 29, 2024 01:21 Inactive
Copy link
Contributor

@bosawt bosawt left a comment

Choose a reason for hiding this comment

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

Confirmed call to MAP::STS with response and log stating response was not from cache:
2024-05-31 14:32:43.162553 I [5866:12180 service.rb:18] Rails -- [MAP][SecurityToken][Service] token success -- { :application => :chatbot, :icn => "123456", :cached_response => false }

Follow up call confirms response came from cache (cached_response => true)
2024-05-31 14:32:56.283599 I [5866:12180 service.rb:18] Rails -- [MAP][SecurityToken][Service] token success -- { :application => :chatbot, :icn => "123456", :cached_response => true }

Confirmed cached_response => false no matter how many times I call system when cache: false in params

@bramleyjl bramleyjl merged commit 1ff6f84 into master Jun 3, 2024
19 checks passed
@bramleyjl bramleyjl deleted the 82969_map_token_cache branch June 3, 2024 15:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
identity identity-backend Identity team backend label
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants