Skip to content

Commit d56ea13

Browse files
committed
Revert "update infra and remove railway"
This reverts commit b23258a.
1 parent 9bea2fa commit d56ea13

9 files changed

Lines changed: 72 additions & 87 deletions

File tree

.github/workflows/build.yml

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ jobs:
5555
uses: docker/setup-buildx-action@v3
5656

5757
- name: Log in to the Container registry
58-
if: github.event_name != 'pull_request' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/staging')
58+
if: github.event_name != 'pull_request' && github.ref == 'refs/heads/main'
5959
uses: docker/login-action@v3
6060
with:
6161
registry: ghcr.io
@@ -69,7 +69,7 @@ jobs:
6969
images: ${{ matrix.image }}
7070
tags: |
7171
type=raw,value=latest-${{ matrix.arch }},enable=${{ github.ref == 'refs/heads/main' }}
72-
type=raw,value=staging-${{ matrix.arch }},enable=${{ github.ref == 'refs/heads/staging' }}
72+
type=raw,value=staging-${{ github.sha }}-${{ matrix.arch }},enable=${{ github.ref == 'refs/heads/staging' }}
7373
type=sha,format=long,suffix=-${{ matrix.arch }}
7474
7575
- name: Build and push Docker image
@@ -78,7 +78,7 @@ jobs:
7878
context: .
7979
file: ${{ matrix.dockerfile }}
8080
platforms: ${{ matrix.platform }}
81-
push: ${{ github.event_name != 'pull_request' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/staging') }}
81+
push: ${{ github.event_name != 'pull_request' && github.ref == 'refs/heads/main' }}
8282
tags: ${{ steps.meta.outputs.tags }}
8383
labels: ${{ steps.meta.outputs.labels }}
8484
cache-from: type=gha,scope=build-v3
@@ -89,7 +89,7 @@ jobs:
8989
create-manifests:
9090
runs-on: ubuntu-latest
9191
needs: build-and-push
92-
if: github.event_name != 'pull_request' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/staging')
92+
if: github.event_name != 'pull_request' && github.ref == 'refs/heads/main'
9393
strategy:
9494
matrix:
9595
include:
@@ -115,7 +115,6 @@ jobs:
115115
images: ${{ matrix.image }}
116116
tags: |
117117
type=raw,value=latest,enable=${{ github.ref == 'refs/heads/main' }}
118-
type=raw,value=staging,enable=${{ github.ref == 'refs/heads/staging' }}
119118
type=sha,format=long
120119
121120
- name: Create and push manifest
@@ -149,32 +148,4 @@ jobs:
149148
docker manifest inspect "$arm64_image" || echo "ARM64 image not found"
150149
exit 1
151150
fi
152-
done
153-
154-
trigger-infrastructure-deploy:
155-
runs-on: ubuntu-latest
156-
needs: create-manifests
157-
if: github.ref == 'refs/heads/staging' || github.ref == 'refs/heads/main'
158-
permissions:
159-
contents: read
160-
161-
steps:
162-
- name: Trigger staging deployment
163-
if: github.ref == 'refs/heads/staging'
164-
run: |
165-
curl -X POST \
166-
-H "Accept: application/vnd.github.v3+json" \
167-
-H "Authorization: token ${{ secrets.INFRA_DEPLOY_TOKEN }}" \
168-
-H "X-GitHub-Api-Version: 2022-11-28" \
169-
https://api.github.com/repos/${{ secrets.INFRA_REPO }}/dispatches \
170-
-d '{"event_type":"staging-deploy","client_payload":{"sha":"${{ github.sha }}","ref":"${{ github.ref }}"}}'
171-
172-
- name: Trigger production deployment
173-
if: github.ref == 'refs/heads/main'
174-
run: |
175-
curl -X POST \
176-
-H "Accept: application/vnd.github.v3+json" \
177-
-H "Authorization: token ${{ secrets.INFRA_DEPLOY_TOKEN }}" \
178-
-H "X-GitHub-Api-Version: 2022-11-28" \
179-
https://api.github.com/repos/${{ secrets.INFRA_REPO }}/dispatches \
180-
-d '{"event_type":"production-deploy","client_payload":{"sha":"${{ github.sha }}","ref":"${{ github.ref }}"}}'
151+
done

.github/workflows/ci.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,25 @@ jobs:
5353
fail_ci_if_error: false
5454
verbose: true
5555

56+
migrations:
57+
name: Apply Database Migrations
58+
runs-on: ubuntu-latest
59+
if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/staging')
60+
needs: test
61+
steps:
62+
- name: Checkout code
63+
uses: actions/checkout@v4
64+
65+
- name: Setup Bun
66+
uses: oven-sh/setup-bun@v2
67+
with:
68+
bun-version: latest
69+
70+
- name: Install dependencies
71+
run: bun install
72+
73+
- name: Apply migrations
74+
working-directory: ./apps/sim
75+
env:
76+
DATABASE_URL: ${{ github.ref == 'refs/heads/main' && secrets.DATABASE_URL || secrets.STAGING_DATABASE_URL }}
77+
run: bunx drizzle-kit migrate

apps/sim/app/api/users/me/api-keys/route.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ export async function GET(request: NextRequest) {
1919

2020
const userId = session.user.id
2121

22-
// Fetch all API keys for this user
2322
const keys = await db
2423
.select({
2524
id: apiKey.id,
@@ -67,7 +66,6 @@ export async function POST(request: NextRequest) {
6766
return NextResponse.json({ error: 'Name cannot be empty.' }, { status: 400 })
6867
}
6968

70-
// Check if a key with this name already exists for the user
7169
const existingKey = await db
7270
.select()
7371
.from(apiKey)
@@ -83,17 +81,15 @@ export async function POST(request: NextRequest) {
8381
)
8482
}
8583

86-
// Create new API key with hashing
8784
const { key: plainKey, hashedKey } = await createApiKey(true)
8885

89-
// Store the hashed version in the database
9086
const [newKey] = await db
9187
.insert(apiKey)
9288
.values({
9389
id: nanoid(),
9490
userId,
9591
name,
96-
key: hashedKey!, // Store the hashed version
92+
key: hashedKey!,
9793
createdAt: new Date(),
9894
updatedAt: new Date(),
9995
})
@@ -103,11 +99,10 @@ export async function POST(request: NextRequest) {
10399
createdAt: apiKey.createdAt,
104100
})
105101

106-
// Return the plain key to the user (they'll never see it again)
107102
return NextResponse.json({
108103
key: {
109104
...newKey,
110-
key: plainKey, // Return the plain text key for user to copy
105+
key: plainKey,
111106
},
112107
})
113108
} catch (error) {

apps/sim/app/api/v1/auth.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ export async function authenticateApiKey(
2828
}
2929

3030
try {
31-
// First, check personal API keys
3231
const [personalKey] = await db
3332
.select({
3433
userId: apiKeyTable.userId,
@@ -56,7 +55,6 @@ export async function authenticateApiKey(
5655
}
5756
}
5857

59-
// If not found in personal keys, check workspace API keys
6058
const [workspaceKey] = await db
6159
.select({
6260
workspaceId: workspaceApiKey.workspaceId,
@@ -79,7 +77,6 @@ export async function authenticateApiKey(
7977
}
8078
}
8179

82-
// If a workflowId is provided, verify that the workflow belongs to this workspace
8380
if (workflowId) {
8481
const [workflowRecord] = await db
8582
.select({
@@ -109,7 +106,7 @@ export async function authenticateApiKey(
109106

110107
return {
111108
authenticated: true,
112-
userId: workspaceKey.workspaceOwnerId!, // Workspace owner is the effective user
109+
userId: workspaceKey.workspaceOwnerId!,
113110
workspaceId: workspaceKey.workspaceId,
114111
keyType: 'workspace',
115112
}

apps/sim/app/api/workflows/[id]/deploy/route.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,8 @@ export async function POST(request: NextRequest, { params }: { params: Promise<{
322322

323323
// If client provided a specific API key, check if it's either personal or workspace key
324324
if (providedApiKey) {
325-
// First check personal API keys
325+
let isValidKey = false
326+
326327
const [personalOwned] = await db
327328
.select({ key: apiKey.key })
328329
.from(apiKey)
@@ -331,15 +332,15 @@ export async function POST(request: NextRequest, { params }: { params: Promise<{
331332

332333
if (personalOwned) {
333334
userKey = providedApiKey
335+
isValidKey = true
334336
} else {
335-
// Check workspace API keys - get the workflow's workspace ID
336337
const [workflowData] = await db
337338
.select({ workspaceId: workflow.workspaceId })
338339
.from(workflow)
339340
.where(eq(workflow.id, id))
340341
.limit(1)
341342

342-
if (workflowData) {
343+
if (workflowData?.workspaceId) {
343344
const [workspaceOwned] = await db
344345
.select({ key: workspaceApiKey.key })
345346
.from(workspaceApiKey)
@@ -353,9 +354,15 @@ export async function POST(request: NextRequest, { params }: { params: Promise<{
353354

354355
if (workspaceOwned) {
355356
userKey = providedApiKey
357+
isValidKey = true
356358
}
357359
}
358360
}
361+
362+
if (!isValidKey) {
363+
logger.warn(`[${requestId}] Invalid API key provided for workflow deployment: ${id}`)
364+
return createErrorResponse('Invalid API key provided', 400)
365+
}
359366
}
360367

361368
// Update the workflow deployment status and save current state as deployed state
@@ -374,14 +381,13 @@ export async function POST(request: NextRequest, { params }: { params: Promise<{
374381
// Update lastUsed for the key we returned (try both personal and workspace keys)
375382
if (userKey) {
376383
try {
377-
// First try to update personal API key
378384
const personalResult = await db
379385
.update(apiKey)
380386
.set({ lastUsed: new Date(), updatedAt: new Date() })
381387
.where(eq(apiKey.key, userKey))
388+
.returning({ id: apiKey.id })
382389

383-
// If no personal key was updated, try workspace API key
384-
if (!personalResult || personalResult.rowCount === 0) {
390+
if (!personalResult || personalResult.length === 0) {
385391
await db
386392
.update(workspaceApiKey)
387393
.set({ lastUsed: new Date(), updatedAt: new Date() })

apps/sim/app/api/workflows/middleware.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ export async function validateWorkflowAccess(
6969
}
7070
} else {
7171
// Check both personal API keys and workspace API keys
72-
73-
// First, check personal API keys belonging to the workflow owner
7472
const personalKeys = await db
7573
.select({
7674
id: apiKey.id,
@@ -81,7 +79,6 @@ export async function validateWorkflowAccess(
8179

8280
let validPersonalKey = null
8381

84-
// Check each personal key with authentication function
8582
for (const key of personalKeys) {
8683
const isValid = await authenticateApiKey(apiKeyHeader, key.key)
8784
if (isValid) {
@@ -90,9 +87,8 @@ export async function validateWorkflowAccess(
9087
}
9188
}
9289

93-
// If not found in personal keys, check workspace API keys
9490
let validWorkspaceKey = null
95-
if (!validPersonalKey) {
91+
if (!validPersonalKey && workflow.workspaceId) {
9692
const workspaceKeys = await db
9793
.select({
9894
id: workspaceApiKey.id,
@@ -103,7 +99,6 @@ export async function validateWorkflowAccess(
10399
.leftJoin(workspace, eq(workspaceApiKey.workspaceId, workspace.id))
104100
.where(eq(workspace.id, workflow.workspaceId)) // Key must belong to the same workspace as the workflow
105101

106-
// Check each workspace key with authentication function
107102
for (const key of workspaceKeys) {
108103
const isValid = await authenticateApiKey(apiKeyHeader, key.key)
109104
if (isValid) {

apps/sim/app/api/workspaces/[id]/api-keys/[keyId]/route.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { and, eq } from 'drizzle-orm'
1+
import { and, eq, not } from 'drizzle-orm'
22
import { type NextRequest, NextResponse } from 'next/server'
33
import { z } from 'zod'
44
import { getSession } from '@/lib/auth'
@@ -30,7 +30,6 @@ export async function PUT(
3030

3131
const userId = session.user.id
3232

33-
// Require admin or write permission to update workspace API keys
3433
const permission = await getUserEntityPermissions(userId, 'workspace', workspaceId)
3534
if (!permission || (permission !== 'admin' && permission !== 'write')) {
3635
return NextResponse.json({ error: 'Forbidden' }, { status: 403 })
@@ -39,7 +38,6 @@ export async function PUT(
3938
const body = await request.json()
4039
const { name } = UpdateKeySchema.parse(body)
4140

42-
// Check if the key exists in this workspace
4341
const existingKey = await db
4442
.select()
4543
.from(workspaceApiKey)
@@ -50,15 +48,14 @@ export async function PUT(
5048
return NextResponse.json({ error: 'API key not found' }, { status: 404 })
5149
}
5250

53-
// Check if a key with the new name already exists (excluding the current key)
5451
const conflictingKey = await db
5552
.select()
5653
.from(workspaceApiKey)
5754
.where(
5855
and(
5956
eq(workspaceApiKey.workspaceId, workspaceId),
6057
eq(workspaceApiKey.name, name),
61-
workspaceApiKey.id.ne(keyId)
58+
not(eq(workspaceApiKey.id, keyId))
6259
)
6360
)
6461
.limit(1)
@@ -70,7 +67,6 @@ export async function PUT(
7067
)
7168
}
7269

73-
// Update the key name
7470
const [updatedKey] = await db
7571
.update(workspaceApiKey)
7672
.set({
@@ -112,13 +108,11 @@ export async function DELETE(
112108

113109
const userId = session.user.id
114110

115-
// Require admin or write permission to delete workspace API keys
116111
const permission = await getUserEntityPermissions(userId, 'workspace', workspaceId)
117112
if (!permission || (permission !== 'admin' && permission !== 'write')) {
118113
return NextResponse.json({ error: 'Forbidden' }, { status: 403 })
119114
}
120115

121-
// Delete the workspace API key
122116
const deletedRows = await db
123117
.delete(workspaceApiKey)
124118
.where(and(eq(workspaceApiKey.workspaceId, workspaceId), eq(workspaceApiKey.id, keyId)))

0 commit comments

Comments
 (0)