Skip to content

feat: sync the served environment set from the proxy config - #18

Open
gagantrivedi wants to merge 1 commit into
feat/environment-discoveryfrom
feat/proxy-config-discovery
Open

feat: sync the served environment set from the proxy config#18
gagantrivedi wants to merge 1 commit into
feat/environment-discoveryfrom
feat/proxy-config-discovery

Conversation

@gagantrivedi

@gagantrivedi gagantrivedi commented Aug 22, 2026

Copy link
Copy Markdown
Member

Thanks for submitting a PR! Please check the boxes below:

  • I have read the Contributing Guide.
  • I have added information to docs/ if required so people know about the feature.
  • I have filled in the "Changes" section below.
  • I have filled in the "How did you test this code" section below.

Changes

Contributes to Flagsmith/edge-proxy#128

Stacked on #17. With a proxy_key configured, the proxy keeps its served environments in sync with the proxy config endpoint (GET /api/v1/proxy/config/, X-Proxy-Key header): a newly created environment is served within one poll interval, rotations and removals propagate automatically, and per-environment configuration is no longer needed.

  • New proxy_key setting (a single key — organisation-level grants make one key multi-org).
  • models/proxy_config.rs: serde DTOs declaring only the fields acted on; everything else the endpoint sends is ignored.
  • EnvironmentIndex::sync_to(desired): inserts new/changed environments, removes absent ones — statically configured environments (the index remembers their keys) are never overridden, removed, or key-hijacked by the config.
  • Sync runs at the top of each poll pass, so new environments get their documents in the same tick. An environment is removed only when a successful fetch no longer lists it — an outage or a rejected proxy key never removes anything.
  • Environments with no usable server key (e.g. freshly created, keys not minted yet) are skipped rather than failing the poll, and a deactivated or expired server key stops authenticating on the next request.

How did you test this code?

cargo test: 64 green — reconcile unit tests plus tests/test_proxy_config_discovery.rs, wiremock against the frozen contract shape: environment added / dropped / key-rotated, static-never-removed, and fetch-failure-removes-nothing. Clippy and fmt clean.

@gagantrivedi
gagantrivedi force-pushed the feat/proxy-config-discovery branch from d49bc46 to 134525a Compare August 22, 2026 10:35
@gagantrivedi
gagantrivedi marked this pull request as ready for review August 22, 2026 10:36
@matthewelwell

Copy link
Copy Markdown

@CodeRabbit review

@coderabbitai

coderabbitai Bot commented Aug 24, 2026

Copy link
Copy Markdown
⚠️ Action not completed

Review rate limited.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai

coderabbitai Bot commented Aug 24, 2026

Copy link
Copy Markdown

Review Change Stack

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Team

Run ID: e6fe4ce4-1d92-4d6d-8d3c-446ab64689ba

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

The change adds an optional validated proxy_key setting and exposes proxy configuration models. EnvironmentIndex now protects static keys, rejects inactive or expired server keys, and synchronises dynamic environments. EnvironmentService fetches proxy configuration before cache refresh, removes unusable environments, and clears affected caches after removal or key rotation. Tests cover discovery, reconciliation, protection, failures, expiry, and rotation.

Estimated code review effort: 4 (Complex) | ~45 minutes

Merge Risk: 🟠 High · up to 13452

The change can allow expired environment keys to access cached documents, flags, or identity data, while overlapping refreshes may restore removed environments or stale keys. These security and correctness risks should be fixed before merging.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@gagantrivedi

Copy link
Copy Markdown
Member Author

@CodeRabbit review

@coderabbitai

coderabbitai Bot commented Aug 25, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 7a4fca1a-71b2-460e-9ff3-9c61e10aa615

📥 Commits

Reviewing files that changed from the base of the PR and between 6eeada4 and 134525a.

📒 Files selected for processing (7)
  • src/config/settings.rs
  • src/environments.rs
  • src/main.rs
  • src/models/mod.rs
  • src/models/proxy_config.rs
  • src/services/environment.rs
  • tests/test_proxy_config_discovery.rs

Included review availability: Your plan provides up to 1 included review per hour; 0 remain after this review.

Comment thread src/environments.rs
Comment thread src/services/environment.rs

@matthewelwell matthewelwell left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Full disclosure: I haven't reviewed the tests

Comment thread src/services/environment.rs Outdated
Comment on lines +106 to +110
self.cache.remove_environment(&keys.client_key).await;
self.clear_endpoint_caches(&keys).await;
self.purge_environment_caches(&keys).await;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I guess this question is valid for the previous code here, but I don't understand the 'purge' here - shouldn't we rebuild instead?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Also, if it simplifies it, I think we should just deprecate the endpoint caches - they are not worth keeping imo.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

gone with the endpoint cache

With a proxy_key configured, the proxy keeps its served environments in
sync with GET /api/v1/proxy/config/: new environments are served within
one poll interval, rotations and removals propagate, and per-environment
configuration is no longer needed.

- proxy_key setting (one key; organisation-level grants make it multi-org)
- models/proxy_config.rs: serde DTOs for the fields acted on
- EnvironmentIndex::sync_to(desired): insert new/changed, remove absent;
  statically configured environments are never overridden or removed
- sync runs at the top of each poll pass; a failed fetch removes nothing
- environments with no usable server key are skipped, and a deactivated
  or expired server key stops authenticating on the next request
@gagantrivedi
gagantrivedi force-pushed the feat/proxy-config-discovery branch from 90f27dd to 43c1d37 Compare September 5, 2026 10:13
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