Initial Commit

This commit is contained in:
Nemo 2019-04-18 20:04:14 +05:30
commit 1015b85c2c
7 changed files with 223 additions and 0 deletions

90
.gitignore vendored Normal file
View File

@ -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

7
LICENSE Normal file
View File

@ -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.

21
README.md Normal file
View File

@ -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.

37
index.js Normal file
View File

@ -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
};

18
package.json Normal file
View File

@ -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 <me@captnemo.in>",
"license": "MIT"
}

44
paths.js Normal file
View File

@ -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
};

6
test.js Normal file
View File

@ -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");