Skip to content

Commit

Permalink
add crendentials manager with save and exit
Browse files Browse the repository at this point in the history
Signed-off-by: Johnny On <[email protected]>
  • Loading branch information
johnnyon-amzn committed May 16, 2022
1 parent 11ce0ab commit d3dd86e
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 38 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const path = require('path');
const fs = require("fs");
const CONFIG_PATH = path.join(__dirname, "config.json");

function getConfig(name, configPath=CONFIG_PATH) {
let config = fs.readFileSync(configPath);
config = JSON.parse(config.toString());
return config[name] || {};
}

function setConfig(config, callback, configPath=CONFIG_PATH) {
console.log('config', config)
let oldConfig = fs.readFileSync(configPath);
oldConfig = JSON.parse(oldConfig.toString());
oldConfig[config.NAME] = config;
fs.writeFile(configPath, JSON.stringify(oldConfig), callback);
}


exports.getConfig = getConfig;
exports.setConfig = setConfig;
10 changes: 5 additions & 5 deletions src/configManager.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,10 @@
<body>

<div class="jumbotron" style="text-align: center;padding: 30px">
<h1>OpenSearch Dashboards Electron</h1>
<h1>Credentials Manager</h1>
</div>

<div style="width:80%; margin-left:10%; margin-top: 30px; margin-bottom: 0">
<div style="margin-bottom: 0" id="info" class="alert alert-info" role="alert">
<strong>Welcome!</strong> Let's get you connected to OpenSearch Dashboards.
</div>
<div id="error" class="alert alert-danger hidden">
<strong>Error!</strong> <span id="error-message"/>
</div>
Expand All @@ -42,6 +39,9 @@ <h1>OpenSearch Dashboards Electron</h1>
<div style="margin:0">
<form style="margin:0">
<div class="form-group">
<label for="name">NAME</label>
<input type="text" id="name" class="form-control">
</input>
<label for="accessKey">AWS ACCESS KEY ID</label>
<input type="text" id="awsAccessKeyId" class="form-control">
</input>
Expand All @@ -62,7 +62,7 @@ <h1>OpenSearch Dashboards Electron</h1>
<div style="width:80%; margin-left:10%; margin-top: 30px">
<div>
<div style="text-align: center">
<a id="submit" class="btn btn-primary btn-lg" href="#">Launch</a>
<a id="submit" class="btn btn-primary btn-lg" href="#">SAVE</a>
</div>
</div>
</div>
Expand Down
16 changes: 16 additions & 0 deletions src/configPreload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const { contextBridge } = require('electron');
const { ipcRenderer } = require("electron");
const configLibrary = require('./config');

contextBridge.exposeInMainWorld(
'electron',
{
ipcRenderer: ipcRenderer,
onName: (fn) => {
ipcRenderer.on("name", (event, ...args) => fn(...args));
},
getConfig: (name) => config.getConfig(name),
setConfig: (config, callback) => configLibrary.setConfig(config, callback),

}
)
53 changes: 53 additions & 0 deletions src/configRenderer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
function init() {
addListeners();
}

// store name variable if exists
window.electron.onName(function (event, name) {
let config = window.electron.getConfig(name);
populateFields(config);
});

function getMapping() {
return [{id: "name", key: "NAME"},
{id: "awsAccessKeyId", key: "AWS_ACCESS_KEY_ID"},
{id: "awsSecretAccessKey", key: "AWS_SECRET_ACCESS_KEY"},
{id: "endpoint", key: "ENDPOINT"},
{id: "osdPath", key: "OSD_PATH"}
]
}
async function populateFields(config) {
let mapping = getMapping();
for (map of mapping) {
let input = document.getElementById(map.id);
input.setAttribute("value", config[map.key]);
}
}

function getNewConfig() {
let mapping = getMapping();
let config = {};
mapping.forEach(function(map) {
let input = document.getElementById(map.id);
config[map.key] = input.value;
})
return config;
}

function addListeners() {
let submit = document.getElementById("submit");
submit.addEventListener("click", async (event) => {
event.preventDefault();
console.log('click');
let newConfig = getNewConfig();
console.log('newConfig', newConfig);
window.electron.setConfig(newConfig, closeWindow);
})
}

function closeWindow() {
window.electron.ipcRenderer.invoke("closeWindow");
}

init();

19 changes: 1 addition & 18 deletions src/dashboards.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,6 @@ const http = require('http');

const superagent = require('superagent');


const CONFIG_PATH = path.join(__dirname, "config.json");


function getConfig(configPath=CONFIG_PATH) {
let config = fs.readFileSync(configPath);
config = JSON.parse(config.toString());
return config;
}

function setConfig(key, value, callback, configPath=CONFIG_PATH) {
let config = getConfig(configPath);
config[key] = value;
fs.writeFile(configPath, JSON.stringify(config), callback);
}

async function getOSDStatus() {
let statuses = {};
try {
Expand Down Expand Up @@ -105,7 +89,6 @@ async function startOSD() {
}

exports.startProxy = startProxy;
exports.getConfig = getConfig;
exports.setConfig = setConfig;

exports.startOSD = startOSD;
exports.getOSDStatus = getOSDStatus;
25 changes: 24 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,22 @@ function createWindow() {
win.webContents.openDevTools();
}

function createConfigWindow() {
const win = new BrowserWindow({
width: 1600,
height: 1200,
webPreferences: {
preload: path.join(__dirname, 'configPreload.js')
}
})

win.loadFile("./src/configManager.html");
win.webContents.openDevTools();
}

async function viewOSD() {
app.whenReady().then(() => {
createWindow();

app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
Expand All @@ -34,5 +46,16 @@ ipcMain.handle('swapURL', async (event, arg) => {
win.loadURL(arg);
})

ipcMain.handle('openConfig', async (event, name) => {
let configWindow = createConfigWindow();
configWindow.webContents.send('name', name);

})

ipcMain.handle('closeWindow', async (event, arg) => {
let win = BrowserWindow.getFocusedWindow();
win.close();
})


viewOSD();
22 changes: 8 additions & 14 deletions src/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ function getMapping() {
]
}
function init() {
window.config = {};
window.config = window.electron.getConfig();
addListeners();
populateFields();
//populateFields();
}

async function populateFields() {
Expand All @@ -23,19 +21,15 @@ async function populateFields() {

function addListeners() {
let listeners = getMapping();
listeners.forEach(function(listener) {
let input = document.getElementById(listener.id);
input.addEventListener('blur', (event) => {

if (window.config[listener.key] != input.value) {
window.config[listener.key] = input.value
//add in success for save file config
window.electron.setConfig(listener.key, input.value, ()=>{});
}
})
let addConfig = document.getElementById("addConfig");
addConfig.addEventListener("click", async (event) => {
event.preventDefault();
window.electron.ipcRenderer.invoke("openConfig");

})

let submit = document.getElementById("submit");

submit.addEventListener("click", async (event) => {
event.preventDefault();
console.log(event);
Expand Down

0 comments on commit d3dd86e

Please sign in to comment.