-
Notifications
You must be signed in to change notification settings - Fork 49
node: send certificates for inter-node TLS connections #4097
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,27 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "crypto/tls" | ||
| "fmt" | ||
|
|
||
| grpcconfig "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config/grpc" | ||
| ) | ||
|
|
||
| func clientCertificateProvider(cfgs []grpcconfig.GRPC) func(*tls.CertificateRequestInfo) (*tls.Certificate, error) { | ||
| for i := range cfgs { | ||
| if !cfgs[i].TLS.Enabled { | ||
| continue | ||
| } | ||
|
|
||
| certFile, keyFile := cfgs[i].TLS.Certificate, cfgs[i].TLS.Key | ||
| return func(*tls.CertificateRequestInfo) (*tls.Certificate, error) { | ||
| cert, err := tls.LoadX509KeyPair(certFile, keyFile) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("reload TLS client certificate: %w", err) | ||
| } | ||
| return &cert, nil | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
This file contains hidden or 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,30 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| grpcconfig "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config/grpc" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestClientCertificateProvider(t *testing.T) { | ||
| require.Nil(t, clientCertificateProvider(nil)) | ||
|
|
||
| provider := clientCertificateProvider([]grpcconfig.GRPC{{ | ||
| TLS: grpcconfig.TLS{ | ||
| Enabled: true, | ||
| Certificate: "missing-certificate", | ||
| Key: "missing-key", | ||
| }, | ||
| }}) | ||
| require.NotNil(t, provider) | ||
| _, err := provider(nil) | ||
| require.ErrorContains(t, err, "reload TLS client certificate") | ||
|
|
||
| provider = clientCertificateProvider([]grpcconfig.GRPC{ | ||
| {TLS: grpcconfig.TLS{Enabled: false, Certificate: "ignored-certificate", Key: "ignored-key"}}, | ||
| {TLS: grpcconfig.TLS{Enabled: true, Certificate: "client-certificate", Key: "client-key"}}, | ||
| }) | ||
| _, err = provider(nil) | ||
| require.ErrorContains(t, err, "client-certificate") | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,46 @@ | ||
| package peerauth | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/ecdsa" | ||
| "crypto/elliptic" | ||
| "crypto/x509" | ||
| "fmt" | ||
|
|
||
| "github.com/nspcc-dev/neo-go/pkg/crypto/keys" | ||
| "google.golang.org/grpc/credentials" | ||
| "google.golang.org/grpc/peer" | ||
| ) | ||
|
|
||
| // CertificatePublicKey returns the P-256 public key from cert. | ||
| func CertificatePublicKey(cert *x509.Certificate) (*keys.PublicKey, error) { | ||
| pub, ok := cert.PublicKey.(*ecdsa.PublicKey) | ||
| if !ok { | ||
| return nil, fmt.Errorf("unsupported public key type %T", cert.PublicKey) | ||
| } | ||
| if pub.Curve != elliptic.P256() { | ||
| return nil, fmt.Errorf("unsupported elliptic curve %s", pub.Curve.Params().Name) | ||
| } | ||
| return (*keys.PublicKey)(pub), nil | ||
| } | ||
|
|
||
| // PeerPublicKey returns the public key authenticated by the TLS connection. | ||
| // It returns nil when the request has no TLS client certificate. | ||
| func PeerPublicKey(ctx context.Context) (*keys.PublicKey, error) { | ||
| p, ok := peer.FromContext(ctx) | ||
| if !ok { | ||
| return nil, nil | ||
| } | ||
| info, ok := p.AuthInfo.(credentials.TLSInfo) | ||
| if !ok { | ||
| return nil, nil | ||
| } | ||
| if len(info.State.PeerCertificates) == 0 { | ||
| return nil, nil | ||
| } | ||
| key, err := CertificatePublicKey(info.State.PeerCertificates[0]) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("invalid TLS peer certificate: %w", err) | ||
| } | ||
| return key, nil | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.