Skip to content

Commit 460ed9e

Browse files
committed
Tests with mocha now works. Added minimal documentation
1 parent 47585a1 commit 460ed9e

5 files changed

Lines changed: 45 additions & 46 deletions

File tree

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Consul TLS manager v1.0.0-beta
2+
Simple NodeJS script to store TLS certificates and keys to a Consul KV and
3+
restore to disk if modified since last check
4+
5+
6+
```bash
7+
$ ./index.js --help
8+
9+
Usage: index [options]
10+
11+
Options:
12+
13+
-h, --help output usage information
14+
-V, --version output the version number
15+
-g, --get Get Value (default)
16+
-s, --set Set value
17+
-u, --url Consul Host
18+
-f, --fqdn [value] FQDN of key certificate, without last dot e.g. mydomain.com
19+
-c, --cert [value] Path to certificate (e.g. ./folder/mydomain.crt)
20+
-k, --key [value] Path to certificate Key (e.g. ./folder/mydomain.key)
21+
-v, --debug Enable verbose / debug
22+
```

index.js

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,35 +25,6 @@ program
2525
//.option('-c, --cheese [type]', 'Add the specified type of cheese [marble]', 'marble')
2626
.parse(process.argv);
2727

28-
//console.log('debug:');
29-
//if (program.get) {
30-
// console.log(' - get');
31-
//}
32-
//
33-
//if (program.set) {
34-
// console.log(' - set');
35-
//}
36-
//
37-
//if (program.url) {
38-
// console.log(' - url');
39-
//}
40-
//
41-
//if (program.fqdn) {
42-
// console.log(' - fqdn');
43-
//}
44-
//
45-
//if (program.cert) {
46-
// console.log(' - cert');
47-
//}
48-
//
49-
//if (program.key) {
50-
// console.log(' - key');
51-
//}
52-
//console.log(' - %s set', program.set);
53-
//console.log(' - %s url', program.url);
54-
//console.log(' - %s fqdn', program.fqdn);
55-
56-
5728
if (program.debug) {
5829
consulTls.init({debug: true});
5930
}
@@ -64,14 +35,14 @@ if (program.set) {
6435
console.log('ERROR:', err);
6536
return 1;
6637
}
67-
console.log(result);
38+
return 0;
6839
}, program.url, program.fqdn, program.cert, program.key);
69-
} else {
40+
} else if (program.get) {
7041
consulTls.executeRestore(function (err, result) {
7142
if (err) {
7243
console.log('ERROR:', err);
7344
return 1;
7445
}
75-
console.log(result);
46+
return 0;
7647
}, program.url, program.fqdn, program.cert, program.key);
7748
}

lib/consul-tls.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ var consulTls = {
106106
{form: JSON.stringify(data)},
107107
function (error, response, body) {
108108
if (!error && response.statusCode === 200) {
109-
console.log(body);
109+
//console.log(body);
110110
cb(null);
111111
} else {
112112
cb({error: response.statusCode, message: error});
@@ -133,7 +133,7 @@ var consulTls = {
133133
};
134134
request.put(options, function (error, response, body) {
135135
if (!error && response.statusCode === 200) {
136-
console.log(body);
136+
//console.log(body);
137137
cb(null);
138138
} else {
139139
cb({error: response.statusCode, message: error});

test-mocha.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,32 @@
33
* @todo Finish tests with mocha and remove actual test.js
44
*/
55

6-
var exec = require('child_process').exec;
6+
var exec = require('child_process').exec, fs = require('fs');
77
var assert = require('chai').assert;
88

99
describe('Store and Restore TLS cert and keys from Consul KV', function () {
10-
11-
var cmd = './index.js --set --url http://localhost:8500 --fqdn=localhost --cert ./test/localhost.crt --key ./test/localhost.key';
10+
var cmd = 'node ./index.js --set --url http://localhost:8500 --fqdn=localhost --cert ./test/localhost.crt --key ./test/localhost.key';
1211
//console.log('================================================================================');
1312
//console.log('TEST: set');
1413
//console.log(cmd);
1514

16-
it('should save on consul KV', function () {
15+
it('should save on consul KV', function (done) {
1716
exec(cmd, function (error, stdout, stderr) {
1817
if (error) {
1918
console.log('error', error);
2019
}
2120
if (stderr) {
2221
console.log('stderr', stderr);
2322
}
24-
assert(!!error);
25-
done();
26-
23+
assert(!error);
2724
console.log(stdout);
25+
done();
2826
});
2927
});
3028

31-
it('should restore from consul KV to disk', function () {
32-
var cmd = './index.js --debug --get --url http://localhost:8500 --fqdn=localhost --cert ./test/result-localhost.crt --key ./test/result-localhost.key';
29+
it('should restore from consul KV to disk', function (done) {
30+
//var cmd = 'node ./index.js --debug --get --url http://localhost:8500 --fqdn=localhost --cert ./test/result-localhost.crt --key ./test/result-localhost.key';
31+
var cmd = 'node ./index.js --get --url http://localhost:8500 --fqdn=localhost --cert ./test/result-localhost.crt --key ./test/result-localhost.key';
3332
//console.log('================================================================================');
3433
//console.log('TEST: get');
3534

@@ -40,15 +39,18 @@ describe('Store and Restore TLS cert and keys from Consul KV', function () {
4039
if (stderr) {
4140
console.log('stderr', stderr);
4241
}
43-
assert(!!error);
42+
assert(!error);
4443
console.log(stdout);
4544

4645
done();
4746
});
4847
});
4948

50-
it('files should be equal', function () {
51-
49+
it('files should be equal: certificate', function () {
50+
assert(fs.readFileSync('./test/localhost.crt', 'utf8') === fs.readFileSync('./test/result-localhost.crt', 'utf8'));
51+
});
52+
it('files should be equal: key', function () {
53+
assert(fs.readFileSync('./test/localhost.key', 'utf8') === fs.readFileSync('./test/result-localhost.key', 'utf8'));
5254
});
5355
});
5456

test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#!/usr/bin/env node
22

3+
/**
4+
* @deprecated file, use test-mocha.js
5+
*/
6+
37
var exec = require('child_process').exec, fs = require('fs');
48

59
var cmd = './index.js --set --url http://localhost:8500 --fqdn=localhost --cert ./test/localhost.crt --key ./test/localhost.key';

0 commit comments

Comments
 (0)