search-backend-node: track document visibility permission by type in IndexBuilder

Tracking permissions by collator ensures that all the documents of a
given type are authorized using the same permission. This is not
critical for result-by-result authorization, but will likely be a
useful constraint when adding in-engine permission filtering to
specific search engines in the future.

Signed-off-by: MT Lewis <mtlewis@users.noreply.github.com>
This commit is contained in:
MT Lewis
2022-01-17 17:59:33 +00:00
parent 9a511968b1
commit f6389e9e5d
5 changed files with 35 additions and 0 deletions
@@ -14,6 +14,7 @@
* limitations under the License.
*/
import { Permission } from '@backstage/plugin-permission-common';
import {
DocumentCollator,
DocumentDecorator,
@@ -37,15 +38,25 @@ type IndexBuilderOptions = {
logger: Logger;
};
export type DocumentTypeInfo = {
/**
* The {@link @backstage/plugin-permission-common#Permission} that controls
* visibility of resources associated with this collator's documents.
*/
visibilityPermission?: Permission;
};
export class IndexBuilder {
private collators: Record<string, CollatorEnvelope>;
private decorators: Record<string, DocumentDecorator[]>;
private documentTypes: Record<string, DocumentTypeInfo>;
private searchEngine: SearchEngine;
private logger: Logger;
constructor({ logger, searchEngine }: IndexBuilderOptions) {
this.collators = {};
this.decorators = {};
this.documentTypes = {};
this.logger = logger;
this.searchEngine = searchEngine;
}
@@ -54,6 +65,10 @@ export class IndexBuilder {
return this.searchEngine;
}
getDocumentTypes(): Record<string, DocumentTypeInfo> {
return this.documentTypes;
}
/**
* Makes the index builder aware of a collator that should be executed at the
* given refresh interval.
@@ -69,6 +84,9 @@ export class IndexBuilder {
refreshInterval: defaultRefreshIntervalSeconds,
collate: collator,
};
this.documentTypes[collator.type] = {
visibilityPermission: collator.visibilityPermission,
};
}
/**
+1
View File
@@ -21,6 +21,7 @@
*/
export { IndexBuilder } from './IndexBuilder';
export type { DocumentTypeInfo } from './IndexBuilder';
export { Scheduler } from './Scheduler';
export { LunrSearchEngine } from './engines';