Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ import (

"github.com/tektoncd/results/pkg/api/server/v1alpha2/auth/impersonation"
"github.com/tektoncd/results/pkg/converter"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -427,7 +425,20 @@ func main() {
// Start server with gRPC and REST handler
log.Infof("gRPC and REST server listening on: %s", serverConfig.SERVER_PORT)
if tlsError != nil {
log.Fatal(http.ListenAndServe(":"+serverConfig.SERVER_PORT, grpcHandler(gs, httpMux)))
// Serve gRPC and REST on plaintext, including HTTP/2 without TLS (h2c)
// for gRPC clients, using net/http's native Protocols support. This
// supports h2c via prior knowledge (RFC 7540 section 3.4), which is
// what gRPC clients use, but not the HTTP/1.1 Upgrade: h2c handshake
// (RFC 7540 section 3.2) that golang.org/x/net/http2/h2c also handled.
var cleartextProtocols http.Protocols
cleartextProtocols.SetHTTP1(true)
cleartextProtocols.SetUnencryptedHTTP2(true)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documented the issue as a breaking change. It's more of a theoretical issue, but indeed it should be properly indicated.

cleartextServer := &http.Server{
Addr: ":" + serverConfig.SERVER_PORT,
Handler: grpcHandler(gs, httpMux),
Protocols: &cleartextProtocols,
}
log.Fatal(cleartextServer.ListenAndServe())
}

// Create HTTP server with TLS config
Expand All @@ -441,13 +452,13 @@ func main() {

// grpcHandler forwards the request to gRPC server based on the Content-Type header.
func grpcHandler(grpcServer *grpc.Server, httpHandler http.Handler) http.Handler {
return h2c.NewHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.ProtoMajor == 2 && strings.HasPrefix(r.Header.Get("Content-Type"), "application/grpc") {
grpcServer.ServeHTTP(w, r)
} else {
httpHandler.ServeHTTP(w, r)
}
}), &http2.Server{})
})
}

// recoveryHandler returns custom messages when server panics
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ require (
go.uber.org/automaxprocs v1.6.0
go.uber.org/zap v1.28.0
gocloud.dev v0.46.0
golang.org/x/net v0.60.0
golang.org/x/oauth2 v0.37.0
google.golang.org/api v0.297.0
google.golang.org/genproto/googleapis/api v0.0.0-20260727163830-6c54dddc4772
Expand Down Expand Up @@ -240,6 +239,7 @@ require (
go.yaml.in/yaml/v3 v3.0.4 // indirect
golang.org/x/crypto v0.57.0 // indirect
golang.org/x/exp v0.0.0-20260312153236-7ab1446f8b90 // indirect
golang.org/x/net v0.60.0 // indirect
golang.org/x/sync v0.23.0 // indirect
golang.org/x/sys v0.48.0 // indirect
golang.org/x/term v0.46.0 // indirect
Expand Down
Loading