Skip to content

Commit

Permalink
feat: add ReadCACert function
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmad-ibra committed Jun 11, 2024
1 parent 99e039f commit e3ee453
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions prompts/prompts.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package prompts

import (
"crypto/x509"
"encoding/pem"
"fmt"
"net"
"net/url"
Expand Down Expand Up @@ -551,6 +553,43 @@ func ReadK8sName(label, defaultVal string, optional bool) (string, error) {
return s, nil
}

func ReadCACert(prompt string, defaultCaCertPath, caCertPathOverride string) (caCertPath string, caCertName string, caCertData []byte, err error) {
if caCertPathOverride != "" {
caCertPath = caCertPathOverride
} else {
caCertPath, err = ReadFilePath(prompt, defaultCaCertPath, "Invalid filepath specified", true)
}
if err != nil {
return "", "", nil, err
}
if caCertPath == "" {
return "", "", nil, nil
}
caFile, _ := os.Stat(caCertPath)
caBytes, err := os.ReadFile(caCertPath) //#nosec
if err != nil {
return "", "", nil, err
}
// Validate CA cert
var blocks []byte
rest := caBytes
for {
var block *pem.Block
block, rest = pem.Decode(rest)
if block == nil {
return "", "", nil, fmt.Errorf("PEM parse failure for %s", caCertPath)
}
blocks = append(blocks, block.Bytes...)
if len(rest) == 0 {
break
}
}
if _, err = x509.ParseCertificates(blocks); err != nil {
return "", "", nil, err
}
return caCertPath, caFile.Name(), caBytes, nil
}

// See: https://pkg.go.dev/golang.org/x/net/http/httpproxy#Config
func ValidateNoProxy(s string) error {
if s == "" {
Expand Down

0 comments on commit e3ee453

Please sign in to comment.