Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,22 @@ jobs:
set -o pipefail
npm run test:run 2>&1 | tee test.log

- name: Setup Java
uses: actions/setup-java@v6
with:
distribution: temurin
java-version: 21

- name: Install Firebase CLI
run: npm install --global firebase-tools@15.24.0

- name: Test Firestore rules
shell: bash
working-directory: functions
run: |
set -o pipefail
npm run test:rules 2>&1 | tee -a test.log

- name: Upload test log
if: always()
uses: actions/upload-artifact@v6
Expand Down
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ DevLog의 Firebase Cloud Functions와 Firestore index 설정을 관리하는 저
```mermaid
flowchart LR
Client["API Client"] --> Hosting["Firebase Hosting"]
Hosting -->|"/api/todos/**<br/>/api/web-pages/**<br/>/api/push-notifications/**<br/>/api/auth/google/**<br/>/api/auth/apple/**<br/>/api/auth/github/**"| CloudRun["Cloud Run<br/>api-v2"]
Hosting -->|"/api/todos/**<br/>/api/push-notifications/**<br/>/api/auth/google/**<br/>/api/auth/apple/**<br/>/api/auth/github/**"| CloudRun["Cloud Run<br/>api-v2"]
Hosting -->|"나머지 /api/**"| Functions["Cloud Functions<br/>api"]
CloudRun --> Auth["Firebase Auth"]
Functions --> Auth
Expand All @@ -28,13 +28,13 @@ flowchart LR
| 실행 구성 | 소스 | 책임 |
| --- | --- | --- |
| Firebase Hosting | `firebase.json` | 공개 요청 진입점과 경로별 rewrite 순서 관리 |
| Cloud Run `api-v2` | `nestjs-api` | Todo, WebPage, PushNotification 요청의 Firebase ID Token 검증과 삭제, Google·Apple·GitHub 인증 관리 |
| Cloud Run `api-v2` | `nestjs-api` | Todo, PushNotification 요청의 Firebase ID Token 검증과 삭제, Google·Apple·GitHub 인증 관리 |
| Cloud Functions `api` | `functions` | Staging의 나머지 `/api/**` 요청에 대한 `404` 응답 경계 유지 |
| Cloud Functions 개별 함수 | `functions` | trigger, Scheduler, Cloud Tasks, FCM 처리 |
| Firebase Auth | — | 인증이 필요한 경로에 전달된 Bearer token 검증 |
| Firestore | — | 검증된 사용자 UID 범위의 공통 데이터 저장 |

Staging Hosting에서는 `/api/todos/**`, `/api/web-pages/**`, `/api/push-notifications/**`, `/api/auth/google/**`, `/api/auth/apple/**`, `/api/auth/github/**`를 Cloud Run `api-v2`가 우선 처리합니다. 나머지 `/api/**`는 Functions `api`가 `404` 응답 경계를 유지합니다.
Staging Hosting에서는 `/api/todos/**`, `/api/push-notifications/**`, `/api/auth/google/**`, `/api/auth/apple/**`, `/api/auth/github/**`를 Cloud Run `api-v2`가 우선 처리합니다. 나머지 `/api/**`는 Functions `api`가 `404` 응답 경계를 유지합니다.

기존 Functions를 한 번에 교체하지 않고 요청 계약을 유지하면서 경로 단위로 NestJS API에 이전하는 구조입니다. 최종적으로 Cloud Functions `api`가 담당하는 HTTP API 전체를 Cloud Run 기반 `api-v2`로 이전하는 것을 목표로 합니다.

Expand Down Expand Up @@ -124,8 +124,11 @@ cd functions
npm ci
npm run build
npm run test:run
npm run test:rules
```

`npm run test:rules`는 `firebase.test.json`의 Firestore emulator를 `demo-devlog` project로 띄워 `firestore.rules`의 허용·거부를 확인합니다. Firebase CLI와 Java가 필요합니다.

### NestJS API

```bash
Expand All @@ -151,8 +154,11 @@ cd functions
npm ci
npm run build
npm run test:run
npm run test:rules
```

`test` job은 `npm run test:rules` 실행 전에 Java 21과 버전을 고정한 Firebase CLI를 설치합니다.

`nestjs-api` job은 다음 명령을 확인하고 실행 로그를 artifact로 보관합니다.

```bash
Expand Down Expand Up @@ -192,4 +198,11 @@ firebase deploy --project <prod-project-id> --only functions:<functionName>

여러 함수를 배포해야 할 때도 전체 일괄 배포보다 필요한 함수 단위로 나누어 배포합니다.

소스에서 제거한 함수는 `functions:<functionName>` 배포로 삭제되지 않습니다. 각 project에서 명시적으로 삭제하고, 삭제한 함수가 사용하던 색인 설정 변경은 함수 삭제 뒤에 배포합니다.

```bash
firebase functions:delete <functionName> --project <project-id> --region asia-northeast3
firebase deploy --project <project-id> --only firestore:indexes
```

GitHub OAuth 전환 배포 전에는 staging과 prod 모두 `oauthSessions.expiresAt`, `oauthTickets.expiresAt` TTL 설정을 먼저 반영합니다.
22 changes: 5 additions & 17 deletions docs/cloud-run-staging.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,10 @@ region, runtime 및 build service account, Secret 연결 이름과 version 참

## 직접 호출 검증

검증 뒤 폐기할 수 있는 Staging Todo, WebPage와 PushNotification ID를 준비합니다. 각 문서는 삭제 요청 전 활성 상태여야 합니다. Firebase ID Token은 화면, shell history, 명령 인자에 남기지 않습니다.
검증 뒤 폐기할 수 있는 Staging Todo와 PushNotification ID를 준비합니다. 각 문서는 삭제 요청 전 활성 상태여야 합니다. Firebase ID Token은 화면, shell history, 명령 인자에 남기지 않습니다.

```bash
read -r -p "Staging Todo ID: " TODO_ID
read -r -p "Staging WebPage ID: " WEB_PAGE_ID
read -r -p "Staging PushNotification ID: " PUSH_NOTIFICATION_ID
read -r -s -p "Firebase ID Token: " FIREBASE_ID_TOKEN
printf '\n'
Expand Down Expand Up @@ -257,16 +256,6 @@ curl -i -X DELETE \
"${SERVICE_URL}/api/todos/${TODO_ID}/deletion-request" \
<<<"Authorization: Bearer ${FIREBASE_ID_TOKEN}"

curl -i -X POST \
-H @- \
"${SERVICE_URL}/api/web-pages/${WEB_PAGE_ID}/deletion-request" \
<<<"Authorization: Bearer ${FIREBASE_ID_TOKEN}"

curl -i -X DELETE \
-H @- \
"${SERVICE_URL}/api/web-pages/${WEB_PAGE_ID}/deletion-request" \
<<<"Authorization: Bearer ${FIREBASE_ID_TOKEN}"

curl -i -X POST \
-H @- \
"${SERVICE_URL}/api/push-notifications/${PUSH_NOTIFICATION_ID}/deletion-request" \
Expand All @@ -277,7 +266,7 @@ curl -i -X DELETE \
"${SERVICE_URL}/api/push-notifications/${PUSH_NOTIFICATION_ID}/deletion-request" \
<<<"Authorization: Bearer ${FIREBASE_ID_TOKEN}"

unset FIREBASE_ID_TOKEN TODO_ID WEB_PAGE_ID PUSH_NOTIFICATION_ID SERVICE_URL
unset FIREBASE_ID_TOKEN TODO_ID PUSH_NOTIFICATION_ID SERVICE_URL
```

응답을 다음 순서로 확인합니다.
Expand All @@ -297,9 +286,8 @@ unset FIREBASE_ID_TOKEN TODO_ID WEB_PAGE_ID PUSH_NOTIFICATION_ID SERVICE_URL
13. Token이 없는 GitHub access-token `DELETE`: `401`, `unauthenticated`
14. Token이 없는 Todo `POST`: `401`
15. Token이 있는 Todo `POST`와 `DELETE`: 각각 `200`, `{"success":true}`
16. Token이 있는 WebPage `POST`와 `DELETE`: 각각 `200`, `{"success":true}`
17. Token이 있는 PushNotification `POST`와 `DELETE`: 각각 `200`, `{"success":true}`
18. 각 `DELETE` 뒤 Todo와 연결 알림, WebPage 및 PushNotification의 삭제 상태 복구
16. Token이 있는 PushNotification `POST`와 `DELETE`: 각각 `200`, `{"success":true}`
17. 각 `DELETE` 뒤 Todo와 연결 알림 및 PushNotification의 삭제 상태 복구

유효한 Firebase ID Token과 일회용 인증 값이 필요한 Google custom token, account-link, account-link 해제, access-token 폐기와 Apple challenge, custom token, account-link, account-link 해제, access-token 갱신·폐기 및 refresh-token 성공 경로, GitHub 로그인·계정 연결·연결 해제·grant 폐기 성공 경로는 Staging 앱에서 별도로 확인합니다. Token과 인증 코드는 로그와 shell history에 남기지 않습니다.

Expand Down Expand Up @@ -344,7 +332,7 @@ gcloud run services update api-v2 \
--invoker-iam-check
```

Token이 없는 요청이 `403`이면 Cloud Run 공개 설정이 적용되지 않은 상태입니다. 원인을 수정한 뒤 배포 명령을 다시 실행해 공개 설정을 적용하고 직접 호출 검증 전체를 반복합니다. 인증된 `POST` 성공 뒤 `DELETE`가 실패한 첫 배포는 수정본을 다시 배포한 뒤 실패한 경로의 `DELETE`부터 실행합니다. 이후 배포에서는 해당 API를 지원하는 이전 revision으로 복구한 뒤 같은 `DELETE`를 실행합니다. Todo와 연결 알림, WebPage 및 PushNotification의 삭제 상태가 복구됐는지 확인한 뒤 직접 호출 검증을 다시 수행합니다.
Token이 없는 요청이 `403`이면 Cloud Run 공개 설정이 적용되지 않은 상태입니다. 원인을 수정한 뒤 배포 명령을 다시 실행해 공개 설정을 적용하고 직접 호출 검증 전체를 반복합니다. 인증된 `POST` 성공 뒤 `DELETE`가 실패한 첫 배포는 수정본을 다시 배포한 뒤 실패한 경로의 `DELETE`부터 실행합니다. 이후 배포에서는 해당 API를 지원하는 이전 revision으로 복구한 뒤 같은 `DELETE`를 실행합니다. Todo와 연결 알림 및 PushNotification의 삭제 상태가 복구됐는지 확인한 뒤 직접 호출 검증을 다시 수행합니다.

## revision 복구

Expand Down
73 changes: 27 additions & 46 deletions docs/firebase-hosting-staging.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
- Cloud Run 서비스: `api-v2`
- region: `asia-northeast3`
- Todo 경로: `/api/todos/**`
- WebPage 경로: `/api/web-pages/**`
- PushNotification 경로: `/api/push-notifications/**`
- Google 인증 경로: `/api/auth/google/**`
- Apple 인증 경로: `/api/auth/apple/**`
Expand Down Expand Up @@ -45,9 +44,8 @@ gcloud run services describe api-v2 \
13. Token이 없는 GitHub access-token `DELETE`: `401`, `unauthenticated`
14. Token이 없는 Todo `POST`: `401`
15. Token이 있는 Todo `POST`와 `DELETE`: 각각 `200`, `{"success":true}`
16. Token이 있는 WebPage `POST`와 `DELETE`: 각각 `200`, `{"success":true}`
17. Token이 있는 PushNotification `POST`와 `DELETE`: 각각 `200`, `{"success":true}`
18. 각 `DELETE` 뒤 Todo와 연결 알림, WebPage 및 PushNotification의 삭제 상태 복구
16. Token이 있는 PushNotification `POST`와 `DELETE`: 각각 `200`, `{"success":true}`
17. 각 `DELETE` 뒤 Todo와 연결 알림 및 PushNotification의 삭제 상태 복구

하나라도 확인되지 않으면 Hosting 라우팅을 배포하지 않습니다.

Expand All @@ -65,12 +63,11 @@ cd ..
`firebase.json`의 `devlog-staging` rewrite가 다음 순서인지 확인합니다.

1. `/api/todos/**` → Cloud Run `api-v2`
2. `/api/web-pages/**` → Cloud Run `api-v2`
3. `/api/push-notifications/**` → Cloud Run `api-v2`
4. `/api/auth/google/**` → Cloud Run `api-v2`
5. `/api/auth/apple/**` → Cloud Run `api-v2`
6. `/api/auth/github/**` → Cloud Run `api-v2`
7. `/api/**` → Functions `api`
2. `/api/push-notifications/**` → Cloud Run `api-v2`
3. `/api/auth/google/**` → Cloud Run `api-v2`
4. `/api/auth/apple/**` → Cloud Run `api-v2`
5. `/api/auth/github/**` → Cloud Run `api-v2`
6. `/api/**` → Functions `api`

Cloud Run rewrite에는 `pinTag`가 없어야 합니다. `devlog-auth-prod` 블록은 변경하지 않습니다.

Expand Down Expand Up @@ -100,11 +97,10 @@ Functions, Firestore rules, index, Cloud Run은 이 명령의 배포 대상이

## 배포 후 요청 검증

Staging 검증용 Todo, WebPage와 PushNotification 및 Firebase ID Token을 준비합니다. 각 문서는 삭제 요청 전 활성 상태여야 합니다. Header는 표준 입력으로 전달해 Token이 process 목록에 남지 않게 합니다.
Staging 검증용 Todo와 PushNotification 및 Firebase ID Token을 준비합니다. 각 문서는 삭제 요청 전 활성 상태여야 합니다. Header는 표준 입력으로 전달해 Token이 process 목록에 남지 않게 합니다.

```bash
read -r -p "Staging Todo ID: " TODO_ID
read -r -p "Staging WebPage ID: " WEB_PAGE_ID
read -r -p "Staging PushNotification ID: " PUSH_NOTIFICATION_ID
read -r -s -p "Firebase ID Token: " FIREBASE_ID_TOKEN
printf '\n'
Expand All @@ -124,19 +120,6 @@ curl -i -X DELETE \
"${HOSTING_URL}/api/todos/${TODO_ID}/deletion-request" \
<<<"Authorization: Bearer ${FIREBASE_ID_TOKEN}"

curl -i -X POST \
"${HOSTING_URL}/api/web-pages/route-check/deletion-request"

curl -i -X POST \
-H @- \
"${HOSTING_URL}/api/web-pages/${WEB_PAGE_ID}/deletion-request" \
<<<"Authorization: Bearer ${FIREBASE_ID_TOKEN}"

curl -i -X DELETE \
-H @- \
"${HOSTING_URL}/api/web-pages/${WEB_PAGE_ID}/deletion-request" \
<<<"Authorization: Bearer ${FIREBASE_ID_TOKEN}"

curl -i -X POST \
"${HOSTING_URL}/api/push-notifications/route-check/deletion-request"

Expand Down Expand Up @@ -216,32 +199,30 @@ curl -i -X DELETE \
curl -i -X DELETE \
"${HOSTING_URL}/api/auth/github/access-token"

unset FIREBASE_ID_TOKEN TODO_ID WEB_PAGE_ID PUSH_NOTIFICATION_ID HOSTING_URL
unset FIREBASE_ID_TOKEN TODO_ID PUSH_NOTIFICATION_ID HOSTING_URL
```

응답을 다음 순서로 확인합니다.

1. Token이 없는 Todo `POST`: `401`
2. Token이 있는 Todo `POST`: `200`, `{"success":true}`
3. Token이 있는 Todo `DELETE`: `200`, `{"success":true}`
4. Token이 없는 WebPage `POST`: Cloud Run `api-v2`의 `401`
5. Token이 있는 WebPage `POST`와 `DELETE`: 각각 `200`, `{"success":true}`
6. Token이 없는 PushNotification `POST`: Cloud Run `api-v2`의 `401`
7. Token이 있는 PushNotification `POST`와 `DELETE`: 각각 `200`, `{"success":true}`
8. `/api/auth/apple/challenges` `POST`: Cloud Run `api-v2`의 `200`, `challengeId`, `hashedNonce`, `expiresAt` 필드가 존재
9. `/api/auth/apple/custom-token` `POST`: Cloud Run `api-v2`의 `400`, `invalid-argument`
10. body가 빈 Google custom token `POST`: Cloud Run `api-v2`의 `400`, `invalid-argument`
11. 잘못된 `serverAuthCode`를 전달한 Google custom token `POST`: Cloud Run `api-v2`의 `401`, `invalid-google-proof`
12. Token이 없는 Google account-link와 access-token `DELETE`: Cloud Run `api-v2`의 `401`
13. Token이 없는 Apple account-link `PUT`/`DELETE`, access-token `POST`/`DELETE`, refresh-token `POST`: Cloud Run `api-v2`의 `401`
14. body가 빈 GitHub 로그인 session `POST`: Cloud Run `api-v2`의 `400`, `invalid-argument`
15. query가 빈 GitHub callback `GET`: Cloud Run `api-v2`의 `302`, `devlog://oauth-callback?error=oauth-failed`
16. body가 빈 GitHub custom token `POST`: Cloud Run `api-v2`의 `400`, `invalid-argument`
17. Token이 없는 GitHub 계정 연결 session `POST`: Cloud Run `api-v2`의 `401`, `unauthenticated`
18. Token이 없는 GitHub account-link `PUT`: Cloud Run `api-v2`의 `401`, `unauthenticated`
19. Token이 없는 GitHub account-link `DELETE`: Cloud Run `api-v2`의 `401`, `unauthenticated`
20. Token이 없는 GitHub access-token `DELETE`: Cloud Run `api-v2`의 `401`, `unauthenticated`
21. 각 `DELETE` 뒤 Todo와 연결 알림, WebPage 및 PushNotification의 삭제 상태 복구
4. Token이 없는 PushNotification `POST`: Cloud Run `api-v2`의 `401`
5. Token이 있는 PushNotification `POST`와 `DELETE`: 각각 `200`, `{"success":true}`
6. `/api/auth/apple/challenges` `POST`: Cloud Run `api-v2`의 `200`, `challengeId`, `hashedNonce`, `expiresAt` 필드가 존재
7. `/api/auth/apple/custom-token` `POST`: Cloud Run `api-v2`의 `400`, `invalid-argument`
8. body가 빈 Google custom token `POST`: Cloud Run `api-v2`의 `400`, `invalid-argument`
9. 잘못된 `serverAuthCode`를 전달한 Google custom token `POST`: Cloud Run `api-v2`의 `401`, `invalid-google-proof`
10. Token이 없는 Google account-link와 access-token `DELETE`: Cloud Run `api-v2`의 `401`
11. Token이 없는 Apple account-link `PUT`/`DELETE`, access-token `POST`/`DELETE`, refresh-token `POST`: Cloud Run `api-v2`의 `401`
12. body가 빈 GitHub 로그인 session `POST`: Cloud Run `api-v2`의 `400`, `invalid-argument`
13. query가 빈 GitHub callback `GET`: Cloud Run `api-v2`의 `302`, `devlog://oauth-callback?error=oauth-failed`
14. body가 빈 GitHub custom token `POST`: Cloud Run `api-v2`의 `400`, `invalid-argument`
15. Token이 없는 GitHub 계정 연결 session `POST`: Cloud Run `api-v2`의 `401`, `unauthenticated`
16. Token이 없는 GitHub account-link `PUT`: Cloud Run `api-v2`의 `401`, `unauthenticated`
17. Token이 없는 GitHub account-link `DELETE`: Cloud Run `api-v2`의 `401`, `unauthenticated`
18. Token이 없는 GitHub access-token `DELETE`: Cloud Run `api-v2`의 `401`, `unauthenticated`
19. 각 `DELETE` 뒤 Todo와 연결 알림 및 PushNotification의 삭제 상태 복구

유효한 일회용 인증 값이 필요한 Apple challenge, custom token, account-link, account-link 해제, access-token 갱신·폐기 및 refresh-token 성공 경로와 GitHub 로그인·계정 연결·연결 해제·grant 폐기 성공 경로는 Staging 앱에서 확인합니다. 성공 응답과 최신 `api-v2` revision 로그를 함께 확인하며 Token과 인증 코드는 로그와 shell history에 남기지 않습니다.

Expand All @@ -268,12 +249,12 @@ firebase functions:log \
unset LATEST_REVISION
```

Todo, WebPage, PushNotification과 Google·Apple·GitHub 인증 요청은 최신 `api-v2` revision에서 확인되어야 합니다. 알려지지 않은 나머지 `/api/**` 요청은 Functions `api`의 `404` 응답으로 확인되어야 합니다.
Todo, PushNotification과 Google·Apple·GitHub 인증 요청은 최신 `api-v2` revision에서 확인되어야 합니다. 알려지지 않은 나머지 `/api/**` 요청은 Functions `api`의 `404` 응답으로 확인되어야 합니다.

## `api-v2` revision 복구

Hosting 전환 이후 `api-v2` 요청 검증이 실패하면 [`cloud-run-staging.md`](./cloud-run-staging.md#revision-복구)에 따라 이전 정상 revision으로 트래픽을 되돌립니다. 첫 revision이라 이전 정상 revision이 없으면 수정본을 배포합니다. `firebase.json`과 Hosting rewrite는 변경하지 않습니다.

복구 뒤 앞 절의 Hosting 요청을 다시 실행하고 Todo, WebPage, PushNotification과 Google·Apple·GitHub 인증 요청이 선택한 `api-v2` revision에 기록되는지 확인합니다. 알려지지 않은 나머지 `/api/**` 요청은 Functions `api`의 `404` 응답과 로그로 확인합니다.
복구 뒤 앞 절의 Hosting 요청을 다시 실행하고 Todo, PushNotification과 Google·Apple·GitHub 인증 요청이 선택한 `api-v2` revision에 기록되는지 확인합니다. 알려지지 않은 나머지 `/api/**` 요청은 Functions `api`의 `404` 응답과 로그로 확인합니다.

복구 과정에서는 Functions, Firestore, Secret과 Production 자원을 삭제하거나 변경하지 않습니다. 원인을 수정한 뒤에는 `api-v2` 직접 호출 검증 전체를 다시 통과하고 이 문서의 정상 전환 절차를 처음부터 수행합니다.
7 changes: 0 additions & 7 deletions firebase.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,6 @@
"region": "asia-northeast3"
}
},
{
"source": "/api/web-pages/**",
"run": {
"serviceId": "api-v2",
"region": "asia-northeast3"
}
},
{
"source": "/api/push-notifications/**",
"run": {
Expand Down
22 changes: 0 additions & 22 deletions firestore.index.json
Original file line number Diff line number Diff line change
Expand Up @@ -748,28 +748,6 @@
}
]
},
{
"collectionGroup": "webPages",
"fieldPath": "isDeleted",
"indexes": [
{
"order": "ASCENDING",
"queryScope": "COLLECTION"
},
{
"order": "DESCENDING",
"queryScope": "COLLECTION"
},
{
"order": "ASCENDING",
"queryScope": "COLLECTION_GROUP"
},
{
"order": "DESCENDING",
"queryScope": "COLLECTION_GROUP"
}
]
},
{
"collectionGroup": "authChallenges",
"fieldPath": "expiresAt",
Expand Down
Loading
Loading