From 1015b85c2cee1d1603d09b5fb8b2d9716fe6d70c Mon Sep 17 00:00:00 2001 From: Nemo Date: Thu, 18 Apr 2019 20:04:14 +0530 Subject: [PATCH] Initial Commit --- .gitignore | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++ LICENSE | 7 ++++ README.md | 21 ++++++++++++ index.js | 37 +++++++++++++++++++++ package.json | 18 +++++++++++ paths.js | 44 +++++++++++++++++++++++++ test.js | 6 ++++ 7 files changed, 223 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 index.js create mode 100644 package.json create mode 100644 paths.js create mode 100644 test.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2603155 --- /dev/null +++ b/.gitignore @@ -0,0 +1,90 @@ +# Created by https://www.gitignore.io/api/node +# Edit at https://www.gitignore.io/?templates=node + +### Node ### +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +lerna-debug.log* + +# Diagnostic reports (https://nodejs.org/api/report.html) +report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# TypeScript v1 declaration files +typings/ + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env +.env.test + +# parcel-bundler cache (https://parceljs.org/) +.cache + +# next.js build output +.next + +# nuxt.js build output +.nuxt + +# vuepress build output +.vuepress/dist + +# Serverless directories +.serverless/ + +# FuseBox cache +.fusebox/ + +# DynamoDB Local files +.dynamodb/ + +# End of https://www.gitignore.io/api/node diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..b21898b --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright 2019 Abhay Rana + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..42e8283 --- /dev/null +++ b/README.md @@ -0,0 +1,21 @@ +# find-root-ca-cert + +Node package to find the OS-installed root CA certificates. This uses the same location list as go. Based upon this [this answer](https://serverfault.com/a/722646) on ServerFault. + +## Installation + +`npm install --save find-root-ca-cert` + +## Usage + +```js +const certFinder = require("find-root-ca-cert"); +certFinder.findCAPath(); +// /etc/ssl/certs/ca-certificates.crt +certFinder.findCABundle(); +// /etc/ssl/certs/ca-certificates.crt +``` + +## License + +Licensed under the [MIT License](https://nemo.mit-license.org/). See LICENSE file for details. diff --git a/index.js b/index.js new file mode 100644 index 0000000..8a2743b --- /dev/null +++ b/index.js @@ -0,0 +1,37 @@ +const PATHS = require("./paths"); +const fs = require("fs"); + +let accessSyncBool = function(path, mode = fs.constants.R_OK) { + try { + fs.accessSync(path, mode); + return true; + } catch (err) { + return false; + } +}; + +let findCABundle = () => + PATHS.FILES.find( + e => + fs.existsSync(e) && + fs.existsSync(fs.realpathSync(e)) && + accessSyncBool(fs.realpathSync(e), fs.constants.R_OK) && + fs.statSync(fs.realpathSync(e)).isFile() + ); + +let findCAPath = () => + PATHS.DIRECTORIES.find( + e => + fs.existsSync(e) && + fs.existsSync(fs.realpathSync(e)) && + accessSyncBool( + fs.realpathSync(e), + fs.constants.R_OK | fs.constants.X_OK + ) && + fs.statSync(fs.realpathSync(e)).isDirectory() + ); + +module.exports = { + findCAPath: findCAPath, + findCABundle: findCABundle +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..c3ca035 --- /dev/null +++ b/package.json @@ -0,0 +1,18 @@ +{ + "name": "find-root-ca-cert", + "version": "1.0.0", + "description": "Finds the first available local CA certificate on Linux machines", + "main": "index.js", + "scripts": { + "test": "node test.js" + }, + "keywords": [ + "ca", + "root", + "root-ca", + "find", + "pem" + ], + "author": "Nemo ", + "license": "MIT" +} diff --git a/paths.js b/paths.js new file mode 100644 index 0000000..bb2d325 --- /dev/null +++ b/paths.js @@ -0,0 +1,44 @@ +// Based on https://golang.org/src/crypto/x509/root_linux.go + +const FILES = [ + // Debian/Ubuntu/Gentoo etc. + "/etc/ssl/certs/ca-certificates.crt", + + // Fedora/RHEL 6 + "/etc/pki/tls/certs/ca-bundle.crt", + + // OpenSUSE + "/etc/ssl/ca-bundle.pem", + + // OpenELEC + "/etc/pki/tls/cacert.pem", + + // CentOS/RHEL 7 + "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem" +]; + +// Based on https://golang.org/src/crypto/x509/root_unix.go +const DIRECTORIES = [ + // SLES10/SLES11, https://golang.org/issue/12139 + "/etc/ssl/certs", + + // Android + "/system/etc/security/cacerts", + + // FreeBSD + "/usr/local/share/certs", + + // Fedora/RHEL + "/etc/pki/tls/certs", + + // NetBSD + "/etc/openssl/certs", + + // AIX + "/var/ssl/certs" +]; + +module.exports = { + FILES: FILES, + DIRECTORIES: DIRECTORIES +}; diff --git a/test.js b/test.js new file mode 100644 index 0000000..4af80bd --- /dev/null +++ b/test.js @@ -0,0 +1,6 @@ +const find = require("./index"); +const assert = require("assert"); + +// This should work on most linux distros +assert(find.findCAPath() === "/etc/ssl/certs"); +assert(find.findCABundle() === "/etc/ssl/certs/ca-certificates.crt");