Skip to content

Commit

Permalink
Add parameters for debug_address & log_level
Browse files Browse the repository at this point in the history
  • Loading branch information
kaklakariada committed Sep 5, 2023
1 parent f944033 commit df9f765
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 25 deletions.
53 changes: 37 additions & 16 deletions extension/src/addInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,37 @@ import { ADAPTER_SCRIPT_NAME, convertSchemaNameToInstanceId } from "./common";
import { ExtendedContext } from "./extension";
import { getAllParameterDefinitions } from "./parameterDefinitions";

interface VirtualSchemaConfig {
virtualSchemaName: string
baseSchemaName: string
excludedCapabilities: string | undefined
tableFilter: string | undefined
debugAddress: string | undefined
logLevel: string | undefined
}

export function addInstance(context: ExtendedContext, version: string, paramValues: ParameterValues): Instance {
if (context.version !== version) {
throw new NotFoundError(`Version '${version}' not supported, can only use '${context.version}'.`)
}
const config = buildVirtualSchemaConfig(paramValues)
const createVirtualSchemaStmt = createVirtualSchemaStatement(context.extensionSchemaName, config);
context.sqlClient.execute(createVirtualSchemaStmt);
const comment = `Created by extension manager for row-level-security-lua ${escapeSingleQuotes(config.virtualSchemaName)} (version ${context.version})`;
context.sqlClient.execute(`COMMENT ON SCHEMA "${config.virtualSchemaName}" IS '${comment}'`);
return { id: convertSchemaNameToInstanceId(config.virtualSchemaName), name: config.virtualSchemaName }
}

function buildVirtualSchemaConfig(paramValues: ParameterValues): VirtualSchemaConfig {
const allParams = getAllParameterDefinitions();
const virtualSchemaName = getParameterValue(paramValues, allParams.virtualSchemaName);
const schemaName = getParameterValue(paramValues, allParams.schemaName);
const excludedCapabilities = getOptionalParameterValue(paramValues, allParams.excludedCapabilities);
const tableFilter = getOptionalParameterValue(paramValues, allParams.tableFilter);

const createVirtualSchemaStmt = createVirtualSchemaStatement(virtualSchemaName, context.extensionSchemaName, schemaName, excludedCapabilities, tableFilter);
context.sqlClient.execute(createVirtualSchemaStmt);
const comment = `Created by extension manager for row-level-security-lua ${escapeSingleQuotes(virtualSchemaName)} (version ${context.version})`;
context.sqlClient.execute(`COMMENT ON SCHEMA "${virtualSchemaName}" IS '${comment}'`);
return { id: convertSchemaNameToInstanceId(virtualSchemaName), name: virtualSchemaName }
return {
virtualSchemaName: getParameterValue(paramValues, allParams.virtualSchemaName),
baseSchemaName: getParameterValue(paramValues, allParams.schemaName),
tableFilter: getOptionalParameterValue(paramValues, allParams.tableFilter),
excludedCapabilities: getOptionalParameterValue(paramValues, allParams.excludedCapabilities),
debugAddress: getOptionalParameterValue(paramValues, allParams.debugAddress),
logLevel: getOptionalParameterValue(paramValues, allParams.logLevel)
}
}

