refactor: Split off AclInitializer.

This commit is contained in:
Ruben Verborgh
2020-12-07 22:03:12 +01:00
committed by Joachim Van Herwegen
parent 9c933fd2d4
commit 8fbb4f592e
8 changed files with 172 additions and 111 deletions

View File

@@ -7,21 +7,36 @@
"Setup:_serverFactory": {
"@id": "urn:solid-server:default:ServerFactory"
},
"Setup:_store": {
"@id": "urn:solid-server:default:ResourceStore"
},
"Setup:_aclManager": {
"@id": "urn:solid-server:default:AclManager"
},
"Setup:_loggerFactory": {
"@id": "urn:solid-server:default:LoggerFactory"
},
"Setup:_initializer": {
"@id": "urn:solid-server:default:Initializer"
},
"Setup:_base": {
"@id": "urn:solid-server:default:variable:baseUrl"
},
"Setup:_port": {
"@id": "urn:solid-server:default:variable:port"
}
},
{
"@id": "urn:solid-server:default:Initializer",
"@type": "AllVoidCompositeHandler",
"AllVoidCompositeHandler:_handlers": [
{
"@type": "AclInitializer",
"AclInitializer:_baseUrl": {
"@id": "urn:solid-server:default:variable:baseUrl"
},
"AclInitializer:_store": {
"@id": "urn:solid-server:default:ResourceStore"
},
"AclInitializer:_aclManager": {
"@id": "urn:solid-server:default:AclManager"
}
}
]
}
]
}

View File

@@ -13,7 +13,9 @@ export * from './authorization/UrlBasedAclManager';
export * from './authorization/WebAclAuthorizer';
// Init
export * from './init/AclInitializer';
export * from './init/CliRunner';
export * from './init/Initializer';
export * from './init/Setup';
// LDP/HTTP/Metadata

View File

