Skip to content

feat: Skip indexes for Azure Cosmos DB for MongoDB #8494

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

Open
wants to merge 2 commits into
base: alpha
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,24 @@ export class Config {
} else if (typeof databaseOptions.schemaCacheTtl !== 'number') {
throw `databaseOptions.schemaCacheTtl must be a number`;
}
if (databaseOptions.disableEnsureUsernameCaseInsensitiveIndex === undefined) {
databaseOptions.disableEnsureUsernameCaseInsensitiveIndex =
DatabaseOptions.disableEnsureUsernameCaseInsensitiveIndex.default;
} else if (typeof databaseOptions.disableEnsureUsernameCaseInsensitiveIndex !== 'boolean') {
throw `databaseOptions.disableEnsureUsernameCaseInsensitiveIndex must be a boolean`;
}
if (databaseOptions.disableEnsureEmailCaseInsensitiveIndex === undefined) {
databaseOptions.disableEnsureEmailCaseInsensitiveIndex =
DatabaseOptions.disableEnsureEmailCaseInsensitiveIndex.default;
} else if (typeof databaseOptions.disableEnsureEmailCaseInsensitiveIndex !== 'boolean') {
throw `databaseOptions.disableEnsureEmailCaseInsensitiveIndex must be a boolean`;
}
if (databaseOptions.disableEnsureIdempotencyExpireIndex === undefined) {
databaseOptions.disableEnsureIdempotencyExpireIndex =
DatabaseOptions.disableEnsureIdempotencyExpireIndex.default;
} else if (typeof databaseOptions.disableEnsureIdempotencyExpireIndex !== 'boolean') {
throw `databaseOptions.disableEnsureIdempotencyExpireIndex must be a boolean`;
}
}

static validateRateLimit(rateLimit) {
Expand Down
55 changes: 30 additions & 25 deletions src/Controllers/DatabaseController.js
Original file line number Diff line number Diff line change
Expand Up @@ -1706,30 +1706,33 @@ class DatabaseController {
throw error;
});

await this.adapter
.ensureIndex('_User', requiredUserFields, ['username'], 'case_insensitive_username', true)
.catch(error => {
logger.warn('Unable to create case insensitive username index: ', error);
throw error;
});
await this.adapter
.ensureIndex('_User', requiredUserFields, ['username'], 'case_insensitive_username', true)
.catch(error => {
logger.warn('Unable to create case insensitive username index: ', error);
throw error;
});

if (!this.options.databaseOptions?.disableEnsureUsernameCaseInsensitiveIndex) {
await this.adapter
.ensureIndex('_User', requiredUserFields, ['username'], 'case_insensitive_username', true)
.catch(error => {
logger.warn('Unable to create case insensitive username index: ', error);
throw error;
});
await this.adapter
.ensureIndex('_User', requiredUserFields, ['username'], 'case_insensitive_username', true)
.catch(error => {
logger.warn('Unable to create case insensitive username index: ', error);
throw error;
});
}
await this.adapter.ensureUniqueness('_User', requiredUserFields, ['email']).catch(error => {
logger.warn('Unable to ensure uniqueness for user email addresses: ', error);
throw error;
});

await this.adapter
.ensureIndex('_User', requiredUserFields, ['email'], 'case_insensitive_email', true)
.catch(error => {
logger.warn('Unable to create case insensitive email index: ', error);
throw error;
});
if (!this.options.databaseOptions?.disableEnsureEmailCaseInsensitiveIndex) {
await this.adapter
.ensureIndex('_User', requiredUserFields, ['email'], 'case_insensitive_email', true)
.catch(error => {
logger.warn('Unable to create case insensitive email index: ', error);
throw error;
});
}

await this.adapter.ensureUniqueness('_Role', requiredRoleFields, ['name']).catch(error => {
logger.warn('Unable to ensure uniqueness for role name: ', error);
Expand All @@ -1755,12 +1758,14 @@ class DatabaseController {
options = this.idempotencyOptions;
options.setIdempotencyFunction = true;
}
await this.adapter
.ensureIndex('_Idempotency', requiredIdempotencyFields, ['expire'], 'ttl', false, options)
.catch(error => {
logger.warn('Unable to create TTL index for idempotency expire date: ', error);
throw error;
});
if (!this.options.databaseOptions.disableEnsureIdempotencyExpireIndex) {
await this.adapter
.ensureIndex('_Idempotency', requiredIdempotencyFields, ['expire'], 'ttl', false, options)
.catch(error => {
logger.warn('Unable to create TTL index for idempotency expire date: ', error);
throw error;
});
}
}
await this.adapter.updateSchemaWithIndexes();
}
Expand Down
21 changes: 21 additions & 0 deletions src/Options/Definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,27 @@ module.exports.FileUploadOptions = {
},
};
module.exports.DatabaseOptions = {
disableEnsureEmailCaseInsensitiveIndex: {
env: 'PARSE_SERVER_DATABASE_DISABLE_ENSURE_EMAIL_CASE_INSENSITIVE_INDEX',
help:
'Disables behavior to ensure case-insensitive index on field email on _User collection. Set to `true` if using a database not supporting case-insensitive indexes.',
action: parsers.booleanParser,
default: false,
},
disableEnsureIdempotencyExpireIndex: {
env: 'PARSE_SERVER_DATABASE_DISABLE_ENSURE_IDEMPOTENCY_EXPIRE_INDEX',
help:
'Disables behavior to ensure time to live index on field expire on _Idempotency collection. Set to `true` if using a database not supporting TTL index on this field.',
action: parsers.booleanParser,
default: false,
},
disableEnsureUsernameCaseInsensitiveIndex: {
env: 'PARSE_SERVER_DATABASE_DISABLE_ENSURE_USERNAME_CASE_INSENSITIVE_INDEX',
help:
'Disables behavior to ensure case-insensitive index on field username on _User collection. Set to `true` if using a database not supporting case-insensitive indexes.',
action: parsers.booleanParser,
default: false,
},
enableSchemaHooks: {
env: 'PARSE_SERVER_DATABASE_ENABLE_SCHEMA_HOOKS',
help:
Expand Down
3 changes: 3 additions & 0 deletions src/Options/docs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions src/Options/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,15 @@ export interface DatabaseOptions {
enableSchemaHooks: ?boolean;
/* The duration in seconds after which the schema cache expires and will be refetched from the database. Use this option if using multiple Parse Servers instances connected to the same database. A low duration will cause the schema cache to be updated too often, causing unnecessary database reads. A high duration will cause the schema to be updated too rarely, increasing the time required until schema changes propagate to all server instances. This feature can be used as an alternative or in conjunction with the option `enableSchemaHooks`. Default is infinite which means the schema cache never expires. */
schemaCacheTtl: ?number;
/* Disables behavior to ensure case-insensitive index on field username on _User collection. Set to `true` if using a database not supporting case-insensitive indexes.
:DEFAULT: false */
disableEnsureUsernameCaseInsensitiveIndex: ?boolean;
/* Disables behavior to ensure case-insensitive index on field email on _User collection. Set to `true` if using a database not supporting case-insensitive indexes.
:DEFAULT: false */
disableEnsureEmailCaseInsensitiveIndex: ?boolean;
/* Disables behavior to ensure time to live index on field expire on _Idempotency collection. Set to `true` if using a database not supporting TTL index on this field.
:DEFAULT: false */
disableEnsureIdempotencyExpireIndex: ?boolean;
Comment on lines +557 to +564
Copy link
Member

@Moumouls Moumouls Apr 8, 2023

Choose a reason for hiding this comment

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

It will introduce security issues to just disable case insensitivity on username and email, i sent a more complete PR to force email and username to lowercase in this cases: #8042

Copy link
Author

Choose a reason for hiding this comment

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

It will introduce security issues to just disable case insensitivity on username and email, i sent a more complete PR to force email and username to lowercase in this cases: #8042

Thanks, I noticed this comment just now
I would update my PR to take your changes once your PR is merged

}

export interface AuthAdapter {
Expand Down