Skip to content
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

Upgrade to github.com/grpc-ecosystem/go-grpc-middleware/v2 #1370

Merged
merged 1 commit into from
Sep 9, 2024

Conversation

Danielius1922
Copy link
Member

No description provided.

Copy link
Contributor

coderabbitai bot commented Sep 5, 2024

Walkthrough

The changes involve updating the gRPC middleware from version 1 to version 2 across multiple files in the codebase. This includes modifications to import paths and function calls related to authentication and metadata handling. Additionally, logging functionality in the gRPC server implementation has been streamlined, with the removal of several logging helper functions and the introduction of a new logging structure.

Changes

Files Change Summary
pkg/net/grpc/auth.go, pkg/net/grpc/authFromOutgoingMD.go, pkg/net/grpc/interceptor.go Updated import paths and function calls to transition from version 1 to version 2 of the gRPC middleware, modifying how authentication and metadata are handled.
pkg/net/grpc/server/makeDefaultOptions.go Removed several logging helper functions and replaced them with a new function for logging fields. Refactored context handling for the logger to improve integration.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Server
    participant AuthMiddleware
    participant Logger

    Client->>Server: gRPC Request
    Server->>AuthMiddleware: Extract Token
    AuthMiddleware->>Server: Token Validated
    Server->>Logger: Log Request Details
    Logger-->>Server: Log Confirmation
    Server-->>Client: gRPC Response
Loading

🐰 "In the meadow, changes bloom,
With hops of joy, we make room.
Middleware dances, logging's bright,
A new dawn's here, oh what delight!
With every hop, we leap and cheer,
For progress made, we hold so dear!" 🌼

Tip

New features

Walkthrough comment now includes:

  • Possibly related PRs: A list of potentially related PRs to help you recall past context.
  • Suggested labels: CodeRabbit can now suggest labels by learning from your past PRs. You can also provide custom labeling instructions in the UI or configuration file.

Notes:

  • Please share any feedback in the discussion post on our Discord.
  • Possibly related PRs, automatic label suggestions based on past PRs, learnings, and possibly related issues require data opt-in (enabled by default).

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@Danielius1922 Danielius1922 marked this pull request as ready for review September 6, 2024 09:58
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 5091170 and 465951b.

