Skip to content

Commit

Permalink
initial poc for credential manager
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 d3dd86e commit 88e1b8c
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 26 deletions.
7 changes: 6 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ 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] || {};
console.log('config', config);
if (name) {
return config[name] || {};
} else {
return config;
}
}

function setConfig(config, callback, configPath=CONFIG_PATH) {
Expand Down
12 changes: 4 additions & 8 deletions src/dashboards.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ async function getOSDStatus() {
return body;
}

async function startProxy() {
//get variables
let config = getConfig();
async function startProxy(config) {
console.log('config Proxy', config);
//export variables in command line
for (let variable in config) {
process.env[variable] = config[variable];
Expand All @@ -64,11 +63,8 @@ async function startProxy() {
return;
}

async function startOSD() {
//get variables
let config = getConfig();
console.log('config', config)

async function startOSD(config) {
console.log('config', config);
//start osd
var osd = spawn(config.OSD_PATH + '/bin/opensearch-dashboards');

Expand Down
2 changes: 1 addition & 1 deletion src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ <h1>OpenSearch Dashboards Electron</h1>
<form style="margin:0">
<div class="form-group">
<label for="accessKey">Profile</label>
<select type="text" id="awsAccessKeyId" class="form-control">
<select type="text" id="profile" class="form-control">
</select>
<div style="text-align: center">
<a id="addConfig" class="btn btn-primary btn-lg" href="#">+</a>
Expand Down
6 changes: 5 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { app, BrowserWindow, ipcMain } = require('electron')
const path = require('path');

let mainWindow = null;
function createWindow() {
const win = new BrowserWindow({
width: 1600,
Expand All @@ -12,6 +13,7 @@ function createWindow() {

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

function createConfigWindow() {
Expand All @@ -29,7 +31,7 @@ function createConfigWindow() {

async function viewOSD() {
app.whenReady().then(() => {
createWindow();
mainWindow = createWindow();
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
Expand All @@ -53,8 +55,10 @@ ipcMain.handle('openConfig', async (event, name) => {
})

ipcMain.handle('closeWindow', async (event, arg) => {
//handle close window and trigger refresh on main page
let win = BrowserWindow.getFocusedWindow();
win.close();
mainWindow.webContents.send('refresh');
})


Expand Down
12 changes: 8 additions & 4 deletions src/preload.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
const { contextBridge } = require('electron');
const { ipcRenderer } = require("electron");
const dashboards = require('./dashboards');
const configLibrary = require('./config');

contextBridge.exposeInMainWorld(
'electron',
{
ipcRenderer: ipcRenderer,
getConfig: () => dashboards.getConfig(),
setConfig: (key, value, callback) => dashboards.setConfig(key, value, callback),
getConfig: (name) => configLibrary.getConfig(name),
setConfig: (config, callback) => configLibrary.setConfig(config, callback),
getOSDStatus: () => dashboards.getOSDStatus(),
startOSD: () => dashboards.startOSD(),
startProxy: () => dashboards.startProxy()
startOSD: (config) => dashboards.startOSD(config),
startProxy: (config) => dashboards.startProxy(config),
onRefresh: (fn) => {
ipcRenderer.on("refresh", (event, ...args) => fn(...args));
},

}
)
40 changes: 29 additions & 11 deletions src/renderer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
let config = {};
function getMapping() {
return [{id: "awsAccessKeyId", key: "AWS_ACCESS_KEY_ID"},
{id: "awsSecretAccessKey", key: "AWS_SECRET_ACCESS_KEY"},
Expand All @@ -7,20 +8,29 @@ function getMapping() {
}
function init() {
addListeners();
//populateFields();
populateFields();
}

async function populateFields() {
let config = window.config;
let mapping = getMapping();
for (map of mapping) {
let input = document.getElementById(map.id);
input.setAttribute("value", config[map.key])
}
config = window.electron.getConfig();
let profile = document.getElementById("profile");
profile.innerHTML = "";

let options = Object.keys(config).map(function(name) {
return `<option value="${name}">${name}</option>`
});
profile.innerHTML += options.join('');

}

function getActiveConfig() {
let profile = document.getElementById("profile");
var name = profile.options[profile.selectedIndex].value;
console.log('name', name);
return config[name] || null;
}

function addListeners() {
let listeners = getMapping();
let addConfig = document.getElementById("addConfig");
addConfig.addEventListener("click", async (event) => {
event.preventDefault();
Expand All @@ -32,9 +42,13 @@ function addListeners() {

submit.addEventListener("click", async (event) => {
event.preventDefault();
console.log(event);
await window.electron.startProxy();
await window.electron.startOSD();

//get active profile config
let activeConfig = getActiveConfig();
console.log(activeConfig);

await window.electron.startProxy(activeConfig);
await window.electron.startOSD(activeConfig);
waitForServer();
})
}
Expand All @@ -50,6 +64,10 @@ async function waitForServer() {
}
}

window.electron.onRefresh(function (event, args) {
populateFields();
});


init();

0 comments on commit 88e1b8c

Please sign in to comment.