# user\_auth\_token\_logs를 정리해야 하나요?

**URL:** https://meta.discourse.org/t/clean-up-user-auth-token-logs/326397
**Category:** Bug
**Created:** [9월 13, 2024, 5:30오후 UTC](https://meta.discourse.org/t/clean-up-user-auth-token-logs/326397 "2024-09-13T17:30:22Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [9월 13, 2024, 5:30오후 UTC](https://meta.discourse.org/t/clean-up-user-auth-token-logs/326397/1 "2024-09-13T17:30:22Z")

</div>

`user_auth_token_logs` 테이블에 6,100만 행(그리고 계속 증가 중)이 있는 포럼이 있습니다.

`user_auth_tokens`는 단 25,000개뿐입니다.

6,100만 행 중 5,400만 행은 더 이상 존재하지 않는 `user_auth_token`을 참조하고 있습니다(즉, 데이터베이스 무결성 문제). 그리고 6,100만 행 중 약 5,800만 행은 2개월 이상 된 데이터입니다(즉, 쓸모없는 것처럼 보임?).

질문:

- 추가적인 무결성 문제의 위험 없이 이 데이터를 정리할 수 있을까요?
- 작업을 통해 이를 자동으로 정리하는 것이 좋은 아이디어일까요?

```plaintext
db=# select count(*) from user_auth_tokens;
 count 
-------
 25648

db=# select count(*) from user_auth_token_logs;
  count   
----------
 61415352

db=# select count(*) from user_auth_token_logs where user_auth_token_id not in (select id from user_auth_tokens);
  count   
----------
 54558442

db=# select count(*) from user_auth_token_logs where created_at < '2024-07-13';
  count   
----------
 58565943

```

---

<div class="post-metadata">

### Author: ![pmusaraj](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pmusaraj/32/119489_2.png) [@pmusaraj](https://meta.discourse.org/u/pmusaraj)
#### Post date: [9월 13, 2024, 6:22오후 UTC](https://meta.discourse.org/t/clean-up-user-auth-token-logs/326397/3 "2024-09-13T18:22:19Z")

</div>

네, `user_auth_token_logs`는 디버깅 목적으로만 존재합니다. 모든 행을 비워도 되는 결과로, 디버깅할 로그가 남지 않는 것뿐입니다.

> [@RGJ](#):
>
> 자동으로 정리하는 작업을 만들어 두는 건 어떨까요?

이 부분은 다음에서 처리됩니다:

[https://github.com/discourse/discourse/blob/main/app/jobs/scheduled/weekly.rb#L13](https://github.com/discourse/discourse/blob/main/app/jobs/scheduled/weekly.rb#L13)

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [9월 13, 2024, 7:07오후 UTC](https://meta.discourse.org/t/clean-up-user-auth-token-logs/326397/5 "2024-09-13T19:07:10Z")

</div>

알려주셔서 감사합니다. 그런 설정이 있는 줄 몰랐습니다.

그럼… [클린업은](https://github.com/discourse/discourse/blob/main/app/models/user_auth_token.rb#L206-L212) `verbose_auth_token_logging`이 `true`일 때만 실행됩니다(이 인스턴스에서는 해당되지 않음).

하지만 비-verbose 로깅은 설정과 무관하게 항상 수행됩니다 😱

> <https://github.com/discourse/discourse/blob/main/app/models/user_auth_token.rb#L29-L35>

[8fb823c](https://github.com/discourse/discourse/commit/8fb823c30f7fd3086f4370c2dc6e4e3737ae6acf)에 따라

이 문제를 #contribute:bug로 이동합니다 🙂

---

<div class="post-metadata">

### Author: ![pmusaraj](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pmusaraj/32/119489_2.png) [@pmusaraj](https://meta.discourse.org/u/pmusaraj)
#### Post date: [9월 13, 2024, 7:45오후 UTC](https://meta.discourse.org/t/clean-up-user-auth-token-logs/326397/6 "2024-09-13T19:45:14Z")

</div>

아 네, 잘 지적해 주셨네요. [214행에서 217행](https://github.com/discourse/discourse/blob/main/app/models/user_auth_token.rb#L214-L217)도 수정이 필요한 것 같습니다.

일정 기간이 지난 후 전역적으로 정리하는 방식에는 동의합니다. @osama (위에서 링크한 커밋의 작성자이므로) 이러한 로그들을 일정 시간이 지난 후 정리할 수 있다고 생각하시나요? (그렇다면 얼마나 지난 후일까요?) 의심스러운 로그인을 감지하기 위해 일부 로그는 유지해야 하는 것 같습니다.

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [9월 13, 2024, 10:16오후 UTC](https://meta.discourse.org/t/clean-up-user-auth-token-logs/326397/7 "2024-09-13T22:16:03Z")

</div>

> [@pmusaraj](#):
>
> [lines 214 to 217](https://github.com/discourse/discourse/blob/main/app/models/user_auth_token.rb#L214-L217)도 수정이 필요한 것 같습니다.

왜 수정이 필요한가요? 🤔 해당 코드는 로테이션된 `UserAuthToken`을 정리하는 것과 관련이 있고, 로그 레코드와는 무관한 것 아닌가요?

업데이트: `SiteSetting.verbose_auth_token_logging`을 활성화하고 주간 작업을 트리거한 후 `VACUUM FULL user_auth_token_logs`를 실행하자 테이블이 16GB에서 687MB로 줄었습니다 👍

오늘 나무 몇 그루를 살렸습니다 🌳

---

<div class="post-metadata">

### Author: ![Osama](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/osama/32/98013_2.png) [@Osama](https://meta.discourse.org/u/Osama)
#### Post date: [9월 16, 2024, 8:15오후 UTC](https://meta.discourse.org/t/clean-up-user-auth-token-logs/326397/8 "2024-09-16T20:15:39Z")

</div>

> [@pmusaraj](#):
>
> 특정 시간 경과 후 전역 정리(clean up)를 수행하는 것에 동의합니다. @osama (위에서 링크된 커밋의 저자이므로) 일정 시간 후 이 모든 로그를 정리할 수 있다고 생각하시나요 (그렇다면 얼마나 후에 정리할까요)? 의심스러운 로그인 탐지를 위해 일부 로그는 유지해야 할 것 같습니다.

네, 대부분의 로그는 정리할 수 있다고 생각합니다. 하지만 일부는 유지해야 합니다. 구체적으로, action이 `suspicious`, `generate`, 또는 `rotate`인 기록은 의심스러운 로그인 탐지 및 관련 보고서 생성에 사용되므로 유지해야 한다고 생각합니다.

> <https://github.com/discourse/discourse/blob/501f07ab1fceafcfa56bbfc074951d03d7d30d22/app/models/concerns/reports/suspicious_logins.rb#L38>

> <https://github.com/discourse/discourse/blob/501f07ab1fceafcfa56bbfc074951d03d7d30d22/app/models/user_auth_token.rb#L61>

---

<div class="post-metadata">

### Author: ![JonahAragon1](https://avatars.discourse-cdn.com/v4/letter/j/f19dbf/32.png) [@JonahAragon1](https://meta.discourse.org/u/JonahAragon1)
#### Post date: [8월 8, 2025, 4:11오후 UTC](https://meta.discourse.org/t/clean-up-user-auth-token-logs/326397/12 "2025-08-08T16:11:48Z")

</div>

포럼에서 이 버그가 아직 수정되지 않았다는 것을 확인했습니다 👀

의심스러운 로그인 보고서는 스태프에게만 적용되는 것 같습니다. 일반 관리자가 아닌 사용자의 로그를 유지해야 하는 특별한 이유가 있나요?

보고서가 작동하려면 계정 생성 시점부터의 데이터가 필요한가요? 최근 6개월 정도로 로그를 줄일 수 있을까요?

현재는 로그 정리 작업이 전혀 이루어지지 않고 있어, 이는 프라이버시 문제를 일으킬 수 있습니다.

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [8월 8, 2025, 4:31오후 UTC](https://meta.discourse.org/t/clean-up-user-auth-token-logs/326397/13 "2025-08-08T16:31:16Z")

</div>

위 논의를 저도 이해하지 못하겠습니다.

버그는 매우 단순합니다: `verbose` 모드가 아니라면 `UserAuthTokenLog`의 정리 작업이 전혀 수행되지 않습니다. 즉, 영원히 수행되지 않습니다. 해당 `if` 문은 제거되어야 합니다.

 ![image](https://global.discourse-cdn.com/meta/original/4X/2/a/1/2a18f24acf25dece617e7579097ca181523b9931.png)

원래 구현은 `SiteSetting.verbose_auth_token_logging`이 true일 때만 로그를 기록했습니다. 이 경우에도 설정을 비활성화한 후 가장 최근 남은 로그가 그대로 남아있는 문제가 있었지만, 이는 사소한 문제였습니다.

하지만 [이 변경 사항](https://github.com/discourse/discourse/commit/8fb823c30f7fd3086f4370c2dc6e4e3737ae6acf)으로 인해 로그 기록이 무조건적으로 수행되게 되었습니다(“_`generate`, `rotate`, `suspicious` 인증 토큰 로그는 이제 `verbose_auth_token_logging` 설정과 관계없이 항상 기록됩니다._”).

요약하자면, 그 변경 사항에서 삭제 작업도 무조건적으로 수행되도록 하는 것을 놓친 것입니다.

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [8월 11, 2025, 2:47오전 UTC](https://meta.discourse.org/t/clean-up-user-auth-token-logs/326397/14 "2025-08-11T02:47:26Z")

</div>

물론, 앞으로 몇 주 안에 문제를 해결하겠습니다. 급한 일이 있다면, 테스트를 완료하고 의도된 대로 작동하는지 확인된 PR을 보내주세요.

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [8월 13, 2025, 10:05오전 UTC](https://meta.discourse.org/t/clean-up-user-auth-token-logs/326397/17 "2025-08-13T10:05:16Z")

</div>

PR을 [Fix: cleanup UserAuthTokenLog unconditionally - Pull Request #34288 - discourse/discourse - GitHub](https://github.com/discourse/discourse/pull/34288) 로 만들었습니다. 3.5 버전에 포함되면 좋겠네요.

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [8월 14, 2025, 5:23오전 UTC](https://meta.discourse.org/t/clean-up-user-auth-token-logs/326397/19 "2025-08-14T05:23:03Z")

</div>

그리고 나보다 먼저 처리한 것 같네요 🙂

> <https://github.com/discourse/discourse/pull/34196/commits>
>
> Fixes: https://meta.discourse.org/t/clean-up-user-auth-token-logs/326397?u=jonah…aragon1
> 
> Currently (since 2021 when this logging was \[made unconditional\](https://github.com/discourse/discourse/commit/8fb823c30f7fd3086f4370c2dc6e4e3737ae6acf)) all user IP addresses and user agent strings for all forum users are continuously logged and never cleared. Keeping unnecessary PII is a massive liability for us, so I hope this can be merged ASAP.
> 
> I observe (in my user archive download) this indefinite logging does not happen on Meta, presumably because you have \_verbose\_ logging enabled, but it does happen on virtually all Discourse-hosted and self-hosted sites, which indicates to me this is unintended behavior. I should not have to enable verbose logging to \_decrease\_ the amount of logging here, but that is what I had to do as a temporary solution.
> 
> This change will keep the logs for a few months by default. I can find no reason the logs should be kept longer, \_especially\_ as they only seem to be used for a feature (suspicious login reporting) which currently only applies to staff accounts and not regular users.
> 
> cc: @OsamaSayegh

---

<div class="post-metadata">

### Author: ![pmusaraj](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pmusaraj/32/119489_2.png) [@pmusaraj](https://meta.discourse.org/u/pmusaraj)
#### Post date: [8월 14, 2025, 5:13오후 UTC](https://meta.discourse.org/t/clean-up-user-auth-token-logs/326397/20 "2025-08-14T17:13:24Z")

</div>

실제로, @Osama 덕분에 해당 PR이 이제 병합되었습니다. 이 PR은 `user_auth_token_logs`의 대부분의 유형을 처리하지만, 모든 유형을 처리하는 것은 아닙니다. 곧 `generate` 항목에 대한 수정 사항을 후속 처리할 예정입니다. (더 많은 배경 정보는 위 PR 링크의 토론을 참조하세요).

후속 처리가 완료될 때까지 이 주제를 열어 두겠습니다.
