Merge pull request 'fix: implémenter correctement tls certfile et keyfile' (#55) from vlbeaudoin/fix/tls into main
Reviewed-on: #55
This commit is contained in:
commit
e14ff3d04e
3 changed files with 38 additions and 23 deletions
|
@ -1,6 +1,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
|
@ -19,7 +20,18 @@ func TestAPI(t *testing.T) {
|
|||
return
|
||||
}
|
||||
|
||||
httpClient := http.DefaultClient
|
||||
//httpClient := http.DefaultClient
|
||||
//defer httpClient.CloseIdleConnections()
|
||||
|
||||
transport := http.Transport{
|
||||
TLSClientConfig: &tls.Config{
|
||||
InsecureSkipVerify: true,
|
||||
},
|
||||
}
|
||||
|
||||
httpClient := http.Client{
|
||||
Transport: &transport,
|
||||
}
|
||||
defer httpClient.CloseIdleConnections()
|
||||
|
||||
var protocol string
|
||||
|
@ -30,7 +42,7 @@ func TestAPI(t *testing.T) {
|
|||
protocol = "http"
|
||||
}
|
||||
|
||||
vokiClient := voki.New(httpClient, "localhost", cfg.API.Key, cfg.API.Port, protocol)
|
||||
vokiClient := voki.New(&httpClient, "localhost", cfg.API.Key, cfg.API.Port, protocol)
|
||||
apiClient := APIClient{vokiClient}
|
||||
|
||||
t.Run("get API health", func(t *testing.T) {
|
||||
|
|
7
cmd.go
7
cmd.go
|
@ -122,11 +122,14 @@ var apiCmd = &cobra.Command{
|
|||
),
|
||||
)
|
||||
case true:
|
||||
//TODO
|
||||
log.Printf("dbg: certfile='%s' keyfile='%s'", cfg.API.TLS.Certfile, cfg.API.TLS.Keyfile)
|
||||
|
||||
e.Logger.Fatal(
|
||||
e.StartTLS(
|
||||
fmt.Sprintf(":%d", cfg.API.Port),
|
||||
cfg.API.TLS.CertificateFile,
|
||||
cfg.API.TLS.PrivateKeyFile,
|
||||
cfg.API.TLS.Certfile,
|
||||
cfg.API.TLS.Keyfile,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
|
38
config.go
38
config.go
|
@ -16,15 +16,15 @@ const (
|
|||
DefaultAPITLSEnabled bool = false
|
||||
DescriptionAPITLSEnabled string = "Whether to use TLS or not. Requires certificate and private key files."
|
||||
|
||||
ViperAPITLSCertificateFile string = "api.tls.certificate_file"
|
||||
FlagAPITLSCertificateFile string = "api-tls-certificate-file"
|
||||
DefaultAPITLSCertificateFile string = ""
|
||||
DescriptionAPITLSCertificateFile string = "Path to TLS certificate file"
|
||||
ViperAPITLSCertfile string = "api.tls.certfile"
|
||||
FlagAPITLSCertfile string = "api-tls-certfile"
|
||||
DefaultAPITLSCertfile string = "/etc/bottin/cert.pem"
|
||||
DescriptionAPITLSCertfile string = "Path to TLS certificate file"
|
||||
|
||||
ViperAPITLSPrivateKeyFile string = "api.tls.private_key_file"
|
||||
FlagAPITLSPrivateKeyFile string = "api-tls-private-key-file"
|
||||
DefaultAPITLSPrivateKeyFile string = ""
|
||||
DescriptionAPITLSPrivateKeyFile string = "Path to TLS private key file"
|
||||
ViperAPITLSKeyfile string = "api.tls.keyfile"
|
||||
FlagAPITLSKeyfile string = "api-tls-keyfile"
|
||||
DefaultAPITLSKeyfile string = "/etc/bottin/key.pem"
|
||||
DescriptionAPITLSKeyFile string = "Path to TLS private key file"
|
||||
|
||||
ViperAPIPort string = "api.port"
|
||||
FlagAPIPort string = "api-port"
|
||||
|
@ -108,11 +108,11 @@ type Config struct {
|
|||
Enabled bool `yaml:"enabled"`
|
||||
|
||||
// Path to file containing TLS certificate
|
||||
CertificateFile string `yaml:"certificate_file"`
|
||||
Certfile string `yaml:"certfile"`
|
||||
|
||||
// Path to file containing TLS private key
|
||||
PrivateKeyFile string `yaml:"private_key_file"`
|
||||
}
|
||||
Keyfile string `yaml:"keyfile"`
|
||||
} `yaml:"tls"`
|
||||
Port int `yaml:"port"`
|
||||
Key string `yaml:"key"`
|
||||
} `yaml:"api"`
|
||||
|
@ -141,8 +141,8 @@ type Config struct {
|
|||
// `Default*` constants defined in this file.
|
||||
func DefaultConfig() (cfg Config) {
|
||||
cfg.API.TLS.Enabled = DefaultAPITLSEnabled
|
||||
cfg.API.TLS.CertificateFile = DefaultAPITLSCertificateFile
|
||||
cfg.API.TLS.PrivateKeyFile = DefaultAPITLSPrivateKeyFile
|
||||
cfg.API.TLS.Certfile = DefaultAPITLSCertfile
|
||||
cfg.API.TLS.Keyfile = DefaultAPITLSKeyfile
|
||||
cfg.API.Port = DefaultAPIPort
|
||||
cfg.API.Key = DefaultAPIKey
|
||||
cfg.DB.Database = DefaultDBDatabase
|
||||
|
@ -178,15 +178,15 @@ func init() {
|
|||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// api.tls.certificate_file
|
||||
apiCmd.Flags().String(FlagAPITLSCertificateFile, DefaultAPITLSCertificateFile, DescriptionAPITLSCertificateFile)
|
||||
if err := viper.BindPFlag(ViperAPITLSCertificateFile, apiCmd.Flags().Lookup(FlagAPITLSCertificateFile)); err != nil {
|
||||
// api.tls.certfile
|
||||
apiCmd.Flags().String(FlagAPITLSCertfile, DefaultAPITLSCertfile, DescriptionAPITLSCertfile)
|
||||
if err := viper.BindPFlag(ViperAPITLSCertfile, apiCmd.Flags().Lookup(FlagAPITLSCertfile)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// api.tls.private_key_file
|
||||
apiCmd.Flags().String(FlagAPITLSPrivateKeyFile, DefaultAPITLSPrivateKeyFile, DescriptionAPITLSPrivateKeyFile)
|
||||
if err := viper.BindPFlag(ViperAPITLSPrivateKeyFile, apiCmd.Flags().Lookup(FlagAPITLSPrivateKeyFile)); err != nil {
|
||||
// api.tls.keyfile
|
||||
apiCmd.Flags().String(FlagAPITLSKeyfile, DefaultAPITLSKeyfile, DescriptionAPITLSKeyFile)
|
||||
if err := viper.BindPFlag(ViperAPITLSKeyfile, apiCmd.Flags().Lookup(FlagAPITLSKeyfile)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue