-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhancement: Don't use external curl command to do http requests
- Loading branch information
Showing
8 changed files
with
200 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
//go:build !windows | ||
|
||
package certStore | ||
|
||
import ( | ||
"crypto/x509" | ||
_ "unsafe" | ||
) | ||
|
||
// TODO: Check on every minor version release if there is a better way to do it, or, at least, it is compatible | ||
|
||
//go:linkname systemRoots crypto/x509.systemRoots | ||
var systemRoots *x509.CertPool | ||
|
||
func ReloadSystemCertificates() { | ||
systemRoots, _ = loadSystemRoots() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package certStore | ||
|
||
// ReloadSystemCertificates No need to reload certificates as the validity is checked by the OS | ||
func ReloadSystemCertificates() { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Source: https://github.com/golang/go/blob/master/src/crypto/x509/root_linux.go | ||
// TODO: Check on every minor version release if there is a better way to do it, or, at least, update it | ||
|
||
package certStore | ||
|
||
// Possible certificate files; stop after finding one. | ||
var certFiles = []string{ | ||
"/etc/ssl/certs/ca-certificates.crt", // Debian/Ubuntu/Gentoo etc. | ||
"/etc/pki/tls/certs/ca-bundle.crt", // Fedora/RHEL 6 | ||
"/etc/ssl/ca-bundle.pem", // OpenSUSE | ||
"/etc/pki/tls/cacert.pem", // OpenELEC | ||
"/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem", // CentOS/RHEL 7 | ||
"/etc/ssl/cert.pem", // Alpine Linux | ||
} | ||
|
||
// Possible directories with certificate files; all will be read. | ||
var certDirectories = []string{ | ||
"/etc/ssl/certs", // SLES10/SLES11, https://golang.org/issue/12139 | ||
"/etc/pki/tls/certs", // Fedora/RHEL | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// Source: https://github.com/golang/go/blob/master/src/crypto/x509/root_unix.go | ||
// TODO: Check on every minor version release if there is a better way to do it, or, at least, update it. | ||
|
||
//go:build aix || dragonfly || freebsd || (js && wasm) || linux || netbsd || openbsd || solaris || wasip1 | ||
|
||
package certStore | ||
|
||
import ( | ||
"crypto/x509" | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
"reflect" | ||
"strings" | ||
) | ||
|
||
const ( | ||
// certFileEnv is the environment variable which identifies where to locate | ||
// the SSL certificate file. If set this overrides the system default. | ||
certFileEnv = "SSL_CERT_FILE" | ||
|
||
// certDirEnv is the environment variable which identifies which directory | ||
// to check for SSL certificate files. If set this overrides the system default. | ||
// It is a colon separated list of directories. | ||
// See https://www.openssl.org/docs/man1.0.2/man1/c_rehash.html. | ||
certDirEnv = "SSL_CERT_DIR" | ||
) | ||
|
||
func loadSystemRoots() (*x509.CertPool, error) { | ||
roots := x509.NewCertPool() | ||
|
||
files := certFiles | ||
if f := os.Getenv(certFileEnv); f != "" { | ||
files = []string{f} | ||
} | ||
|
||
var firstErr error | ||
for _, file := range files { | ||
data, err := os.ReadFile(file) | ||
if err == nil { | ||
roots.AppendCertsFromPEM(data) | ||
break | ||
} | ||
if firstErr == nil && !os.IsNotExist(err) { | ||
firstErr = err | ||
} | ||
} | ||
|
||
dirs := certDirectories | ||
if d := os.Getenv(certDirEnv); d != "" { | ||
// OpenSSL and BoringSSL both use ":" as the SSL_CERT_DIR separator. | ||
// See: | ||
// * https://golang.org/issue/35325 | ||
// * https://www.openssl.org/docs/man1.0.2/man1/c_rehash.html | ||
dirs = strings.Split(d, ":") | ||
} | ||
|
||
for _, directory := range dirs { | ||
fis, err := readUniqueDirectoryEntries(directory) | ||
if err != nil { | ||
if firstErr == nil && !os.IsNotExist(err) { | ||
firstErr = err | ||
} | ||
continue | ||
} | ||
for _, fi := range fis { | ||
data, err := os.ReadFile(directory + "/" + fi.Name()) | ||
if err == nil { | ||
roots.AppendCertsFromPEM(data) | ||
} | ||
} | ||
} | ||
|
||
if firstErr != nil { | ||
return roots, nil | ||
} | ||
|
||
rootsValue := reflect.ValueOf(roots) | ||
lenMethod := rootsValue.MethodByName("len") | ||
results := lenMethod.Call(nil) | ||
|
||
if results[0].Int() > 0 { | ||
return roots, nil | ||
} | ||
|
||
return nil, firstErr | ||
} | ||
|
||
// readUniqueDirectoryEntries is like os.ReadDir but omits | ||
// symlinks that point within the directory. | ||
func readUniqueDirectoryEntries(dir string) ([]fs.DirEntry, error) { | ||
files, err := os.ReadDir(dir) | ||
if err != nil { | ||
return nil, err | ||
} | ||
uniq := files[:0] | ||
for _, f := range files { | ||
if !isSameDirSymlink(f, dir) { | ||
uniq = append(uniq, f) | ||
} | ||
} | ||
return uniq, nil | ||
} | ||
|
||
// isSameDirSymlink reports whether fi in dir is a symlink with a | ||
// target not containing a slash. | ||
func isSameDirSymlink(f fs.DirEntry, dir string) bool { | ||
if f.Type()&fs.ModeSymlink == 0 { | ||
return false | ||
} | ||
target, err := os.Readlink(filepath.Join(dir, f.Name())) | ||
return err == nil && !strings.Contains(target, "/") | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters