Skip to content

Commit

Permalink
jv: add -cacert option
Browse files Browse the repository at this point in the history
  • Loading branch information
santhosh-tekuri committed May 2, 2024
1 parent 2ccf96b commit 148d5f7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
17 changes: 14 additions & 3 deletions cmd/jv/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"crypto/tls"
"crypto/x509"
"fmt"
"net/http"
"os"
Expand All @@ -13,11 +14,21 @@ import (
"gopkg.in/yaml.v3"
)

func newLoader(insecure bool) jsonschema.URLLoader {
func newLoader(insecure bool, cacert string) (jsonschema.URLLoader, error) {
httpLoader := HTTPLoader(http.Client{
Timeout: 15 * time.Second,
})
if insecure {
if cacert != "" {
pem, err := os.ReadFile(cacert)
if err != nil {
return nil, err
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(pem)
httpLoader.Transport = &http.Transport{
TLSClientConfig: &tls.Config{RootCAs: caCertPool},
}
} else if insecure {
httpLoader.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
Expand All @@ -26,7 +37,7 @@ func newLoader(insecure bool) jsonschema.URLLoader {
"file": FileLoader{},
"http": &httpLoader,
"https": &httpLoader,
}
}, nil
}

type FileLoader struct{}
Expand Down
8 changes: 7 additions & 1 deletion cmd/jv/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func main() {
assertFormat := flag.Bool("f", false, "Enable format assertions with draft >= 2019")
assertContent := flag.Bool("c", false, "Enable content assertions with draft >= 7")
insecure := flag.Bool("k", false, "Use insecure TLS connection")
cacert := flag.String("cacert", "", "Use the specified `PEM-certificate-file` to verify the peer. The file may contain multiple CA certificates")
flag.Parse()

// help --
Expand Down Expand Up @@ -85,7 +86,12 @@ func main() {
if *assertContent {
c.AssertContent()
}
c.UseLoader(newLoader(*insecure))
loader, err := newLoader(*insecure, *cacert)
if err != nil {
eprintln("%v", err)
os.Exit(2)
}
c.UseLoader(loader)

// compile
sch, err := func() (*jsonschema.Schema, error) {
Expand Down

0 comments on commit 148d5f7

Please sign in to comment.