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

Secure Share v1 #1438

Merged
merged 9 commits into from
Oct 16, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ node_modules
*.tgz
*.gz
.parcel-cache
packages/pangea-node-sdk/download/
13 changes: 13 additions & 0 deletions dev/run_all_examples.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
#!/usr/bin/env bash

set -e
skip_items=("/node_modules/", "cli.mjs")

# Root directory
root_directory=$(pwd)

# Find all *.mjs files and run them with node
find . -type f -name '*.mjs' | while read -r file; do
# Exclusions.
skip=false
for item in "${skip_items[@]}"; do
if [[ $file == *"$item"* ]]; then
skip=true
break
fi
done
if [ "$skip" = true ]; then
continue
fi

echo -e "\n\n--------------------------------------------------------------\nRunning: $file"
node "$file"
echo -e "\nFinish: $file\n--------------------------------------------------------------\n"
Expand Down
2 changes: 2 additions & 0 deletions examples/.examples-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ examples-tests:
- "intel"
- "redact"
- "vault"
- "share"
image: node:${NODE_VERSION}
before_script:
- export PANGEA_AUDIT_CONFIG_ID="${PANGEA_AUDIT_CONFIG_ID_1_LVE_AWS}"
Expand All @@ -35,6 +36,7 @@ examples-tests:
- export PANGEA_URL_INTEL_TOKEN="${PANGEA_INTEGRATION_TOKEN_LVE_AWS}"
- export PANGEA_USER_INTEL_TOKEN="${PANGEA_INTEGRATION_TOKEN_LVE_AWS}"
- export PANGEA_VAULT_TOKEN="${PANGEA_INTEGRATION_TOKEN_LVE_AWS}"
- export PANGEA_SHARE_TOKEN="${PANGEA_INTEGRATION_TOKEN_LVE_AWS}"
- pushd packages/pangea-node-sdk
- tar -xf package.tgz --strip-components 1 -C .
- popd
Expand Down
2 changes: 1 addition & 1 deletion examples/file_scan/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Pangea",
"license": "ISC",
"license": "MIT",
"dependencies": {
"pangea-node-sdk": "3.11.0"
}
Expand Down
2 changes: 1 addition & 1 deletion examples/intel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Pangea",
"license": "ISC",
"license": "MIT",
"dependencies": {
"pangea-node-sdk": "3.11.0"
}
Expand Down
2 changes: 1 addition & 1 deletion examples/redact/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Pangea",
"license": "ISC",
"license": "MIT",
"dependencies": {
"pangea-node-sdk": "3.11.0"
}
Expand Down
12 changes: 12 additions & 0 deletions examples/share/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Pangea Secure Share examples

## Setup

Set up environment variables ([Instructions](https://pangea.cloud/docs/getting-started/integrate/#set-environment-variables)) `PANGEA_SHARE_TOKEN` and `PANGEA_DOMAIN` with your project token configured on Pangea User Console (token should have access to Store service [Instructions](https://pangea.cloud/docs/getting-started/configure-services/#configure-a-pangea-service)) and with your Pangea domain.

## Run example

```
yarn install
node folder_create_and_delete.mjs
```
115 changes: 115 additions & 0 deletions examples/share/cli.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#!/usr/bin/env node

// @ts-check

import fs from "node:fs/promises";
import path from "node:path";
import process from "node:process";

import { defineCommand, runMain } from "citty";
import { PangeaConfig, Share, ShareService } from "pangea-node-sdk";

const main = defineCommand({
meta: {
name: "share",
version: "0.0.0",
description:
"An example command line utility that creates an email+code, SMS+code, " +
"or password-secured download/upload/editor share-link for a given " +
"file or for each file in a given directory.",
},
args: {
input: {
type: "string",
required: true,
alias: "i",
description: "Local path to upload.",
},
dest: {
type: "string",
default: "/",
description: "Destination path in Share.",
},
email: {
type: "string",
description: "Email address to protect the share link with.",
},
phone: {
type: "string",
description: "Phone number to protect the share link with.",
},
password: {
type: "string",
description: "Password to protect the share link with.",
},
link_type: {
type: "string",
default: Share.LinkType.DOWNLOAD,
description: "Type of link.",
},
},
async run({ args }) {
if (!args.email && !args.phone && !args.password) {
throw new Error(
"At least one of --email, --phone, or --password must be provided."
);
}

/** @type {Set<Share.Authenticator>} */
const authenticators = new Set();
if (args.password) {
authenticators.add({
auth_type: Share.AuthenticatorType.PASSWORD,
auth_context: args.password,
});
}
if (args.email) {
authenticators.add({
auth_type: Share.AuthenticatorType.EMAIL_OTP,
auth_context: args.email,
});
}
if (args.phone) {
authenticators.add({
auth_type: Share.AuthenticatorType.SMS_OTP,
auth_context: args.phone,
});
}

const share = new ShareService(
// @ts-expect-error
process.env.PANGEA_SHARE_TOKEN,
new PangeaConfig({ domain: process.env.PANGEA_DOMAIN })
);

// Upload files.
const files = (await fs.lstat(args.input)).isDirectory()
? (await fs.readdir(args.input)).map((x) => path.resolve(args.input, x))
: [args.input];
const objectIds = new Set();
for (const file of files) {
const uploadResponse = await share.put(
{
path: `${args.dest}/${path.basename(file)}`,
},
{ file, name: "file" }
);
objectIds.add(uploadResponse.result.object.id);
}

// Create share link.
const linkResponse = await share.shareLinkCreate({
links: [
{
targets: Array.from(objectIds),
// @ts-expect-error
link_type: args.link_type,
authenticators: Array.from(authenticators),
},
],
});
console.log(linkResponse.result.share_link_objects[0].link);
},
});

runMain(main);
31 changes: 31 additions & 0 deletions examples/share/folder_create_and_delete.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* eslint-disable no-console */

import { PangeaConfig, ShareService } from "pangea-node-sdk";

// Load Pangea token and domain from environment variables
const token = process.env.PANGEA_SHARE_TOKEN;
const config = new PangeaConfig({ domain: process.env.PANGEA_DOMAIN });

// Create Share client
const client = new ShareService(token, config);

// Create unique folder path
const time = Math.round(Date.now() / 1000);
const folderPath = "/sdk_examples/node/delete/" + time;

(async () => {
try {
console.log("Creating folder...");
const respCreate = await client.folderCreate({ folder: folderPath });

const id = respCreate.result.object.id;
console.log(`Folder create success. Folder ID: ${id}`);

console.log("Deleting folder...");
const respDelete = await client.delete({ id });

console.log(`Deleted ${respDelete.result.count} item(s)`);
} catch (e) {
console.log(e);
}
})();
Loading
Loading