-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
story(http): support tls and http2 (#42)
* feat(issue-34): add tls config option * feat(issue-34): add http2 only support * fix(issue-34): support only allow http2 requests if config is set * example(issue-34): add tls and http2 examples * feat(issue-34): add validator for min http proto * refactor(issue-34): simplify by using httpvalidator for min proto
- Loading branch information
Showing
9 changed files
with
407 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Copyright (c) 2023 Z5Labs and Contributors | ||
# | ||
# This software is released under the MIT License. | ||
# https://opensource.org/licenses/MIT | ||
|
||
FROM scratch | ||
EXPOSE 8080 | ||
COPY http2 / | ||
ENTRYPOINT ["/http2"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Copyright (c) 2023 Z5Labs and Contributors | ||
// | ||
// This software is released under the MIT License. | ||
// https://opensource.org/licenses/MIT | ||
|
||
package main | ||
|
||
import ( | ||
"crypto/ed25519" | ||
"crypto/rand" | ||
"crypto/tls" | ||
"crypto/x509" | ||
"crypto/x509/pkix" | ||
"fmt" | ||
"log/slog" | ||
"math/big" | ||
"net/http" | ||
"os" | ||
"time" | ||
|
||
"github.com/z5labs/bedrock" | ||
brhttp "github.com/z5labs/bedrock/http" | ||
"github.com/z5labs/bedrock/pkg/otelconfig" | ||
) | ||
|
||
func createCert() (tls.Certificate, error) { | ||
_, priv, err := ed25519.GenerateKey(rand.Reader) | ||
if err != nil { | ||
return tls.Certificate{}, err | ||
} | ||
// ECDSA, ED25519 and RSA subject keys should have the DigitalSignature | ||
// KeyUsage bits set in the x509.Certificate template | ||
notBefore := time.Now() | ||
notAfter := notBefore.Add(365 * 24 * time.Hour) | ||
|
||
template := x509.Certificate{ | ||
SerialNumber: big.NewInt(time.Now().Unix()), | ||
Subject: pkix.Name{ | ||
Organization: []string{"Acme Co"}, | ||
}, | ||
NotBefore: notBefore, | ||
NotAfter: notAfter, | ||
SubjectKeyId: []byte{113, 117, 105, 99, 107, 115, 101, 114, 118, 101}, | ||
BasicConstraintsValid: true, | ||
IsCA: true, | ||
KeyUsage: x509.KeyUsageKeyEncipherment | | ||
x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign, | ||
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, | ||
} | ||
|
||
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, priv.Public().(ed25519.PublicKey), priv) | ||
if err != nil { | ||
return tls.Certificate{}, nil | ||
} | ||
|
||
var cert tls.Certificate | ||
cert.Certificate = append(cert.Certificate, derBytes) | ||
cert.PrivateKey = priv | ||
return cert, nil | ||
} | ||
|
||
func initRuntime(bc bedrock.BuildContext) (bedrock.Runtime, error) { | ||
logHandler := slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{AddSource: true}) | ||
|
||
cert, err := createCert() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
rt := brhttp.NewRuntime( | ||
brhttp.ListenOnPort(8080), | ||
brhttp.LogHandler(logHandler), | ||
brhttp.TLSConfig(&tls.Config{ | ||
Certificates: []tls.Certificate{cert}, | ||
}), | ||
brhttp.Http2Only(), | ||
brhttp.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||
fmt.Fprint(w, "hello, world") | ||
}), | ||
) | ||
return rt, nil | ||
} | ||
|
||
func main() { | ||
bedrock.New( | ||
bedrock.InitTracerProvider(func(bc bedrock.BuildContext) (otelconfig.Initializer, error) { | ||
return otelconfig.Local( | ||
otelconfig.ServiceName("http2"), | ||
), nil | ||
}), | ||
bedrock.WithRuntimeBuilderFunc(initRuntime), | ||
).Run() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Copyright (c) 2023 Z5Labs and Contributors | ||
# | ||
# This software is released under the MIT License. | ||
# https://opensource.org/licenses/MIT | ||
|
||
FROM scratch | ||
EXPOSE 8080 | ||
COPY tls_http / | ||
ENTRYPOINT ["/tls_http"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Copyright (c) 2023 Z5Labs and Contributors | ||
// | ||
// This software is released under the MIT License. | ||
// https://opensource.org/licenses/MIT | ||
|
||
package main | ||
|
||
import ( | ||
"crypto/ed25519" | ||
"crypto/rand" | ||
"crypto/tls" | ||
"crypto/x509" | ||
"crypto/x509/pkix" | ||
"fmt" | ||
"log/slog" | ||
"math/big" | ||
"net/http" | ||
"os" | ||
"time" | ||
|
||
"github.com/z5labs/bedrock" | ||
brhttp "github.com/z5labs/bedrock/http" | ||
"github.com/z5labs/bedrock/pkg/otelconfig" | ||
) | ||
|
||
func createCert() (tls.Certificate, error) { | ||
_, priv, err := ed25519.GenerateKey(rand.Reader) | ||
if err != nil { | ||
return tls.Certificate{}, err | ||
} | ||
// ECDSA, ED25519 and RSA subject keys should have the DigitalSignature | ||
// KeyUsage bits set in the x509.Certificate template | ||
notBefore := time.Now() | ||
notAfter := notBefore.Add(365 * 24 * time.Hour) | ||
|
||
template := x509.Certificate{ | ||
SerialNumber: big.NewInt(time.Now().Unix()), | ||
Subject: pkix.Name{ | ||
Organization: []string{"Acme Co"}, | ||
}, | ||
NotBefore: notBefore, | ||
NotAfter: notAfter, | ||
SubjectKeyId: []byte{113, 117, 105, 99, 107, 115, 101, 114, 118, 101}, | ||
BasicConstraintsValid: true, | ||
IsCA: true, | ||
KeyUsage: x509.KeyUsageKeyEncipherment | | ||
x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign, | ||
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, | ||
} | ||
|
||
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, priv.Public().(ed25519.PublicKey), priv) | ||
if err != nil { | ||
return tls.Certificate{}, nil | ||
} | ||
|
||
var cert tls.Certificate | ||
cert.Certificate = append(cert.Certificate, derBytes) | ||
cert.PrivateKey = priv | ||
return cert, nil | ||
} | ||
|
||
func initRuntime(bc bedrock.BuildContext) (bedrock.Runtime, error) { | ||
logHandler := slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{AddSource: true}) | ||
|
||
cert, err := createCert() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
rt := brhttp.NewRuntime( | ||
brhttp.ListenOnPort(8080), | ||
brhttp.LogHandler(logHandler), | ||
brhttp.TLSConfig(&tls.Config{ | ||
Certificates: []tls.Certificate{cert}, | ||
}), | ||
brhttp.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||
fmt.Fprint(w, "hello, world") | ||
}), | ||
) | ||
return rt, nil | ||
} | ||
|
||
func main() { | ||
bedrock.New( | ||
bedrock.InitTracerProvider(func(bc bedrock.BuildContext) (otelconfig.Initializer, error) { | ||
return otelconfig.Local( | ||
otelconfig.ServiceName("tls_http"), | ||
), nil | ||
}), | ||
bedrock.WithRuntimeBuilderFunc(initRuntime), | ||
).Run() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.