Code Monkey home page Code Monkey logo

wtz_file_crypto_suiteapp's Introduction

WTZ_File_Crypto_SuiteApp

A proof of concept for Encrypting and Decrypting files using OpenPGP in Netsuite with the OpenPGP.js library. It works ok for files around 1mb. One 2mb pdf that I tried failed. There can absolutely be issues with encoding. I have tested it to work with UTF-8 encoded text-files.

Beware, there are many flaws in the design.

  • No restart of processing M/R script if more queue records are created while it is still processing the first batch
  • Keys are stored in plain-text (maybe they could be encrypted using NS-encryption functions and a key stored in the API Secrets that is bundled.
  • ...more

There are three Custom record types:

WTZ File Crypto Types

A list of different types of encryption, the idea was to be able to expand with other forms of encryptions.

WTZ File Crypto Job Setup

In these records you setup rules for handling files in the file cabinet. What type of encryption and keys for it

WTZ File Crypto Queue

This record will keep track of the en-/decryption tasks that are pending

There are three main scripts

WTZ File Crypto Folder Monitor MR

Monitors the input folders according to the "WTZ File Crypto Job Setup"-records and creates queue-records for files in the folders.

WTZ File Crypto Queue UE

On afterSubmit it will try to submit a Map/reduce task if the record has the status "Pending"

WTZ File Crypto Job MR

This will process all queue-records that has the status "Pending"

Suggested file structure:

folderstruct

wtz_file_crypto_suiteapp's People

Contributors

tomaswatz avatar watzen avatar

Stargazers

 avatar  avatar

Watchers

 avatar

wtz_file_crypto_suiteapp's Issues

TypeError: global.Set is not a constructor

While trying to implement a simple PGP encryption solution using your PGP library in lib folder , i run into this error :

TypeError: global.Set is not a constructor [at Object.<anonymous> (/SuiteScripts/Third Party PGP Solution/lib/openpgp.js:31665:39)]

I had been trying to use OpenPGP.js for a long time when I stumbled upon your solution. In my expedition, I always ran into the issue of not converting OpenPGP to AMD fully because of so many dependencies on Node or Browser. Your solution seems to be working because you also included the TextEncoder that I was not able to implement. But now I have ran into this issue.

Would you please help me out?

P.S. this is my Map/Reduce file :

/**
 * @NApiVersion 2.1
 * @NScriptType MapReduceScript
 */
define(["N/file", "N/log", "./lib/openpgp.js"], function (file, log, openpgp) {
  /* ================== ENCRYPT FILE ======================== */

  async function encryptPGP(params) {
    const publicKeyArmored = params.publicKeyArmored;
    const inputFileContents = params.inputFileContents;
    const isText = params.isText;

    const encryptMessage = isText
      ? openpgp.message.fromText(inputFileContents)
      : openpgp.message.fromBinary(inputFileContents);

    const publicKeys = (await openpgp.key.readArmored(publicKeyArmored)).keys;

    const encryptionResult = await openpgp.encrypt({
      message: encryptMessage,
      publicKeys: publicKeys,
    });

    const encrypted = encryptionResult.data;

    return encrypted;
  }

  /* ======================== MAIN FUNCTION ======================= */
  async function mainAsyncExecution(processingObj) {
    log.debug("mainAsyncExecution - start -", processingObj);

    const inputFile = file.load({
      id: processingObj.inputFileId,
    }); //10 units

    const publicKeyFile = file.load({
      id: processingObj.publicKeyFileId,
    }); //10 units

    await Promise.all([inputFile, publicKeyFile]); //wait for the two inputFileContents and cryptoType loads to complete

    const inputFileContents = inputFile.isText
      ? inputFile.getContents()
      : Base64Binary.decode(inputFile.getContents());

    const publicKeyFileContents = publicKeyFile.isText
      ? publicKeyFile.getContents()
      : Base64Binary.decode(publicKeyFile.getContents());

    log.debug("inputFileContents", inputFileContents);
    log.debug("publicKeyFileContents", publicKeyFileContents);

    const outputFolderId = processingObj.outputFolderId;

    let newFileId;

    let encryptedContents = await encryptPGP({
      publicKeyArmored: publicKeyFileContents,
      inputFileContents: inputFileContents,
      isText: inputFile.isText,
    });

    const newFile = file.create({
      name: inputFile.name + ".pgp",
      fileType: file.Type.PLAINTEXT,
      contents: encryptedContents,
      folder: outputFolderId,
    });

    newFileId = newFile.save();

    log.debug("mainAsyncExecution", "new encrypted fileId:" + newFileId);

    return newFileId;
  }

  function getInputData() {
    return { myKey: "1" };
  }

  function map(mapContext) {
    const key = JSON.parse(mapContext.key);
    const value = JSON.parse(mapContext.value);
    mapContext.write({ key: key, value: value });
  }

  function reduce(reduceContext) {
    const executionOptions = {
      inputFileId: "856",
      outputFolderId: "44",
      publicKeyFileId: "808",
    };

    mainAsyncExecution(executionOptions);
  }

  return { getInputData: getInputData, map: map, reduce: reduce };
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.