Skip to content

Commit

Permalink
patch: get tls ConnectionState directly from request
Browse files Browse the repository at this point in the history
- `http.Server.ConnContext` is called before the tls connection is established, meaning themis' request builder `RequestBuilderFunc(setConnectionState)` will not find the context embedded tls ConnectionState.
- Have have `RequestBuilderFunc(setConnectionState)`get the tls ConnectionState directly from the request's `TLS` field instead from the context, since request's `TLS` field is the connection's ConnectionState.
  • Loading branch information
denopink committed Nov 18, 2024
1 parent d408d05 commit f0b0516
Show file tree
Hide file tree
Showing 5 changed files with 3 additions and 34 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ report.json

# for VSCode
.vscode/
.dev/

# for releases
.ignore/
Expand Down
4 changes: 2 additions & 2 deletions token/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ func (prb partnerIDRequestBuilder) Build(original *http.Request, tr *Request) er

// setConnectionState sets the tls.ConnectionState for the given request.
func setConnectionState(original *http.Request, tr *Request) error {
if cs, ok := xhttpserver.ConnectionState(original.Context()); ok {
tr.ConnectionState = cs
if original.TLS != nil {
tr.ConnectionState = *original.TLS

Check warning on line 214 in token/transport.go

View check run for this annotation

Codecov / codecov/patch

token/transport.go#L214

Added line #L214 was not covered by tests
}

return nil
Expand Down
13 changes: 0 additions & 13 deletions xhttp/xhttpserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package xhttpserver

import (
"context"
"crypto/tls"
"net"
"net/http"
"time"
Expand Down Expand Up @@ -99,18 +98,6 @@ func New(o Options, l *zap.Logger, h http.Handler) Interface {
o.Address,
l,
),

ConnContext: func(ctx context.Context, c net.Conn) context.Context {
type connectionStater interface {
ConnectionState() tls.ConnectionState
}

if cs, ok := c.(connectionStater); ok {
ctx = SetConnectionState(ctx, cs.ConnectionState())
}

return ctx
},
}

if o.LogConnectionState {
Expand Down
1 change: 0 additions & 1 deletion xhttp/xhttpserver/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ func testNewSimple(t *testing.T) {
assert.Greater(output.Len(), 0)

assert.Nil(s.(*http.Server).ConnState)
assert.NotNil(s.(*http.Server).ConnContext)
}

func testNewFull(t *testing.T) {
Expand Down
18 changes: 0 additions & 18 deletions xhttp/xhttpserver/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package xhttpserver

import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
Expand Down Expand Up @@ -250,20 +249,3 @@ func NewTlsConfig(t *Tls, extra ...PeerVerifier) (*tls.Config, error) {
tc.BuildNameToCertificate() // nolint: staticcheck
return tc, nil
}

type connectionStateKey struct{}

// ConnectionState returns the tls.ConnectionState from the given context.
func ConnectionState(ctx context.Context) (cs tls.ConnectionState, present bool) {
cs, present = ctx.Value(connectionStateKey{}).(tls.ConnectionState)
return
}

// SetConnectionState associates a tls.ConnectionState with the given context.
func SetConnectionState(ctx context.Context, cs tls.ConnectionState) context.Context {
return context.WithValue(
ctx,
connectionStateKey{},
cs,
)
}

0 comments on commit f0b0516

Please sign in to comment.