Skip to content

Commit

Permalink
credentials manager initial poc (#24)
Browse files Browse the repository at this point in the history
* credentials manager initial poc

Signed-off-by: Johnny On <[email protected]>

* remove console.logs

Signed-off-by: Johnny On <[email protected]>

* remove empty line

Signed-off-by: Johnny On <[email protected]>

* add apache license

Signed-off-by: Johnny On <[email protected]>

* profile as a const

Signed-off-by: Johnny On <[email protected]>
  • Loading branch information
johnnyon-amzn authored May 19, 2022
1 parent 8733807 commit 90a75e0
Show file tree
Hide file tree
Showing 12 changed files with 365 additions and 82 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.
42 changes: 42 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
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());
if (name) {
return config[name] || {};
} else {
return config;
}
}

function setConfig(config, callback, configPath=CONFIG_PATH) {
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;
74 changes: 74 additions & 0 deletions src/configManager.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>OpenSearch Dashboards | Configuration</title>
<link rel="stylesheet" href="./css/bootstrap.min.css"/>
<style>
label {
font-weight: bold
}
input[type="radio"] {
margin-left: 12px;
margin-right: 6px;
font-weight: 800;
}

.hidden {
display: none;
visibility: hidden
}
label {
margin-top: 16px;
}
</style>
</head>
<body>

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

<div style="width:80%; margin-left:10%; margin-top: 30px; margin-bottom: 0">
<div id="error" class="alert alert-danger hidden">
<strong>Error!</strong> <span id="error-message"/>
</div>
<div id="warn" class="alert alert-warning hidden" role="alert">
<strong>Warning!</strong> <span id="warn-message"/>
</div>
<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>

<label for="secretAccessKey">AWS SECRET ACCESS KEY</label>
<input type="text" id="awsSecretAccessKey" class="form-control">
</input>
<label for="endpoint">ENDPOINT</label>
<input type="text" id="endpoint" class="form-control">
</input>
<label for="osdPath">Path to OpenSearch Dashboards</label>
<input type="text" id="osdPath" class="form-control">
</input>
</div>
</form>
</div>

<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="#">SAVE</a>
</div>
</div>
</div>
</div>
<script src="./configRenderer.js"></script>
</body>


</html>
34 changes: 34 additions & 0 deletions src/configPreload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/

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) => configLibrary.getConfig(name),
setConfig: (config, callback) => configLibrary.setConfig(config, callback),
}
)
69 changes: 69 additions & 0 deletions src/configRenderer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
function init() {
addListeners();
}

// store name variable if exists
window.electron.onName(function (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();
let newConfig = getNewConfig();
window.electron.setConfig(newConfig, closeWindow);
})
}

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

init();

60 changes: 21 additions & 39 deletions src/dashboards.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@


/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
const { BrowserWindow } = require('electron')
const { spawn, exec } = require('child_process');
const fs = require("fs");
Expand All @@ -9,29 +25,11 @@ 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 {
const osStatus = await apiRequest('localhost:9200');
const osdStatus = await apiRequest('localhost:5601/api/status');
console.log('osStatus', osStatus)
console.log('osdStatus', osdStatus);
if (osStatus.name) {
statuses["os"] = "green"
}
Expand All @@ -40,7 +38,6 @@ async function getOSDStatus() {
statuses["osd"] = osdStatus.status.overall.state;
}
} catch (e) {
console.log('e', e);
}

return statuses;
Expand All @@ -54,9 +51,7 @@ async function getOSDStatus() {
return body;
}

async function startProxy() {
//get variables
let config = getConfig();
async function startProxy(config) {
//export variables in command line
for (let variable in config) {
process.env[variable] = config[variable];
Expand All @@ -67,45 +62,32 @@ async function startProxy() {

proxy.stdout.on('data', function(data) {
data = JSON.parse(data.toString());
//console.log('stdout: ' + JSON.stringify(data));
})

proxy.stderr.on('data', function(data) {
//console.log('stderr: ' + data.toString());
})

proxy.on('exit', function(code) {
console.log('child process exited with code ' + code.toString());
})
return;
}

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

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

osd.stdout.on('data', function(data) {
data = JSON.parse(data.toString());
console.log('stdout: ' + JSON.stringify(data));
})

osd.stderr.on('data', function(data) {

console.log('stderr: ' + data.toString());
})

osd.on('exit', function(code) {
console.log('child process exited with code ' + code.toString());

})
}

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

exports.startOSD = startOSD;
exports.getOSDStatus = getOSDStatus;
23 changes: 10 additions & 13 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,19 @@ <h1>OpenSearch Dashboards Electron</h1>
<div id="warn" class="alert alert-warning hidden" role="alert">
<strong>Warning!</strong> <span id="warn-message"/>
</div>
<div style="margin:0">
<div style="margin:0">
<form style="margin:0">
<div class="form-group">
<label for="accessKey">AWS ACCESS KEY ID</label>
<input type="text" id="awsAccessKeyId" class="form-control">
</input>
<label for="accessKey">Profile</label>
<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>
</div>

<label for="secretAccessKey">AWS SECRET ACCESS KEY</label>
<input type="text" id="awsSecretAccessKey" class="form-control">
</input>
<label for="endpoint">ENDPOINT</label>
<input type="text" id="endpoint" class="form-control">
</input>
<label for="osdPath">Path to OpenSearch Dashboards</label>
<input type="text" id="osdPath" class="form-control">
</input>
<div style="text-align: center">
<a id="editConfig" class="btn btn-primary btn-lg" href="#">Edit</a>
</div>
</div>
</form>
</div>
Expand Down
Loading

0 comments on commit 90a75e0

Please sign in to comment.