@@ -0,0 +1,80 @@
import type { AclManager } from '../authorization/AclManager';
import { RepresentationMetadata } from '../ldp/representation/RepresentationMetadata';
import type { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier';
import { getLoggerFor } from '../logging/LogUtil';
import type { ResourceStore } from '../storage/ResourceStore';
import { TEXT_TURTLE } from '../util/ContentTypes';
import { NotFoundHttpError } from '../util/errors/NotFoundHttpError';
import { guardedStreamFrom } from '../util/StreamUtil';
import { CONTENT_TYPE } from '../util/UriConstants';
import { Initializer } from './Initializer';
/**
* Ensures that a root ACL is present.
*/
export class AclInitializer extends Initializer {
protected readonly logger = getLoggerFor(this);
private readonly store: ResourceStore;
private readonly aclManager: AclManager;
private readonly baseUrl: string;
public constructor(
baseUrl: string,
store: ResourceStore,
aclManager: AclManager,
) {
super();
this.baseUrl = baseUrl;
this.store = store;
this.aclManager = aclManager;
}
public async handle(): Promise<void> {
const rootAcl = await this.aclManager.getAclDocument({ path: this.baseUrl });
if (!await this.hasRootAclDocument(rootAcl)) {
await this.setRootAclDocument(rootAcl);
}
}
protected async hasRootAclDocument(rootAcl: ResourceIdentifier): Promise<boolean> {
try {
const result = await this.store.getRepresentation(rootAcl, {});
this.logger.debug(`Existing root ACL document found at ${rootAcl.path}`);
result.data.destroy();
return true;
} catch (error: unknown) {
if (error instanceof NotFoundHttpError) {
return false;
}
throw error;
}
}
// Set up ACL so everything can still be done by default
// Note that this will need to be adapted to go through all the correct channels later on
protected async setRootAclDocument(rootAcl: ResourceIdentifier): Promise<void> {
const acl = `@prefix acl: <http://www.w3.org/ns/auth/acl#>.
@prefix foaf: <http://xmlns.com/foaf/0.1/>.
<#authorization>
a acl:Authorization;
acl:agentClass foaf:Agent;
acl:mode acl:Read;
acl:mode acl:Write;
acl:mode acl:Append;
acl:mode acl:Delete;
acl:mode acl:Control;
acl:accessTo <${this.baseUrl}>;
acl:default <${this.baseUrl}>.`;
const metadata = new RepresentationMetadata(rootAcl.path, { [CONTENT_TYPE]: TEXT_TURTLE });
this.logger.debug(`Installing root ACL document at ${rootAcl.path}`);
await this.store.setRepresentation(
rootAcl,
{
binary: true,
data: guardedStreamFrom([ acl ]),
metadata,
},
);
}
}

3
src/init/Initializer.ts Normal file
View File

@@ -0,0 +1,3 @@
import { AsyncHandler } from '../util/AsyncHandler';
export abstract class Initializer extends AsyncHandler {}

View File

@@ -1,14 +1,7 @@
import type { AclManager } from '../authorization/AclManager';
import { RepresentationMetadata } from '../ldp/representation/RepresentationMetadata';
import type { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier';
import type { LoggerFactory } from '../logging/LoggerFactory';
import { getLoggerFor, setGlobalLoggerFactory } from '../logging/LogUtil';
import type { HttpServerFactory } from '../server/HttpServerFactory';
import type { ResourceStore } from '../storage/ResourceStore';
import { TEXT_TURTLE } from '../util/ContentTypes';
import { NotFoundHttpError } from '../util/errors/NotFoundHttpError';
import { guardedStreamFrom } from '../util/StreamUtil';
import { CONTENT_TYPE } from '../util/UriConstants';
import type { Initializer } from './Initializer';
/**
* Invokes all logic to setup a server.
@@ -16,24 +9,21 @@ import { CONTENT_TYPE } from '../util/UriConstants';
export class Setup {
protected readonly logger = getLoggerFor(this);
private readonly serverFactory: HttpServerFactory;
private readonly store: ResourceStore;
private readonly aclManager: AclManager;
private readonly loggerFactory: LoggerFactory;
private readonly initializer: Initializer;
private readonly base: string;
private readonly port: number;
public constructor(
serverFactory: HttpServerFactory,
store: ResourceStore,
aclManager: AclManager,
loggerFactory: LoggerFactory,
initializer: Initializer,
base: string,
port: number,
) {
this.serverFactory = serverFactory;
this.store = store;
this.aclManager = aclManager;
this.loggerFactory = loggerFactory;
this.initializer = initializer;
this.base = base;
this.port = port;
}
@@ -44,54 +34,9 @@ export class Setup {
public async setup(): Promise<string> {
setGlobalLoggerFactory(this.loggerFactory);
const rootAcl = await this.aclManager.getAclDocument({ path: this.base });
if (!await this.hasRootAclDocument(rootAcl)) {
await this.setRootAclDocument(rootAcl);
}
await this.initializer.handleSafe();
this.serverFactory.startServer(this.port);
return this.base;
}
protected async hasRootAclDocument(rootAcl: ResourceIdentifier): Promise<boolean> {
try {
const result = await this.store.getRepresentation(rootAcl, {});
this.logger.debug(`Existing root ACL document found at ${rootAcl.path}`);
result.data.destroy();
return true;
} catch (error: unknown) {
if (error instanceof NotFoundHttpError) {
return false;
}
throw error;
}
}
// Set up ACL so everything can still be done by default
// Note that this will need to be adapted to go through all the correct channels later on
protected async setRootAclDocument(rootAcl: ResourceIdentifier): Promise<void> {
const acl = `@prefix acl: <http://www.w3.org/ns/auth/acl#>.
@prefix foaf: <http://xmlns.com/foaf/0.1/>.
<#authorization>
a acl:Authorization;
acl:agentClass foaf:Agent;
acl:mode acl:Read;
acl:mode acl:Write;
acl:mode acl:Append;
acl:mode acl:Delete;
acl:mode acl:Control;
acl:accessTo <${this.base}>;
acl:default <${this.base}>.`;
const metadata = new RepresentationMetadata(rootAcl.path, { [CONTENT_TYPE]: TEXT_TURTLE });
this.logger.debug(`Installing root ACL document at ${rootAcl.path}`);
await this.store.setRepresentation(
rootAcl,
{
binary: true,
data: guardedStreamFrom([ acl ]),
metadata,
},
);
}
}

View File

@@ -1,7 +1,7 @@
/**
* Simple interface for classes that can potentially handle a specific kind of data asynchronously.
*/
export abstract class AsyncHandler<TInput, TOutput = void> {
export abstract class AsyncHandler<TInput = void, TOutput = void> {
/**
* Checks if the input data can be handled by this class.
* Throws an error if it can't handle the data.

View File

@@ -0,0 +1,52 @@
import type { AclManager } from '../../../src/authorization/AclManager';
import { AclInitializer } from '../../../src/init/AclInitializer';
import type { ResourceIdentifier } from '../../../src/ldp/representation/ResourceIdentifier';
import type { ResourceStore } from '../../../src/storage/ResourceStore';
import { NotFoundHttpError } from '../../../src/util/errors/NotFoundHttpError';
describe('AclInitializer', (): void => {
const store: jest.Mocked<ResourceStore> = {
getRepresentation: jest.fn().mockRejectedValue(new NotFoundHttpError()),
setRepresentation: jest.fn(),
} as any;
const aclManager: jest.Mocked<AclManager> = {
getAclDocument: jest.fn(async(): Promise<ResourceIdentifier> => ({ path: 'http://test.com/.acl' })),
} as any;
const baseUrl = 'http://localhost:3000/';
let initializer: AclInitializer;
beforeEach(async(): Promise<void> => {
initializer = new AclInitializer(baseUrl, store, aclManager);
});
afterEach((): void => {
jest.clearAllMocks();
});
it('invokes ACL initialization.', async(): Promise<void> => {
await initializer.handle();
expect(aclManager.getAclDocument).toHaveBeenCalledWith({ path: 'http://localhost:3000/' });
expect(store.getRepresentation).toHaveBeenCalledTimes(1);
expect(store.getRepresentation).toHaveBeenCalledWith({ path: 'http://test.com/.acl' }, {});
expect(store.setRepresentation).toHaveBeenCalledTimes(1);
});
it('does not invoke ACL initialization when a root ACL already exists.', async(): Promise<void> => {
store.getRepresentation.mockReturnValueOnce(Promise.resolve({
data: { destroy: jest.fn() },
} as any));
await initializer.handle();
expect(aclManager.getAclDocument).toHaveBeenCalledWith({ path: 'http://localhost:3000/' });
expect(store.getRepresentation).toHaveBeenCalledTimes(1);
expect(store.getRepresentation).toHaveBeenCalledWith({ path: 'http://test.com/.acl' }, {});
expect(store.setRepresentation).toHaveBeenCalledTimes(0);
});
it('errors when the root ACL check errors.', async(): Promise<void> => {
store.getRepresentation.mockRejectedValueOnce(new Error('Fatal'));
await expect(initializer.handle()).rejects.toThrow('Fatal');
});
});

View File

@@ -1,62 +1,26 @@
import type { AclManager } from '../../../src/authorization/AclManager';
import type { Initializer } from '../../../src/init/Initializer';
import { Setup } from '../../../src/init/Setup';
import type { ResourceIdentifier } from '../../../src/ldp/representation/ResourceIdentifier';
import { VoidLoggerFactory } from '../../../src/logging/VoidLoggerFactory';
import type { HttpServerFactory } from '../../../src/server/HttpServerFactory';
import type { ResourceStore } from '../../../src/storage/ResourceStore';
import { NotFoundHttpError } from '../../../src/util/errors/NotFoundHttpError';
describe('Setup', (): void => {
const serverFactory: jest.Mocked<HttpServerFactory> = {
startServer: jest.fn(),
};
const store: jest.Mocked<ResourceStore> = {
getRepresentation: jest.fn().mockRejectedValue(new NotFoundHttpError()),
setRepresentation: jest.fn(),
} as any;
const aclManager: jest.Mocked<AclManager> = {
getAclDocument: jest.fn(async(): Promise<ResourceIdentifier> => ({ path: 'http://test.com/.acl' })),
const initializer: jest.Mocked<Initializer> = {
handleSafe: jest.fn(),
} as any;
let setup: Setup;
beforeEach(async(): Promise<void> => {
setup = new Setup(serverFactory, store, aclManager, new VoidLoggerFactory(), 'http://localhost:3000/', 3000);
});
afterEach((): void => {
jest.clearAllMocks();
beforeAll(async(): Promise<void> => {
const setup = new Setup(serverFactory, new VoidLoggerFactory(), initializer, 'http://localhost:3000/', 3000);
await setup.setup();
});
it('starts an HTTP server.', async(): Promise<void> => {
await setup.setup();
expect(serverFactory.startServer).toHaveBeenCalledWith(3000);
});
it('invokes ACL initialization.', async(): Promise<void> => {
await setup.setup();
expect(aclManager.getAclDocument).toHaveBeenCalledWith({ path: 'http://localhost:3000/' });
expect(store.getRepresentation).toHaveBeenCalledTimes(1);
expect(store.getRepresentation).toHaveBeenCalledWith({ path: 'http://test.com/.acl' }, {});
expect(store.setRepresentation).toHaveBeenCalledTimes(1);
});
it('does not invoke ACL initialization when a root ACL already exists.', async(): Promise<void> => {
store.getRepresentation.mockReturnValueOnce(Promise.resolve({
data: { destroy: jest.fn() },
} as any));
await setup.setup();
expect(aclManager.getAclDocument).toHaveBeenCalledWith({ path: 'http://localhost:3000/' });
expect(store.getRepresentation).toHaveBeenCalledTimes(1);
expect(store.getRepresentation).toHaveBeenCalledWith({ path: 'http://test.com/.acl' }, {});
expect(store.setRepresentation).toHaveBeenCalledTimes(0);
});
it('errors when the root ACL check errors.', async(): Promise<void> => {
store.getRepresentation.mockRejectedValueOnce(new Error('Fatal'));
await expect(setup.setup()).rejects.toThrow('Fatal');
it('calls the initializer.', async(): Promise<void> => {
expect(initializer.handleSafe).toHaveBeenCalledTimes(1);
});
});