2018-08-29 12:36:03 +03:00
package integration
import (
"crypto/tls"
"net/http"
"os"
2024-01-09 19:00:07 +03:00
"testing"
2018-08-29 12:36:03 +03:00
"time"
2024-01-09 19:00:07 +03:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
2023-02-03 17:24:05 +03:00
"github.com/traefik/traefik/v3/integration/try"
2018-08-29 12:36:03 +03:00
)
const (
rootCertPath = "./fixtures/tlsclientheaders/root.pem"
certPemPath = "./fixtures/tlsclientheaders/server.pem"
certKeyPath = "./fixtures/tlsclientheaders/server.key"
)
type TLSClientHeadersSuite struct { BaseSuite }
2024-01-09 19:00:07 +03:00
func TestTLSClientHeadersSuite ( t * testing . T ) {
suite . Run ( t , new ( TLSClientHeadersSuite ) )
2018-08-29 12:36:03 +03:00
}
2024-01-09 19:00:07 +03:00
func ( s * TLSClientHeadersSuite ) SetupSuite ( ) {
s . BaseSuite . SetupSuite ( )
s . createComposeProject ( "tlsclientheaders" )
s . composeUp ( )
}
func ( s * TLSClientHeadersSuite ) TearDownSuite ( ) {
s . BaseSuite . TearDownSuite ( )
}
func ( s * TLSClientHeadersSuite ) TestTLSClientHeaders ( ) {
2021-03-04 22:08:03 +03:00
rootCertContent , err := os . ReadFile ( rootCertPath )
2024-01-09 19:00:07 +03:00
assert . NoError ( s . T ( ) , err )
2021-03-04 22:08:03 +03:00
serverCertContent , err := os . ReadFile ( certPemPath )
2024-01-09 19:00:07 +03:00
assert . NoError ( s . T ( ) , err )
2021-03-04 22:08:03 +03:00
ServerKeyContent , err := os . ReadFile ( certKeyPath )
2024-01-09 19:00:07 +03:00
assert . NoError ( s . T ( ) , err )
2018-08-29 12:36:03 +03:00
2024-01-09 19:00:07 +03:00
file := s . adaptFile ( "fixtures/tlsclientheaders/simple.toml" , struct {
2018-08-29 12:36:03 +03:00
RootCertContent string
ServerCertContent string
ServerKeyContent string
} {
RootCertContent : string ( rootCertContent ) ,
ServerCertContent : string ( serverCertContent ) ,
ServerKeyContent : string ( ServerKeyContent ) ,
} )
2024-01-09 19:00:07 +03:00
s . traefikCmd ( withConfigFile ( file ) )
2018-08-29 12:36:03 +03:00
2021-01-28 18:16:05 +03:00
err = try . GetRequest ( "http://127.0.0.1:8080/api/rawdata" , 2 * time . Second , try . BodyContains ( "PathPrefix(`/foo`)" ) )
2024-01-09 19:00:07 +03:00
require . NoError ( s . T ( ) , err )
2018-08-29 12:36:03 +03:00
2021-01-28 18:16:05 +03:00
request , err := http . NewRequest ( http . MethodGet , "https://127.0.0.1:8443/foo" , nil )
2024-01-09 19:00:07 +03:00
require . NoError ( s . T ( ) , err )
2018-08-29 12:36:03 +03:00
certificate , err := tls . LoadX509KeyPair ( certPemPath , certKeyPath )
2024-01-09 19:00:07 +03:00
require . NoError ( s . T ( ) , err )
2018-08-29 12:36:03 +03:00
tr := & http . Transport {
TLSClientConfig : & tls . Config {
InsecureSkipVerify : true ,
Certificates : [ ] tls . Certificate { certificate } ,
} ,
}
2022-10-13 16:08:08 +03:00
err = try . RequestWithTransport ( request , 2 * time . Second , tr , try . BodyContains ( "Forwarded-Tls-Client-Cert: MIIDNTCCAh0CFD0QQcHXUJuKwMBYDA+bBExVSP26MA0GCSqGSIb3DQEBCwUAMFYxCzAJBgNVBAYTAkZSMQ8wDQYDVQQIDAZGcmFuY2UxFTATBgNVBAoMDFRyYWVmaWsgTGFiczEQMA4GA1UECwwHdHJhZWZpazENMAsGA1UEAwwEcm9vdDAeFw0yMTAxMDgxNzQ0MjRaFw0zMTAxMDYxNzQ0MjRaMFgxCzAJBgNVBAYTAkZSMQ8wDQYDVQQIDAZGcmFuY2UxFTATBgNVBAoMDFRyYWVmaWsgTGFiczEQMA4GA1UECwwHdHJhZWZpazEPMA0GA1UEAwwGc2VydmVyMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvYK2z8gLPOfFLgXNWP2460aeJ9vrH47x/lhKLlv4amSDHDx8Cmz/6blOUM8XOfMRW1xx++AgCh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