Merge pull request #485 from iamqizhao/master
Expose peer info on server side
diff --git a/credentials/credentials.go b/credentials/credentials.go
index cde38dc..681f64e 100644
--- a/credentials/credentials.go
+++ b/credentials/credentials.go
@@ -87,19 +87,6 @@
AuthType() string
}
-type authInfoKey struct{}
-
-// NewContext creates a new context with authInfo attached.
-func NewContext(ctx context.Context, authInfo AuthInfo) context.Context {
- return context.WithValue(ctx, authInfoKey{}, authInfo)
-}
-
-// FromContext returns the authInfo in ctx if it exists.
-func FromContext(ctx context.Context) (authInfo AuthInfo, ok bool) {
- authInfo, ok = ctx.Value(authInfoKey{}).(AuthInfo)
- return
-}
-
// TransportAuthenticator defines the common interface for all the live gRPC wire
// protocols and supported transport security protocols (e.g., TLS, SSL).
type TransportAuthenticator interface {
diff --git a/peer/peer.go b/peer/peer.go
new file mode 100644
index 0000000..bfa6205
--- /dev/null
+++ b/peer/peer.go
@@ -0,0 +1,65 @@
+/*
+ *
+ * Copyright 2014, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+// Package peer defines various peer information associated with RPCs and
+// corresponding utils.
+package peer
+
+import (
+ "net"
+
+ "golang.org/x/net/context"
+ "google.golang.org/grpc/credentials"
+)
+
+// Peer contains the information of the peer for an RPC.
+type Peer struct {
+ // Addr is the peer address.
+ Addr net.Addr
+ // AuthInfo is the authentication information of the transport.
+ // It is nil if there is no transport security being used.
+ AuthInfo credentials.AuthInfo
+}
+
+type peerKey struct{}
+
+// NewContext creates a new context with peer information attached.
+func NewContext(ctx context.Context, p *Peer) context.Context {
+ return context.WithValue(ctx, peerKey{}, p)
+}
+
+// FromContext returns the peer information in ctx if it exists.
+func FromContext(ctx context.Context) (p *Peer, ok bool) {
+ p, ok = ctx.Value(peerKey{}).(*Peer)
+ return
+}
diff --git a/test/end2end_test.go b/test/end2end_test.go
index 71d0204..93944f8 100644
--- a/test/end2end_test.go
+++ b/test/end2end_test.go
@@ -53,6 +53,7 @@
"google.golang.org/grpc/health"
healthpb "google.golang.org/grpc/health/grpc_health_v1alpha"
"google.golang.org/grpc/metadata"
+ "google.golang.org/grpc/peer"
testpb "google.golang.org/grpc/test/grpc_testing"
)
@@ -110,14 +111,17 @@
}
grpc.SetTrailer(ctx, md)
}
+ pr, ok := peer.FromContext(ctx)
+ if !ok {
+ return nil, fmt.Errorf("failed to get peer from ctx")
+ }
+ if pr.Addr == net.Addr(nil) {
+ return nil, fmt.Errorf("failed to get peer address")
+ }
if s.security != "" {
// Check Auth info
- authInfo, ok := credentials.FromContext(ctx)
- if !ok {
- return nil, fmt.Errorf("Failed to get AuthInfo from ctx.")
- }
var authType, serverName string
- switch info := authInfo.(type) {
+ switch info := pr.AuthInfo.(type) {
case credentials.TLSInfo:
authType = info.AuthType()
serverName = info.State.ServerName
diff --git a/transport/http2_client.go b/transport/http2_client.go
index 07b0c11..9eae37d 100644
--- a/transport/http2_client.go
+++ b/transport/http2_client.go
@@ -50,6 +50,7 @@
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/grpclog"
"google.golang.org/grpc/metadata"
+ "google.golang.org/grpc/peer"
)
// http2Client implements the ClientTransport interface with HTTP2.
@@ -238,10 +239,14 @@
return nil, ContextErr(context.DeadlineExceeded)
}
}
+ pr := &peer.Peer{
+ Addr: t.conn.RemoteAddr(),
+ }
// Attach Auth info if there is any.
if t.authInfo != nil {
- ctx = credentials.NewContext(ctx, t.authInfo)
+ pr.AuthInfo = t.authInfo
}
+ ctx = peer.NewContext(ctx, pr)
authData := make(map[string]string)
for _, c := range t.authCreds {
// Construct URI required to get auth request metadata.
diff --git a/transport/http2_server.go b/transport/http2_server.go
index e16c63c..98088d9 100644
--- a/transport/http2_server.go
+++ b/transport/http2_server.go
@@ -49,6 +49,7 @@
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/grpclog"
"google.golang.org/grpc/metadata"
+ "google.golang.org/grpc/peer"
)
// ErrIllegalHeaderWrite indicates that setting header is illegal because of
@@ -168,10 +169,14 @@
} else {
s.ctx, s.cancel = context.WithCancel(context.TODO())
}
+ pr := &peer.Peer{
+ Addr: t.conn.RemoteAddr(),
+ }
// Attach Auth info if there is any.
if t.authInfo != nil {
- s.ctx = credentials.NewContext(s.ctx, t.authInfo)
+ pr.AuthInfo = t.authInfo
}
+ s.ctx = peer.NewContext(s.ctx, pr)
// Cache the current stream to the context so that the server application
// can find out. Required when the server wants to send some metadata
// back to the client (unary call only).