Skip to content

feat: add connect_as field for service database credentials#338

Open
rshoemaker wants to merge 3 commits intomainfrom
feat/PLAT-539/service_connect_as
Open

feat: add connect_as field for service database credentials#338
rshoemaker wants to merge 3 commits intomainfrom
feat/PLAT-539/service_connect_as

Conversation

@rshoemaker
Copy link
Copy Markdown
Contributor

Summary

  • Services now specify which database_users entry to connect as via the new required connect_as field on ServiceSpec, replacing the auto-created svc_*_ro/svc_*_rw service accounts for MCP
  • Validates connect_as references an existing database_users entry, and that allow_writes: true requires db_owner: true
  • MCP config resource reads credentials from the spec instead of ServiceUserRole state
  • ServiceUserRole creation is disconnected for MCP (PostgREST/RAG still use it until they adopt connect_as)
  • State migration (v2.0.0) removes swarm.service_user_role resources and dependency references from existing deployments
  • E2E fixtures updated with connect_as

Test plan

  • Unit tests pass (976 tests)
  • Manual testing: RW mode (connect_as: "app", allow_writes: true) — reads and writes succeed
  • Manual testing: RO mode (connect_as: "app_read_only", allow_writes: false) — reads succeed, writes rejected with read-only transaction
  • Manual testing: config swap via update-database without container restart (SIGHUP reload)
  • Manual testing: pg_read_all_data covers spock schema, admin-created tables, other-user tables, custom schemas
  • Golden tests regenerated
  • State migration tested (removes service_user_role resources + dependency refs)
  • E2E tests against Lima fixture
  • Verify PostgREST/RAG provisioning is unaffected

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 10, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: a8d2c638-8409-414a-8373-b7f7bdaaee7f

📥 Commits

Reviewing files that changed from the base of the PR and between 1f90726 and 81e9b63.

