@@ -784,6 +784,26 @@ func TestTLSConfigInvalidCA(t *testing.T) {
784784 InsecureSkipVerify : false },
785785 errorMessage : fmt .Sprintf ("unable to read specified client key (%s):" , MissingKey ),
786786 },
787+ {
788+ configTLSConfig : TLSConfig {
789+ CAFile : "" ,
790+ Cert : readFile (t , ClientCertificatePath ),
791+ CertFile : ClientCertificatePath ,
792+ KeyFile : ClientKeyNoPassPath ,
793+ ServerName : "" ,
794+ InsecureSkipVerify : false },
795+ errorMessage : "at most one of cert and cert_file must be configured" ,
796+ },
797+ {
798+ configTLSConfig : TLSConfig {
799+ CAFile : "" ,
800+ CertFile : ClientCertificatePath ,
801+ Key : Secret (readFile (t , ClientKeyNoPassPath )),
802+ KeyFile : ClientKeyNoPassPath ,
803+ ServerName : "" ,
804+ InsecureSkipVerify : false },
805+ errorMessage : "at most one of key and key_file must be configured" ,
806+ },
787807 }
788808
789809 for _ , anInvalididTLSConfig := range invalidTLSConfig {
@@ -1046,6 +1066,127 @@ func TestTLSRoundTripper(t *testing.T) {
10461066 }
10471067}
10481068
1069+ func TestTLSRoundTripper_Inline (t * testing.T ) {
1070+ handler := func (w http.ResponseWriter , r * http.Request ) {
1071+ fmt .Fprint (w , ExpectedMessage )
1072+ }
1073+ testServer , err := newTestServer (handler )
1074+ if err != nil {
1075+ t .Fatal (err .Error ())
1076+ }
1077+ defer testServer .Close ()
1078+
1079+ testCases := []struct {
1080+ caText , caFile string
1081+ certText , certFile string
1082+ keyText , keyFile string
1083+
1084+ errMsg string
1085+ }{
1086+ {
1087+ // File-based everything.
1088+ caFile : TLSCAChainPath ,
1089+ certFile : ClientCertificatePath ,
1090+ keyFile : ClientKeyNoPassPath ,
1091+ },
1092+ {
1093+ // Inline CA.
1094+ caText : readFile (t , TLSCAChainPath ),
1095+ certFile : ClientCertificatePath ,
1096+ keyFile : ClientKeyNoPassPath ,
1097+ },
1098+ {
1099+ // Inline cert.
1100+ caFile : TLSCAChainPath ,
1101+ certText : readFile (t , ClientCertificatePath ),
1102+ keyFile : ClientKeyNoPassPath ,
1103+ },
1104+ {
1105+ // Inline key.
1106+ caFile : TLSCAChainPath ,
1107+ certFile : ClientCertificatePath ,
1108+ keyText : readFile (t , ClientKeyNoPassPath ),
1109+ },
1110+ {
1111+ // Inline everything.
1112+ caText : readFile (t , TLSCAChainPath ),
1113+ certText : readFile (t , ClientCertificatePath ),
1114+ keyText : readFile (t , ClientKeyNoPassPath ),
1115+ },
1116+
1117+ {
1118+ // Invalid inline CA.
1119+ caText : "badca" ,
1120+ certText : readFile (t , ClientCertificatePath ),
1121+ keyText : readFile (t , ClientKeyNoPassPath ),
1122+
1123+ errMsg : "unable to use inline CA cert" ,
1124+ },
1125+ {
1126+ // Invalid cert.
1127+ caText : readFile (t , TLSCAChainPath ),
1128+ certText : "badcert" ,
1129+ keyText : readFile (t , ClientKeyNoPassPath ),
1130+
1131+ errMsg : "failed to find any PEM data in certificate input" ,
1132+ },
1133+ {
1134+ // Invalid key.
1135+ caText : readFile (t , TLSCAChainPath ),
1136+ certText : readFile (t , ClientCertificatePath ),
1137+ keyText : "badkey" ,
1138+
1139+ errMsg : "failed to find any PEM data in key input" ,
1140+ },
1141+ }
1142+
1143+ for i , tc := range testCases {
1144+ tc := tc
1145+ t .Run (strconv .Itoa (i ), func (t * testing.T ) {
1146+ cfg := HTTPClientConfig {
1147+ TLSConfig : TLSConfig {
1148+ CA : tc .caText ,
1149+ CAFile : tc .caFile ,
1150+ Cert : tc .certText ,
1151+ CertFile : tc .certFile ,
1152+ Key : Secret (tc .keyText ),
1153+ KeyFile : tc .keyFile ,
1154+ InsecureSkipVerify : false },
1155+ }
1156+
1157+ c , err := NewClientFromConfig (cfg , "test" )
1158+ if tc .errMsg != "" {
1159+ if ! strings .Contains (err .Error (), tc .errMsg ) {
1160+ t .Fatalf ("Expected error message to contain %q, got %q" , tc .errMsg , err )
1161+ }
1162+ return
1163+ } else if err != nil {
1164+ t .Fatalf ("Error creating HTTP Client: %v" , err )
1165+ }
1166+
1167+ req , err := http .NewRequest (http .MethodGet , testServer .URL , nil )
1168+ if err != nil {
1169+ t .Fatalf ("Error creating HTTP request: %v" , err )
1170+ }
1171+ r , err := c .Do (req )
1172+ if err != nil {
1173+ t .Fatalf ("Can't connect to the test server" )
1174+ }
1175+
1176+ b , err := io .ReadAll (r .Body )
1177+ r .Body .Close ()
1178+ if err != nil {
1179+ t .Errorf ("Can't read the server response body" )
1180+ }
1181+
1182+ got := strings .TrimSpace (string (b ))
1183+ if ExpectedMessage != got {
1184+ t .Errorf ("The expected message %q differs from the obtained message %q" , ExpectedMessage , got )
1185+ }
1186+ })
1187+ }
1188+ }
1189+
10491190func TestTLSRoundTripperRaces (t * testing.T ) {
10501191 bs := getCertificateBlobs (t )
10511192
@@ -1838,3 +1979,14 @@ no_proxy: promcon.io,cncf.io`, proxyServer.URL),
18381979 })
18391980 }
18401981}
1982+
1983+ func readFile (t * testing.T , filename string ) string {
1984+ t .Helper ()
1985+
1986+ content , err := os .ReadFile (filename )
1987+ if err != nil {
1988+ t .Fatalf ("Failed to read file %q: %s" , filename , err )
1989+ }
1990+
1991+ return string (content )
1992+ }
0 commit comments