mirror of
https://github.com/oauth2-proxy/oauth2-proxy.git
synced 2025-02-03 13:21:51 +02:00
b49e62f9b2
* init TLS.Config when connecting to Redis with TLS * don't overwrite TLS config if it exists * add tests for Redis with TLS * remove hardcoded certs * add GenerateCert func * use GenerateCert util func * fix issue reported by go fmt * limit return statements in GenerateCert
62 lines
1.6 KiB
Go
62 lines
1.6 KiB
Go
package http
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"encoding/pem"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
|
|
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
|
|
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/util"
|
|
. "github.com/onsi/ginkgo"
|
|
. "github.com/onsi/gomega"
|
|
)
|
|
|
|
var certData []byte
|
|
var certDataSource, keyDataSource options.SecretSource
|
|
var client *http.Client
|
|
|
|
func TestHTTPSuite(t *testing.T) {
|
|
logger.SetOutput(GinkgoWriter)
|
|
logger.SetErrOutput(GinkgoWriter)
|
|
|
|
RegisterFailHandler(Fail)
|
|
RunSpecs(t, "HTTP")
|
|
}
|
|
|
|
var _ = BeforeSuite(func() {
|
|
By("Generating a self-signed cert for TLS tests", func() {
|
|
certBytes, keyBytes, err := util.GenerateCert()
|
|
Expect(err).ToNot(HaveOccurred())
|
|
certData = certBytes
|
|
|
|
certOut := new(bytes.Buffer)
|
|
Expect(pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: certBytes})).To(Succeed())
|
|
certDataSource.Value = certOut.Bytes()
|
|
keyOut := new(bytes.Buffer)
|
|
Expect(pem.Encode(keyOut, &pem.Block{Type: "PRIVATE KEY", Bytes: keyBytes})).To(Succeed())
|
|
keyDataSource.Value = keyOut.Bytes()
|
|
})
|
|
|
|
By("Setting up a http client", func() {
|
|
cert, err := tls.X509KeyPair(certDataSource.Value, keyDataSource.Value)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
certificate, err := x509.ParseCertificate(cert.Certificate[0])
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
certpool := x509.NewCertPool()
|
|
certpool.AddCert(certificate)
|
|
|
|
transport := http.DefaultTransport.(*http.Transport).Clone()
|
|
transport.TLSClientConfig.RootCAs = certpool
|
|
|
|
client = &http.Client{
|
|
Transport: transport,
|
|
}
|
|
})
|
|
})
|