Skip to content
This repository has been archived by the owner on Jan 15, 2025. It is now read-only.

[#177116764] Allow caching EYCA CCDB API's SessionId #39

Merged
merged 4 commits into from
Mar 19, 2021

Conversation

AleDore
Copy link
Contributor

@AleDore AleDore commented Mar 17, 2021

List of Changes

  • Extract EYCA CCDB API util methods into a dedicated eyca module
  • Update EYCA OpenAPI specs in order to manage a session_id auth param
  • Enable caching of session_id through Redis
  • Enable retrieve of session_id from Redis before all CCDB Api calls

Motivation and Context

In order to optimize authentication through EYCA CCDB API's platform, we must handle and cache a session_id token for all subsequent api calls after authLogin operation (performed using username and password credentials). Please note that a new session_id is valid for 30 minutes after it is released and a new authLogin operation does not invalidate previous released session_id until its expiration has not reached.
In case of Redis isn't available we fallback to authLogin operation avoiding retries for missing session_id hit in cache.
See this story for further informations.

How Has This Been Tested?

Unit tests done.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.

@pagopa-github-bot
Copy link
Contributor

pagopa-github-bot commented Mar 17, 2021

Warnings
⚠️ This PR changes a total of 638 LOCs, that is more than a reasonable size of 250. Consider splitting the pull request into smaller ones.

Affected stories

  • ⚙️ #177116764: Ottimizzare la gestione dell' autenticazione sulle chiamate esposte dalla piattaforma EYCA, in modo da non dover utilizzare username e password ad ogni chiamata ma utilizzare il token di sessione che viene staccato dall' api di login.

Generated by 🚫 dangerJS against cc14df4

@codecov
Copy link

codecov bot commented Mar 17, 2021

Codecov Report

Merging #39 (cc14df4) into master (f348113) will increase coverage by 0.37%.
The diff coverage is 84.07%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #39      +/-   ##
==========================================
+ Coverage   82.89%   83.27%   +0.37%     
==========================================
  Files          36       40       +4     
  Lines        1076     1238     +162     
  Branches      121      139      +18     
==========================================
+ Hits          892     1031     +139     
- Misses        179      202      +23     
  Partials        5        5              
Impacted Files Coverage Δ
utils/activation.ts 75.00% <0.00%> (-3.27%) ⬇️
utils/models.ts 66.66% <ø> (ø)
utils/cgnCode.ts 68.96% <38.46%> (-24.79%) ⬇️
GetEycaStatus/handler.ts 88.23% <50.00%> (ø)
StartEycaActivation/handler.ts 87.01% <50.00%> (ø)
utils/messages.ts 81.81% <50.00%> (-3.19%) ⬇️
StartCgnActivation/handler.ts 78.20% <55.55%> (ø)
utils/config.ts 63.15% <60.00%> (-6.85%) ⬇️
UpdateCgnOrchestrator/handler.ts 69.86% <69.86%> (ø)
GetCgnActivation/handler.ts 90.38% <83.33%> (+0.38%) ⬆️
... and 18 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 45cb33c...cc14df4. Read the comment docs.

import { getTask, setWithExpirationTask } from "../utils/redis_storage";

export const CCDB_SESSION_ID_KEY = "CCDB_SESSION_ID";
export const CCDB_SESSION_ID_TTL = 1500;

Choose a reason for hiding this comment

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

The CCDB_SESSION session have a TTL of 30 minutes (1800 seconds), why we are using 1500? Can we set this parameter with an ENV configuration?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd rather to maintain it as an hardcoded constant. It will never change. Therefore I will change the value to 1700 seconds avoiding that a minimum delay can compromise the token validity :)

eycaClient: ReturnType<EycaAPIClient>,
username: NonEmptyString,
password: NonEmptyString
) =>

Choose a reason for hiding this comment

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

Can we add a return type definition? The function behaviour becomes more readable.

export const CCDB_SESSION_ID_KEY = "CCDB_SESSION_ID";
export const CCDB_SESSION_ID_TTL = 1500;

const ccdbLogin = (

Choose a reason for hiding this comment

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

Can we add a small description?

Comment on lines 46 to 51
const retrieveCcdbSessionId = (
redisClient: RedisClient,
eycaClient: ReturnType<EycaAPIClient>,
username: NonEmptyString,
password: NonEmptyString
) =>

Choose a reason for hiding this comment

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

The same consideration of ccdbLogin about description and return type.

password: NonEmptyString
) =>
getTask(redisClient, CCDB_SESSION_ID_KEY).foldTaskEither(
() => ccdbLogin(eycaClient, username, password),

Choose a reason for hiding this comment

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

Are we using ccdbLogin as fallback on Redis errors? We can only return an error, retrieveCcdbSessionId is used inside SuccessEycaActivationActivity that has a retry logic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep I will restore the default behaviour. So we let the activity to retry and log errors in case Redis get or set fails.

Comment on lines 58 to 64
setWithExpirationTask(
redisClient,
CCDB_SESSION_ID_KEY,
_,
CCDB_SESSION_ID_TTL
).foldTaskEither(
() => taskEither.of(_),

Choose a reason for hiding this comment

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

If we have an error we can't see it. We don't have any logs.

Comment on lines 75 to 76
activationDate: new Date(),
expirationDate: extractEycaExpirationDate(aFiscalCode).value as Date
expirationDate: extractEycaExpirationDate(aFiscalCode).value as Date,

Choose a reason for hiding this comment

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

Sometime in the future, all the tests that use this value start to fail.

@AleDore AleDore requested a review from BurnedMarshal March 19, 2021 12:58
Copy link

@BurnedMarshal BurnedMarshal left a comment

Choose a reason for hiding this comment

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

LGTM

Comment on lines 66 to 72
ccdbLogin(eycaClient, username, password).chain(_ =>
setWithExpirationTask(
redisClient,
CCDB_SESSION_ID_KEY,
_,
CCDB_SESSION_ID_TTL
).map(() => _)

Choose a reason for hiding this comment

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

Only a small readability improvement

Suggested change
ccdbLogin(eycaClient, username, password).chain(_ =>
setWithExpirationTask(
redisClient,
CCDB_SESSION_ID_KEY,
_,
CCDB_SESSION_ID_TTL
).map(() => _)
ccdbLogin(eycaClient, username, password).chain(sessionId =>
setWithExpirationTask(
redisClient,
CCDB_SESSION_ID_KEY,
sessionId,
CCDB_SESSION_ID_TTL
).map(() => sessionId)

@AleDore AleDore merged commit 1cf032d into master Mar 19, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants