-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from Tecnobutrul/allow-http-client-configuration
Added support for http client configuration via command arguments
- Loading branch information
Showing
4 changed files
with
93 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 set.") | ||
} | ||
if !certExists && keyExists { | ||
return tls.Certificate{}, fmt.Errorf("Client TLS cert is empty, but client TLS private key was set.") | ||
} | ||
return tls.X509KeyPair([]byte(cert), []byte(key)) | ||
} | ||
|
||
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 | ||
} |