@@ -7,18 +7,19 @@ import (
7
7
"crypto/x509"
8
8
"crypto/x509/pkix"
9
9
"encoding/pem"
10
- "github.com/armon/go-socks5"
11
- "github.com/elazarl/goproxy"
12
- "github.com/elazarl/goproxy/ext/auth"
13
- "github.com/grepplabs/kafka-proxy/config"
14
- "github.com/pkg/errors"
15
- "golang.org/x/net/proxy"
16
10
"io/ioutil"
17
11
"math/big"
18
12
"net"
19
13
"net/http"
20
14
"os"
21
15
"time"
16
+
17
+ "github.com/armon/go-socks5"
18
+ "github.com/elazarl/goproxy"
19
+ "github.com/elazarl/goproxy/ext/auth"
20
+ "github.com/grepplabs/kafka-proxy/config"
21
+ "github.com/pkg/errors"
22
+ "golang.org/x/net/proxy"
22
23
)
23
24
24
25
type testAcceptResult struct {
@@ -380,18 +381,23 @@ func makeHttpProxyPipe() (net.Conn, net.Conn, func(), error) {
380
381
}
381
382
382
383
func generateCert (catls * tls.Certificate , certFile * os.File , keyFile * os.File ) error {
384
+ return generateCertWithSubject (catls , certFile , keyFile , pkix.Name {
385
+ Organization : []string {"ORGANIZATION_NAME" },
386
+ OrganizationalUnit : []string {"ORGANIZATIONAL_UNIT" },
387
+ Country : []string {"COUNTRY_CODE" },
388
+ Province : []string {"PROVINCE" },
389
+ Locality : []string {"CITY" },
390
+ StreetAddress : []string {"ADDRESS" },
391
+ PostalCode : []string {"POSTAL_CODE" },
392
+ CommonName : "localhost" ,
393
+ })
394
+ }
395
+
396
+ func generateCertWithSubject (catls * tls.Certificate , certFile * os.File , keyFile * os.File , subject pkix.Name ) error {
383
397
// Prepare certificate
384
398
cert := & x509.Certificate {
385
399
SerialNumber : big .NewInt (1 ),
386
- Subject : pkix.Name {
387
- Organization : []string {"ORGANIZATION_NAME" },
388
- Country : []string {"COUNTRY_CODE" },
389
- Province : []string {"PROVINCE" },
390
- Locality : []string {"CITY" },
391
- StreetAddress : []string {"ADDRESS" },
392
- PostalCode : []string {"POSTAL_CODE" },
393
- CommonName : "localhost" ,
394
- },
400
+ Subject : subject ,
395
401
NotBefore : time .Now (),
396
402
NotAfter : time .Now ().AddDate (10 , 0 , 0 ),
397
403
SubjectKeyId : []byte {1 , 2 , 3 , 4 , 6 },
@@ -539,6 +545,52 @@ func NewCertsBundle() *CertsBundle {
539
545
return bundle
540
546
}
541
547
548
+ func NewCertsBundleWithSubject (subject pkix.Name ) * CertsBundle {
549
+ bundle := & CertsBundle {}
550
+ dirName , err := ioutil .TempDir ("" , "tls-test" )
551
+ if err != nil {
552
+ panic (err )
553
+ }
554
+ bundle .CACert , err = ioutil .TempFile (dirName , "ca-cert-" )
555
+ if err != nil {
556
+ panic (err )
557
+ }
558
+ bundle .CAKey , err = ioutil .TempFile (dirName , "ca-key-" )
559
+ if err != nil {
560
+ panic (err )
561
+ }
562
+ bundle .ServerCert , err = ioutil .TempFile (dirName , "server-cert-" )
563
+ if err != nil {
564
+ panic (err )
565
+ }
566
+ bundle .ServerKey , err = ioutil .TempFile (dirName , "server-key-" )
567
+ if err != nil {
568
+ panic (err )
569
+ }
570
+ bundle .ClientCert , err = ioutil .TempFile (dirName , "client-cert-" )
571
+ if err != nil {
572
+ panic (err )
573
+ }
574
+ bundle .ClientKey , err = ioutil .TempFile ("" , "client-key-" )
575
+ if err != nil {
576
+ panic (err )
577
+ }
578
+ // generate certs
579
+ catls , err := generateCA (bundle .CACert , bundle .CAKey )
580
+ if err != nil {
581
+ panic (err )
582
+ }
583
+ err = generateCert (catls , bundle .ServerCert , bundle .ServerKey )
584
+ if err != nil {
585
+ panic (err )
586
+ }
587
+ err = generateCertWithSubject (catls , bundle .ClientCert , bundle .ClientKey , subject )
588
+ if err != nil {
589
+ panic (err )
590
+ }
591
+ return bundle
592
+ }
593
+
542
594
func (bundle * CertsBundle ) Close () {
543
595
_ = os .Remove (bundle .CACert .Name ())
544
596
_ = os .Remove (bundle .CAKey .Name ())
0 commit comments