⛔ Files ignored due to path filters (9)
  • api/apiv1/gen/control_plane/service.go is excluded by !**/gen/**
  • api/apiv1/gen/http/control_plane/client/encode_decode.go is excluded by !**/gen/**
  • api/apiv1/gen/http/control_plane/client/types.go is excluded by !**/gen/**
  • api/apiv1/gen/http/control_plane/server/encode_decode.go is excluded by !**/gen/**
  • api/apiv1/gen/http/control_plane/server/types.go is excluded by !**/gen/**
  • api/apiv1/gen/http/openapi.json is excluded by !**/gen/**
  • api/apiv1/gen/http/openapi.yaml is excluded by !**/gen/**
  • api/apiv1/gen/http/openapi3.json is excluded by !**/gen/**
  • api/apiv1/gen/http/openapi3.yaml is excluded by !**/gen/**
📒 Files selected for processing (16)
  • api/apiv1/design/database.go
  • e2e/service_provisioning_test.go
  • server/internal/api/apiv1/convert.go
  • server/internal/api/apiv1/validate.go
  • server/internal/api/apiv1/validate_test.go
  • server/internal/database/operations/golden_test/TestUpdateDatabase/add_service_to_existing_database.json
  • server/internal/database/operations/golden_test/TestUpdateDatabase/remove_service_from_existing_database.json
  • server/internal/database/operations/golden_test/TestUpdateDatabase/single_node_with_service_from_empty.json
  • server/internal/database/operations/helpers_test.go
  • server/internal/database/service_instance.go
  • server/internal/database/spec.go
  • server/internal/orchestrator/swarm/mcp_config_resource.go
  • server/internal/orchestrator/swarm/orchestrator.go
  • server/internal/orchestrator/swarm/service_instance.go
  • server/internal/orchestrator/swarm/service_instance_spec.go
  • server/internal/workflows/plan_update.go
💤 Files with no reviewable changes (4)
  • server/internal/database/operations/golden_test/TestUpdateDatabase/remove_service_from_existing_database.json
  • server/internal/database/operations/golden_test/TestUpdateDatabase/single_node_with_service_from_empty.json
  • server/internal/database/operations/helpers_test.go
  • server/internal/database/operations/golden_test/TestUpdateDatabase/add_service_to_existing_database.json
✅ Files skipped from review due to trivial changes (4)
  • server/internal/workflows/plan_update.go
  • server/internal/database/service_instance.go
  • server/internal/api/apiv1/validate_test.go
  • e2e/service_provisioning_test.go
🚧 Files skipped from review as they are similar to previous changes (5)
  • server/internal/api/apiv1/convert.go
  • server/internal/database/spec.go
  • api/apiv1/design/database.go
  • server/internal/api/apiv1/validate.go
  • server/internal/orchestrator/swarm/orchestrator.go

📝 Walkthrough

Walkthrough

Adds a new connect_as string to ServiceSpec, validates it against DatabaseUsers (with MCP write-owner constraints), propagates it through API conversions, database models, orchestrator resources, and workflows, and updates tests and golden fixtures to remove ServiceUserRole usage for MCP.

Changes

Cohort / File(s) Summary
API Schema & Conversion
api/apiv1/design/database.go, server/internal/api/apiv1/convert.go
Added connect_as/ConnectAs to ServiceSpec schema and mapped it in API↔DB conversion.
API Validation
server/internal/api/apiv1/validate.go, server/internal/api/apiv1/validate_test.go
Extended validateServiceSpec signature to accept DatabaseUsers and added validateConnectAs enforcing existence and MCP db_owner when allow_writes=true. Tests updated and new unit tests added.
Database Model & Clone
server/internal/database/spec.go, server/internal/database/service_instance.go
Added ConnectAs to ServiceSpec; ServiceInstanceSpec gains ConnectAsUsername and ConnectAsPassword. Clone updated.
Workflows
server/internal/workflows/plan_update.go
getServiceResources now resolves connect_as against spec.DatabaseUsers, populating ConnectAsUsername/ConnectAsPassword or returning an error if missing.
Orchestrator: MCP & Service Instances
server/internal/orchestrator/swarm/mcp_config_resource.go, server/internal/orchestrator/swarm/orchestrator.go, server/internal/orchestrator/swarm/service_instance.go, server/internal/orchestrator/swarm/service_instance_spec.go
Replaced RO/RW credential fields with unified connect_as_username/connect_as_password in MCPConfigResource (version bump). Removed unconditional ServiceUserRole creation/dependencies for mcp; wired resolved credentials into MCP config and skipped role-based credential lookups. ServiceInstanceResource now includes ServiceType.
Operations Golden Tests & Helpers
server/internal/database/operations/golden_test/.../*.json, server/internal/database/operations/helpers_test.go
Removed expected create/delete steps and test stub for swarm.service_user_role and cleaned up related dependencies in helpers.
E2E Tests
e2e/service_provisioning_test.go
Updated service specs in e2e tests to set ConnectAs: "admin" where applicable.

Poem

🐰 I nibble lines of schema bright,
connect_as tucked in, snug and light.
One user hops, credentials shared,
MCP sheds the roles it bared.
Code carrots ready—deploy tonight!

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The PR title accurately summarizes the main feature addition: introducing a required connect_as field for services to specify database credentials.
Description check ✅ Passed The PR description is comprehensive and well-structured, covering summary, changes, test plan, and addressing checklist items including testing, issue linking, and migration considerations.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/PLAT-539/service_connect_as

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 and usage tips.

@codacy-production
Copy link
Copy Markdown

codacy-production bot commented Apr 10, 2026

Up to standards ✅

🟢 Issues 1 medium

Results:
1 new issue

Category Results
Complexity 1 medium

View in Codacy

🟢 Metrics 5 complexity · -2 duplication

Metric Results
Complexity 5
Duplication -2

View in Codacy

TIP This summary will be updated as you push new changes. Give us feedback

Copy link
Copy Markdown

@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: 4

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@server/internal/orchestrator/swarm/mcp_config_resource.go`:
- Around line 55-60: Remove the connect_as credential paths from
MCPConfigResource.DiffIgnore so changes to "/connect_as_username" and
"/connect_as_password" will be detected and trigger Update; additionally, adjust
Refresh logic (related to MCPConfigResource.Refresh) to only check for the
existence of "config.yaml" and avoid rewriting runtime-owned files
("tokens.yaml" and "users.yaml") so those files remain runtime-managed while
config.yaml presence is used as the sole refresh existence check.

In `@server/internal/orchestrator/swarm/service_instance_spec.go`:
- Around line 93-96: The MCP branch in populateCredentials returns early without
clearing existing credentials, which can leave stale secrets in s.Credentials;
modify populateCredentials so that when s.ServiceSpec.ServiceType == "mcp" you
explicitly reset/clear s.Credentials (e.g., set to nil or an empty credentials
struct) before returning, ensuring old credentials are not carried forward.

In `@server/internal/resource/migrations/2_0_0.go`:
- Around line 19-35: The migration currently deletes all swarm.service_user_role
entries; instead restrict deletions to MCP-owned role state only: in
Version_2_0_0.Run, when iterating state.Resources for serviceUserRoleType and
when filtering data.Dependencies, check each resource's ownership/metadata
(e.g., an owner/managedBy field on the resource or data.Metadata) and only
remove resources and dependency edges where that ownership indicates MCP (e.g.,
managedBy == "mcp"); leave non-MCP service_user_role resources and their
dependency edges untouched so ServiceUserRole resolution in ServiceInstanceSpec
and rag_config_resource continues to work.

In `@server/internal/workflows/plan_update.go`:
- Around line 126-138: The planner currently copies the referenced user's
ConnectAsPassword from spec.DatabaseUsers into the service without validating
it; if the referenced database.User has an empty password the plan will succeed
but service DB auth will fail. Update the lookup block that handles
serviceSpec.ConnectAs in plan_update.go (where connectAsUser is resolved from
spec.DatabaseUsers) to reject empty secrets: after finding database.User (type
database.User) check that its Password (or ConnectAsPassword field used when
assigning to the service) is non-empty and return an error like "connect_as user
%q has empty password" if empty; apply the same validation for the additional
occurrence noted around lines 154-156 so the planner never copies an empty
ConnectAsPassword into the service spec.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: ab39b018-ec33-43a9-9704-06e8bac3b037

📥 Commits

Reviewing files that changed from the base of the PR and between 93f273f and d2d43c5.

⛔ Files ignored due to path filters (9)
  • api/apiv1/gen/control_plane/service.go is excluded by !**/gen/**
  • api/apiv1/gen/http/control_plane/client/encode_decode.go is excluded by !**/gen/**
  • api/apiv1/gen/http/control_plane/client/types.go is excluded by !**/gen/**
  • api/apiv1/gen/http/control_plane/server/encode_decode.go is excluded by !**/gen/**
  • api/apiv1/gen/http/control_plane/server/types.go is excluded by !**/gen/**
  • api/apiv1/gen/http/openapi.json is excluded by !**/gen/**
  • api/apiv1/gen/http/openapi.yaml is excluded by !**/gen/**
  • api/apiv1/gen/http/openapi3.json is excluded by !**/gen/**
  • api/apiv1/gen/http/openapi3.yaml is excluded by !**/gen/**
📒 Files selected for processing (26)
  • api/apiv1/design/database.go
  • e2e/service_provisioning_test.go
  • server/internal/api/apiv1/convert.go
  • server/internal/api/apiv1/validate.go
  • server/internal/api/apiv1/validate_test.go
  • server/internal/database/operations/golden_test/TestUpdateDatabase/add_service_to_existing_database.json
  • server/internal/database/operations/golden_test/TestUpdateDatabase/remove_service_from_existing_database.json
  • server/internal/database/operations/golden_test/TestUpdateDatabase/single_node_with_service_from_empty.json
  • server/internal/database/operations/helpers_test.go
  • server/internal/database/service_instance.go
  • server/internal/database/spec.go
  • server/internal/orchestrator/swarm/mcp_config_resource.go
  • server/internal/orchestrator/swarm/orchestrator.go
  • server/internal/orchestrator/swarm/service_instance.go
  • server/internal/orchestrator/swarm/service_instance_spec.go
  • server/internal/resource/migrations/2_0_0.go
  • server/internal/resource/migrations/2_0_0_test.go
  • server/internal/resource/migrations/golden_test/TestVersion_1_0_0/empty.json
  • server/internal/resource/migrations/golden_test/TestVersion_1_0_0/no_nodes.json
  • server/internal/resource/migrations/golden_test/TestVersion_1_0_0/populate_n3_with_n1_source.json
  • server/internal/resource/migrations/golden_test/TestVersion_1_0_0/single_node_with_replicas.json
  • server/internal/resource/migrations/golden_test/TestVersion_1_0_0/three_nodes.json
  • server/internal/resource/migrations/golden_test/TestVersion_1_0_0/with_restore_config.json
  • server/internal/resource/migrations/provide.go
  • server/internal/resource/state.go
  • server/internal/workflows/plan_update.go
💤 Files with no reviewable changes (4)
  • server/internal/database/operations/golden_test/TestUpdateDatabase/remove_service_from_existing_database.json
  • server/internal/database/operations/golden_test/TestUpdateDatabase/add_service_to_existing_database.json
  • server/internal/database/operations/golden_test/TestUpdateDatabase/single_node_with_service_from_empty.json
  • server/internal/database/operations/helpers_test.go

Services now specify which database_users entry to connect as via the
new required connect_as field on ServiceSpec, replacing the auto-created
svc_*_ro and svc_*_rw service accounts for MCP.

- Add connect_as to Goa DSL, domain model, and API convert layer
- Validate connect_as references an existing database_users entry
- Validate allow_writes requires db_owner on the connect_as user
- Wire connect_as credentials through ServiceInstanceSpec into
  MCPConfigResource, bypassing ServiceUserRole for MCP
- Disconnect ServiceUserRole creation for MCP services (PostgREST
  and RAG still use it until they adopt connect_as)
- Add state migration (v2.0.0) to remove swarm.service_user_role
  resources and dependency references from existing deployments
- Update E2E test fixtures with connect_as
…nore so credential changes trigger an Update
@rshoemaker rshoemaker force-pushed the feat/PLAT-539/service_connect_as branch from 1f90726 to 81e9b63 Compare April 13, 2026 18:28
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.

1 participant