Skip to content

Commit

Permalink
added secret size
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeTWC1984 committed Mar 15, 2024
1 parent 0387467 commit 748c425
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 32 deletions.
6 changes: 5 additions & 1 deletion htdocs/js/pages/admin/Categories.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ Class.add( Page.Admin, {
// edit existing Category
var html = '';
let category = find_object( app.categories, { id: args.id } );
if(!category) return app.doError("Could not locate Category with ID: " + args.id);
let secret = find_object( app.secrets, { id: args.id } ) || {};

this.category = deep_copy_object( category )

Expand All @@ -213,9 +215,11 @@ Class.add( Page.Admin, {
['users', "Users"]
]
);

let secretInfo = secret.size > 0 ? `Edit Secrets (${secret.size})` : 'Attach Secrets'

html += `<div style="padding:20px;"><div class="subtitle">Editing Category &ldquo;${category.title}&rdquo;
<div class="subtitle_widget"><a href="#Admin?sub=secrets&id=${category.id}" ><b>Attach Secret</b></a></div>
<div class="subtitle_widget"><a href="#Admin?sub=secrets&id=${category.id}" ><b>${secretInfo}</b></a></div>
</div></div><div style="padding:0px 20px 50px 20px"><center>
<table style="margin:0;">
`
Expand Down
7 changes: 5 additions & 2 deletions htdocs/js/pages/admin/Plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ Class.add( Page.Admin, {
// edit plugin subpage
let plugin = find_object( app.plugins, { id: args.id } );
if (!plugin) return app.doError("Could not locate Plugin with ID: " + args.id);
let secret = find_object( app.secrets, { id: args.id } ) || {};

// make local copy so edits don't affect main app list until save
this.plugin = deep_copy_object( plugin );
Expand All @@ -240,9 +241,11 @@ Class.add( Page.Admin, {
['users', "Users"]
]
);

let secretInfo = secret.size > 0 ? `Edit Secrets (${secret.size})` : 'Attach Secrets'

html += `<div style="padding:20px;"><div class="subtitle">Editing Plugin &ldquo;${plugin.title}&rdquo;
<div class="subtitle_widget"><a href="#Admin?sub=secrets&id=${plugin.id}" ><b>Attach Secret</b></a></div>
<div class="subtitle_widget"><a href="#Admin?sub=secrets&id=${plugin.id}" ><b>${secretInfo}</b></a></div>
</div></div><div style="padding:0px 20px 50px 20px"><center>
<table style="margin:0;">
`
Expand Down Expand Up @@ -376,7 +379,7 @@ Class.add( Page.Admin, {

editor.on('change', (cm) => { plugin.script= cm.getValue() });
editor.setValue(plugin.script || '');
editor.setSize('600px', '30vh')
editor.setSize('900px', '25vh')
},

get_plugin_edit_html: function() {
Expand Down
65 changes: 36 additions & 29 deletions lib/api/secret.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

const Class = require("pixl-class");
const Tools = require("pixl-tools");
const dotenv = require('dotenv')

module.exports = Class.create({

Expand All @@ -22,17 +23,19 @@ module.exports = Class.create({
if (!self.requireAdmin(session, user, callback)) return;

self.storage.listFind('global/secrets', { id: params.id }, async function (err, item) {
if (err ) {
if (err) {
return self.doError('secret', "Failed to locate Secret: " + params.id, callback);
}

if(!item) { // if secret doesn't exist return a placeholder user can save later
if (!item) { // if secret doesn't exist return a placeholder user can save later
return callback({
code: 0, secret: {
code: 0, secret: {
id: params.id,
encrypted: true,
user: user.username,
virtual: true
virtual: true,
type: 'prop',
size: 0
}
});
}
Expand Down Expand Up @@ -60,7 +63,7 @@ module.exports = Class.create({
let params = args.params;
if (!this.requiremanager(args, callback)) return;
// sanity check
if(params.virtual) return self.doError('secret', 'cannot save virtual secret', callback);
if (params.virtual) return self.doError('secret', 'cannot save virtual secret', callback);

if (!this.requireParams(params, {
id: /\S/,
Expand All @@ -77,6 +80,7 @@ module.exports = Class.create({
params.id = params.id || self.getUniqueID('s');
params.username = user.username;
params.created = params.modified = Tools.timeNow(true);
params.size = Object.keys(dotenv.parse(params.data || '')).length

if (!params.active) params.active = 1;
if (!params.description) params.description = "";
Expand All @@ -94,35 +98,32 @@ module.exports = Class.create({

self.logDebug(6, "Creating new Secret: " + params.id, params.id);

self.storage.listGet('global/secrets', 0, 0, function (err, items, list) {
let alreadyExist = await self.validateUnique('global/secrets', params, ["id"])
if (alreadyExist > 0) {
return self.doError('event', `Secret with id ${params.id} already exists`, callback);
}

self.storage.listUnshift('global/secrets', params, function (err) {
if (err) {
// no secret info, ignore
return self.doError('secret', "Failed to create secret: " + err, callback);
}
if (items) {
let sec = items.filter(e => e.id === params.id) // secret only has ID, no title
if (sec.length) {
return self.doError('secret', `Secret with id ${sec.id} already exist`, callback);
}
}

self.storage.listUnshift('global/secrets', params, function (err) {
if (err) {
return self.doError('secret', "Failed to create secret: " + err, callback);
}

self.logDebug(6, "Successfully created secret: " + params.id, params.id);
self.logTransaction('secret_create', params.id, self.getClientInfo(args, { secret: params.id }));
self.logActivity('secret_create', { secret: params.id, encrypted: params.encrypted }, args);
self.logDebug(6, "Successfully created secret: " + params.id, params.id);
self.logTransaction('secret_create', params.id, self.getClientInfo(args, { secret: params.id }));
self.logActivity('secret_create', { secret: params.id, encrypted: params.encrypted }, args);

callback({ code: 0, id: params.id, key: params.key });
callback({ code: 0, id: params.id, key: params.key });

// broadcast update to all websocket clients
self.authSocketEmit('update', { secrets: {} });
// self.updateSecrets();
self.updateClientData('secrets');
}); // list insert

}); // get all secrets
// broadcast update to all websocket clients
self.authSocketEmit('update', { secrets: {} });
// self.updateSecrets();
self.updateClientData('secrets');
// reset cache if needed
if (self.server.config.get('cache_secrets')) {
self.secretCache = {};
self.logDebug(6, "Secret cache cleared");
}
}); // list insert

}); // load session
},
Expand All @@ -146,6 +147,7 @@ module.exports = Class.create({
args.session = session;

params.modified = Tools.timeNow(true);
params.size = Object.keys(dotenv.parse(params.data || '')).length

self.logDebug(6, "Updating Secret: " + params.id, params.id);

Expand Down Expand Up @@ -173,6 +175,11 @@ module.exports = Class.create({
// broadcast update to all websocket clients
self.authSocketEmit('update', { secrets: {} });
// self.updateSecrets();
// reset cache if needed
if (self.server.config.get('cache_secrets')) {
self.secretCache = {};
self.logDebug(6, "Secret cache cleared");
}
self.updateClientData('secrets');
});

Expand Down

0 comments on commit 748c425

Please sign in to comment.