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/move whitelist into database #422

Merged
merged 11 commits into from
Apr 20, 2022

Conversation

alexanderleegs
Copy link
Contributor

@alexanderleegs alexanderleegs commented Apr 11, 2022

Problem

This PR modifies our existing method for verifying the domains were whitelisted - we were previously relying on env vars for the list of whitelisted domains, but as the number of sites may increase with the CWP migration, we have opted to move this into a database, which removes the character restriction we previously had on the list of names.

Closes #381

Solution

Adds a new table to our database for whitelisted domains.

Breaking Changes

  • Yes - this PR contains breaking changes
    • Run migrations on staging/prod db before merging

Fixes

This PR also fixes the User table types - we were previously not correctly specifying that the email and contact_number could be a null value.

New scripts:

  • db:migrate : modifies this script to use db:migrate instead of npm run db:migrate

Copy link
Contributor

@seaerchin seaerchin left a comment

Choose a reason for hiding this comment

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

main concerns are regarding migration script + the db model, rest are misc comments which i'm fine w/ not addressing

unique: true,
type: Sequelize.TEXT,
validate: {
isEmail: true,
Copy link
Contributor

Choose a reason for hiding this comment

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

not sure this is going to work - we only validate a suffix, not an email. this change means that organizational emails have to be added individually (eg: for .edu.sg, we have to add all indiv emails rather than just the suffix)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right, thanks for catching! Fixed in a8805db

Comment on lines 18 to 19
expiry: {
type: Sequelize.DATE,
allowNull: true,
defaultValue: null,
},
Copy link
Contributor

Choose a reason for hiding this comment

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

could we do expiry of 6 months from now and let the null be the exception? don't foresee us adding many indefinite whitelists as they would be vendors

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a good point, made the change in a8805db

type: DataType.DATE,
defaultValue: null,
})
expiry!: Date
Copy link
Contributor

Choose a reason for hiding this comment

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

wrong typing - the exclamation mark means that it can never be null, which is not what's declared

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch, fa8737f

Copy link
Contributor

Choose a reason for hiding this comment

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

the type should be Date | null i think

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, fixed in df92bcb

@@ -15,7 +15,7 @@
"format-fix": "npx prettier --write .",
"prepare": "husky install",
"version": "auto-changelog -p && git add CHANGELOG.md",
"npm run db:migrate": "source .env && npx sequelize-cli db:migrate",
"db:migrate": "source .env && npx sequelize-cli db:migrate",
Copy link
Contributor

Choose a reason for hiding this comment

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

good fix, i can't believe i didn't catch this when i wrote it @___@

Comment on lines +87 to +97
const whitelistEntries = await this.whitelist.findAll({
attributes: ["email"],
where: {
expiry: {
[Op.or]: [{ [Op.is]: null }, { [Op.gt]: new Date() }],
},
},
})
const whitelistDomains = whitelistEntries.map((entry) => entry.email)
const hasMatchDomain =
this.whitelistDomains.filter((domain) => email.endsWith(domain)).length >
0
whitelistDomains.filter((domain) => email.endsWith(domain)).length > 0
Copy link
Contributor

Choose a reason for hiding this comment

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

this is fine as-is and it might be over-eager from my end, but wdyt about having a WhitelistService that

  1. returns valid whitelists
  2. checks if a given email is valid

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As discussed offline, we currently don't foresee the whitelist being used anywhere other than login - as such, we'll leave it as is for now

type: DataType.DATE,
defaultValue: null,
})
expiry!: Date
Copy link
Contributor

Choose a reason for hiding this comment

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

the type should be Date | null i think

@@ -0,0 +1,36 @@
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.createTable("whitelists", {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: could we change the table name to be singular as this is a singular whitelist of emails

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense, changed in 61220d8

@alexanderleegs alexanderleegs force-pushed the feat/move-whitelist-into-database branch from 50033fc to f19045c Compare April 19, 2022 05:30
Copy link
Contributor

@seaerchin seaerchin left a comment

Choose a reason for hiding this comment

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

lgtm

@alexanderleegs alexanderleegs force-pushed the feat/move-whitelist-into-database branch from 61220d8 to d9b00da Compare April 20, 2022 02:26
@alexanderleegs alexanderleegs merged commit 2a0071e into develop Apr 20, 2022
@alexanderleegs alexanderleegs deleted the feat/move-whitelist-into-database branch April 20, 2022 02:34
@alexanderleegs alexanderleegs mentioned this pull request Apr 21, 2022
1 task
harishv7 pushed a commit that referenced this pull request Feb 17, 2023
* Feat: add whitelist model

* Feat: add migration

* Feat: add whitelist table to initialisation

* Feat: update canSendEmailOtp to use database

* Chore: update tests

* Fix: use db:migrate for local migrations

* Fix: migration

* Fix: whitelist model

* fix: database model types

* Fix: allow expiry type to be null

* Chore: rename table to whitelist
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.

[Identity] Whitelisting of domains
2 participants