-
Notifications
You must be signed in to change notification settings - Fork 229
Add grpc interceptors #938
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
base: master
Are you sure you want to change the base?
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #938 +/- ##
==========================================
+ Coverage 84.00% 84.32% +0.31%
==========================================
Files 50 52 +2
Lines 5171 5340 +169
==========================================
+ Hits 4344 4503 +159
- Misses 673 680 +7
- Partials 154 157 +3 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
grpc/server.go
Outdated
// CaptureRequestBody determines whether to capture and send request bodies to Sentry. | ||
CaptureRequestBody bool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be handled by the base SDK.
grpc/server.go
Outdated
// OperationName overrides the default operation name (grpc.server). | ||
OperationName string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's not expose this to users.
grpc/server.go
Outdated
|
||
options := []sentry.SpanOption{ | ||
sentry.ContinueTrace(hub, sentryTraceHeader, sentryBaggageHeader), | ||
sentry.WithOpName(opts.OperationName), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's hard code this grpc.server
.
examplepb.RegisterExampleServiceServer(server, &ExampleServiceServer{}) | ||
|
||
// Start the server | ||
listener, err := net.Listen("tcp", grpcPort) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Semgrep identified an issue in your code:
Detected a network listener listening on 0.0.0.0 or an empty string. This could unexpectedly expose the server publicly as it binds to all available interfaces. Instead, specify another IP address that is not 0.0.0.0 nor the empty string.
To resolve this comment:
✨ Commit Assistant Fix Suggestion
- Update the value of
grpcPort
so it is not just a port or set to0.0.0.0
.
For example, ifgrpcPort
is":50051"
, change it to"127.0.0.1:50051"
or another appropriate interface (like your private network IP address). - If you need the server to be accessible only from the local machine, use
"127.0.0.1:<port>"
as the address when callingnet.Listen
. - If you do need remote access, restrict the IP address as much as possible to only the needed network interface, rather than using
0.0.0.0
or a blank string. - Example:
listener, err := net.Listen("tcp", "127.0.0.1:50051")
When a server binds to 0.0.0.0
or just the port (like ":50051"
), it listens on all interfaces, which could make your service accessible from unwanted sources. Use a specific IP to limit access.
💬 Ignore this finding
Reply with Semgrep commands to ignore this finding.
/fp <comment>
for false positive/ar <comment>
for acceptable risk/other <comment>
for all other reasons
Alternatively, triage in Semgrep AppSec Platform to ignore the finding created by avoid-bind-to-all-interfaces.
You can view more details about this finding in the Semgrep AppSec Platform.
grpc/client.go
Outdated
// OperationName overrides the default operation name (grpc.client). | ||
OperationName string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's remove this here as well.
grpc/server.go
Outdated
// ReportOn defines the conditions under which errors are reported to Sentry. | ||
ReportOn func(error) bool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's remove this
grpc/server.go
Outdated
func reportErrorToSentry(hub *sentry.Hub, err error, methodName string, req any, md map[string]string) { | ||
hub.WithScope(func(scope *sentry.Scope) { | ||
scope.SetExtras(map[string]any{ | ||
"grpc.method": methodName, | ||
"grpc.error": err.Error(), | ||
}) | ||
|
||
if req != nil { | ||
scope.SetExtra("request", req) | ||
} | ||
|
||
if len(md) > 0 { | ||
scope.SetExtra("metadata", md) | ||
} | ||
|
||
defer hub.CaptureException(err) | ||
|
||
statusErr, ok := status.FromError(err) | ||
if !ok { | ||
return | ||
} | ||
|
||
for _, detail := range statusErr.Details() { | ||
debugInfo, ok := detail.(*errdetails.DebugInfo) | ||
if !ok { | ||
continue | ||
} | ||
hub.AddBreadcrumb(&sentry.Breadcrumb{ | ||
Type: "debug", | ||
Category: "grpc.server", | ||
Message: debugInfo.Detail, | ||
Data: map[string]any{"stackTrace": strings.Join(debugInfo.StackEntries, "\n")}, | ||
Level: sentry.LevelError, | ||
Timestamp: time.Now(), | ||
}, nil) | ||
} | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reported errors here add zero value. Let's remove this.
Resolves #240
Sample error logged to Sentry:
Possible improvements / todos:
sentry
andsentry-docs