forked from hyperledger-cacti/cacti
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Youngone Lee <youngone.lee@accenture.com>
- Loading branch information
1 parent
046ee33
commit 951b9f1
Showing
4 changed files
with
370 additions
and
229 deletions.
There are no files selected for viewing
45 changes: 33 additions & 12 deletions
45
packages/cactus-plugin-keychain-azure-kv/src/main/typescript/plugin-factory-keychain.ts
This file contains 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 |
---|---|---|
@@ -1,27 +1,48 @@ | ||
import { v4 as uuidv4 } from "uuid"; | ||
import { Checks } from "@hyperledger/cactus-common"; | ||
import { | ||
IPluginFactoryOptions, | ||
IPluginKeychain, | ||
PluginFactory, | ||
PluginImportType, | ||
} from "@hyperledger/cactus-core-api"; | ||
|
||
import { | ||
Configuration, | ||
DefaultApi, | ||
} from "./generated/openapi/typescript-axios"; | ||
import { | ||
IPluginKeychainAzureKvOptions, | ||
PluginKeychainAzureKv, | ||
} from "./plugin-keychain-azure-kv"; | ||
|
||
import { PluginKeychainAzureKvRemoteAdapter } from "./plugin-keychain-azure-kv-remote-adapter"; | ||
|
||
export class PluginFactoryKeychain extends PluginFactory< | ||
PluginKeychainAzureKv, | ||
IPluginKeychain, | ||
IPluginKeychainAzureKvOptions, | ||
IPluginFactoryOptions | ||
> { | ||
async create( | ||
pluginOptions: IPluginKeychainAzureKvOptions = { | ||
instanceId: uuidv4(), | ||
keychainId: uuidv4(), | ||
azureEndpoint: "", | ||
logLevel: "TRACE", | ||
}, | ||
): Promise<PluginKeychainAzureKv> { | ||
return new PluginKeychainAzureKv(pluginOptions); | ||
async create(options: any): Promise<IPluginKeychain> { | ||
const fnTag = "PluginFactoryKeychain#create()"; | ||
|
||
const { pluginImportType } = this.options; | ||
Checks.truthy(options, `${fnTag}:options`); | ||
|
||
switch (pluginImportType) { | ||
case PluginImportType.Local: { | ||
return new PluginKeychainAzureKv(options); | ||
} | ||
case PluginImportType.Remote: { | ||
const { remoteConfig } = options; | ||
Checks.truthy(remoteConfig, `${fnTag}:options.remoteConfig`); | ||
Checks.truthy(remoteConfig.basePath, `${fnTag}:remoteConfig.basePath`); | ||
const configuration: Configuration = options.remoteConfig; | ||
const backend = new DefaultApi(configuration); | ||
const optionsDecorated = { ...options, backend }; | ||
return new PluginKeychainAzureKvRemoteAdapter(optionsDecorated); | ||
} | ||
default: { | ||
throw new Error(`${fnTag} No PluginImportType: ${pluginImportType}`); | ||
} | ||
} | ||
} | ||
} |
This file contains 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
100 changes: 100 additions & 0 deletions
100
...-plugin-keychain-azure-kv/src/main/typescript/web-services/has-keychain-entry-endpoint.ts
This file contains 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,100 @@ | ||
import { Express, Response } from "express"; | ||
|
||
import { | ||
Logger, | ||
Checks, | ||
LogLevelDesc, | ||
LoggerProvider, | ||
IAsyncProvider, | ||
} from "@hyperledger/cactus-common"; | ||
import { | ||
IEndpointAuthzOptions, | ||
IExpressRequestHandler, | ||
IWebServiceEndpoint, | ||
} from "@hyperledger/cactus-core-api"; | ||
import { registerWebServiceEndpoint } from "@hyperledger/cactus-core"; | ||
|
||
import { PluginKeychainAzureKv } from "../plugin-keychain-azure-kv"; | ||
|
||
import OAS from "../../json/openapi.json"; | ||
|
||
export interface IHasKeychainEntryEndpointOptions { | ||
logLevel?: LogLevelDesc; | ||
connector: PluginKeychainAzureKv; | ||
} | ||
|
||
export class HasKeychainEntryEndpoint implements IWebServiceEndpoint { | ||
public static readonly CLASS_NAME = "HasKeychainEntryEndpoint"; | ||
|
||
private readonly log: Logger; | ||
|
||
public get className(): string { | ||
return HasKeychainEntryEndpoint.CLASS_NAME; | ||
} | ||
|
||
constructor(public readonly options: IHasKeychainEntryEndpointOptions) { | ||
const fnTag = `${this.className}#constructor()`; | ||
Checks.truthy(options, `${fnTag} arg options`); | ||
Checks.truthy(options.connector, `${fnTag} arg options.connector`); | ||
|
||
const level = this.options.logLevel || "INFO"; | ||
const label = this.className; | ||
this.log = LoggerProvider.getOrCreate({ level, label }); | ||
} | ||
|
||
public getOasPath() { | ||
return OAS.paths[ | ||
"/api/v1/plugins/@hyperledger/cactus-plugin-keychain-azure-kv/has-keychain-entry" | ||
]; | ||
} | ||
|
||
public getPath(): string { | ||
const apiPath = this.getOasPath(); | ||
return apiPath.post["x-hyperledger-cactus"].http.path; | ||
} | ||
|
||
public getVerbLowerCase(): string { | ||
const apiPath = this.getOasPath(); | ||
return apiPath.post["x-hyperledger-cactus"].http.verbLowerCase; | ||
} | ||
|
||
public getOperationId(): string { | ||
return this.getOasPath().post.operationId; | ||
} | ||
|
||
getAuthorizationOptionsProvider(): IAsyncProvider<IEndpointAuthzOptions> { | ||
// TODO: make this an injectable dependency in the constructor | ||
return { | ||
get: async () => ({ | ||
isProtected: true, | ||
requiredRoles: [], | ||
}), | ||
}; | ||
} | ||
|
||
public async registerExpress( | ||
expressApp: Express, | ||
): Promise<IWebServiceEndpoint> { | ||
await registerWebServiceEndpoint(expressApp, this); | ||
return this; | ||
} | ||
|
||
public getExpressRequestHandler(): IExpressRequestHandler { | ||
return this.handleRequest.bind(this); | ||
} | ||
|
||
public async handleRequest(res: Response): Promise<void> { | ||
const reqTag = `${this.getVerbLowerCase()} - ${this.getPath()}`; | ||
this.log.debug(reqTag); | ||
try { | ||
const resBody = await this.options.connector.getKeychainId(); | ||
res.json(resBody); | ||
} catch (ex) { | ||
this.log.error(`Crash while serving ${reqTag}`, ex); | ||
res.status(500).json({ | ||
message: "Internal Server Error", | ||
error: ex?.stack || ex?.message, | ||
}); | ||
} | ||
} | ||
} |
Oops, something went wrong.