Skip to content

Commit

Permalink
example(issue-34): add tls and http2 examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaba505 committed Dec 18, 2023
1 parent d8f356f commit a0da5d4
Show file tree
Hide file tree
Showing 5 changed files with 267 additions and 0 deletions.
64 changes: 64 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ before:
- go mod tidy -v

builds:
- id: http2
env:
- CGO_ENABLED=0
goos:
- linux
goarch:
- amd64
- arm64
goamd64:
- v3
main: example/http2/main.go
binary: http2

- id: otlp
env:
- CGO_ENABLED=0
Expand Down Expand Up @@ -58,7 +71,39 @@ builds:
main: example/simple_queue/main.go
binary: simple_queue

- id: tls_http
env:
- CGO_ENABLED=0
goos:
- linux
goarch:
- amd64
- arm64
goamd64:
- v3
main: example/tls_http/main.go
binary: tls_http

dockers:
- id: http2
goos: linux
goarch: amd64
goamd64: v3
ids:
- http2
image_templates:
- "ghcr.io/z5labs/bedrock/example/http2:latest"
- "ghcr.io/z5labs/bedrock/example/http2:{{ .Tag }}"
dockerfile: example/http2/Containerfile
use: docker
build_flag_templates:
- "--pull"
- "--label=org.opencontainers.image.created={{.Date}}"
- "--label=org.opencontainers.image.title={{.ProjectName}}"
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
- "--label=org.opencontainers.image.version={{.Version}}"
- "--platform=linux/amd64"

- id: otlp
goos: linux
goarch: amd64
Expand Down Expand Up @@ -134,3 +179,22 @@ dockers:
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
- "--label=org.opencontainers.image.version={{.Version}}"
- "--platform=linux/amd64"

- id: tls_http
goos: linux
goarch: amd64
goamd64: v3
ids:
- tls_http
image_templates:
- "ghcr.io/z5labs/bedrock/example/tls_http:latest"
- "ghcr.io/z5labs/bedrock/example/tls_http:{{ .Tag }}"
dockerfile: example/tls_http/Containerfile
use: docker
build_flag_templates:
- "--pull"
- "--label=org.opencontainers.image.created={{.Date}}"
- "--label=org.opencontainers.image.title={{.ProjectName}}"
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
- "--label=org.opencontainers.image.version={{.Version}}"
- "--platform=linux/amd64"
9 changes: 9 additions & 0 deletions example/http2/Containerfile
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"]
93 changes: 93 additions & 0 deletions example/http2/main.go
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()
}
9 changes: 9 additions & 0 deletions example/tls_http/Containerfile
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"]
92 changes: 92 additions & 0 deletions example/tls_http/main.go
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()
}

0 comments on commit a0da5d4

Please sign in to comment.