| 
									
										
										
										
											2015-12-15 15:21:27 +00:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var fs = require('fs'); | 
					
						
							|  |  |  | var PromiseA = require('bluebird'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-17 08:46:40 +00:00
										 |  |  | module.exports.tplConfigDir = function merge(configDir, defaults) { | 
					
						
							|  |  |  |   Object.keys(defaults).forEach(function (key) { | 
					
						
							|  |  |  |     if ('string' === typeof defaults[key]) { | 
					
						
							|  |  |  |       defaults[key] = defaults[key].replace(':config', configDir).replace(':conf', configDir); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2015-12-17 05:44:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-17 08:46:40 +00:00
										 |  |  | module.exports.merge = function merge(defaults, args) { | 
					
						
							|  |  |  |   var copy = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Object.keys(defaults).forEach(function (key) { | 
					
						
							|  |  |  |     copy[key] = defaults[key]; | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   Object.keys(args).forEach(function (key) { | 
					
						
							|  |  |  |     copy[key] = args[key]; | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return copy; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports.tplHostname = function merge(hostname, copy) { | 
					
						
							|  |  |  |   Object.keys(copy).forEach(function (key) { | 
					
						
							|  |  |  |     if ('string' === typeof copy[key]) { | 
					
						
							|  |  |  |       copy[key] = copy[key].replace(':hostname', hostname).replace(':host', hostname); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   //return copy;
 | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports.fetchFromDisk = function (args) { | 
					
						
							|  |  |  |   // TODO NO HARD-CODED DEFAULTS
 | 
					
						
							|  |  |  |   if (!args.fullchainPath || !args.privkeyPath || !args.certPath || !args.chainPath) { | 
					
						
							|  |  |  |     console.warn("missing one or more of args.privkeyPath, args.fullchainPath, args.certPath, args.chainPath"); | 
					
						
							|  |  |  |     console.warn("hard-coded conventional pathnames were for debugging and are not a stable part of the API"); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-12-15 15:21:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-17 05:44:41 +00:00
										 |  |  |   //, fs.readFileAsync(fullchainPath, 'ascii')
 | 
					
						
							| 
									
										
										
										
											2015-12-17 08:46:40 +00:00
										 |  |  |   // note: if this ^^ gets added back in, the arrays below must change
 | 
					
						
							|  |  |  |   return PromiseA.all([ | 
					
						
							|  |  |  |     fs.readFileAsync(args.privkeyPath, 'ascii')   // 0
 | 
					
						
							|  |  |  |   , fs.readFileAsync(args.certPath, 'ascii')      // 1
 | 
					
						
							|  |  |  |   , fs.readFileAsync(args.chainPath, 'ascii')     // 2
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 15:21:27 +00:00
										 |  |  |     // stat the file, not the link
 | 
					
						
							| 
									
										
										
										
											2015-12-17 08:46:40 +00:00
										 |  |  |   , fs.statAsync(args.certPath)                   // 3
 | 
					
						
							| 
									
										
										
										
											2015-12-15 15:21:27 +00:00
										 |  |  |   ]).then(function (arr) { | 
					
						
							| 
									
										
										
										
											2015-12-17 08:46:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 15:21:27 +00:00
										 |  |  |     return { | 
					
						
							| 
									
										
										
										
											2015-12-17 05:44:41 +00:00
										 |  |  |       key: arr[0]                           // privkey.pem
 | 
					
						
							| 
									
										
										
										
											2015-12-17 08:46:40 +00:00
										 |  |  |     , privkey: arr[0]                       // privkey.pem
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     , fullchain: arr[1] + '\n' + arr[2]     // fullchain.pem
 | 
					
						
							| 
									
										
										
										
											2015-12-17 05:44:41 +00:00
										 |  |  |     , cert: arr[1]                          // cert.pem
 | 
					
						
							| 
									
										
										
										
											2015-12-17 08:46:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-17 05:44:41 +00:00
										 |  |  |     , chain: arr[2]                         // chain.pem
 | 
					
						
							| 
									
										
										
										
											2015-12-17 08:46:40 +00:00
										 |  |  |     , ca: arr[2]                            // chain.pem
 | 
					
						
							| 
									
										
										
										
											2015-12-17 05:44:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-17 09:05:18 +00:00
										 |  |  |     , privkeyPath: args.privkeyPath | 
					
						
							|  |  |  |     , fullchainPath: args.fullchainPath | 
					
						
							|  |  |  |     , certPath: args.certPath | 
					
						
							|  |  |  |     , chainPath: args.chainPath | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-17 08:46:40 +00:00
										 |  |  |     , issuedAt: arr[3].mtime.valueOf()      // ??? TODO parse to determine expiresAt and lifetime
 | 
					
						
							| 
									
										
										
										
											2015-12-17 09:05:18 +00:00
										 |  |  |     , lifetime: args.lifetime | 
					
						
							| 
									
										
										
										
											2015-12-15 15:21:27 +00:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2015-12-17 08:46:40 +00:00
										 |  |  |   }, function (err) { | 
					
						
							| 
									
										
										
										
											2015-12-17 09:17:27 +00:00
										 |  |  |     if (args.debug) { | 
					
						
							| 
									
										
										
										
											2015-12-17 08:46:40 +00:00
										 |  |  |       console.error(err.stack); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-12-15 15:21:27 +00:00
										 |  |  |     return null; | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }; |