-
Notifications
You must be signed in to change notification settings - Fork 99
refactor(agents-core): enforce data-access layer boundary for auth queries #2196
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: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { eq } from 'drizzle-orm'; | ||
| import { member, ssoProvider } from '../../auth/auth-schema'; | ||
| import type { AgentsRunDatabaseClient } from '../../db/runtime/runtime-client'; | ||
|
|
||
| export const getInitialOrganizationForUser = | ||
| (db: AgentsRunDatabaseClient) => | ||
| async (userId: string): Promise<{ organizationId: string } | null> => { | ||
| const [result] = await db | ||
| .select({ organizationId: member.organizationId }) | ||
| .from(member) | ||
| .where(eq(member.userId, userId)) | ||
| .orderBy(member.createdAt) | ||
| .limit(1); | ||
|
|
||
| return result ?? null; | ||
| }; | ||
|
|
||
| export const getSSOProviderByProviderId = | ||
| (db: AgentsRunDatabaseClient) => | ||
| async (providerId: string): Promise<{ id: string } | null> => { | ||
| const [result] = await db | ||
| .select({ id: ssoProvider.id }) | ||
| .from(ssoProvider) | ||
| .where(eq(ssoProvider.providerId, providerId)) | ||
| .limit(1); | ||
|
|
||
| return result ?? null; | ||
| }; | ||
|
|
||
| export const createSSOProvider = | ||
| (db: AgentsRunDatabaseClient) => | ||
| async (data: { | ||
| id: string; | ||
| providerId: string; | ||
| issuer: string; | ||
| domain: string; | ||
| oidcConfig?: string | null; | ||
| samlConfig?: string | null; | ||
| organizationId?: string | null; | ||
| }): Promise<void> => { | ||
| await db.insert(ssoProvider).values({ | ||
| id: data.id, | ||
| providerId: data.providerId, | ||
| issuer: data.issuer, | ||
| domain: data.domain, | ||
| oidcConfig: data.oidcConfig ?? null, | ||
| samlConfig: data.samlConfig ?? null, | ||
| userId: null, | ||
| organizationId: data.organizationId ?? null, | ||
| }); | ||
| }; |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,37 @@ | ||||||||||||||||||||||
| #!/usr/bin/env bash | ||||||||||||||||||||||
| # Enforces that drizzle-orm imports only appear in allowed directories. | ||||||||||||||||||||||
| # Designed to run via lint-staged on staged files only. | ||||||||||||||||||||||
| # | ||||||||||||||||||||||
| # Usage: scripts/lint-data-access-boundary.sh file1.ts file2.ts ... | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| set -euo pipefail | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| VIOLATIONS=() | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| for file in "$@"; do | ||||||||||||||||||||||
| # Skip allowed directories | ||||||||||||||||||||||
| case "$file" in | ||||||||||||||||||||||
| */data-access/* | */db/* | */dolt/* | */__tests__/* | *.test.ts | *.spec.ts | */test-*) | ||||||||||||||||||||||
| continue | ||||||||||||||||||||||
| ;; | ||||||||||||||||||||||
| esac | ||||||||||||||||||||||
|
Comment on lines
+13
to
+17
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠 MAJOR Allowlist missing Issue: The allowlist doesn't include Why:
These are schema/type definition files, not query logic that should be encapsulated in data-access. Fix: (1-click apply)
Suggested change
Refs: |
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| # Check for drizzle-orm imports | ||||||||||||||||||||||
| if grep -qE "from ['\"]drizzle-orm" "$file" 2>/dev/null; then | ||||||||||||||||||||||
| VIOLATIONS+=("$file") | ||||||||||||||||||||||
| fi | ||||||||||||||||||||||
| done | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if [ ${#VIOLATIONS[@]} -gt 0 ]; then | ||||||||||||||||||||||
| echo "" | ||||||||||||||||||||||
| echo "ERROR: drizzle-orm imports found outside the data-access layer:" | ||||||||||||||||||||||
| echo "" | ||||||||||||||||||||||
| for v in "${VIOLATIONS[@]}"; do | ||||||||||||||||||||||
| echo " $v" | ||||||||||||||||||||||
| done | ||||||||||||||||||||||
| echo "" | ||||||||||||||||||||||
| echo "Move database queries to packages/agents-core/src/data-access/ and import" | ||||||||||||||||||||||
| echo "the data-access functions instead. See CLAUDE.md for architecture guidelines." | ||||||||||||||||||||||
| echo "" | ||||||||||||||||||||||
| exit 1 | ||||||||||||||||||||||
| fi | ||||||||||||||||||||||
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.
🟡 Minor Comment placement inconsistent with established pattern
Issue: The section comment
// Runtime data access (Postgres - not versioned)was moved to betweenapiKeysandauthexports, but the established pattern places section comments before the first export of that section.Why: Minor readability impact — section comments demarcate where a group starts.
Fix: (1-click apply)
Refs: