-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
267 lines (243 loc) · 7.51 KB
/
extension.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// author: Aldrin Arabis (aldrinyarabis@gmail.com)
// date: 2023-06-05
const vscode = require("vscode");
const { exec } = require("child_process");
const EncryptProvider = require("./providers/encrypt-provider");
const DecryptProvider = require("./providers/decrypt-provider");
const path = require("path");
let registry = {};
let extPath = "";
const unsafeDecorationType = vscode.window.createTextEditorDecorationType({
gutterIconPath: path.join(__filename, "..", "media", "lock-unsafe.png"),
gutterIconSize: "cover",
cursor: "hand",
});
/**
* @param {string} cmd
* @returns {Promise<string>}
* @description Execute shell command
* @see https://stackoverflow.com/a/64598488
* @example
* const out = await execShell('ls -al');
* console.log(out);
*/
const execShell = (cmd) =>
new Promise((resolve, reject) => {
exec(cmd, (err, out) => {
if (err) {
return reject(err);
}
return resolve(out);
});
});
async function getSecureKey() {
return await vscode.window.showInputBox({
prompt: "Enter Secure Key",
password: true,
});
}
/**
* @param {import("vscode").ExtensionContext} context
* @param {String} action
* @param {vscode.Range} range
*/
async function execAction(context, action, range) {
// get document path
const docPath = vscode.window.activeTextEditor.document.uri.fsPath;
// get key from keyRegistry
let key = registry[docPath].key;
// if key is not found in keyRegistry, ask user to enter key
if (!key) {
key = registry[docPath].key = await getSecureKey();
context.workspaceState.update("registry", registry);
}
// get algorithm, mode from setting
const algorithm =
registry[docPath].algorithm ||
vscode.workspace.getConfiguration().get("mule-secure-properties.algorithm");
const mode =
registry[docPath].mode ||
vscode.workspace.getConfiguration().get("mule-secure-properties.mode");
// get value from range argument
const editor = vscode.window.activeTextEditor;
const txt = editor.document.getText(range);
const prop = txt.split(/=(.*)/s);
let value = prop[1];
if (action === "decrypt") {
// remove ![ and ] from decrypted value
value = value.replace(/!\[|\]/g, "");
}
if (!key || !value) return;
try {
let result = "";
// execute secure properties tool
if (process.platform === "win32") {
result = await execShell(
`call ${extPath}/lib/crypt-tool.bat ${action} ${algorithm} ${mode} ${key} "${value}"`
);
} else {
result = await execShell(
`sh ${extPath}/lib/crypt-tool.sh ${action} ${algorithm} ${mode} ${key} "${value}"`
);
}
result = result.replace(/\r?\n/g, "");
if (action === "encrypt") {
// add ![ and ] to encrypted value
result = `![${result}]`;
}
prop[1] = result;
// remove empty string at the end of array
prop.pop();
// replace value in editor
editor.edit((edit) => edit.replace(range, prop.join("=")));
} catch (e) {
vscode.window.showErrorMessage(e.message);
}
}
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
registry = context.workspaceState.get("registry", {});
const encryptProvider = new EncryptProvider();
const decryptProvider = new DecryptProvider();
extPath = context.extensionPath;
["encrypt", "decrypt"]
.map((action) => {
return vscode.commands.registerCommand(
`mule-secure-properties.${action}`,
(range) => {
// if range is null, get current range on cursor
if (range === null) {
const editor = vscode.window.activeTextEditor;
const selection = editor.selection;
range = selection.active.line;
}
execAction(context, action, range);
}
);
})
.forEach((command) => context.subscriptions.push(command));
// only active lense on secure properties file
[encryptProvider, decryptProvider].map((provider) => {
context.subscriptions.push(
vscode.languages.registerCodeLensProvider(
{
scheme: "file",
language: "properties",
pattern: "**/*secure*.properties",
},
provider
),
vscode.languages.registerCodeLensProvider(
{
scheme: "file",
language: "java-properties",
pattern: "**/*secure*.properties",
},
provider
)
);
});
// update decoration on active editor change
context.subscriptions.push(
vscode.window.onDidChangeActiveTextEditor((editor) => {
if (
editor &&
editor.document.languageId.includes("properties") &&
editor.document.fileName.includes("secure")
) {
updateDecorations(editor);
ensureRegistry();
}
})
);
// allow setting key by action
context.subscriptions.push(
vscode.commands.registerCommand(
"mule-secure-properties.setKey",
async () => {
const docPath = vscode.window.activeTextEditor.document.uri.fsPath;
registry[docPath].key = await getSecureKey();
context.workspaceState.update("keyRegistry", registry);
}
)
);
context.subscriptions.push(
vscode.commands.registerCommand(
"mule-secure-properties.setAlgorithm",
async () => {
const docPath = vscode.window.activeTextEditor.document.uri.fsPath;
const algorithm = await vscode.window.showQuickPick(
["AES", "Blowfish", "DES", "DESede", "RC2", "RCA"],
{
placeHolder: "Select Encryption Algorithm",
}
);
registry[docPath].algorithm = algorithm;
}
)
);
context.subscriptions.push(
vscode.commands.registerCommand(
"mule-secure-properties.setMode",
async () => {
const docPath = vscode.window.activeTextEditor.document.uri.fsPath;
const mode = await vscode.window.showQuickPick(
["CBC", "CFB", "OFB", "ECB"],
{
placeHolder: "Select Encryption Mode",
}
);
registry[docPath].mode = mode;
}
)
);
// update decoration on text change
context.subscriptions.push(
vscode.workspace.onDidChangeTextDocument((event) => {
if (
active.document.languageId.includes("properties") &&
event.document.fileName.includes("secure")
) {
updateDecorations(vscode.window.activeTextEditor);
ensureRegistry();
}
})
);
// update decoration on current active editor on load
const active = vscode.window.activeTextEditor;
if (
active.document.languageId.includes("properties") &&
active.document.fileName.includes("secure")
) {
updateDecorations(vscode.window.activeTextEditor);
ensureRegistry();
}
}
/**
* @param {vscode.TextEditor} editor
* @description Update decoration
*/
function updateDecorations(editor) {
if (!editor.document.languageId.includes("properties")) return;
// for each line, check if it contains ![ and ], if not add unsafe decoration
const safeDecorations = [];
const unsafeDecorations = [];
for (let line = 0; line < editor.document.lineCount; line++) {
const lineRange = editor.document.lineAt(line).range;
const txt = editor.document.getText(lineRange);
if (txt.trim() == "" || txt.startsWith("#")) continue;
if (!txt.includes("=![")) {
unsafeDecorations.push({ range: lineRange });
}
}
editor.setDecorations(unsafeDecorationType, unsafeDecorations);
}
module.exports = {
activate,
};
function ensureRegistry() {
const docPath = vscode.window.activeTextEditor.document.uri.fsPath;
registry[docPath] = registry[docPath] || {};
}