logsafe

package
v0.25.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 25, 2026 License: MIT Imports: 16 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package logsafe 는 로그·오류·트레이스로 나가는 값에서 개인정보를 걷어내는 헬퍼를 모은다(#297).

배경: 통화 녹음 파일 이름은 기본 설정(PII_NUMBER_HASHING_ENABLED=false)에서 "{전화번호}_{YYYYMMDDHHMMSS}.m4a" 모양이다. 그래서 파일 경로, 그 경로에서 만든 source_id("transcript:{relPath}"), 그리고 경로를 문구에 담는 *fs.PathError· *os.LinkError 가 모두 전화번호를 로그로 흘린다.

이 패키지의 원칙:

  • 파일은 경로 대신 file_ref(키 있는 해시)로 가리킨다. 키 없는 해시는 번호 공간(약 10^8)이 작아 전수 대입으로 몇 초 만에 되돌릴 수 있다.
  • 오류는 문구 대신 모양(타입·연산 이름·errno·시간 초과 여부)만 남긴다.
  • source_id 는 모양이 안전하다고 확인한 것만 그대로 두고 나머지는 ref 로 바꾼다(허용 목록 방식).

Index

Constants

View Source
const EnvKey = "LOG_REF_KEY"

EnvKey 는 file_ref 계산에 쓰는 HMAC 키를 담는 환경 변수 이름이다(선택). 비어 있으면 프로세스마다 무작위 키를 만든다.

View Source
const MinKeyBytes = 16

MinKeyBytes 는 LOG_REF_KEY 의 최소 길이다. 짧은 키는 전수 대입을 막지 못한다.

Variables

View Source
var ErrKeyTooShort = fmt.Errorf("logsafe: %s must be at least %d bytes", EnvKey, MinKeyBytes)

ErrKeyTooShort 는 LOG_REF_KEY 가 MinKeyBytes 보다 짧을 때 돌려준다. 오류 문구에 키 값을 담지 않는다.

Functions

func ErrAttrs

func ErrAttrs(err error) []any

ErrAttrs 는 오류를 로그에 남길 때 쓰는 속성이다. 오류 문구는 남기지 않는다.

  • err_type: 맨 위 오류의 타입
  • cause_type: 사슬 맨 안쪽 오류의 타입(맨 위와 다를 때만)
  • op: *fs.PathError·*os.LinkError(또는 StripPath 결과)의 연산 이름
  • errno: syscall.Errno 문구(예: "no space left on device") — 고정 문구다
  • timeout: net.Error 이면 시간 초과 여부
  • truncated: io.ErrUnexpectedEOF 이면 true
  • 사슬 안 LogAttrer 가 내놓는 속성

속성 이름 err_type·op·errno·timeout·truncated 는 v0.25.3 녹음 핸들러 (api.recordingErrAttrs)와 같다. 운영 로그 대조 쿼리가 계속 맞는다.

func FileAttrs

func FileAttrs(root, path string) []any

FileAttrs 는 파일을 가리키는 로그 속성이다: "file_ref", "ext". 경로·파일 이름은 담지 않는다.

func FileRef

func FileRef(s string) string

FileRef 는 s 의 키 있는 해시 hex(HMAC-SHA256(key, s)[:8]) 다. 같은 키에서 같은 입력은 같은 ref 를 받으므로 한 파일의 로그 줄들을 서로 맞춰 볼 수 있다.

whisper 수집기는 오디오 디렉터리 기준 상대 경로(relPath)를 넣는다. 그래서 SafeSourceID("transcript:"+relPath) 의 ref 와 whisper 로그의 file_ref 가 같은 값이 된다.

func PrefixedErrAttrs

func PrefixedErrAttrs(prefix string, err error) []any

PrefixedErrAttrs 는 ErrAttrs 와 같지만 속성 이름 앞에 prefix 를 붙인다. 한 로그 줄에 오류가 둘 이상일 때 쓴다(예: "mkdir_err_type").

func RelPath

func RelPath(root, path string) string

RelPath 는 root 기준 상대 경로다. 계산할 수 없으면 path 를 그대로 쓴다. whisper 수집기가 source_id 를 만들 때와 같은 규칙이다(같은 파일 → 같은 ref).

func SafeExt

func SafeExt(path string) string

SafeExt 는 path 의 확장자(소문자)를 돌려준다. 모양이 안전하지 않으면 "other". 확장자가 없으면 "".

func SafeSourceID

func SafeSourceID(id string) string

SafeSourceID 는 source_id 를 로그에 남길 수 있는 모양으로 바꾼다.

  • 빈 값은 "".
  • 모양을 확인한 것(gmail·discord·insight·UUID)은 그대로.
  • sms·call-log 는 "{접두사}:{시각}:ref={FileRef(나머지)}".
  • "transcript:{relPath}" 를 포함한 그 밖의 "{접두사}:{나머지}" 는 "{접두사}:ref={FileRef(나머지)}". transcript 의 ref 는 whisper 로그의 file_ref 와 같은 값이다.
  • 접두사가 없으면 "ref={FileRef(id)}".

허용 목록 방식이다 — 새 수집기가 생겨도 기본값은 ref 로 가린 모양이다.

func StripPath

func StripPath(err error) error

StripPath 는 err 가 *fs.PathError 나 *os.LinkError 이면 경로를 뺀 오류로 바꾼다. 그 밖의 오류는 그대로 돌려준다.

맨 위 오류만 본다 — 이미 fmt.Errorf 로 감싼 오류 안의 경로는 뺄 수 없다. 그래서 os 호출 바로 뒤, 감싸기 전에 부른다:

fmt.Errorf("read audio file: %w", logsafe.StripPath(err)) // 감싸기 전에 경로를 뺀다

Types

type KeySource

type KeySource string

KeySource 는 현재 키가 어디서 왔는지 나타낸다. 시작 로그에는 이 값만 남기고 키 자체는 절대 남기지 않는다.

const (
	// KeySourceEnv: LOG_REF_KEY 에서 읽은 고정 키. 운영자가 같은 키로 로컬에서
	// ref 를 계산해 로그와 맞춰 볼 수 있다.
	KeySourceEnv KeySource = "env"
	// KeySourceEphemeral: 프로세스 시작 때 만든 무작위 키. 같은 프로세스
	// 안에서만 서로 맞춰 볼 수 있다.
	KeySourceEphemeral KeySource = "ephemeral"
)

func Configure

func Configure(key string) (KeySource, error)

Configure 는 file_ref 키를 정한다. key 가 비어 있으면(앞뒤 공백 제거 후) 무작위 키를 새로 만든다. 프로세스 시작 때 한 번 부른다.

func ConfigureFromEnv

func ConfigureFromEnv() (KeySource, error)

ConfigureFromEnv 는 LOG_REF_KEY 로 Configure 를 부른다.

func CurrentKeySource

func CurrentKeySource() KeySource

CurrentKeySource 는 지금 쓰는 키의 출처다.

type LogAttrer

type LogAttrer interface {
	LogAttrs() []any
}

LogAttrer 는 로그에 남겨도 안전한 속성을 스스로 아는 오류가 구현한다. ErrAttrs 는 오류 사슬의 모든 LogAttrer 에서 속성을 모은다. 속성 값에는 고정 문구·숫자·검증한 코드만 넣어야 한다.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL