Skip to content

Commit

Permalink
Merge pull request #4735 from alkem-io/fix/faster-auth
Browse files Browse the repository at this point in the history
More stable auth
  • Loading branch information
valentinyanakiev authored Nov 26, 2024
2 parents 58c8afe + cc55173 commit b80c0bf
Show file tree
Hide file tree
Showing 14 changed files with 34 additions and 25 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "alkemio-server",
"version": "0.96.1",
"version": "0.96.2",
"description": "Alkemio server, responsible for managing the shared Alkemio platform",
"author": "Alkemio Foundation",
"private": false,
Expand Down
2 changes: 1 addition & 1 deletion src/domain/collaboration/link/link.resolver.mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export class LinkResolverMutations {
document = await this.documentService.saveDocument(document);

const documentAuthorizations =
this.documentAuthorizationService.applyAuthorizationPolicy(
await this.documentAuthorizationService.applyAuthorizationPolicy(
document,
storageBucket.authorization
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,18 @@ export class AuthorizationPolicyService {
}

async saveAll(authorizationPolicies: IAuthorizationPolicy[]): Promise<void> {
this.logger.verbose?.(
`Saving ${authorizationPolicies.length} authorization policies`,
LogContext.AUTH
);
if (authorizationPolicies.length > 500)
this.logger.warn?.(
`Saving ${authorizationPolicies.length} authorization policies of type ${authorizationPolicies[0].type}`,
LogContext.AUTH
);
else {
this.logger.verbose?.(
`Saving ${authorizationPolicies.length} authorization policies`,
LogContext.AUTH
);
}

await this.authorizationPolicyRepository.save(authorizationPolicies, {
chunk: this.authChunkSize,
});
Expand Down
6 changes: 3 additions & 3 deletions src/domain/common/profile/profile.service.authorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ export class ProfileAuthorizationService {
}

const storageBucketAuthorizations =
this.storageBucketAuthorizationService.applyAuthorizationPolicy(
await this.storageBucketAuthorizationService.applyAuthorizationPolicy(
profile.storageBucket,
profile.authorization
);
updatedAuthorizations.push(...storageBucketAuthorizations);
await this.authorizationPolicyService.saveAll(updatedAuthorizations);
return [];

return updatedAuthorizations;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export class ReferenceResolverMutations {
document = await this.documentService.saveDocument(document);

const documentAuthorizations =
this.documentAuthorizationService.applyAuthorizationPolicy(
await this.documentAuthorizationService.applyAuthorizationPolicy(
document,
storageBucket.authorization
);
Expand Down
2 changes: 1 addition & 1 deletion src/domain/common/visual/visual.resolver.mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export class VisualResolverMutations {
await this.documentService.saveDocument(visualDocument);
// Ensure authorization is updated
const documentAuthorizations =
this.documentAuthorizationService.applyAuthorizationPolicy(
await this.documentAuthorizationService.applyAuthorizationPolicy(
visualDocument,
storageBucket.authorization
);
Expand Down
2 changes: 1 addition & 1 deletion src/domain/profile-documents/profile.documents.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class ProfileDocumentsService {
await this.documentService.saveDocument(newDoc);

const authorizations =
this.documentAuthorizationService.applyAuthorizationPolicy(
await this.documentAuthorizationService.applyAuthorizationPolicy(
newDoc,
storageBucketToCheck.authorization
);
Expand Down
1 change: 0 additions & 1 deletion src/domain/space/account/account.service.authorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ export class AccountAuthorizationService {
account.authorization = await this.authorizationPolicyService.save(
account.authorization
);
updatedAuthorizations.push(account.authorization);

const childUpdatedAuthorizations =
await this.applyAuthorizationPolicyForChildEntities(account);
Expand Down
7 changes: 4 additions & 3 deletions src/domain/storage/document/document.service.authorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import { RelationshipNotFoundException } from '@common/exceptions/relationship.n
export class DocumentAuthorizationService {
constructor(private authorizationPolicyService: AuthorizationPolicyService) {}

applyAuthorizationPolicy(
public async applyAuthorizationPolicy(
document: IDocument,
parentAuthorization: IAuthorizationPolicy | undefined
): IAuthorizationPolicy[] {
): Promise<IAuthorizationPolicy[]> {
if (!document.tagset || !document.tagset.authorization) {
throw new RelationshipNotFoundException(
`Unable to find entities required to reset auth for Document ${document.id} `,
Expand All @@ -44,7 +44,8 @@ export class DocumentAuthorizationService {
);
updatedAuthorizations.push(document.tagset.authorization);

return updatedAuthorizations;
await this.authorizationPolicyService.saveAll(updatedAuthorizations);
return [];
}

private appendCredentialRules(document: IDocument): IAuthorizationPolicy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class StorageAggregatorAuthorizationService {
updatedAuthorizations.push(storageAggregator.authorization);

const bucketAuthorizations =
this.storageBucketAuthorizationService.applyAuthorizationPolicy(
await this.storageBucketAuthorizationService.applyAuthorizationPolicy(
storageAggregator.directStorage,
storageAggregator.authorization
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ export class StorageBucketAuthorizationService {
private documentAuthorizationService: DocumentAuthorizationService
) {}

applyAuthorizationPolicy(
public async applyAuthorizationPolicy(
storageBucket: IStorageBucket,
parentAuthorization: IAuthorizationPolicy | undefined
): IAuthorizationPolicy[] {
): Promise<IAuthorizationPolicy[]> {
if (!storageBucket.documents) {
throw new RelationshipNotFoundException(
`Unable to load entities to reset auth for StorageBucket ${storageBucket.id} `,
Expand All @@ -49,14 +49,15 @@ export class StorageBucketAuthorizationService {
// Cascade down
for (const document of storageBucket.documents) {
const documentAuthorizations =
this.documentAuthorizationService.applyAuthorizationPolicy(
await this.documentAuthorizationService.applyAuthorizationPolicy(
document,
storageBucket.authorization
);
updatedAuthorizations.push(...documentAuthorizations);
}

return updatedAuthorizations;
await this.authorizationPolicyService.saveAll(updatedAuthorizations);
return [];
}

private appendPrivilegeRules(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class AdminSearchContributorsMutations {
}

const authorizations =
this.storageBucketAuthorizationService.applyAuthorizationPolicy(
await this.storageBucketAuthorizationService.applyAuthorizationPolicy(
profile.storageBucket,
profile.authorization
);
Expand Down
2 changes: 1 addition & 1 deletion src/platform/admin/whiteboards/admin.whiteboard.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export class AdminWhiteboardService {
);
document = await this.documentService.saveDocument(document);
const documentAuthorizations =
this.documentAuthorizationService.applyAuthorizationPolicy(
await this.documentAuthorizationService.applyAuthorizationPolicy(
document,
profile.storageBucket.authorization
);
Expand Down

0 comments on commit b80c0bf

Please sign in to comment.