Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Terminus json model #2612

Merged
merged 8 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions components/terminus/Blockly/BlocklyComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ function BlocklyComponent(props) {

const uploadModel = async () => {
const domToPretty = modelToXML();
const cueModel = generateModel(primaryWorkspace.current, roles);
const model = generateModel(primaryWorkspace.current, roles);

const body = {
cue_schema: cueModel,
blockly_schema: domToPretty,
model: model,
blockly_model: domToPretty,
};

const requestOptions = {
Expand Down
109 changes: 40 additions & 69 deletions components/terminus/blocks/customblocks.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,46 @@
import Blockly from 'blockly/core';

// TODO wire with CUE configuration files
// TODO extract prefixes
function getEncryption() {
return [
['AES_256', 'crypto.#EnAES_256'],
['FPE_FF1', 'crypto.#EnFPE_FF1'],
['FPE_FF3_1', 'crypto.#EnFPE_FF3_1'],
['NoEncryption', 'crypto.#EnNoEncryption'],
// ['RSA_2048', 'crypto.#EnRSA_2048'],
// ['Blowfish_448', 'crypto.#EnBlowfish_448'],
// ['FPE', 'crypto.#EnFPE'],
// ['B64', 'crypto.#EnB64'],
['AES_256', 'AES_256'],
['FPE_FF1', 'FPE_FF1'],
['FPE_FF3_1', 'FPE_FF3_1'],
['NoEncryption', 'NoEncryption'],
// ['RSA_2048', 'RSA_2048'],
// ['Blowfish_448', 'Blowfish_448'],
// ['FPE', 'FPE'],
// ['B64', 'B64'],
];
}

function getMasks() {
return [
['Clear', 'masking.#MClear'],
['Generic', 'masking.#MGeneric'],
['Redact', 'masking.#MRedact'],
['Password', 'masking.#MPassword'],
['Name', 'masking.#MName'],
['Address', 'masking.#MAddress'],
['Email', 'masking.#MEmail'],
['Mobile', 'masking.#MMobile'],
['Telephone', 'masking.#MTelephone'],
['ID', 'masking.#MID'],
['CreditCard', 'masking.#MCreditCard'],
['Struct', 'masking.#MStruct'],
['URL', 'masking.#MURL'],
['Clear', 'Clear'],
['Redact', 'Redact'],
['Generic', 'Generic'],
['Password', 'Password'],
['Name', 'Name'],
['Address', 'Address'],
['Email', 'Email'],
['Mobile', 'Mobile'],
['Telephone', 'Telephone'],
['ID', 'ID'],
['CreditCard', 'CreditCard'],
['Struct', 'Struct'],
['URL', 'URL'],
];
}

function getPredefinedDataTypes() {
return [
['Letters', 'defs.#Letters'],
['LettersWithSpaces', 'defs.#LettersWithSpaces'],
['Alphanumerical', 'defs.#Alphanumerical'],
['AlphanumericalWithSpaces', 'defs.#AlphanumericalWithSpaces'],
['AlphanumericalNotAccented', 'defs.#AlphanumericalNotAccented'],
['AlphanumericalNotAccentedWithSpaces', 'defs.#AlphanumericalNotAccentedWithSpaces'],
['SimpleDate (2006-11-24)', 'defs.#SimpleDateFormat'],
['String', 'String'],
['Letters', 'Letters'],
['LettersWithSpaces', 'LettersWithSpaces'],
['Alphanumerical', 'Alphanumerical'],
['AlphanumericalWithSpaces', 'AlphanumericalWithSpaces'],
['AlphanumericalNotAccented', 'AlphanumericalNotAccented'],
['AlphanumericalNotAccentedWithSpaces', 'AlphanumericalNotAccentedWithSpaces'],
['SimpleDate (2006-11-24)', 'Date'],
];
}

Expand Down Expand Up @@ -132,32 +133,6 @@ Blockly.Blocks['data_object_field_type'] = {
this.jsonInit({
type: 'data_object_field_type',
message0: 'type %1 %2',
args0: [
{
type: 'field_input',
name: 'object_type',
text: 'string',
},
{
type: 'input_value',
name: 'input',
check: ['Boolean', 'String'],
},
],
output: null,
colour: 230,
tooltip: '',
helpUrl: '',
});
// this.setStyle('loop_blocks');
},
};

Blockly.Blocks['data_object_field_default_types'] = {
init: function () {
this.jsonInit({
type: 'data_object_field_default_types',
message0: 'type %1 %2',
args0: [
{
type: 'field_dropdown',
Expand All @@ -167,7 +142,7 @@ Blockly.Blocks['data_object_field_default_types'] = {
{
type: 'input_value',
name: 'input',
check: ['Boolean', 'String'],
check: 'String',
},
],
output: null,
Expand All @@ -193,7 +168,7 @@ Blockly.Blocks['data_object_field_encryption'] = {
{
type: 'input_value',
name: 'input',
check: ['Boolean', 'String'],
check: 'String',
},
],
output: null,
Expand All @@ -204,26 +179,27 @@ Blockly.Blocks['data_object_field_encryption'] = {
// this.setStyle('loop_blocks');
},
};

Blockly.Blocks['data_object_field_mask'] = {
init: function () {
this.jsonInit({
type: 'data_object_field_mask',
message0: 'mask (Admin:%1) (Member:%2) %3',
message0: 'mask (admin:%1) (member:%2) %3',
args0: [
{
type: 'field_dropdown',
name: 'object_type_ADMIN',
name: 'object_type_admin',
options: getMasks(),
},
{
type: 'field_dropdown',
name: 'object_type_MEMBER',
name: 'object_type_member',
options: getMasks(),
},
{
type: 'input_value',
name: 'object_type',
check: ['Boolean', 'String'],
check: 'String',
},
],
output: null,
Expand All @@ -234,16 +210,11 @@ Blockly.Blocks['data_object_field_mask'] = {
},
};

const capitalize = (s: string) => {
if (typeof s !== 'string') return '';
return s.charAt(0).toUpperCase() + s.slice(1).toLowerCase();
};

export const maskSetup = (roles: string[]) => {
let maskMessage = 'mask (';
const args: any[] = [];
for (let i = 0; i < roles.length; i++) {
maskMessage += `${capitalize(roles[i])}:%${i + 1}`;
maskMessage += `${roles[i]}:%${i + 1}`;
if (i < roles.length - 1) {
maskMessage += ') (';
}
Expand All @@ -263,7 +234,7 @@ export const maskSetup = (roles: string[]) => {
{
type: 'input_value',
name: 'object_type',
check: ['Boolean', 'String'],
check: 'String',
},
],
output: null,
Expand Down
86 changes: 19 additions & 67 deletions components/terminus/blocks/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,6 @@ let currentObject, currentField;

const IGNORE_FIELDS = [CONST_OBJ_GLB_ENCR];

// TODO this must come from CUE and the regexp are hacked here for generation
const simpleRegex = '^[a-zA-Z]+$';
const regexMap = {
'defs.#Letters': simpleRegex,
'defs.#LettersWithSpaces': simpleRegex,
'defs.#Alphanumerical': simpleRegex,
'defs.#AlphanumericalWithSpaces': simpleRegex,
'defs.#AlphanumericalNotAccented': simpleRegex,
'defs.#AlphanumericalNotAccentedWithSpaces': simpleRegex,
'defs.#SimpleDateFormat': '^20[0-9]{2}-[0-1][1-2]-[0-2][1-8]$', // regex restricted.
};

export const generateModel = (workspace, roles: string[]) => {
ObjectMap.clear();

Expand All @@ -35,72 +23,38 @@ export const generateModel = (workspace, roles: string[]) => {

// trigger the BLOCKLY processing which will run our custom code generation
javascriptGenerator.workspaceToCode(workspace);
const ret = generateCUEStructure(roles);
const ret = generateStructure(roles);

// add specific BoxyHQ imports
return `
EncryptedDefinitions: ${JSON.stringify(ret[1])}
${ret[0]}
`;
return JSON.stringify(ret);
};

// Rudimentary way of generating a CUE file
const generateCUEStructure = (roles: string[]) => {
let defs = ``;
const encrObjects = [];
const generateStructure = (roles: string[]) => {
const model: any = {};
for (const [key, value] of Object.entries(Object.fromEntries(ObjectMap))) {
encrObjects.push(key as never);
model.name = key;
model.attributes = {};
const valuesMap = Object.fromEntries(value as any);

// DEFINITIONS
let definitions = ``;
for (const [field, values] of Object.entries(valuesMap)) {
if (IGNORE_FIELDS.includes(field)) {
continue;
const rolesMap = {};
for (let i = 0; i < roles.length; i++) {
rolesMap[roles[i]] = values[i + 2];
}
definitions += `\n\t\t\t${field}: ${values[0]}`;

let pattern = regexMap[values[0]];
if (pattern == null) {
pattern = '.*';
}
}

// ENCRYPTION
let encryption = ``;
if (valuesMap[CONST_OBJ_GLB_ENCR] != null) {
encryption += `${valuesMap[CONST_OBJ_GLB_ENCR]}`;
} else {
for (const [field, values] of Object.entries(valuesMap)) {
encryption += `\n\t\t\t${field}: ${values[1]}`;
}
encryption = `{ ${encryption}
}`;
}

// MASKS
let maskString = '';
for (const [field, values] of Object.entries(valuesMap)) {
model.attributes[field] = {
type: values[0],
encryption: values[1],
masking: {
roles: rolesMap,
},
};
if (IGNORE_FIELDS.includes(field)) {
continue;
}
let index = 2;
for (const role of roles) {
const maskKey = `#Mask_${role.toLowerCase()}`;
const maskVal = `\n\t\t\t${field}: ${values.length > index ? values[index++] : 'masking.#MRedact'}`;
maskString += `\n${maskKey}: { ${maskVal}
}`;
}
}
const objectOutput = `\n#${key}: {
#Definition: { ${definitions}
}
#Encryption: ${encryption}${maskString}
}`;
defs += objectOutput;
}

return [defs, encrObjects];
return model;
};

javascriptGenerator['data_object_wrapper'] = function (block) {
Expand Down Expand Up @@ -138,16 +92,14 @@ javascriptGenerator['data_object_field_wrapper'] = function (block) {
javascriptGenerator['data_object_field_type'] = function (block) {
const objectName = block.getFieldValue('object_type');
currentField[0] = objectName; // type
currentField[2] = 'masking.#MRedact'; // mask
currentField[3] = 'masking.#MRedact'; // mask
currentField[2] = 'Redact'; // mask
currentField[3] = 'Redact'; // mask

javascriptGenerator.statementToCode(block, 'input', javascriptGenerator.ORDER_NONE);

return '';
};

javascriptGenerator['data_object_field_default_types'] = javascriptGenerator['data_object_field_type'];

javascriptGenerator['data_object_field_encryption'] = function (block) {
const objectName = block.getFieldValue('object_type');
currentField[1] = objectName; // encryption
Expand Down
1 change: 0 additions & 1 deletion pages/admin/terminus/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ const TerminusIndexPage: NextPage<Props> = ({ host }: Props) => {
<Block type='data_object_wrapper_with_encryption' />
<Block type='data_object_field_wrapper' />
<Block type='data_object_field_type' />
<Block type='data_object_field_default_types' />
<Block type='data_object_field_encryption' />
<Block type='data_object_field_mask' />
</BlocklyComponent>
Expand Down