mirror of
https://github.com/node-red/node-red-nodes.git
synced 2023-10-10 13:36:58 +02:00
Support external credentials
Add base node with credentials
This commit is contained in:
parent
8a8b9bf7d8
commit
17a040adfe
@ -16,12 +16,8 @@
|
|||||||
|
|
||||||
<script type="text/x-red" data-template-name="ddb out">
|
<script type="text/x-red" data-template-name="ddb out">
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
<label for="node-input-accessKey"><i class="icon-briefcase"></i> Access Key</label>
|
<label for="node-input-credentials"><i class="icon-tag"></i> Credentials</label>
|
||||||
<input type="text" id="node-input-accessKey" placeholder="Access Key">
|
<input type="text" id="node-input-credentials">
|
||||||
</div>
|
|
||||||
<div class="form-row">
|
|
||||||
<label for="node-input-secretAccessKey"><i class="icon-briefcase"></i> Secret Access Key</label>
|
|
||||||
<input type="text" id="node-input-secretAccessKey" placeholder="Secret Access Key">
|
|
||||||
</div>
|
</div>
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
<label for="node-input-region"><i class="icon-th"></i> Region</label>
|
<label for="node-input-region"><i class="icon-th"></i> Region</label>
|
||||||
@ -55,8 +51,7 @@
|
|||||||
category: "storage-output",
|
category: "storage-output",
|
||||||
color: "#ffaaaa",
|
color: "#ffaaaa",
|
||||||
defaults: {
|
defaults: {
|
||||||
accessKey: { value: "", required: true },
|
credentials: { type: "awscredentials", required: true },
|
||||||
secretAccessKey: { value: "", required: true },
|
|
||||||
region: { value: "us-east-1" },
|
region: { value: "us-east-1" },
|
||||||
table: { value: "", required: true },
|
table: { value: "", required: true },
|
||||||
name: { value: "" }
|
name: { value: "" }
|
||||||
@ -70,3 +65,57 @@
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
<script type="text/x-red" data-template-name="awscredentials">
|
||||||
|
<div class="form-row">
|
||||||
|
<label for="node-config-input-accessKey"><i class="icon-briefcase"></i> Access Key</label>
|
||||||
|
<input type="text" id="node-config-input-accessKey" placeholder="Access Key">
|
||||||
|
</div>
|
||||||
|
<div class="form-row">
|
||||||
|
<label for="node-config-input-secretAccessKey"><i class="icon-briefcase"></i> Secret Access Key</label>
|
||||||
|
<input type="text" id="node-config-input-secretAccessKey" placeholder="Secret Access Key">
|
||||||
|
</div>
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<script type="text/javascript">
|
||||||
|
RED.nodes.registerType("awscredentials", {
|
||||||
|
category: "config",
|
||||||
|
defaults: {
|
||||||
|
accessKey: { value: "", required: true },
|
||||||
|
secretAccessKey: { value: "", required: true }
|
||||||
|
},
|
||||||
|
label: function() {
|
||||||
|
return this.accessKey;
|
||||||
|
},
|
||||||
|
oneditprepare: function() {
|
||||||
|
$.getJSON("awscredentials/" + this.id, function(data) {
|
||||||
|
if (data.accessKey) {
|
||||||
|
$("#node-config-input-accessKey").val(data.accessKey);
|
||||||
|
}
|
||||||
|
if (data.secretAccessKey) {
|
||||||
|
$("#node-config-input-secretAccessKey").val(data.secretAccessKey);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
oneditsave: function() {
|
||||||
|
var newAccessKey = $("#node-config-input-accessKey").val();
|
||||||
|
var newSecretAccessKey = $("#node-config-input-secretAccessKey").val();
|
||||||
|
var credentials = {};
|
||||||
|
credentials.accessKey = newAccessKey;
|
||||||
|
credentials.secretAccessKey = newSecretAccessKey;
|
||||||
|
$.ajax({
|
||||||
|
url: "awscredentials/" + this.id,
|
||||||
|
type: "POST",
|
||||||
|
data: credentials,
|
||||||
|
success: function(result) {}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
ondelete: function() {
|
||||||
|
$.ajax({
|
||||||
|
url: "awscredentials/" + this.id,
|
||||||
|
type: "DELETE",
|
||||||
|
success: function(result) {}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
@ -16,18 +16,65 @@
|
|||||||
|
|
||||||
var RED = require(process.env.NODE_RED_HOME+"/red/red");
|
var RED = require(process.env.NODE_RED_HOME+"/red/red");
|
||||||
var util = require("util");
|
var util = require("util");
|
||||||
|
var querystring = require('querystring');
|
||||||
var aws = require("aws-sdk");
|
var aws = require("aws-sdk");
|
||||||
var attrWrapper = require("dynamodb-data-types").AttributeValue;
|
var attrWrapper = require("dynamodb-data-types").AttributeValue;
|
||||||
|
|
||||||
|
function DDBNode(n) {
|
||||||
|
RED.nodes.createNode(this, n);
|
||||||
|
var credentials = RED.nodes.getCredentials(n.id);
|
||||||
|
if (credentials) {
|
||||||
|
this.accessKey = credentials.accessKey;
|
||||||
|
this.secretAccessKey = credentials.secretAccessKey;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
RED.nodes.registerType("awscredentials", DDBNode);
|
||||||
|
|
||||||
|
RED.app.get('/awscredentials/:id', function(req, res) {
|
||||||
|
var credentials = RED.nodes.getCredentials(req.params.id);
|
||||||
|
if (credentials) {
|
||||||
|
res.send(JSON.stringify({ accessKey: credentials.accessKey, secretAccessKey: credentials.secretAccessKey }));
|
||||||
|
} else {
|
||||||
|
res.send(JSON.stringify({}));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
RED.app.delete('/awscredentials/:id', function(req, res) {
|
||||||
|
RED.nodes.deleteCredentials(req.params.id);
|
||||||
|
res.send(200);
|
||||||
|
});
|
||||||
|
|
||||||
|
RED.app.post('/awscredentials/:id', function(req, res) {
|
||||||
|
var body = "";
|
||||||
|
req.on("data", function(chunk) {
|
||||||
|
body += chunk;
|
||||||
|
});
|
||||||
|
req.on("end", function() {
|
||||||
|
var newCreds = querystring.parse(body);
|
||||||
|
var credentials = RED.nodes.getCredentials(req.params.id) || {};
|
||||||
|
if (newCreds.accessKey == null || newCreds.accessKey == "") {
|
||||||
|
delete credentials.accessKey;
|
||||||
|
} else {
|
||||||
|
credentials.accessKey = newCreds.accessKey || credentials.accessKey;
|
||||||
|
}
|
||||||
|
if (newCreds.secretAccessKey == null || newCreds.secretAccessKey == "") {
|
||||||
|
delete credentials.secretAccessKey;
|
||||||
|
} else {
|
||||||
|
credentials.secretAccessKey = newCreds.secretAccessKey || credentials.secretAccessKey;
|
||||||
|
}
|
||||||
|
RED.nodes.addCredentials(req.params.id, credentials);
|
||||||
|
res.send(200);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
function DDBOutNode(n) {
|
function DDBOutNode(n) {
|
||||||
RED.nodes.createNode(this, n);
|
RED.nodes.createNode(this, n);
|
||||||
this.accessKey = n.accessKey;
|
this.credentials = RED.nodes.getNode(n.credentials);
|
||||||
this.secretAccessKey = n.secretAccessKey;
|
|
||||||
this.region = n.region || "us-east-1";
|
this.region = n.region || "us-east-1";
|
||||||
this.table = n.table;
|
this.table = n.table;
|
||||||
|
|
||||||
aws.config.update({ accessKeyId: this.accessKey,
|
aws.config.update({ accessKeyId: this.credentials.accessKey,
|
||||||
secretAccessKey: this.secretAccessKey,
|
secretAccessKey: this.credentials.secretAccessKey,
|
||||||
region: this.region });
|
region: this.region });
|
||||||
|
|
||||||
var ddb = new aws.DynamoDB();
|
var ddb = new aws.DynamoDB();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user