@@ -8,17 +8,9 @@ package ddflareextensionimpl
8
8
9
9
import (
10
10
"context"
11
- "crypto/rand"
12
- "crypto/rsa"
13
11
"crypto/tls"
14
- "crypto/x509"
15
- "crypto/x509/pkix"
16
- "encoding/pem"
17
- "fmt"
18
- "math/big"
19
12
"net"
20
13
"net/http"
21
- "time"
22
14
23
15
"github.com/gorilla/mux"
24
16
@@ -41,75 +33,6 @@ func validateToken(next http.Handler) http.Handler {
41
33
}
42
34
43
35
func newServer (endpoint string , handler http.Handler , auth bool ) (* server , error ) {
44
-
45
- // Generate a self-signed certificate
46
- key , err := rsa .GenerateKey (rand .Reader , 2048 )
47
- if err != nil {
48
- return nil , err
49
- }
50
-
51
- serialNumberLimit := new (big.Int ).Lsh (big .NewInt (1 ), 128 )
52
- serialNumber , err := rand .Int (rand .Reader , serialNumberLimit )
53
- if err != nil {
54
- return nil , err
55
- }
56
-
57
- template := x509.Certificate {
58
- SerialNumber : serialNumber ,
59
- Subject : pkix.Name {
60
- Organization : []string {"Datadog Inc." },
61
- },
62
- NotBefore : time .Now (),
63
- NotAfter : time .Now ().Add (10 * 365 * 24 * time .Hour ),
64
- IsCA : true ,
65
- KeyUsage : x509 .KeyUsageCertSign | x509 .KeyUsageDigitalSignature | x509 .KeyUsageCRLSign ,
66
- ExtKeyUsage : []x509.ExtKeyUsage {x509 .ExtKeyUsageServerAuth , x509 .ExtKeyUsageClientAuth },
67
- BasicConstraintsValid : true ,
68
- }
69
-
70
- for _ , h := range []string {"127.0.0.1" , "localhost" , "::1" } {
71
- if ip := net .ParseIP (h ); ip != nil {
72
- template .IPAddresses = append (template .IPAddresses , ip )
73
- } else {
74
- template .DNSNames = append (template .DNSNames , h )
75
- }
76
- }
77
-
78
- certDER , err := x509 .CreateCertificate (rand .Reader , & template , & template , & key .PublicKey , key )
79
- if err != nil {
80
- return nil , err
81
- }
82
- // parse the resulting certificate so we can use it again
83
- _ , err = x509 .ParseCertificate (certDER )
84
- if err != nil {
85
- return nil , err
86
- }
87
- // PEM encode the certificate (this is a standard TLS encoding)
88
- b := pem.Block {Type : "CERTIFICATE" , Bytes : certDER }
89
- certPEM := pem .EncodeToMemory (& b )
90
-
91
- keyPEM := pem .EncodeToMemory (& pem.Block {
92
- Type : "RSA PRIVATE KEY" , Bytes : x509 .MarshalPKCS1PrivateKey (key ),
93
- })
94
-
95
- pair , err := tls .X509KeyPair (certPEM , keyPEM )
96
- if err != nil {
97
- return nil , fmt .Errorf ("unable to generate TLS key pair: %v" , err )
98
- }
99
-
100
- tlsCertPool := x509 .NewCertPool ()
101
- ok := tlsCertPool .AppendCertsFromPEM (certPEM )
102
- if ! ok {
103
- return nil , fmt .Errorf ("unable to add new certificate to pool" )
104
- }
105
-
106
- // Create TLS configuration
107
- tlsConfig := & tls.Config {
108
- Certificates : []tls.Certificate {pair },
109
- NextProtos : []string {"h2" },
110
- MinVersion : tls .VersionTLS12 ,
111
- }
112
-
113
36
r := mux .NewRouter ()
114
37
r .Handle ("/" , handler )
115
38
@@ -122,7 +45,7 @@ func newServer(endpoint string, handler http.Handler, auth bool) (*server, error
122
45
123
46
s := & http.Server {
124
47
Addr : endpoint ,
125
- TLSConfig : tlsConfig ,
48
+ TLSConfig : util . GetTLSServerConfig () ,
126
49
Handler : r ,
127
50
}
128
51
0 commit comments