function getParameterValue(paramValues: ParameterValues, definition: Parameter): string {
Expand All @@ -38,13 +53,19 @@ function getOptionalParameterValue(paramValues: ParameterValues, definition: Par
return undefined
}

function createVirtualSchemaStatement(name: string, adapterSchema: string, baseSchemaName: string, excludedCapabilities: string | undefined, tableFilter: string | undefined): string {
let stmt = `CREATE VIRTUAL SCHEMA "${name}" USING "${adapterSchema}"."${ADAPTER_SCRIPT_NAME}" WITH SCHEMA_NAME = '${baseSchemaName}'`
if (excludedCapabilities) {
stmt += ` EXCLUDED_CAPABILITIES='${excludedCapabilities}'`
function createVirtualSchemaStatement(adapterSchema: string, config: VirtualSchemaConfig): string {
let stmt = `CREATE VIRTUAL SCHEMA "${config.virtualSchemaName}" USING "${adapterSchema}"."${ADAPTER_SCRIPT_NAME}" WITH SCHEMA_NAME = '${config.baseSchemaName}'`
if (config.excludedCapabilities) {
stmt += ` EXCLUDED_CAPABILITIES='${config.excludedCapabilities}'`
}
if (config.excludedCapabilities) {
stmt += ` TABLE_FILTER='${config.tableFilter}'`
}
if (config.debugAddress) {
stmt += ` DEBUG_ADDRESS='${config.debugAddress}'`
}
if (excludedCapabilities) {
stmt += ` TABLE_FILTER='${tableFilter}'`
if (config.logLevel) {
stmt += ` LOG_LEVEL='${config.logLevel}'`
}
return stmt;
}
Expand Down
32 changes: 23 additions & 9 deletions extension/src/parameterDefinitions.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
import { Parameter } from "@exasol/extension-manager-interface";
import { Parameter, SelectOption } from "@exasol/extension-manager-interface";

export type ScopedParameter = Parameter & { scope: "general" | "vs" }
export type ScopedParameters = { [key: string]: ScopedParameter }

const allParams: ScopedParameters = {
virtualSchemaName: { scope: "general", id: "virtualSchemaName", name: "Name of the new virtual schema", type: "string", required: true },
const REMOTELOG_LUA_LOG_LEVELS: string[] = ["NONE", "FATAL", "ERROR", "WARN", "INFO", "CONFIG", "DEBUG", "TRACE"];
const LOG_LEVEL_OPTIONS: SelectOption[] = REMOTELOG_LUA_LOG_LEVELS.map(level => { return { id: level, name: level } })

interface AllParameters {
virtualSchemaName: Parameter
schemaName: Parameter
excludedCapabilities: Parameter
tableFilter: Parameter
debugAddress: Parameter
logLevel: Parameter
}

const allParams: AllParameters = {
virtualSchemaName: { id: "virtualSchemaName", name: "Name of the new virtual schema", type: "string", required: true },

// Virtual Schema parameters
schemaName: { scope: "vs", id: "SCHEMA_NAME", name: "Name of the schema for which to apply row-level security", type: "string", required: true, multiline: false },
excludedCapabilities: { scope: "vs", id: "EXCLUDED_CAPABILITIES", name: "Comma-separated list of capabilities that should not be pushed-down, e.g. 'SELECTLIST_PROJECTION, ORDER_BY_COLUMN'", type: "string", required: false, multiline: false },
tableFilter: { scope: "vs", id: "TABLE_FILTER", name: "Comma-separated list of tables that should be added to the schema, e.g. 'ORDERS, ORDER_ITEMS, PRODUCTS'. If this is empty, all tables are added.", type: "string", required: false, multiline: false },
schemaName: { id: "SCHEMA_NAME", name: "Name of the schema for which to apply row-level security", type: "string", required: true, multiline: false },
excludedCapabilities: { id: "EXCLUDED_CAPABILITIES", name: "Comma-separated list of capabilities that should not be pushed-down, e.g. 'SELECTLIST_PROJECTION, ORDER_BY_COLUMN'", type: "string", required: false, multiline: false },
tableFilter: { id: "TABLE_FILTER", name: "Comma-separated list of tables that should be added to the schema, e.g. 'ORDERS, ORDER_ITEMS, PRODUCTS'. If this is empty, all tables are added.", type: "string", required: false, multiline: false },
debugAddress: { id: "DEBUG_ADDRESS", name: "Network address and port to which to send debug output, e.g. '192.168.179.38:3000'", type: "string", required: false, multiline: false },
logLevel: { id: "LOG_LEVEL", name: "Log level for debug output", type: "select", required: false, options: LOG_LEVEL_OPTIONS },
};

export function getAllParameterDefinitions(): ScopedParameters {
export function getAllParameterDefinitions(): AllParameters {
return allParams;
}

Expand All @@ -22,5 +34,7 @@ export function createInstanceParameters(): Parameter[] {
allParams.schemaName,
allParams.excludedCapabilities,
allParams.tableFilter,
allParams.debugAddress,
allParams.logLevel,
];
}
4 changes: 4 additions & 0 deletions src/test/java/com/exasol/rls/extension/ExtensionIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ void createInstance() throws SQLException {
.insert(2, "b").insert(3, "c");
setup.client().createInstance(
List.of(param("virtualSchemaName", virtualSchemaName), param("SCHEMA_NAME", schema.getName())));
extracted(virtualSchemaName, table);
}

private void extracted(final String virtualSchemaName, final Table table) throws SQLException {
assertResult("select * from " + virtualSchemaName + "." + table.getName() + " order by id",
table("INTEGER", "VARCHAR").row(1, "a").row(2, "b").row(3, "c").matches());
}
Expand Down

0 comments on commit df9f765

Please sign in to comment.