Skip to content

Commit

Permalink
update Credential database, add provider types
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Werfling committed May 23, 2024
1 parent 91773d2 commit 60feaf1
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 32 deletions.
5 changes: 3 additions & 2 deletions backend/src/Routes/Main/Credential.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {Router} from 'express';
import {DefaultRoute} from 'flyingfish_core';
import {List} from './Credential/List.js';
import {Provider} from './Credential/Provider.js';

export class Credential extends DefaultRoute {

Expand All @@ -15,7 +16,7 @@ export class Credential extends DefaultRoute {
res
) => {
if (this.isUserLogin(req, res)) {
res.status(200).json(List.getCredentials());
res.status(200).json(await Provider.getProviders());
}
}
);
Expand All @@ -27,7 +28,7 @@ export class Credential extends DefaultRoute {
res
) => {
if (this.isUserLogin(req, res)) {
res.status(200).json(List.getCredentials());
res.status(200).json(await List.getCredentials());
}
}
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
BaseProviders,
ICredentialProvider,
ICredentialProviders
} from 'flyingfish_core';
import {BaseProviders, ICredentialProvider, ICredentialProviders, ProviderType} from 'flyingfish_core';
import {ProviderEntry} from 'flyingfish_schemas';
import {CredentialProvider as CredentialProviderDatabase} from './Database/CredentialProvider.js';

Expand All @@ -22,7 +18,7 @@ export class CredentialProviders extends BaseProviders implements ICredentialPro
return new CredentialProviderDatabase();

default:
return this._getProvider<ICredentialProvider>(name);
return this._getProvider<ICredentialProvider>(name, ProviderType.credential);
}
}

Expand All @@ -31,9 +27,11 @@ export class CredentialProviders extends BaseProviders implements ICredentialPro
* @returns {ProviderEntry[]}
*/
public async getProviders(): Promise<ProviderEntry[]> {
const list: ProviderEntry[] = [];
const list: ProviderEntry[] = [
new CredentialProviderDatabase().getProviderEntry()
];

const plist = await this._getProviders<ICredentialProvider>();
const plist = await this._getProviders<ICredentialProvider>(ProviderType.credential);

if (plist) {
list.push(...plist);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {ICredential, ICredentialProvider} from 'flyingfish_core';
import {ICredential, ICredentialProvider, ProviderType} from 'flyingfish_core';
import {ProviderEntry} from 'flyingfish_schemas';
import {CredentialDatabase} from './CredentialDatabase.js';

/**
Expand All @@ -7,6 +8,7 @@ import {CredentialDatabase} from './CredentialDatabase.js';
export class CredentialProvider implements ICredentialProvider {

public static NAME = 'intern_database';
public static TITLE = 'Intern database';

/**
* Return credential
Expand All @@ -25,7 +27,26 @@ export class CredentialProvider implements ICredentialProvider {
}

public getTitle(): string {
return 'Intern database';
return CredentialProvider.TITLE;
}

/**
* Return the type of provider
* @returns {ProviderType}
*/
public getType(): ProviderType {
return ProviderType.credential;
}

/**
* Return the provider entry
* @returns {ProviderEntry}
*/
public getProviderEntry(): ProviderEntry {
return {
name: CredentialProvider.NAME,
title: CredentialProvider.TITLE
};
}

}
10 changes: 3 additions & 7 deletions backend/src/inc/Provider/SslCertProvider/SslCertProviders.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
BaseProviders,
ISslCertProvider,
ISslCertProviders
} from 'flyingfish_core';
import {BaseProviders, ISslCertProvider, ISslCertProviders, ProviderType} from 'flyingfish_core';
import {ProviderEntry} from 'flyingfish_schemas';

/**
Expand All @@ -16,15 +12,15 @@ export class SslCertProviders extends BaseProviders implements ISslCertProviders
* @returns {ISslCertProvider|null}
*/
public async getProvider(name: string): Promise<ISslCertProvider | null> {
return this._getProvider<ISslCertProvider>(name);
return this._getProvider<ISslCertProvider>(name, ProviderType.sslcert);
}

/**
* Get a provider list with name and title.
* @returns {ProviderEntry[]}
*/
public async getProviders(): Promise<ProviderEntry[]> {
return this._getProviders<ISslCertProvider>();
return this._getProviders<ISslCertProvider>(ProviderType.sslcert);
}

}
16 changes: 9 additions & 7 deletions core/src/inc/Provider/BaseProviders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,25 @@ import {ProviderEntry} from 'flyingfish_schemas';
import {PluginManager} from '../PluginSystem/PluginManager.js';
import {AProviderOnLoadEvent} from './AProviderOnLoadEvent.js';
import {IProvider} from './IProvider.js';
import {ProviderType} from './ProviderType.js';

export class BaseProviders {

/**
* Return a provider by name
* @param {string} name
* @param {ProviderType} type
* @protected
* @returns {T extends IProvider|null}
*/
protected async _getProvider<T extends IProvider>(name: string): Promise<T | null> {
protected async _getProvider<T extends IProvider>(name: string, type: ProviderType): Promise<T | null> {
const events = PluginManager.getInstance().getAllEvents<AProviderOnLoadEvent<T>>(AProviderOnLoadEvent<T>);

for await (const event of events) {
const providers = await event.getProviders();

for (const provider of providers) {
if (provider.getName() === name) {
if (provider.getType() === type && provider.getName() === name) {
return provider;
}
}
Expand All @@ -29,10 +31,11 @@ export class BaseProviders {

/**
* Get a provider list with name and title.
* @param {ProviderType} type
* @protected
* @returns {ProviderEntry[]}
*/
protected async _getProviders<T extends IProvider>(): Promise<ProviderEntry[]> {
protected async _getProviders<T extends IProvider>(type: ProviderType): Promise<ProviderEntry[]> {
const list: ProviderEntry[] = [];

const events = PluginManager.getInstance().getAllEvents<AProviderOnLoadEvent<T>>(AProviderOnLoadEvent<T>);
Expand All @@ -41,10 +44,9 @@ export class BaseProviders {
const providers = await event.getProviders();

for (const provider of providers) {
list.push({
name: provider.getName(),
title: provider.getTitle()
});
if (provider.getType() === type) {
list.push(provider.getProviderEntry());
}
}
}

Expand Down
15 changes: 15 additions & 0 deletions core/src/inc/Provider/IProvider.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import {ProviderEntry} from 'flyingfish_schemas';
import {ProviderType} from './ProviderType.js';

/**
* Interface for a Provider
*/
Expand All @@ -15,4 +18,16 @@ export interface IProvider {
*/
getTitle(): string;

/**
* Return the type of provider
* @returns {ProviderType}
*/
getType(): ProviderType;

/**
* Return the provider entry
* @returns {ProviderEntry}
*/
getProviderEntry(): ProviderEntry;

}
7 changes: 7 additions & 0 deletions core/src/inc/Provider/ProviderType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Provider types for provider
*/
export enum ProviderType {
sslcert = 'sslcert',
credential = 'credential'
};
1 change: 1 addition & 0 deletions core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export {PluginInformation, PluginManager} from './inc/PluginSystem/PluginManager
export {APluginEvent} from './inc/PluginSystem/APluginEvent.js';

// Provider
export {ProviderType} from './inc/Provider/ProviderType.js';
export {IProvider} from './inc/Provider/IProvider.js';
export {IProviders} from './inc/Provider/IProviders.js';
export {AProviderOnLoadEvent} from './inc/Provider/AProviderOnLoadEvent.js';
Expand Down
29 changes: 26 additions & 3 deletions plugins/letsencrypt/src/Acme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import {
FileHelper,
FSslCertProviderOnReset,
ISslCertProvider,
Logger, SslCertBundel, SslCertBundelOptions, SslCertCreateGlobal, SslCertCreateOptions,
Logger, ProviderType, SslCertBundel, SslCertBundelOptions, SslCertCreateGlobal, SslCertCreateOptions,
SslCertExistOptions
} from 'flyingfish_core';
import {ProviderEntry} from 'flyingfish_schemas';
import path from 'path';
import {Client, LetsEncryptDnsChallengeAndFinalize} from './Acme/Client.js';

Expand All @@ -15,6 +16,9 @@ import {Client, LetsEncryptDnsChallengeAndFinalize} from './Acme/Client.js';
*/
export class Acme implements ISslCertProvider {

public static NAME = 'letsencrypt_dns01';
public static TITLE = 'LetsEncrypt (DNS-01)';

public static readonly LIMIT_REQUESTS = 5;
public static readonly LIMIT_TIME_HOUR = 1;

Expand All @@ -34,15 +38,34 @@ export class Acme implements ISslCertProvider {
* @returns {string}
*/
public getName(): string {
return 'letsencrypt_dns01';
return Acme.NAME;
}

/**
* Return the title for provider (for frontend).
* @returns {string}
*/
public getTitle(): string {
return 'LetsEncrypt (DNS-01)';
return Acme.TITLE;
}

/**
* Return the type of provider
* @returns {ProviderType}
*/
public getType(): ProviderType {
return ProviderType.sslcert;
}

/**
* Return the provider entry
* @returns {ProviderEntry}
*/
public getProviderEntry(): ProviderEntry {
return {
name: Acme.NAME,
title: Acme.TITLE
};
}

/**
Expand Down
29 changes: 26 additions & 3 deletions plugins/letsencrypt/src/Certbot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import {
FileHelper,
FSslCertProviderOnReset,
ISslCertProvider,
Logger,
Logger, ProviderType,
SslCertBundel, SslCertCreateOptions
} from 'flyingfish_core';
import {ProviderEntry} from 'flyingfish_schemas';
import path from 'path';
import {spawn} from 'child_process';

Expand All @@ -14,6 +15,9 @@ import {spawn} from 'child_process';
*/
export class Certbot implements ISslCertProvider {

public static NAME = 'letsencrypt';
public static TITLE = 'LetsEncrypt (HTTP-01)';

public static readonly LIMIT_REQUESTS = 5;
public static readonly LIMIT_TIME_HOUR = 1;

Expand Down Expand Up @@ -45,15 +49,34 @@ export class Certbot implements ISslCertProvider {
* @returns {string}
*/
public getName(): string {
return 'letsencrypt';
return Certbot.NAME;
}

/**
* Return the title for provider (for frontend).
* @returns {string}
*/
public getTitle(): string {
return 'LetsEncrypt (HTTP-01)';
return Certbot.TITLE;
}

/**
* Return the type of provider
* @returns {ProviderType}
*/
public getType(): ProviderType {
return ProviderType.sslcert;
}

/**
* Return the provider entry
* @returns {ProviderEntry}
*/
public getProviderEntry(): ProviderEntry {
return {
name: Certbot.NAME,
title: Certbot.TITLE
};
}

/**
Expand Down

0 comments on commit 60feaf1

Please sign in to comment.