mirror of
https://github.com/passbolt/go-passbolt-cli.git
synced 2025-09-13 14:59:09 +00:00
Added support for http client configuration via command arguments
This commit is contained in:
parent
d9703ff6fd
commit
d5e2df49db
4 changed files with 62 additions and 2 deletions
44
util/http.go
Normal file
44
util/http.go
Normal file
|
@ -0,0 +1,44 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func GetClientCertificate() (tls.Certificate, error) {
|
||||
cert := viper.GetString("tlsClientCert")
|
||||
certExists := cert != ""
|
||||
key := viper.GetString("tlsClientPrivateKey")
|
||||
keyExists := key != ""
|
||||
if !certExists && !keyExists {
|
||||
return tls.Certificate{}, nil
|
||||
}
|
||||
if certExists && !keyExists {
|
||||
return tls.Certificate{}, fmt.Errorf("Client TLS private key is empty, but client TLS cert was sent.")
|
||||
}
|
||||
if !certExists && keyExists {
|
||||
return tls.Certificate{}, fmt.Errorf("Client TLS cert is empty, but client TLS private key was sent.")
|
||||
}
|
||||
return tls.LoadX509KeyPair("client.cert", "client-key.pem")
|
||||
}
|
||||
|
||||
func GetHttpClient() (*http.Client, error) {
|
||||
tlsSkipVerify := viper.GetBool("tlsSkipVerify")
|
||||
cert, err := GetClientCertificate()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
httpClient := http.Client{
|
||||
Transport: &http.Transport{
|
||||
TLSClientConfig: &tls.Config{
|
||||
Certificates: []tls.Certificate{cert},
|
||||
InsecureSkipVerify: tlsSkipVerify,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
return &httpClient, nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue