multicloud_rule_manager 1.0.0 → 1.0.2

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.
@@ -0,0 +1,23 @@
1
+ import { saveAlias } from "../utils/aliasManager.js";
2
+
3
+ export default {
4
+ command: "alias:set <alias>",
5
+ describe: "Setta le credenziali sotto un alias",
6
+ builder: {
7
+ clientId: { type: "string", demandOption: true },
8
+ clientSecret: { type: "string", demandOption: true },
9
+ username: { type: "string", demandOption: true },
10
+ password: { type: "string", demandOption: true },
11
+ realm: { type: "string", demandOption: true }
12
+ },
13
+ handler: (argv) => {
14
+ saveAlias(argv.alias, {
15
+ clientId: argv.clientId,
16
+ clientSecret: argv.clientSecret,
17
+ username: argv.username,
18
+ password: argv.password,
19
+ realm: argv.realm
20
+ });
21
+ console.log(`✅ Alias "${argv.alias}" salvato correttamente`);
22
+ }
23
+ };
@@ -0,0 +1,42 @@
1
+ import fetch from "node-fetch";
2
+ import fs from "fs";
3
+ import { getAlias } from "../utils/aliasManager.js";
4
+
5
+ export default {
6
+ command: "retrieve <alias> [output]",
7
+ describe: "Recupera dati API con token automatico",
8
+ builder: {
9
+ output: { type: "string", default: "retrieve.json", describe: "File di output" }
10
+ },
11
+ handler: async (argv) => {
12
+ try {
13
+ const cred = getAlias(argv.alias);
14
+
15
+ // Token
16
+ const loginResp = await fetch(`${cred.realm}/token`, {
17
+ method: "POST",
18
+ headers: { "Content-Type": "application/json" },
19
+ body: JSON.stringify({
20
+ client_id: cred.clientId,
21
+ client_secret: cred.clientSecret,
22
+ username: cred.username,
23
+ password: cred.password
24
+ })
25
+ });
26
+
27
+ const { access_token } = await loginResp.json();
28
+ if (!access_token) throw new Error("Token non ricevuto");
29
+
30
+ // Retrieve
31
+ const apiResp = await fetch(`${cred.realm}/retrieve`, {
32
+ headers: { Authorization: `Bearer ${access_token}` }
33
+ });
34
+
35
+ const data = await apiResp.json();
36
+ fs.writeFileSync(argv.output, JSON.stringify(data, null, 2));
37
+ console.log(`🚀 Dati salvati in ${argv.output}`);
38
+ } catch (err) {
39
+ console.error("❌ Errore:", err.message);
40
+ }
41
+ }
42
+ };
@@ -0,0 +1,44 @@
1
+ import fetch from "node-fetch";
2
+ import fs from "fs";
3
+ import { getAlias } from "../utils/aliasManager.js";
4
+
5
+ export default {
6
+ command: "upload <alias> <file>",
7
+ describe: "Upload dati API con token automatico",
8
+ handler: async (argv) => {
9
+ try {
10
+ const cred = getAlias(argv.alias);
11
+ const content = fs.readFileSync(argv.file, "utf-8");
12
+
13
+ // Token
14
+ const loginResp = await fetch(`${cred.realm}/token`, {
15
+ method: "POST",
16
+ headers: { "Content-Type": "application/json" },
17
+ body: JSON.stringify({
18
+ client_id: cred.clientId,
19
+ client_secret: cred.clientSecret,
20
+ username: cred.username,
21
+ password: cred.password
22
+ })
23
+ });
24
+
25
+ const { access_token } = await loginResp.json();
26
+ if (!access_token) throw new Error("Token non ricevuto");
27
+
28
+ // Upload
29
+ const uploadResp = await fetch(`${cred.realm}/upload`, {
30
+ method: "POST",
31
+ headers: {
32
+ Authorization: `Bearer ${access_token}`,
33
+ "Content-Type": "application/json"
34
+ },
35
+ body: content
36
+ });
37
+
38
+ const result = await uploadResp.json();
39
+ console.log("✅ Upload completato:", result);
40
+ } catch (err) {
41
+ console.error("❌ Errore:", err.message);
42
+ }
43
+ }
44
+ };
package/index.js ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env node
2
+ import yargs from "yargs";
3
+ import { hideBin } from "yargs/helpers";
4
+
5
+ import aliasSet from "./commands/aliasSet.js";
6
+ import retrieve from "./commands/retrieve.js";
7
+ import upload from "./commands/upload.js";
8
+
9
+ yargs(hideBin(process.argv))
10
+ .command(aliasSet)
11
+ .command(retrieve)
12
+ .command(upload)
13
+ .demandCommand()
14
+ .help()
15
+ .parse();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "multicloud_rule_manager",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "CLI interna per retrieve/upload con token",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -0,0 +1,38 @@
1
+ import fs from "fs";
2
+ import path from "path";
3
+ import CryptoJS from "crypto-js";
4
+
5
+ const storagePath = path.resolve("./storage.json");
6
+ const secretKey = "bit2win-secret-key"; // chiave privata interna, cambiare in produzione
7
+
8
+ export function saveAlias(alias, creds) {
9
+ let storage = {};
10
+ if (fs.existsSync(storagePath)) storage = JSON.parse(fs.readFileSync(storagePath));
11
+
12
+ storage[alias] = {
13
+ clientId: CryptoJS.AES.encrypt(creds.clientId, secretKey).toString(),
14
+ clientSecret: CryptoJS.AES.encrypt(creds.clientSecret, secretKey).toString(),
15
+ password: CryptoJS.AES.encrypt(creds.password, secretKey).toString(),
16
+ username: creds.username,
17
+ realm: creds.realm
18
+ };
19
+
20
+ fs.writeFileSync(storagePath, JSON.stringify(storage, null, 2));
21
+ }
22
+
23
+ export function getAlias(alias) {
24
+ if (!fs.existsSync(storagePath)) throw new Error("Nessun alias salvato");
25
+
26
+ const storage = JSON.parse(fs.readFileSync(storagePath));
27
+ if (!storage[alias]) throw new Error(`Alias "${alias}" non trovato`);
28
+
29
+ const decrypt = (str) => CryptoJS.AES.decrypt(str, secretKey).toString(CryptoJS.enc.Utf8);
30
+
31
+ return {
32
+ clientId: decrypt(storage[alias].clientId),
33
+ clientSecret: decrypt(storage[alias].clientSecret),
34
+ username: storage[alias].username,
35
+ password: decrypt(storage[alias].password),
36
+ realm: storage[alias].realm
37
+ };
38
+ }