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

feat: add directive to validate auth token and feature flag #122

Closed

Conversation

Rudge
Copy link
Contributor

@Rudge Rudge commented Nov 15, 2023

What problem is this solving?

Enable the auth token validation for insecure operations. Previously, we put the directive @auditAccess and now we add the directive @checkAccessWithFeatureFlag.

How to test it?

  • Configure the feature flag in masterdata
curl --location 'https://tokenvalidation--b2bsuite.vtexcommercestable.com.br/api/dataentities/auth_validation_config/documents' \
--header 'VtexIdclientAutCookie: {{TOKEN}}' \
--header 'Content-Type: application/json' \
--data '{
    "id": "storefront-permissions",
    "enable": false
 }
'
  • Update the config
curl --location --request PATCH 'https://tokenvalidation--b2bsuite.vtexcommercestable.com.br/api/dataentities/auth_validation_config/documents/storefront-permissions' \
--header 'VtexIdclientAutCookie: {{token}}' \
--header 'Content-Type: application/json' \
--data '{
    "enable": true
 }
'
  • Call an operation that has the new directive.

Copy link

vtex-io-ci-cd bot commented Nov 15, 2023

Hi! I'm VTEX IO CI/CD Bot and I'll be helping you to publish your app! 🤖

Please select which version do you want to release:

  • Patch (backwards-compatible bug fixes)

  • Minor (backwards-compatible functionality)

  • Major (incompatible API changes)

And then you just need to merge your PR when you are ready! There is no need to create a release commit/tag.

  • No thanks, I would rather do it manually 😞

@vtex-io-docs-bot
Copy link

vtex-io-docs-bot bot commented Nov 15, 2023

Beep boop 🤖

I noticed you didn't make any changes at the docs/ folder

  • There's nothing new to document 🤔
  • I'll do it later 😞

In order to keep track, I'll create an issue if you decide now is not a good time

  • I just updated 🎉🎉

@Rudge Rudge requested a review from a team November 15, 2023 20:43
Copy link

github-actions bot commented Nov 15, 2023

Messages
📖 ❤️ Thanks!
📖

🎉 PR additions = 130, PR deletions = 46

Generated by 🚫 dangerJS against d8055a7

Copy link
Contributor

@enzomerca enzomerca left a comment

Choose a reason for hiding this comment

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

I'm approving, but if you could just add to the PR why some APIs have the new directive and why other don't, for future reference...


if (adminUserAuthToken) {
try {
await identity.validateToken({ token: adminUserAuthToken })
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this cause us to send duplicated checks for validating these tokens? Or are we not already doing this validation on any of these fields today?

Copy link
Contributor Author

@Rudge Rudge Nov 16, 2023

Choose a reason for hiding this comment

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

It was implemented previously. I refactored it into a new function to reuse in another directive.

Copy link
Contributor

Choose a reason for hiding this comment

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

I saw that, but I mean if there are other files that are doing these checks inside field resolvers. Some of the fields where you added this new checkAccessFeatureFlag directive may already be doing these checks internally, so we'd do it twice now, duplicating the work. If the only place doing this was the function you refactored than we should be good.

throw new ForbiddenError('Unauthorized Access')
}
}
await checkUserOrAdminTokenAccess(context, field.astNode?.name?.value)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think I'm missing something, here, but from what I understood we now have both directives:

  1. @auditAccess. Always checks the token and throws errors when it's not present or valid.
  2. @checkAccessWithFeatureFlag. Does the same as the other one, but only when the feature flag is enabled.

Why have both? The auditAccess will already do the check and throw before checkAccessWithFeatureFlag ever runs right? Shouldn't we only have checkAccessWithFeatureFlag, so that we can control when the check is made? And having both will make us call vtexid/identity twice when the feature flag is enabled and the check passes, since we'll check the same thing that has been checked by the first directive.

I think I misunderstood something though, can you explain the idea of the two directives with more details?

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 think you said about the @checkUserAccess instead of @auditAccess, is it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@checkUserAccess - Validates the storeUserToken or adminToken, but is not applied for all operations.
@auditAccess - Generate metrics about the use of the operation and if it uses the token or not.
@checkAccessWithFeatureFlag - It was created for temporary use. We put in some operations with @auditAccess, without token validation, and mapped in the task. The intention is to control whether we can enable or disable token validation for these operations in production.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, got it! I was confusing auditAccess with checkUserAccess, then. I get it now!

Copy link
Contributor

Choose a reason for hiding this comment

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

Final question then: will we be able to remove this auditAccess directive after all the missing access checks are fully launched? We won't need those metrics anymore when that happens right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, this is the intention. I almost removed it in this PR, but I let it go for the next one.

@Rudge
Copy link
Contributor Author

Rudge commented Nov 16, 2023

I'm approving, but if you could just add to the PR why some APIs have the new directive and why other don't, for future reference...

I putted for all operations declared in this task and that has the @auditAccess previously. For some, it isn't necessary because they have the directive @checkUserAccess or @checkAdminAccess. Do you have an example that doesn't fit in this case?

Copy link
Contributor

@mairatma mairatma left a comment

Choose a reason for hiding this comment

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

Some final comments before approving. They also apply for the other PR that's similar to this one.

Btw it would be nice if we could share this logic between these apps, but I don't have a good enough suggestion for now. Just throwing it for the future, we need to determine a way to share code between these B2B Suite IO apps, they have too much duplicated code already.

} catch (err) {
logger.warn({
error: err,
message: `CheckUserAccess: Invalid admin token for ${operation}`,
Copy link
Contributor

@mairatma mairatma Nov 23, 2023

Choose a reason for hiding this comment

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

My only remaining suggestion here is to remove this ${operation} text from these log messages. You already have the operation as one of the fields being logged here. It's a good practice to keep log messages static, without variables within them, using just fields instead. That helps analyze and aggregate them all, and you can use the field when you want to focus on the the differences between different operations. You'll also won't lose historic log data for these log messages, you'll just be adding a field to it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Same for the other log messages in this file.

Copy link
Contributor Author

@Rudge Rudge Nov 27, 2023

Choose a reason for hiding this comment

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

Yes, I know, but just a reminder that this is an IO app, so it isn't easy to add a new field to index. If everybody wants to put a new field, there is a problem.

error: err,
message: `CheckUserAccess: Invalid admin token for ${operation}`,
operation,
token: adminUserAuthToken,
Copy link
Contributor

Choose a reason for hiding this comment

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

Auth tokens shouldn't be logged, that's sensitive data so it's a security guideline never to log them.

Copy link
Contributor

Choose a reason for hiding this comment

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

Same for the other log messages in this file.

Copy link
Contributor Author

@Rudge Rudge Nov 27, 2023

Choose a reason for hiding this comment

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

I agree, and I will remove it. The problem was in the previous solution.

- remove token from field and operation from message
Copy link
Contributor

@mairatma mairatma left a comment

Choose a reason for hiding this comment

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

Thanks, lgtm!

@Rudge Rudge force-pushed the feature/B2BTEAM-1484-flag-enable-token-validation branch from e38ab2b to d8055a7 Compare November 30, 2023 01:51
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

0.0% 0.0% Coverage
0.0% 0.0% Duplication

warning The version of Java (11.0.17) you have used to run this analysis is deprecated and we will stop accepting it soon. Please update to at least Java 17.
Read more here

@Rudge
Copy link
Contributor Author

Rudge commented Dec 19, 2023

We deployed the version without a feature flag and it doesn’t need to deploy this one.

@Rudge Rudge closed this Dec 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants