Skip to content

Commit

Permalink
oh
Browse files Browse the repository at this point in the history
  • Loading branch information
dmihalcik-virtru committed Sep 2, 2022
1 parent 675edc4 commit 076fa9d
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 122 deletions.
4 changes: 2 additions & 2 deletions cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

158 changes: 71 additions & 87 deletions cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ import yargs from 'yargs';
import { readFile, stat, writeFile } from 'fs/promises';
import { hideBin } from 'yargs/helpers';
import {
FileClient,
NanoTDFClient,
NanoTDFDatasetClient,
AuthProviders,
version,
//@ts-ignore
} from '@opentdf/client/nano-node-esm';
//@ts-ignore
import { FileClient } from '@opentdf/client/tdf3';
} from '@opentdf/client';
import { CLIError, Level, log } from './logger.js';
import { type AuthProvider } from '@opentdf/client/dist/types/src/auth/auth.js';

type AuthToProcess = {
auth?: string;
Expand All @@ -21,7 +18,6 @@ type AuthToProcess = {
};

const containerTypes = ['tdf3', 'nano', 'dataset'] as const;
type ContainerType = typeof containerTypes[number];

async function processAuth({ auth, clientId, clientSecret, oidcEndpoint }: AuthToProcess) {
log('DEBUG', 'Processing auth params');
Expand All @@ -47,18 +43,16 @@ async function processAuth({ auth, clientId, clientSecret, oidcEndpoint }: AuthT
});
}

async function processClient(auth: AuthProvider, kasEndpoint: string, type: ContainerType) {
switch (type) {
case 'nano':
log('DEBUG', `Nano Client`);
return new NanoTDFClient(auth, kasEndpoint);
case 'dataset':
log('DEBUG', `Dataset Client`);
return new NanoTDFDatasetClient(auth, kasEndpoint);
case 'tdf3':
log('DEBUG', `TDF3 Client`);
return new FileClient({ authProvider: auth, kasEndpoint });
type AnyClient = FileClient | NanoTDFClient | NanoTDFDatasetClient;

function addParams(client: AnyClient, argv: Partial<mainArgs>) {
if (argv.attributes?.length) {
client.dataAttributes = argv.attributes.split(',');
}
if (argv['users-with-access']?.length) {
client.dissems = argv['users-with-access'].split(',');
}
log('SILLY', `Built encrypt params dissems: ${client.dissems}, attrs: ${client.dataAttributes}`);
}

async function processDataIn(file: string) {
Expand Down Expand Up @@ -200,40 +194,38 @@ export const handleArgs = (args: string[]) => {
});
},
async (argv) => {
try {
log('DEBUG', 'Running decrypt command');
const authProvider = await processAuth(argv);
log('DEBUG', `Initialized auth provider ${JSON.stringify(authProvider)}`);
const client = await processClient(
authProvider,
argv.kasEndpoint,
argv.containerType as ContainerType
);
log('DEBUG', `Initialized client ${JSON.stringify(client)}`);
log('DEBUG', 'Running decrypt command');
const authProvider = await processAuth(argv);
log('DEBUG', `Initialized auth provider ${JSON.stringify(authProvider)}`);

const kasEndpoint = argv.kasEndpoint;
if (argv.containerType === 'tdf3') {
log('DEBUG', `TDF3 Client`);
const client = new FileClient({ authProvider, kasEndpoint });
log('SILLY', `Initialized client ${JSON.stringify(client)}`);
log('DEBUG', `About to decrypt [${argv.file}]`);
if ('tdf3' === argv.containerType) {
const ct = await client.decrypt(argv.file);
if (argv.output) {
await ct.toFile(argv.output);
} else {
console.log(await ct.toString());
}
const ct = await client.decrypt(argv.file as string);
if (argv.output) {
await ct.toFile(argv.output);
} else {
const buffer = await processDataIn(argv.file as string);
console.log(await ct.toString());
}
} else {
const client =
argv.containerType === 'nano'
? new NanoTDFClient(authProvider, kasEndpoint)
: new NanoTDFDatasetClient(authProvider, kasEndpoint);
const buffer = await processDataIn(argv.file as string);

log('DEBUG', 'Decrypt data.');
const plaintext = await client.decrypt(buffer);
log('DEBUG', 'Decrypt data.');
const plaintext = await client.decrypt(buffer);

log('DEBUG', 'Handle output.');
if (argv.output) {
await writeFile(argv.output, Buffer.from(plaintext));
} else {
console.log(Buffer.from(plaintext).toString('utf8'));
}
log('DEBUG', 'Handle output.');
if (argv.output) {
await writeFile(argv.output, Buffer.from(plaintext));
} else {
console.log(Buffer.from(plaintext).toString('utf8'));
}
} catch (e) {
log(e);
}
}
)
Expand All @@ -248,50 +240,42 @@ export const handleArgs = (args: string[]) => {
});
},
async (argv) => {
try {
log('DEBUG', 'Running encrypt command');
const authProvider = await processAuth(argv);
log('DEBUG', `Initialized auth provider ${JSON.stringify(authProvider)}`);
const client = await processClient(
authProvider,
argv.kasEndpoint,
argv.containerType as ContainerType
);
log('DEBUG', 'Running encrypt command');
const authProvider = await processAuth(argv);
log('DEBUG', `Initialized auth provider ${JSON.stringify(authProvider)}`);
const kasEndpoint = argv.kasEndpoint;

if ('tdf3' === argv.containerType) {
log('DEBUG', `TDF3 Client`);
const client = new FileClient({ authProvider, kasEndpoint });
log('SILLY', `Initialized client ${JSON.stringify(client)}`);

if (argv.attributes?.length) {
client.dataAttributes = argv.attributes.split(',');
}
if (argv['users-with-access']?.length) {
client.dissems = argv['users-with-access'].split(',');
}
log(
'SILLY',
`Built encrypt params dissems: ${client.dissems}, attrs: ${client.dataAttributes}`
);
log('DEBUG', 'Encrypting data');

if ('tdf3' === argv.containerType) {
const ct = await client.encrypt(argv.file);
if (argv.output) {
await ct.toFile(argv.output);
} else {
console.log(await ct.toString());
addParams(client, argv);
const ct = await client.encrypt(argv.file as string);
if (argv.output) {
if (ct.toFile) {
await ct.toFile(argv.output as string);
}
} else {
const buffer = await processDataIn(argv.file as string);
const cyphertext = await client.encrypt(buffer);
console.log(await ct.toString());
}
} else {
const client =
argv.containerType === 'nano'
? new NanoTDFClient(authProvider, kasEndpoint)
: new NanoTDFDatasetClient(authProvider, kasEndpoint);
log('SILLY', `Initialized client ${JSON.stringify(client)}`);

log('DEBUG', `Handle cyphertext output ${JSON.stringify(cyphertext)}`);
if (argv.output) {
await writeFile(argv.output, Buffer.from(cyphertext));
} else {
console.log(Buffer.from(cyphertext).toString('base64'));
}
addParams(client, argv);

const buffer = await processDataIn(argv.file as string);
const cyphertext = await client.encrypt(buffer);

log('DEBUG', `Handle cyphertext output ${JSON.stringify(cyphertext)}`);
if (argv.output) {
await writeFile(argv.output, Buffer.from(cyphertext));
} else {
console.log(Buffer.from(cyphertext).toString('base64'));
}
} catch (e) {
log(e);
}
}
)
Expand Down Expand Up @@ -319,13 +303,13 @@ export const handleArgs = (args: string[]) => {
);
};

export type mainArgs = ReturnType<typeof handleArgs>;
export type mainArgs = Awaited<ReturnType<typeof handleArgs>>;
export const main = async (argsPromise: mainArgs) => {
await argsPromise;
argsPromise;
};

const a = handleArgs(hideBin(process.argv));
main(a)
handleArgs(hideBin(process.argv))
.then(main)
.then(() => {
// Nothing;
})
Expand Down
26 changes: 23 additions & 3 deletions cli/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
{
"extends": "../lib/tsconfig-esm.json",
"compilerOptions": {
"lib": ["es2020", "dom"],
"alwaysStrict": true,
"baseUrl": "./",
"composite": true,
"declaration": true,
"declarationDir": "./dist/types",
"declarationMap": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"inlineSourceMap": true,
"lib": ["es2020"],
"module": "esnext",
"moduleResolution": "node",
"noImplicitAny": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"outDir": "dist",
"skipLibCheck": true,
"strict": false,
"strictBindCallApply": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"strictPropertyInitialization": false,
"target": "es2020",
"moduleResolution": "node",
"typeRoots": ["./node_modules/@types"]
"typeRoots": ["./node_modules/@types"],
},
"include": ["src", "test", "types"],
"exclude": ["**/node_modules"]
Expand Down
40 changes: 21 additions & 19 deletions lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,31 @@
"author": "Virtru",
"browser": "./dist/client/client-web.js",
"main": "./dist/server/node.cjs.js",
"types": "./dist/types/src/index.d.ts",
"types": "./dist/types/tdf3/index.d.ts",
"exports": {
".": {
"types": "./dist/types/src/index.d.ts",
"import": "./dist/client/client-web.js",
"require": "./dist/server/node.cjs.js"
"node": {
"types": "./dist/types/tdf3/index.d.ts",
"import": "./dist/esm/tdf3/index.js",
"require": "./dist/server/tdf3.node.js"
},
"default": {
"types": "./dist/types/tdf3/index.d.ts",
"import": "./dist/esm/tdf3/index-web.js",
"require": "./dist/client/tdf3.web.js"
}
},
"./nano": {
"types": "./dist/types/src/index.d.ts",
"import": "./dist/esm/src/index.js",
"require": "./dist/server/node-nano.cjs.js"
},
"./nano-node-esm": {
"types": "./dist/types/src/index.d.ts",
"import": "./dist/esm/src/index.node.js"
},
"./tdf3": {
"types": "./dist/types/tdf3/index.d.ts",
"import": "./dist/esm/tdf3/index.js"
},
"./tdf3-web-esm": {
"import": "./dist/esm/tdf3/index-web.js",
"types": "./dist/types/tdf3/index.d.ts"
"node": {
"types": "./dist/types/src/index.d.ts",
"import": "./dist/esm/src/index.node.js",
"require": "./dist/server/nano.node.js"
},
"default": {
"types": "./dist/types/src/index.d.ts",
"import": "./dist/esm/src/index.js",
"require": "./dist/client/nano.web.js"
}
}
},
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export function isBrowser() {
return typeof window !== 'undefined'; // eslint-disable-line
}

export const isFirefox = (): boolean => typeof window.InstallTrigger !== 'undefined';
export const isFirefox = (): boolean => isBrowser() && 'InstallTrigger' in window;

export const rstrip = (str: string, suffix = ' '): string => {
while (str && suffix && str.endsWith(suffix)) {
Expand Down
10 changes: 6 additions & 4 deletions lib/tdf3/src/FileClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import { type InputSource } from '../../src/types';
import { type AuthProvider } from 'src/auth/auth';

interface FileClientConfig {
authProvider?: AuthProvider;
clientId: string;
oidcOrigin: string;
kasEndpoint: string;
clientId?: string;
oidcOrigin?: string;
clientSecret?: string;
oidcRefreshToken?: string;

authProvider?: AuthProvider;

kasEndpoint?: string;
}

function isNodeStream(source: InputSource): source is NodeJS.ReadableStream {
Expand Down
8 changes: 4 additions & 4 deletions lib/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module.exports = (env, argv) => {
output: {
publicPath: '',
library: '@opentdf/client',
filename: `client-nano.web.js`,
filename: `nano.web.js`,
libraryTarget: 'umd',
globalObject: 'this',
umdNamedDefine: true,
Expand Down Expand Up @@ -67,7 +67,7 @@ module.exports = (env, argv) => {
output: {
libraryExport: 'default',
libraryTarget: 'umd',
filename: `node-nano.cjs.js`,
filename: `nano.node.js`,
path: path.resolve(__dirname, 'dist/server'),
},
};
Expand All @@ -93,7 +93,7 @@ module.exports = (env, argv) => {
},
output: {
...clientConfig.output,
filename: `client-web.js`,
filename: `tdf3.web.js`,
},
};

Expand All @@ -114,7 +114,7 @@ module.exports = (env, argv) => {
},
output: {
...serverConfig.output,
filename: `node.cjs.js`,
filename: `tdf3.node.js`,
},
};

Expand Down
Loading

0 comments on commit 076fa9d

Please sign in to comment.