Skip to content

Commit

Permalink
add cert support
Browse files Browse the repository at this point in the history
  • Loading branch information
fr3h4g committed Nov 2, 2024
1 parent 68e3418 commit 6eec3c8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
.DS_Store
/mjau
/mjau-test.yaml
*.crt
*.key
*.pem
*.csr
35 changes: 34 additions & 1 deletion cmd/mjau/run.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package mjau

import (
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"regexp"
Expand Down Expand Up @@ -42,6 +45,9 @@ type Request struct {
PreCommands []Command `yaml:"pre_commands"`
Commands []Command `yaml:"commands"`
Asserts []Assert `yaml:"asserts"`
Cert string `yaml:"cert"`
Key string `yaml:"key"`
CaCert string `yaml:"ca_cert"`
}

type Command struct {
Expand Down Expand Up @@ -296,6 +302,33 @@ func RunRequest(cmd *cobra.Command, requestName string, config *Config) {
config.StoreVariable("request.url", request.URL)
config.StoreVariable("request.body", request.Body)

client := &http.Client{}

if request.Cert != "" && request.Key != "" {
cert, err := tls.LoadX509KeyPair(request.Cert, request.Key)
if err != nil {
log.Fatal(err)
}
client = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
Certificates: []tls.Certificate{cert},
},
},
}

if request.CaCert != "" {
caCertPool := x509.NewCertPool()
caCert, err := os.ReadFile(request.CaCert)
if err != nil {
log.Fatal(err)
}
caCertPool.AppendCertsFromPEM(caCert)

client.Transport.(*http.Transport).TLSClientConfig.RootCAs = caCertPool
}
}

req, err := http.NewRequest(
request.Method,
request.URL,
Expand Down Expand Up @@ -343,7 +376,7 @@ func RunRequest(cmd *cobra.Command, requestName string, config *Config) {
fmt.Println("\n" + request.Body)
}
start := time.Now()
resp, err := http.DefaultClient.Do(req)
resp, err := client.Do(req)
elapsed := time.Since(start)
if err != nil {
fmt.Println(err)
Expand Down

0 comments on commit 6eec3c8

Please sign in to comment.