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

feat: allow to conditionally block mutation via expressions #1480

Merged

Conversation

StarpTech
Copy link
Contributor

@StarpTech StarpTech commented Dec 25, 2024

Motivation and Context

This pull request introduces the capability to conditionally block mutations, subscriptions, and non-persisted operations. Instead of relying on YAML configuration alone, we have implemented support for dynamic expressions that are evaluated at runtime. Users can leverage request headers, URL parameters, query parameters, and authentication context (such as claims and scopes) to define the condition when operations are blocked. If no condition is provided all operations are blocked.

Example Configuration

Block by a header condition

security:
  block_mutations:
    enabled: true
    condition: |
      // Case insensitive
      request.header.Get('x-block-mutation') == 'yes'

Block by a scope condition

security:
  block_mutations:
    enabled: true
    condition: |
      'write' in request.auth.scopes

The expression parser and runtime are powered by expr-lang, a widely used library in the Go ecosystem. We chose expr for its elegant DSL and robust error reporting.

During router bootstrap, the system will validate that the expression are valid and returns the expected type (a boolean for conditions). If the expression evaluates to an incorrect type, the process will fail with a detailed error message.

This step forward, will also lay the foundation for a more dynamic approach in applying configuration or extracting values at runtime.

Warning

As part of the preparations for Cosmo V1, targeted for release in Q1 2025, this pull request introduces essential changes to enhance long-term stability and maintainability. While we strive to minimize breaking changes, they are sometimes necessary to lay the foundation for a more robust and scalable system. This update includes non-backward compatible modifications, specifically impacting the configuration of operation blocking mechanisms.

Checklist

Copy link

github-actions bot commented Dec 25, 2024

Router image scan passed

✅ No security vulnerabilities found in image:

ghcr.io/wundergraph/cosmo/router:sha-cb32dac23cb444d0b45784e4170a5a54c0d0e94d

@StarpTech StarpTech marked this pull request as ready for review January 2, 2025 09:33
router/internal/expr/expr.go Outdated Show resolved Hide resolved
router/internal/expr/expr.go Outdated Show resolved Hide resolved
router-tests/block_operations_test.go Show resolved Hide resolved
router-tests/block_operations_test.go Outdated Show resolved Hide resolved
router-tests/block_operations_test.go Outdated Show resolved Hide resolved
router/core/operation_blocker.go Show resolved Hide resolved
router/core/websocket.go Show resolved Hide resolved
router/internal/expr/expr.go Show resolved Hide resolved
router/internal/expr/expr.go Outdated Show resolved Hide resolved
router/pkg/config/config.go Outdated Show resolved Hide resolved
@StarpTech StarpTech requested review from Noroth and jensneuse January 2, 2025 17:33
Copy link
Contributor

@Noroth Noroth left a comment

Choose a reason for hiding this comment

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

Let me know about my last comment otherwise LGTM

router/internal/expr/expr.go Outdated Show resolved Hide resolved
router/core/websocket.go Outdated Show resolved Hide resolved
router/internal/expr/expr.go Outdated Show resolved Hide resolved
router/internal/expr/expr.go Outdated Show resolved Hide resolved
router/internal/expr/expr.go Outdated Show resolved Hide resolved
router/internal/expr/expr.go Show resolved Hide resolved
router/internal/expr/expr.go Show resolved Hide resolved
Copy link
Contributor

@Noroth Noroth left a comment

Choose a reason for hiding this comment

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

LGTM

@StarpTech StarpTech requested a review from jensneuse January 3, 2025 11:11
@StarpTech StarpTech merged commit 750f7dc into main Jan 3, 2025
13 checks passed
@StarpTech StarpTech deleted the dustin/eng-6203-allow-to-block-mutations-conditionally branch January 3, 2025 12:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants