Skip to content

Commit

Permalink
refactor: test cases
Browse files Browse the repository at this point in the history
Signed-off-by: Youngone Lee <youngone.lee@accenture.com>
  • Loading branch information
Leeyoungone committed Aug 20, 2021
1 parent 046ee33 commit 951b9f1
Show file tree
Hide file tree
Showing 4 changed files with 370 additions and 229 deletions.
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}`);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ import {
IWebServiceEndpoint,
} from "@hyperledger/cactus-core-api";

//import {homedir} from "os";
//import { PluginRegistry } from "@hyperledger/cactus-core";
import { SetKeychainEntryEndpoint } from "./web-services/set-keychain-entry-endpoint";
import { GetKeychainEntryEndpoint } from "./web-services/get-keychain-entry-endpoint";
import { DeleteKeychainEntryEndpoint } from "./web-services/delete-keychain-entry-endpoint";

import { KeyVaultSecret, SecretClient } from "@azure/keyvault-secrets";
import {
UsernamePasswordCredential,
Expand All @@ -42,9 +48,9 @@ export interface IAzureInMemoryCredentials {
}

export interface IPluginKeychainAzureKvOptions extends ICactusPluginOptions {
containerId(containerId: any);
stop();
destroy();
// containerId(containerId: any);
// stop();
// destroy();
logLevel?: LogLevelDesc;
keychainId: string;
instanceId: string;
Expand All @@ -56,9 +62,6 @@ export interface IPluginKeychainAzureKvOptions extends ICactusPluginOptions {

export class PluginKeychainAzureKv
implements ICactusPlugin, IPluginWebService, IPluginKeychain {
deleteKeychainId() {
throw new Error("Method not implemented.");
}
public static readonly CLASS_NAME = "PluginKeychainAzureKv";

readonly vaultUrl: string;
Expand Down Expand Up @@ -140,7 +143,21 @@ export class PluginKeychainAzureKv
if (Array.isArray(this.endpoints)) {
return this.endpoints;
}
const endpoints: IWebServiceEndpoint[] = [];
//const endpoints: IWebServiceEndpoint[] = [];
const endpoints: IWebServiceEndpoint[] = [
new SetKeychainEntryEndpoint({
connector: this,
logLevel: this.opts.logLevel,
}),
new GetKeychainEntryEndpoint({
connector: this,
logLevel: this.opts.logLevel,
}),
new DeleteKeychainEntryEndpoint({
connector: this,
logLevel: this.opts.logLevel,
}),
];

// TODO: Writing the getExpressRequestHandler() method for
// GetKeychainEntryEndpointV1 and SetKeychainEntryEndpointV1
Expand Down Expand Up @@ -200,6 +217,9 @@ export class PluginKeychainAzureKv
return this.azureKvClient;
}

deleteKeychainId() {
throw new Error("Method not implemented.");
}
async get<T>(key: string): Promise<T> {
const keyVaultSecret: KeyVaultSecret = await this.azureKvClient.getSecret(
key,
Expand Down
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,
});
}
}
}
Loading

0 comments on commit 951b9f1

Please sign in to comment.