Skip to content

Commit 91dfea7

Browse files
committed
adding keystores capability to the library
1 parent 4dd2d2e commit 91dfea7

File tree

9 files changed

+674
-33
lines changed

9 files changed

+674
-33
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ The callback will return (e, org), where e is an error, possibly null, and org i
8484
| flowhooks | get, put |
8585
| products | get, create, del |
8686
| developers | get, create, del |
87+
| keystores | get, create, del, import key and cert |
8788
| developerapps | get, create, del |
8889
| appcredentials | add, del |
8990

@@ -211,6 +212,27 @@ org.proxies.get({}, function(e, proxies) {
211212
});
212213
```
213214
215+
### Create a Keystore and load a Key and Cert
216+
217+
```
218+
var options = {
219+
environment : 'test',
220+
name : 'keystore1'
221+
};
222+
org.keystores.create(options, function(e, result){
223+
if (e) { ... }
224+
console.log('ok. created');
225+
options.certFile = './mycert.cert';
226+
options.keyFile = './mykey.pem';
227+
options.alias = 'alias1';
228+
options.keyPassword = 'optional password for key file';
229+
org.keystores.importCert(options, function(e, result){
230+
if (e) { ... }
231+
console.log('ok. key and cert stored.');
232+
});
233+
});
234+
```
235+
214236
### More Examples
215237
216238
See [the examples directory](./examples) for a set of working example tools.

examples/createKeystore.js

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#! /usr/local/bin/node
2+
/*jslint node:true */
3+
// createKeystore.js
4+
// ------------------------------------------------------------------
5+
// provision a keystore with a key and cert in Apigee Edge
6+
// ex:
7+
// node ./createKeystore.js -v -n -o amer-demo4 -s ks1 -e test -k ./dchiesa.net.key -c ./dchiesa.net.cert -a alias1
8+
//
9+
// Copyright 2017-2018 Google Inc.
10+
//
11+
// Licensed under the Apache License, Version 2.0 (the "License");
12+
// you may not use this file except in compliance with the License.
13+
// You may obtain a copy of the License at
14+
//
15+
// https://www.apache.org/licenses/LICENSE-2.0
16+
//
17+
// Unless required by applicable law or agreed to in writing, software
18+
// distributed under the License is distributed on an "AS IS" BASIS,
19+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20+
// See the License for the specific language governing permissions and
21+
// limitations under the License.
22+
//
23+
// last saved: <2018-April-02 17:16:46>
24+
25+
const edgejs = require('apigee-edge-js'),
26+
fs = require('fs'),
27+
common = edgejs.utility,
28+
apigeeEdge = edgejs.edge,
29+
sprintf = require('sprintf-js').sprintf,
30+
Getopt = require('node-getopt'),
31+
version = '20180402-1656',
32+
getopt = new Getopt(common.commonOptions.concat([
33+
['s' , 'keystore=ARG', 'required. name of the keystore to create'],
34+
['k' , 'keyfile=ARG', 'required. path to the key file (PEM format)'],
35+
['c' , 'certfile=ARG', 'required. path to the cert file'],
36+
['e' , 'environment=ARG', 'required. environment in which the keystore will be created'],
37+
['a' , 'alias=ARG', 'required. alias for the key'],
38+
['P' , 'keypassword=ARG', 'optional. password for the RSA Key']
39+
])).bindHelp();
40+
41+
// ========================================================
42+
43+
console.log(
44+
'Apigee Edge Keystore creation tool, version: ' + version + '\n' +
45+
'Node.js ' + process.version + '\n');
46+
47+
common.logWrite('start');
48+
49+
// process.argv array starts with 'node' and 'scriptname.js'
50+
var opt = getopt.parse(process.argv.slice(2));
51+
52+
if ( !opt.options.environment ) {
53+
console.log('You must specify an environment');
54+
getopt.showHelp();
55+
process.exit(1);
56+
}
57+
58+
if ( !opt.options.keystore ) {
59+
console.log('You must specify a keystore');
60+
getopt.showHelp();
61+
process.exit(1);
62+
}
63+
64+
if ( !opt.options.keyfile || !fs.existsSync(opt.options.keyfile) ) {
65+
console.log('You must specify a path to a key file');
66+
getopt.showHelp();
67+
process.exit(1);
68+
}
69+
if ( !opt.options.certfile || !fs.existsSync(opt.options.certfile) ) {
70+
console.log('You must specify a path to a cert file');
71+
getopt.showHelp();
72+
process.exit(1);
73+
}
74+
75+
if ( !opt.options.alias ) {
76+
console.log('You must specify an alias');
77+
getopt.showHelp();
78+
process.exit(1);
79+
}
80+
81+
common.verifyCommonRequiredParameters(opt.options, getopt);
82+
83+
var options = {
84+
mgmtServer: opt.options.mgmtserver,
85+
org : opt.options.org,
86+
user: opt.options.username,
87+
password: opt.options.password,
88+
no_token: opt.options.notoken,
89+
verbosity: opt.options.verbose || 0
90+
};
91+
92+
apigeeEdge.connect(options, function(e, org) {
93+
if (e) {
94+
common.logWrite(JSON.stringify(e, null, 2));
95+
common.logWrite(JSON.stringify(result, null, 2));
96+
process.exit(1);
97+
}
98+
common.logWrite('connected');
99+
100+
var options = {
101+
environment : opt.options.environment,
102+
name : opt.options.keystore
103+
};
104+
org.keystores.create(options, function(e, result){
105+
if (e) {
106+
common.logWrite(JSON.stringify(e, null, 2));
107+
common.logWrite(JSON.stringify(result, null, 2));
108+
//console.log(e.stack);
109+
process.exit(1);
110+
}
111+
common.logWrite('ok. created');
112+
options.certFile = opt.options.certfile;
113+
options.keyFile = opt.options.keyfile;
114+
options.alias = opt.options.alias;
115+
if (opt.options.keypassword) {
116+
options.keyPassword = opt.options.keypassword;
117+
}
118+
org.keystores.importCert(options, function(e, result){
119+
if (e) {
120+
common.logWrite(JSON.stringify(e, null, 2));
121+
common.logWrite(JSON.stringify(result, null, 2));
122+
//console.log(e.stack);
123+
process.exit(1);
124+
}
125+
common.logWrite('ok. key and cert stored.');
126+
});
127+
});
128+
});

examples/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "apigee-edge-js-examples",
3-
"version": "0.1.9",
3+
"version": "0.1.10",
44
"description": "Examples for using the Apigee Edge nodeJS library",
55
"main": "importAndDeploy.js",
66
"scripts": {
@@ -9,7 +9,7 @@
99
"author": "[email protected]",
1010
"license": "Apache-2.0",
1111
"dependencies": {
12-
"apigee-edge-js": "^0.2.28",
12+
"apigee-edge-js": "^0.2.29",
1313
"async": "^2.2.0",
1414
"merge": "^1.2.0",
1515
"mkdirp": "^0.5.1",

0 commit comments

Comments
 (0)