-
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.
example(issue-34): add tls and http2 examples
- Loading branch information
Showing
5 changed files
with
267 additions
and
0 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() | ||
} |