|
| 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 | +}); |
0 commit comments