Files ignored due to path filters (2)
  • go.mod is excluded by !**/*.mod
  • go.sum is excluded by !**/*.sum, !**/*.sum
Files selected for processing (4)
  • pkg/net/grpc/auth.go (3 hunks)
  • pkg/net/grpc/authFromOutgoingMD.go (3 hunks)
  • pkg/net/grpc/interceptor.go (1 hunks)
  • pkg/net/grpc/server/makeDefaultOptions.go (5 hunks)
Additional comments not posted (15)
pkg/net/grpc/interceptor.go (3)

Line range hint 14-21: Verify the use of WrapServerStream under the new middleware version.

The function StreamServerInterceptor uses WrapServerStream from the new middleware version. Ensure that this method's behavior hasn't changed in a way that affects the interceptor's functionality.

Run the following script to verify the specific use of WrapServerStream:


Line range hint 23-31: Verify the use of unary interceptor under the new middleware version.

The function UnaryServerInterceptor uses the new middleware version. Ensure that the unary interceptor's behavior hasn't changed in a way that affects its functionality.

Run the following script to verify the specific use of the unary interceptor:


6-6: Verify compatibility with the new middleware version.

The import statement has been updated to use version 2 of the go-grpc-middleware. It's crucial to ensure that the new version's features and breaking changes are compatible with the existing code.

Run the following script to verify the compatibility:

pkg/net/grpc/authFromOutgoingMD.go (3)

7-8: Verify compatibility with the new middleware version.

The import paths for authentication and metadata have been updated to use version 2 of the go-grpc-middleware. It's crucial to ensure that the new version's features and breaking changes are compatible with the existing code.

Run the following script to verify the compatibility:


40-40: Verify the use of AuthFromMD under the new middleware version.

The function TokenFromMD uses AuthFromMD from the new middleware version. Ensure that this method's behavior hasn't changed in a way that affects the function's functionality.

Run the following script to verify the specific use of AuthFromMD:


24-24: Verify the use of ExtractOutgoing under the new middleware version.

The function TokenFromOutgoingMD uses ExtractOutgoing from the new middleware version. Ensure that this method's behavior hasn't changed in a way that affects the function's functionality.

Run the following script to verify the specific use of ExtractOutgoing:

pkg/net/grpc/auth.go (4)

52-52: Verify the use of AuthFromMD under the new middleware version.

The function ValidateJWTWithValidator uses AuthFromMD from the new middleware version. Ensure that this method's behavior hasn't changed in a way that affects the function's functionality.

Run the following script to verify the specific use of AuthFromMD:


73-73: Verify the use of ExtractIncoming under the new middleware version.

The function CtxWithIncomingToken uses ExtractIncoming from the new middleware version. Ensure that this method's behavior hasn't changed in a way that affects the function's functionality.

Run the following script to verify the specific use of ExtractIncoming:


66-66: Verify the use of ExtractOutgoing under the new middleware version.

The function CtxWithToken uses ExtractOutgoing from the new middleware version. Ensure that this method's behavior hasn't changed in a way that affects the function's functionality.

Run the following script to verify the specific use of ExtractOutgoing:


8-9: Verify compatibility with the new middleware version.

The import paths for authentication and metadata have been updated to use version 2 of the go-grpc-middleware. It's crucial to ensure that the new version's features and breaking changes are compatible with the existing code.

Run the following script to verify the compatibility:

pkg/net/grpc/server/makeDefaultOptions.go (5)

9-9: Approved: Import changes and new constant introduction.

The new imports support the updated logging functionality, and the introduction of loggerCtxMarkerKey is crucial for the new context handling strategy.

Also applies to: 29-32


99-138: Approved: Refactoring of defaultMessageProducer.

The refactoring enhances clarity and efficiency by consolidating the logic into fewer functions. Verify the correct implementation of the new context handling strategy, particularly the use of loggerCtxMarkerKey.

Run the following script to verify the correct implementation of the new context handling strategy:

Verification successful

Verified: Correct implementation of context handling strategy using loggerCtxMarkerKey.

The loggerCtxMarkerKey is correctly used to store and retrieve the logger from the context within the pkg/net/grpc/server/makeDefaultOptions.go file. The context handling strategy is implemented as intended. No further issues were found.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the correct implementation of the new context handling strategy.

# Test: Search for the usage of `loggerCtxMarkerKey`. Expect: Correct implementation across the codebase.
rg --type go -A 5 $'loggerCtxMarkerKey'

Length of output: 1531


220-221: Approved: Update to MakeDefaultOptions.

The updated interceptor chaining improves modularity and efficiency. Verify the correct implementation of this setup.

Run the following script to verify the correct implementation of the interceptor chaining:


142-142: Approved: Update to MakeDefaultMessageProducer.

The use of context.WithValue to store the logger simplifies context management and improves integration. Verify the correct usage of this method across the codebase.

Run the following script to verify the correct usage of context.WithValue:

Verification successful

Verification Successful: Correct Usage of context.WithValue

The keys used with context.WithValue are defined uniquely and are not exported, ensuring proper encapsulation and usage. The implementation across the codebase adheres to best practices. No issues found.

  • oauth2.HTTPClient: Used in multiple files, related to HTTP client configuration.
  • &authCtxKey and &log.StartTimeKey: Defined as pointers in coap-gateway/service/service.go.
  • loggerCtxMarkerKey: Defined as a struct type in pkg/net/grpc/server/makeDefaultOptions.go.
  • authorizationKey: Defined in pkg/net/grpc/auth.go and pkg/net/http/jwt/token.go.
  • lablelerContextKey: Defined as a custom type in pkg/opentelemetry/otelhttp/labeler.go.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the correct usage of `context.WithValue`.

# Test: Search for the usage of `context.WithValue`. Expect: Correct implementation across the codebase.
rg --type go -A 5 $'context.WithValue'

Length of output: 4470


Script:

#!/bin/bash
# Description: Verify the uniqueness and visibility of keys used with `context.WithValue`.

# Extract keys used with `context.WithValue` and search for their definitions.
rg --type go -o 'context.WithValue\([^,]+, ([^,]+),' | awk -F', ' '{print $2}' | sort | uniq | while read -r key; do
  echo "Searching for key definition: $key"
  rg --type go -A 3 -B 3 "$key"
done

Length of output: 257


Script:

#!/bin/bash
# Description: Re-verify the uniqueness and visibility of keys used with `context.WithValue`.

# Extract keys used with `context.WithValue` and search for their definitions.
rg --type go 'context.WithValue\([^,]+, ([^,]+),' -o -r '$1' | sort | uniq | while read -r key; do
  echo "Searching for key definition: $key"
  rg --type go -A 3 -B 3 "$key"
done

Length of output: 381


Script:

#!/bin/bash
# Description: Re-verify the uniqueness and visibility of keys used with `context.WithValue`.

# Extract keys used with `context.WithValue` and search for their definitions.
rg --type go 'context.WithValue\([^,]+, ([^,]+),' -o -r '$1' | sort | uniq | while read -r key; do
  # Remove any file path prefix from the key
  clean_key=$(echo "$key" | sed 's/.*://')
  echo "Searching for key definition: $clean_key"
  rg --type go -A 3 -B 3 "$clean_key"
done

Length of output: 830


Script:

#!/bin/bash
# Description: Search for the definitions of keys used with `context.WithValue`.

# List of keys to search for
keys=("oauth2.HTTPClient" "&authCtxKey" "&log.StartTimeKey" "loggerCtxMarkerKey" "authorizationKey" "lablelerContextKey")

# Search for the definition of each key
for key in "${keys[@]}"; do
  echo "Searching for definition of key: $key"
  rg --type go -A 3 -B 3 "$key"
done

Length of output: 11214


62-78: Approved: Implementation of toZapFields.

The function efficiently converts logging fields into a format compatible with the Zap logger. Ensure that all necessary data types are covered in the switch-case structure.

Run the following script to verify the coverage of data types:

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 465951b and 518da0f.

Files ignored due to path filters (2)
  • go.mod is excluded by !**/*.mod
  • go.sum is excluded by !**/*.sum, !**/*.sum
