-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: extract peer address from nginx headers
- Added additional interceptors function to log the address - Tests to cover the client IP address case Signed-off-by: Rohit Dandamudi <rohit.dandamudi@siderolabs.com>
- Loading branch information
Rohit Dandamudi
committed
Nov 17, 2021
1 parent
a0e6313
commit d85ea91
Showing
5 changed files
with
93 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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 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,39 @@ | ||
// Copyright (c) 2021 Sidero Labs, Inc. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the LICENSE file. | ||
|
||
package server | ||
|
||
import ( | ||
"context" | ||
"net" | ||
|
||
"google.golang.org/grpc/metadata" | ||
"google.golang.org/grpc/peer" | ||
"inet.af/netaddr" | ||
) | ||
|
||
// PeerAddress is used to extract peer address from the client. | ||
// it will try to extract the actual client's IP when called via | ||
// Nginx ingress first if not it will get the nginx or the machine | ||
// which calls the server, if everything fails returns an empty address. | ||
func PeerAddress(ctx context.Context) netaddr.IP { | ||
if md, ok := metadata.FromIncomingContext(ctx); ok { | ||
if vals := md.Get("X-Real-IP"); vals != nil { | ||
if ip, err := netaddr.ParseIP(vals[0]); err == nil { | ||
return ip | ||
} | ||
} | ||
} | ||
|
||
if peer, ok := peer.FromContext(ctx); ok { | ||
if addr, ok := peer.Addr.(*net.TCPAddr); ok { | ||
if ip, ok := netaddr.FromStdIP(addr.IP); ok { | ||
return ip | ||
} | ||
} | ||
} | ||
|
||
return netaddr.IP{} | ||
} |
This file contains 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 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 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