Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to add custom CA roots for downloads via env var #28

Merged
merged 1 commit into from
Apr 7, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion pkg/download/downloader.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package download

import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"runtime"
"time"
Expand All @@ -11,7 +15,7 @@ import (
"github.com/develar/app-builder/pkg/log"
"github.com/develar/app-builder/pkg/util"
"github.com/develar/errors"
"github.com/develar/go-fs-util"
fsutil "github.com/develar/go-fs-util"
"github.com/dustin/go-humanize"
"go.uber.org/zap"
)
Expand All @@ -27,6 +31,35 @@ func getUserAgent() string {
return util.GetEnvOrDefault("DOWNLOADER_USER_AGENT", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36")
}

func getTlsConfig() *tls.Config {
localCertFile := os.Getenv("NODE_EXTRA_CA_CERTS")
if len(localCertFile) == 0 {
return &tls.Config{}
}

// Get the SystemCertPool, continue with an empty pool on error
rootCAs, _ := x509.SystemCertPool()
if rootCAs == nil {
rootCAs = x509.NewCertPool()
}

// Read in the cert file
certs, err := ioutil.ReadFile(localCertFile)
if err != nil {
log.Warn("Failed to append to root certificates", zap.String("extraCert", localCertFile), zap.Error(err))
}

// Append our cert to the system pool
if ok := rootCAs.AppendCertsFromPEM(certs); !ok {
log.Warn("No certs appended, using system certs only")
}

// Trust the augmented cert pool in our client
return &tls.Config{
RootCAs: rootCAs,
}
}

func getMaxPartCount() int {
const maxPartCount = 8
result := runtime.NumCPU() * 2
Expand Down Expand Up @@ -56,6 +89,7 @@ type Downloader struct {
func NewDownloader() *Downloader {
return NewDownloaderWithTransport(&http.Transport{
Proxy: util.ProxyFromEnvironmentAndNpm,
TLSClientConfig: getTlsConfig(),
MaxIdleConns: 64,
MaxIdleConnsPerHost: 64,
IdleConnTimeout: 30 * time.Second,
Expand Down