-
Notifications
You must be signed in to change notification settings - Fork 13.7k
feat: ldap custom variables / string manipulation #35717
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
79609eb
wip: LDAP sync for federation
pierre-lehnen-rc c672ea3
fixed replace operation
pierre-lehnen-rc e86d817
added split operation
pierre-lehnen-rc 2fc1062
add variable processing logs
pierre-lehnen-rc 2e51f1c
changeset
pierre-lehnen-rc 9eabbc9
Update .changeset/beige-days-push.md
pierre-lehnen-rc 2c5d2a2
Update .changeset/beige-days-push.md
pierre-lehnen-rc 159c1fe
unit tests
pierre-lehnen-rc 2ce7dca
improvements over the old code
pierre-lehnen-rc daa2941
add test for empty key
pierre-lehnen-rc 9d5c292
Merge branch 'develop' into feat/ldap-variables
pierre-lehnen-rc 1fece62
merge error
pierre-lehnen-rc dfb09f1
Merge branch 'develop' into feat/ldap-variables
kodiakhq[bot] dddc99f
Merge branch 'develop' into feat/ldap-variables
kodiakhq[bot] ffa345d
Merge branch 'develop' into feat/ldap-variables
kodiakhq[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| '@rocket.chat/core-typings': minor | ||
| '@rocket.chat/i18n': minor | ||
| '@rocket.chat/meteor': minor | ||
| --- | ||
|
|
||
| Adds new settings to allow configuring custom variables with string manipulation functions on the LDAP data mapper |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
pierre-lehnen-rc marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import type { ILDAPEntry } from '@rocket.chat/core-typings'; | ||
|
|
||
| import { getLdapString } from './getLdapString'; | ||
| import { ldapKeyExists } from './ldapKeyExists'; | ||
|
|
||
| export function getLdapDynamicValue(ldapUser: ILDAPEntry, attributeSetting: string | undefined): string | undefined { | ||
| if (!attributeSetting) { | ||
| return; | ||
| } | ||
|
|
||
| // If the attribute setting is a template, then convert the variables in it | ||
| if (attributeSetting.includes('#{')) { | ||
| return attributeSetting.replace(/#{(.+?)}/g, (_match, field) => { | ||
| const key = field.trim(); | ||
|
|
||
| if (ldapKeyExists(ldapUser, key)) { | ||
| return getLdapString(ldapUser, key); | ||
| } | ||
|
|
||
| return ''; | ||
|
pierre-lehnen-rc marked this conversation as resolved.
|
||
| }); | ||
| } | ||
|
|
||
| // If it's not a template, then treat the setting as a CSV list of possible attribute names and return the first valid one. | ||
| const attributeList: string[] = attributeSetting.replace(/\s/g, '').split(','); | ||
|
pierre-lehnen-rc marked this conversation as resolved.
|
||
| const key = attributeList.find((field) => ldapKeyExists(ldapUser, field)); | ||
| if (key) { | ||
| return getLdapString(ldapUser, key); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import type { ILDAPEntry } from '@rocket.chat/core-typings'; | ||
|
|
||
| export function getLdapString(ldapUser: ILDAPEntry, key: string): string { | ||
|
pierre-lehnen-rc marked this conversation as resolved.
Outdated
|
||
| return ldapUser[key.trim()]; | ||
|
pierre-lehnen-rc marked this conversation as resolved.
pierre-lehnen-rc marked this conversation as resolved.
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import type { ILDAPEntry } from '@rocket.chat/core-typings'; | ||
| import _ from 'underscore'; | ||
|
|
||
| export function ldapKeyExists(ldapUser: ILDAPEntry, key: string): boolean { | ||
| return !_.isEmpty(ldapUser[key.trim()]); | ||
|
pierre-lehnen-rc marked this conversation as resolved.
|
||
| } | ||
31 changes: 31 additions & 0 deletions
31
apps/meteor/server/lib/ldap/operations/executeOperation.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import type { ILDAPEntry } from '@rocket.chat/core-typings'; | ||
|
|
||
| import { executeFallback, type LDAPVariableFallback } from './fallback'; | ||
| import { executeMatch, type LDAPVariableMatch } from './match'; | ||
| import { executeReplace, type LDAPVariableReplace } from './replace'; | ||
| import { executeSplit, type LDAPVariableSplit } from './split'; | ||
| import { executeSubstring, type LDAPVariableSubString } from './substring'; | ||
|
|
||
| export type LDAPVariableOperation = | ||
| | LDAPVariableReplace | ||
| | LDAPVariableMatch | ||
| | LDAPVariableSubString | ||
| | LDAPVariableFallback | ||
| | LDAPVariableSplit; | ||
|
|
||
| export function executeOperation(ldapUser: ILDAPEntry, input: string, operation?: LDAPVariableOperation): string | undefined { | ||
| switch (operation?.operation) { | ||
| case 'replace': | ||
| return executeReplace(input, operation); | ||
| case 'match': | ||
| return executeMatch(input, operation); | ||
| case 'substring': | ||
| return executeSubstring(input, operation); | ||
| case 'fallback': | ||
| return executeFallback(ldapUser, input, operation); | ||
| case 'split': | ||
| return executeSplit(input, operation); | ||
| } | ||
|
|
||
| return input; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import type { ILDAPEntry } from '@rocket.chat/core-typings'; | ||
|
|
||
| import { getLdapDynamicValue } from '../getLdapDynamicValue'; | ||
|
|
||
| export type LDAPVariableFallback = { | ||
| operation: 'fallback'; | ||
| fallback: string; | ||
|
|
||
| minLength?: number; | ||
| }; | ||
|
|
||
| export function executeFallback(ldapUser: ILDAPEntry, input: string, operation: LDAPVariableFallback): string | undefined { | ||
| let valid = Boolean(input); | ||
|
|
||
| if (valid && typeof operation.minLength === 'number') { | ||
| valid = input.length >= operation.minLength; | ||
| } | ||
|
|
||
| if (valid) { | ||
| return input; | ||
| } | ||
|
|
||
| return getLdapDynamicValue(ldapUser, operation.fallback); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| export type LDAPVariableMatch = { | ||
| operation: 'match'; | ||
| pattern: string; | ||
| regex?: boolean; | ||
| flags?: string; | ||
| indexToUse?: number; | ||
| valueIfTrue?: string; | ||
| valueIfFalse?: string; | ||
| }; | ||
|
|
||
| export function executeMatch(input: string, operation: LDAPVariableMatch): string | undefined { | ||
|
pierre-lehnen-rc marked this conversation as resolved.
|
||
| if (!operation.pattern || (typeof operation.valueIfTrue !== 'string' && typeof operation.indexToUse !== 'number')) { | ||
| throw new Error('Invalid MATCH operation.'); | ||
| } | ||
|
|
||
| const pattern = operation.regex ? new RegExp(operation.pattern, operation.flags) : operation.pattern; | ||
|
pierre-lehnen-rc marked this conversation as resolved.
pierre-lehnen-rc marked this conversation as resolved.
|
||
|
|
||
| const result = input.match(pattern); | ||
| if (!result) { | ||
| return operation.valueIfFalse; | ||
| } | ||
|
|
||
| if (typeof operation.indexToUse === 'number' && result.length > operation.indexToUse) { | ||
| return result[operation.indexToUse]; | ||
| } | ||
|
|
||
| return operation.valueIfTrue; | ||
|
pierre-lehnen-rc marked this conversation as resolved.
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| export type LDAPVariableReplace = { | ||
| operation: 'replace'; | ||
| pattern: string; | ||
| regex?: boolean; | ||
| flags?: string; | ||
| all?: boolean; | ||
| replacement: string; | ||
| }; | ||
|
|
||
| export function executeReplace(input: string, operation: LDAPVariableReplace): string { | ||
|
pierre-lehnen-rc marked this conversation as resolved.
|
||
| if (!operation.pattern || typeof operation.replacement !== 'string') { | ||
| throw new Error('Invalid REPLACE operation.'); | ||
| } | ||
|
|
||
| const flags = operation.regex && operation.all ? `${operation.flags || ''}${operation.flags?.includes('g') ? '' : 'g'}` : operation.flags; | ||
| const pattern = operation.regex ? new RegExp(operation.pattern, flags) : operation.pattern; | ||
|
pierre-lehnen-rc marked this conversation as resolved.
pierre-lehnen-rc marked this conversation as resolved.
|
||
|
|
||
| if (operation.all) { | ||
| return input.replaceAll(pattern, operation.replacement); | ||
| } | ||
|
|
||
| return input.replace(pattern, operation.replacement); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| export type LDAPVariableSplit = { | ||
| operation: 'split'; | ||
| pattern: string; | ||
| indexToUse?: number; | ||
| }; | ||
|
|
||
| export function executeSplit(input: string, operation: LDAPVariableSplit): string | undefined { | ||
|
pierre-lehnen-rc marked this conversation as resolved.
|
||
| if (!operation.pattern) { | ||
| throw new Error('Invalid SPLIT operation.'); | ||
| } | ||
|
|
||
| const result = input.split(operation.pattern); | ||
| if (!result) { | ||
| return; | ||
| } | ||
|
|
||
| if (typeof operation.indexToUse === 'number') { | ||
| if (result.length > operation.indexToUse) { | ||
|
pierre-lehnen-rc marked this conversation as resolved.
|
||
| return result[operation.indexToUse]; | ||
| } | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| return result.shift(); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| export type LDAPVariableSubString = { | ||
| operation: 'substring'; | ||
| start: number; | ||
| end?: number; | ||
| }; | ||
|
|
||
| export function executeSubstring(input: string, operation: LDAPVariableSubString): string | undefined { | ||
|
pierre-lehnen-rc marked this conversation as resolved.
|
||
| if (typeof operation.start !== 'number' || (operation.end !== undefined && typeof operation.end !== 'number')) { | ||
|
pierre-lehnen-rc marked this conversation as resolved.
pierre-lehnen-rc marked this conversation as resolved.
pierre-lehnen-rc marked this conversation as resolved.
|
||
| throw new Error('Invalid SUBSTRING operation.'); | ||
| } | ||
|
|
||
| return input.substring(operation.start, operation.end); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import type { ILDAPEntry } from '@rocket.chat/core-typings'; | ||
|
|
||
| import { mapLogger } from './Logger'; | ||
| import { getLdapDynamicValue } from './getLdapDynamicValue'; | ||
| import { executeOperation, type LDAPVariableOperation } from './operations/executeOperation'; | ||
|
|
||
| export type LDAPVariableConfiguration = { | ||
| input: string; | ||
| output?: LDAPVariableOperation; | ||
| }; | ||
| export type LDAPVariableMap = Record<string, LDAPVariableConfiguration>; | ||
|
|
||
| export function processLdapVariables(entry: ILDAPEntry, variableMap: LDAPVariableMap): ILDAPEntry { | ||
| if (!variableMap || !Object.keys(variableMap).length) { | ||
| mapLogger.debug('No LDAP variables to process.'); | ||
| return entry; | ||
| } | ||
|
|
||
| for (const variableName in variableMap) { | ||
| if (!variableMap.hasOwnProperty(variableName)) { | ||
| continue; | ||
| } | ||
|
|
||
| const variableData = variableMap[variableName]; | ||
| if (!variableData?.input) { | ||
| continue; | ||
| } | ||
|
|
||
| const input = getLdapDynamicValue(entry, variableData.input) || ''; | ||
| const output = executeOperation(entry, input, variableData.output) || ''; | ||
|
pierre-lehnen-rc marked this conversation as resolved.
pierre-lehnen-rc marked this conversation as resolved.
|
||
|
|
||
| mapLogger.debug({ msg: 'Processed LDAP variable.', variableName, input, output }); | ||
|
|
||
| entry[variableName] = output; | ||
|
pierre-lehnen-rc marked this conversation as resolved.
|
||
| } | ||
|
|
||
| return entry; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.