more stuff here on creating and removing keys

This commit is contained in:
Aaron Meihm 2017-05-03 12:59:34 -05:00
Родитель 09787dd44c
Коммит 9fc59416ff
3 изменённых файлов: 217 добавлений и 7 удалений

151
main.go
Просмотреть файл

@ -13,6 +13,7 @@ import (
"net/http"
"os"
"regexp"
"strconv"
"strings"
"github.com/go-yaml/yaml"
@ -27,6 +28,7 @@ type config struct {
APIUrl string
APIKey string
SkipVerifyCert bool
ExpectEnv string
}
var cfg config
@ -35,10 +37,20 @@ type remoteUserType int
const remoteUser remoteUserType = 0
// Response to a key status request
type loadersReply struct {
Loaders []mig.LoaderEntry `json:"loaders"`
}
// Payload submitted for a new key request
type newkeyRequest struct {
SlotID string `json:"slot"`
}
func (n *newkeyRequest) validate() error {
return nil
}
type requestDetails struct {
remoteUser string
loaders []mig.LoaderEntry
@ -48,6 +60,20 @@ func (r *requestDetails) searchUserString() string {
return "migss-" + r.remoteUser + "-%"
}
func (r *requestDetails) convertSlotID(slotid string) (string, error) {
ret := "migss-" + r.remoteUser + "-"
sv := strings.Replace(slotid, "slot", "", 1)
svint, err := strconv.ParseInt(sv, 10, 64)
if err != nil {
return "", err
}
if (svint < 1) || (svint > 3) {
return "", fmt.Errorf("invalid slot id")
}
ret = ret + sv
return ret, nil
}
func (r *requestDetails) addKeys(cli client.Client) error {
p := migdbsearch.NewParameters()
p.Type = "loader"
@ -139,6 +165,129 @@ func handleKeyStatus(rw http.ResponseWriter, req *http.Request) {
fmt.Fprint(rw, string(buf))
}
func handleNewKey(rw http.ResponseWriter, req *http.Request) {
var (
newkey newkeyRequest
le mig.LoaderEntry
)
rdetails, err := newRequestDetails(req)
if err != nil {
http.Error(rw, err.Error(), 500)
}
decoder := json.NewDecoder(req.Body)
defer req.Body.Close()
err = decoder.Decode(&newkey)
if err != nil {
http.Error(rw, err.Error(), 500)
}
cli, err := newMIGClient()
if err != nil {
http.Error(rw, err.Error(), 500)
}
// Add any existing loader entries for this user to rdetails
err = rdetails.addKeys(cli)
le.Name, err = rdetails.convertSlotID(newkey.SlotID)
if err != nil {
http.Error(rw, err.Error(), 500)
}
le.ExpectEnv = cfg.ExpectEnv
// At this point the loader is ready to be created, but first check and see if an
// entry for this slot already exists. If so we will enable and rekey this entry
// rather than create it.
var newle mig.LoaderEntry
found := false
for _, x := range rdetails.loaders {
if x.Name == le.Name {
newle = x
found = true
break
}
}
if found {
err = cli.LoaderEntryStatus(newle, true)
if err != nil {
http.Error(rw, err.Error(), 500)
return
}
newle, err = cli.LoaderEntryKey(newle)
if err != nil {
http.Error(rw, err.Error(), 500)
return
}
} else {
newle, err = cli.PostNewLoader(le)
if err != nil {
http.Error(rw, err.Error(), 500)
}
// Also enable the new loader entry
err = cli.LoaderEntryStatus(newle, true)
if err != nil {
http.Error(rw, err.Error(), 500)
}
}
buf, err := json.Marshal(&newle)
if err != nil {
http.Error(rw, err.Error(), 500)
}
rw.Header().Set("Content-Type", "application/json")
fmt.Fprint(rw, string(buf))
}
func handleDelKey(rw http.ResponseWriter, req *http.Request) {
var (
newkey newkeyRequest
le mig.LoaderEntry
)
rdetails, err := newRequestDetails(req)
if err != nil {
http.Error(rw, err.Error(), 500)
}
decoder := json.NewDecoder(req.Body)
defer req.Body.Close()
err = decoder.Decode(&newkey)
fmt.Printf("%+v\n", newkey)
if err != nil {
http.Error(rw, err.Error(), 500)
}
cli, err := newMIGClient()
if err != nil {
http.Error(rw, err.Error(), 500)
}
err = rdetails.addKeys(cli)
if err != nil {
http.Error(rw, err.Error(), 500)
}
le.Name, err = rdetails.convertSlotID(newkey.SlotID)
if err != nil {
http.Error(rw, err.Error(), 500)
}
// We need the loader ID to change the status of the entry, locate the ID
// in rdetails based on our loader name and add it to the request
found := false
for _, x := range rdetails.loaders {
if x.Name == le.Name {
le.ID = x.ID
found = true
break
}
}
if !found {
http.Error(rw, "unable to locate loader ID for slot", 500)
}
err = cli.LoaderEntryStatus(le, false)
if err != nil {
http.Error(rw, err.Error(), 500)
}
fmt.Printf("%+v\n", le)
fmt.Println("REMOVE")
}
func handlePing(rw http.ResponseWriter, req *http.Request) {
fmt.Fprint(rw, "pong\n")
}
@ -187,6 +336,8 @@ func main() {
r.HandleFunc("/ping", handlePing).Methods("GET")
r.HandleFunc("/", setContext(handleMain)).Methods("GET")
r.HandleFunc("/keystatus", setContext(handleKeyStatus)).Methods("GET")
r.HandleFunc("/newkey", setContext(handleNewKey)).Methods("POST")
r.HandleFunc("/delkey", setContext(handleDelKey)).Methods("POST")
sp := http.StripPrefix("/static/", http.FileServer(http.Dir("./static/")))
r.PathPrefix("/static").Handler(sp)

Просмотреть файл

@ -24,13 +24,13 @@ var mainTmpl = `<html>
<table>
<thead>
<tr>
<td>Device slot</td><td>Assigned key<td><td>Action</td>
<td>Device slot</td><td>Assigned key<td><td>Action</td><td>In use?</td>
</tr>
</thead>
<tbody>
<tr id="slot1"><td>1</td><td>Loading</td><td>Loading</td></tr>
<tr id="slot2"><td>2</td><td>Loading</td><td>Loading</td></tr>
<tr id="slot3"><td>3</td><td>Loading</td><td>Loading</td></tr>
<tr id="slot1"><td>1</td><td>Loading</td><td>Loading</td><td>Loading</td></tr>
<tr id="slot2"><td>2</td><td>Loading</td><td>Loading</td><td>Loading</td></tr>
<tr id="slot3"><td>3</td><td>Loading</td><td>Loading</td><td>Loading</td></tr>
</tbody>
</table>
</div>

Просмотреть файл

@ -1,15 +1,29 @@
function ldrToSlot(ldrname) {
ri = ldrname.lastIndexOf("-");
slotval = ldrname.slice(ri + 1);
slotnum = parseInt(slotval);
if (isNaN(slotnum)) {
return undefined;
}
return "slot" + slotnum
}
function keyParser(data) {
for (var i = 1; i < 4; i++) {
var found = false;
for (var j = 0; j < data.loaders.length; j++) {
ldr = data.loaders[j];
console.log(ldr);
if (ldr["enabled"] == false) {
continue;
}
ri = ldr["name"].lastIndexOf("-");
slotval = ldr["name"].slice(ri + 1);
if (parseInt(slotval) == i) {
var slotid = "slot" + i;
t = $("#" + slotid).find("td");
t.eq(1).html("Assigned");
t.eq(2).html("Remove");
t.eq(2).html("<a id=\"" + slotid + "\" href=\"#\">Remove</a>").on("click.rem", removeFunc(slotid));
found = true;
break;
}
@ -20,14 +34,59 @@ function keyParser(data) {
var slotid = "slot" + i;
t = $("#" + slotid).find("td");
t.eq(1).html("Not set");
t.eq(2).html("Generate key");
t.eq(2).html("<a id=\"" + slotid + "\" href=\"#\">Generate key</a>").on("click.gen", generateFunc(slotid));
}
}
function removeFunc(slotid) {
return function() {
$.ajax({
url: "/delkey",
type: "post",
dataType: "text",
contentType: "application/json",
data: JSON.stringify({ "slot": slotid }),
success: loadKeys,
error: function(xhr, status, error) {
alert(error);
}
});
}
}
function generateFunc(slotid) {
return function() {
$.ajax({
url: "/newkey",
type: "post",
dataType: "json",
contentType: "application/json",
data: JSON.stringify({ "slot": slotid }),
success: showInitialKey,
error: function(xhr, status, error) {
alert(error);
}
});
}
}
function showInitialKey(data, textstat, xhr) {
var slotid = ldrToSlot(data["name"]);
var keyval = data["prefix"] + data["key"];
t = $("#" + slotid).find("td");
t.eq(1).html(keyval);
t.eq(2).html("Created");
}
function loadKeys() {
for (var i = 1; i < 4; i++) {
var slotid = "slot" + i;
$("#" + slotid).find("td").eq(2).off("click.gen");
$("#" + slotid).find("td").eq(2).off("click.rem");
}
$.ajax({url: "/keystatus", success: keyParser});
}
document.addEventListener("DOMContentLoaded", function() {
$(document).ready(function() {
loadKeys();
});