envis-node 0.0.2 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +56 -0
- package/package.json +5 -1
- package/src/index.js +3 -1
- package/src/main.js +106 -35
- package/src/session.js +2 -78
package/README.md
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# envis-node
|
|
2
|
+
|
|
3
|
+
-----
|
|
4
|
+
|
|
5
|
+
## Table of Contents
|
|
6
|
+
|
|
7
|
+
- [Overview](#overview)
|
|
8
|
+
- [Installation](#installation)
|
|
9
|
+
- [Usage](#usage)
|
|
10
|
+
- [License](#license)
|
|
11
|
+
- [Contact](#contact)
|
|
12
|
+
|
|
13
|
+
## Overview
|
|
14
|
+
Envisible is a simple, secure secret management platform. The SDK allows users to integrate their secrets (configured [on our web dashboard](https://envisible.netlify.app)) directly into their Node.js code.
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```console
|
|
19
|
+
cd node-sdk
|
|
20
|
+
npm install
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
1. Sign up for an Envisible account on the dashboard.
|
|
26
|
+
2. Install the SDK and authenticate when prompted (the SDK will open a secure browser window).
|
|
27
|
+
3. Read and manage secrets from any Node.js runtime:
|
|
28
|
+
|
|
29
|
+
### Fetching a secret
|
|
30
|
+
|
|
31
|
+
```javascript
|
|
32
|
+
const envis = require("envis-node");
|
|
33
|
+
|
|
34
|
+
async function main() {
|
|
35
|
+
const secret = await envis.get("project_id", "secret_name");
|
|
36
|
+
console.log(secret);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
main();
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Logging out
|
|
43
|
+
|
|
44
|
+
```javascript
|
|
45
|
+
const envis = require("envis-node");
|
|
46
|
+
|
|
47
|
+
envis.logout();
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## License
|
|
51
|
+
|
|
52
|
+
`envis-node` is released under a private source-available license: you can use the SDK to integrate with [our web dashboard](https://envisible.netlify.app), but the backend platform remains proprietary.
|
|
53
|
+
|
|
54
|
+
## Contact
|
|
55
|
+
- [Website](https://envisible.netlify.app)
|
|
56
|
+
- [Email](mailto:contact@uarham.me)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "envis-node",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "Secure, simple secret management",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"secret",
|
|
@@ -10,6 +10,10 @@
|
|
|
10
10
|
],
|
|
11
11
|
"license": "ISC",
|
|
12
12
|
"author": "Umair Arham",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "https://github.com/umairx25/Envisible-Clients"
|
|
16
|
+
},
|
|
13
17
|
"type": "commonjs",
|
|
14
18
|
"main": "src/index.js",
|
|
15
19
|
"scripts": {
|
package/src/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const { get, logout } = require("./main");
|
|
1
|
+
const { get, getMany, getAll, logout } = require("./main");
|
|
2
2
|
const {
|
|
3
3
|
ensureSession,
|
|
4
4
|
loadSession,
|
|
@@ -10,6 +10,8 @@ const {
|
|
|
10
10
|
|
|
11
11
|
const envis = {
|
|
12
12
|
get,
|
|
13
|
+
getMany,
|
|
14
|
+
getAll,
|
|
13
15
|
logout,
|
|
14
16
|
ensureSession,
|
|
15
17
|
loadSession,
|
package/src/main.js
CHANGED
|
@@ -28,34 +28,19 @@ async function logout() {
|
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
async function
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
const ciToken = process.env.ENVIS_CI_TOKEN;
|
|
37
|
-
let session = null;
|
|
38
|
-
if (!ciToken) {
|
|
39
|
-
await ensureSession();
|
|
40
|
-
session = await loadSession();
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const headers = {
|
|
31
|
+
async function buildAuthHeaders() {
|
|
32
|
+
await ensureSession();
|
|
33
|
+
const session = await loadSession();
|
|
34
|
+
return {
|
|
35
|
+
Authorization: `Bearer ${session.access_token}`,
|
|
44
36
|
"Content-Type": "application/json",
|
|
45
37
|
};
|
|
46
|
-
|
|
47
|
-
headers["X-CI-Token"] = ciToken;
|
|
48
|
-
} else {
|
|
49
|
-
headers.Authorization = `Bearer ${session.access_token}`;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const url = `${BASE_URL}/v1/projects/${encodeURIComponent(
|
|
53
|
-
projectId
|
|
54
|
-
)}/secrets/${encodeURIComponent(secretName)}`;
|
|
38
|
+
}
|
|
55
39
|
|
|
40
|
+
async function fetchJSON(url, options, errorAction, errorContext) {
|
|
56
41
|
let response;
|
|
57
42
|
try {
|
|
58
|
-
response = await fetch(url, {
|
|
43
|
+
response = await fetch(url, { timeout: 10000, ...options });
|
|
59
44
|
} catch (error) {
|
|
60
45
|
throw new Error(`Failed to reach Envault API: ${error.message}`);
|
|
61
46
|
}
|
|
@@ -68,7 +53,7 @@ async function get(projectId, secretName) {
|
|
|
68
53
|
detail = error.message;
|
|
69
54
|
}
|
|
70
55
|
throw new Error(
|
|
71
|
-
`Failed to
|
|
56
|
+
`Failed to ${errorAction} (${response.status || "HTTP error"}): ${
|
|
72
57
|
detail || "No response body provided."
|
|
73
58
|
}`
|
|
74
59
|
);
|
|
@@ -83,32 +68,118 @@ async function get(projectId, secretName) {
|
|
|
83
68
|
|
|
84
69
|
if (!bodyText) {
|
|
85
70
|
throw new Error(
|
|
86
|
-
|
|
71
|
+
`API returned an empty, non-JSON response when ${errorContext || errorAction}.`
|
|
87
72
|
);
|
|
88
73
|
}
|
|
89
74
|
|
|
90
75
|
try {
|
|
91
|
-
|
|
92
|
-
if (
|
|
93
|
-
payload &&
|
|
94
|
-
typeof payload === "object" &&
|
|
95
|
-
Object.prototype.hasOwnProperty.call(payload, "value")
|
|
96
|
-
) {
|
|
97
|
-
return payload.value;
|
|
98
|
-
}
|
|
99
|
-
return payload;
|
|
76
|
+
return JSON.parse(bodyText);
|
|
100
77
|
} catch {
|
|
101
78
|
const raw = bodyText.trim();
|
|
102
79
|
if (raw) {
|
|
103
80
|
return { raw };
|
|
104
81
|
}
|
|
105
82
|
throw new Error(
|
|
106
|
-
|
|
83
|
+
`API returned an empty, non-JSON response when ${errorContext || errorAction}.`
|
|
107
84
|
);
|
|
108
85
|
}
|
|
109
86
|
}
|
|
110
87
|
|
|
88
|
+
async function get(projectId, secretName) {
|
|
89
|
+
if (!projectId || !secretName) {
|
|
90
|
+
throw new Error("Both project id and secret name are required.");
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const headers = await buildAuthHeaders();
|
|
94
|
+
|
|
95
|
+
const url = `${BASE_URL}/v1/projects/${encodeURIComponent(
|
|
96
|
+
projectId
|
|
97
|
+
)}/secrets/${encodeURIComponent(secretName)}`;
|
|
98
|
+
|
|
99
|
+
const payload = await fetchJSON(
|
|
100
|
+
url,
|
|
101
|
+
{ headers },
|
|
102
|
+
"fetch secret",
|
|
103
|
+
"fetching secret"
|
|
104
|
+
);
|
|
105
|
+
if (
|
|
106
|
+
payload &&
|
|
107
|
+
typeof payload === "object" &&
|
|
108
|
+
Object.prototype.hasOwnProperty.call(payload, "value")
|
|
109
|
+
) {
|
|
110
|
+
return payload.value;
|
|
111
|
+
}
|
|
112
|
+
return payload;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async function getMany(projectId, secretNames) {
|
|
116
|
+
if (!projectId || !Array.isArray(secretNames)) {
|
|
117
|
+
throw new Error(
|
|
118
|
+
"Project id and an array of secret names are required."
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
const names = secretNames
|
|
122
|
+
.map((name) => (typeof name === "string" ? name.trim() : ""))
|
|
123
|
+
.filter(Boolean);
|
|
124
|
+
if (names.length === 0) {
|
|
125
|
+
throw new Error("At least one secret name is required.");
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const headers = await buildAuthHeaders();
|
|
129
|
+
const url = `${BASE_URL}/v1/projects/${encodeURIComponent(
|
|
130
|
+
projectId
|
|
131
|
+
)}/secrets/batch`;
|
|
132
|
+
|
|
133
|
+
const payload = await fetchJSON(
|
|
134
|
+
url,
|
|
135
|
+
{
|
|
136
|
+
method: "POST",
|
|
137
|
+
headers,
|
|
138
|
+
body: JSON.stringify({ names }),
|
|
139
|
+
},
|
|
140
|
+
"fetch secrets",
|
|
141
|
+
"fetching secrets"
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
if (
|
|
145
|
+
payload &&
|
|
146
|
+
typeof payload === "object" &&
|
|
147
|
+
Object.prototype.hasOwnProperty.call(payload, "secrets")
|
|
148
|
+
) {
|
|
149
|
+
return payload.secrets;
|
|
150
|
+
}
|
|
151
|
+
return payload;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
async function getAll(projectId) {
|
|
155
|
+
if (!projectId) {
|
|
156
|
+
throw new Error("Project id is required.");
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const headers = await buildAuthHeaders();
|
|
160
|
+
const url = `${BASE_URL}/v1/projects/${encodeURIComponent(
|
|
161
|
+
projectId
|
|
162
|
+
)}/secrets/all`;
|
|
163
|
+
|
|
164
|
+
const payload = await fetchJSON(
|
|
165
|
+
url,
|
|
166
|
+
{ headers },
|
|
167
|
+
"fetch secrets",
|
|
168
|
+
"fetching secrets"
|
|
169
|
+
);
|
|
170
|
+
if (
|
|
171
|
+
payload &&
|
|
172
|
+
typeof payload === "object" &&
|
|
173
|
+
Object.prototype.hasOwnProperty.call(payload, "secrets")
|
|
174
|
+
) {
|
|
175
|
+
return payload.secrets;
|
|
176
|
+
}
|
|
177
|
+
return payload;
|
|
178
|
+
}
|
|
179
|
+
|
|
111
180
|
module.exports = {
|
|
112
181
|
get,
|
|
182
|
+
getMany,
|
|
183
|
+
getAll,
|
|
113
184
|
logout,
|
|
114
185
|
};
|
package/src/session.js
CHANGED
|
@@ -11,76 +11,6 @@ const REQUIRED_SESSION_KEYS = ["access_token", "refresh_token"];
|
|
|
11
11
|
const WAIT_TIME = 120; // seconds
|
|
12
12
|
const POLL_DELAY_SECONDS = 5;
|
|
13
13
|
|
|
14
|
-
function findDotEnv(startDir) {
|
|
15
|
-
let current = startDir;
|
|
16
|
-
while (current && current !== path.dirname(current)) {
|
|
17
|
-
const candidate = path.join(current, ".env");
|
|
18
|
-
if (fs.existsSync(candidate)) {
|
|
19
|
-
return candidate;
|
|
20
|
-
}
|
|
21
|
-
current = path.dirname(current);
|
|
22
|
-
}
|
|
23
|
-
const fallback = path.join(startDir, ".env");
|
|
24
|
-
return fs.existsSync(fallback) ? fallback : null;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function parseDotEnvLine(line) {
|
|
28
|
-
const trimmed = line.trim();
|
|
29
|
-
if (!trimmed || trimmed.startsWith("#")) {
|
|
30
|
-
return null;
|
|
31
|
-
}
|
|
32
|
-
const normalized = trimmed.startsWith("export ")
|
|
33
|
-
? trimmed.slice(7).trim()
|
|
34
|
-
: trimmed;
|
|
35
|
-
const splitIndex = normalized.indexOf("=");
|
|
36
|
-
if (splitIndex <= 0) {
|
|
37
|
-
return null;
|
|
38
|
-
}
|
|
39
|
-
const key = normalized.slice(0, splitIndex).trim();
|
|
40
|
-
let value = normalized.slice(splitIndex + 1).trim();
|
|
41
|
-
if (
|
|
42
|
-
(value.startsWith('"') && value.endsWith('"')) ||
|
|
43
|
-
(value.startsWith("'") && value.endsWith("'"))
|
|
44
|
-
) {
|
|
45
|
-
value = value.slice(1, -1);
|
|
46
|
-
}
|
|
47
|
-
value = value.replace(/\\n/g, "\n");
|
|
48
|
-
return { key, value };
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function loadDotEnv() {
|
|
52
|
-
const envPath = findDotEnv(process.cwd());
|
|
53
|
-
if (!envPath) {
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
let raw;
|
|
57
|
-
try {
|
|
58
|
-
raw = fs.readFileSync(envPath, "utf-8");
|
|
59
|
-
} catch {
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
|
-
raw.split(/\r?\n/).forEach((line) => {
|
|
63
|
-
const parsed = parseDotEnvLine(line);
|
|
64
|
-
if (!parsed || process.env[parsed.key] !== undefined) {
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
process.env[parsed.key] = parsed.value;
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function isHeadless() {
|
|
72
|
-
if (process.env.ENVIS_CI_TOKEN) {
|
|
73
|
-
return true;
|
|
74
|
-
}
|
|
75
|
-
try {
|
|
76
|
-
return !(process.stdin.isTTY && process.stdout.isTTY);
|
|
77
|
-
} catch {
|
|
78
|
-
return true;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
loadDotEnv();
|
|
83
|
-
|
|
84
14
|
function printc(text, color = "white") {
|
|
85
15
|
const colorFn = chalk[color];
|
|
86
16
|
if (typeof colorFn === "function") {
|
|
@@ -100,12 +30,12 @@ function getEnvUrl(varName, fallback) {
|
|
|
100
30
|
|
|
101
31
|
const BASE_URL = getEnvUrl(
|
|
102
32
|
"ENVIS_API_URL",
|
|
103
|
-
"https://
|
|
33
|
+
"https://api.envisible.dev"
|
|
104
34
|
);
|
|
105
35
|
|
|
106
36
|
const FRONTEND_URL = getEnvUrl(
|
|
107
37
|
"ENVIS_DASH_URL",
|
|
108
|
-
"https://envisible.
|
|
38
|
+
"https://envisible.dev"
|
|
109
39
|
);
|
|
110
40
|
|
|
111
41
|
async function pathExists(targetPath) {
|
|
@@ -276,12 +206,6 @@ async function ensureSession() {
|
|
|
276
206
|
return;
|
|
277
207
|
}
|
|
278
208
|
|
|
279
|
-
if (isHeadless()) {
|
|
280
|
-
throw new Error(
|
|
281
|
-
"No cached session and headless environment detected. Set ENVIS_CI_TOKEN or run `envis login` on a machine with a browser."
|
|
282
|
-
);
|
|
283
|
-
}
|
|
284
|
-
|
|
285
209
|
const { waitForAuth } = require("./auth");
|
|
286
210
|
let openBrowser;
|
|
287
211
|
try {
|