Files selected for processing (4)
  • pkg/net/grpc/auth.go (3 hunks)
  • pkg/net/grpc/authFromOutgoingMD.go (3 hunks)
  • pkg/net/grpc/interceptor.go (1 hunks)
  • pkg/net/grpc/server/makeDefaultOptions.go (5 hunks)
Files skipped from review as they are similar to previous changes (3)
  • pkg/net/grpc/auth.go
  • pkg/net/grpc/authFromOutgoingMD.go
  • pkg/net/grpc/interceptor.go
Additional comments not posted (6)
pkg/net/grpc/server/makeDefaultOptions.go (6)

9-9: Approved import addition.

The addition of github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging aligns with the changes in logging functionality described in the PR.


29-32: Approved context marker changes.

The introduction of loggerCtxMarker and loggerCtxMarkerKey simplifies the context management for logging, aligning with the PR's objectives to improve logger integration.


81-114: Approved streamlined logging function.

The function defaultMessageFields effectively consolidates logging field extraction and construction, enhancing both clarity and efficiency in the logging process.


136-143: Approved refactored message producer function.

The refactoring of defaultMessageProducer to use the new logging structure and context handling improves the integration of the logger with the gRPC context, aligning with the PR's objectives.


147-147: Approved function wrapper for message production.

The function MakeDefaultMessageProducer effectively wraps the defaultMessageProducer with a custom context, simplifying the integration of the logger with the gRPC context.


225-226: Approved updates to interceptor chaining.

The updates to the chaining of stream and unary interceptors align with the PR's objectives to improve the gRPC server configuration.

pkg/net/grpc/server/makeDefaultOptions.go Show resolved Hide resolved
Copy link

sonarcloud bot commented Sep 9, 2024

Quality Gate Failed Quality Gate failed

Failed conditions
14.3% Coverage on New Code (required ≥ 75%)

See analysis details on SonarCloud

@Danielius1922 Danielius1922 merged commit 5369297 into main Sep 9, 2024
41 of 42 checks passed
@Danielius1922 Danielius1922 deleted the adam/feature/grpc-middleware-v2 branch September 9, 2024 11:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants