4528a55e6a
为什么:T6a 需要一张批次/差异表、全量只读扫描、R1-R8 判定和 finding 级处置;现有解析监控只转述台账状态,无法回答库内是否一致。 做什么:迁移 000084(upload_md5、runs/findings/actions、共享 removal_candidates 视图);WeKnora 字段与 1000/页扫描;runtime 只读客户端(0400 Key、503 降级);扫描/矩阵/动作/定时/僵死回收;9 条接口与 RBAC;overview 队列与删两张常量卡。 边界:只对测试库;对账零写请求;R1/R2 单条确认;R6 复用 /removal;R2 只管理员;生产链路未改。 验证:make verify 全绿(17 项);新增矩阵/扫描 R1-R8/零写/失败整批/动作/运行时/密钥权限测试。
89 lines
2.4 KiB
Go
89 lines
2.4 KiB
Go
package weknora
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"strings"
|
|
"syscall"
|
|
)
|
|
|
|
const maxAPIKeyFileBytes = 16 * 1024
|
|
|
|
func ReadAPIKeyFile(path string) (string, error) {
|
|
fd, err := syscall.Open(path, syscall.O_RDONLY|syscall.O_NOFOLLOW, 0)
|
|
if err != nil {
|
|
return "", fmt.Errorf("open API key file: %w", err)
|
|
}
|
|
file := os.NewFile(uintptr(fd), "test-weknora-api-key")
|
|
if file == nil {
|
|
_ = syscall.Close(fd)
|
|
return "", errors.New("open API key file: invalid file descriptor")
|
|
}
|
|
defer file.Close()
|
|
|
|
info, err := file.Stat()
|
|
if err != nil {
|
|
return "", fmt.Errorf("inspect API key file: %w", err)
|
|
}
|
|
if !info.Mode().IsRegular() {
|
|
return "", errors.New("API key file must be a regular file")
|
|
}
|
|
if info.Mode().Perm() != 0o600 {
|
|
return "", errors.New("API key file permissions must be 0600")
|
|
}
|
|
|
|
body, err := io.ReadAll(io.LimitReader(file, maxAPIKeyFileBytes+1))
|
|
if err != nil {
|
|
return "", fmt.Errorf("read API key file: %w", err)
|
|
}
|
|
if len(body) > maxAPIKeyFileBytes {
|
|
return "", errors.New("API key file is too large")
|
|
}
|
|
key := strings.TrimSpace(string(body))
|
|
if key == "" {
|
|
return "", errors.New("API key file is empty")
|
|
}
|
|
return key, nil
|
|
}
|
|
|
|
// ReadRuntimeKeyFile reads the platform-level read-only key. Q7 fixes the
|
|
// contract at mode 0400: unlike tenant keys (0600), this key must not be
|
|
// readable by the process owner's group and never leaves the API process.
|
|
func ReadRuntimeKeyFile(path string) (string, error) {
|
|
fd, err := syscall.Open(path, syscall.O_RDONLY|syscall.O_NOFOLLOW, 0)
|
|
if err != nil {
|
|
return "", fmt.Errorf("open runtime key file: %w", err)
|
|
}
|
|
file := os.NewFile(uintptr(fd), "weknora-runtime-key")
|
|
if file == nil {
|
|
_ = syscall.Close(fd)
|
|
return "", errors.New("open runtime key file: invalid file descriptor")
|
|
}
|
|
defer file.Close()
|
|
|
|
info, err := file.Stat()
|
|
if err != nil {
|
|
return "", fmt.Errorf("inspect runtime key file: %w", err)
|
|
}
|
|
if !info.Mode().IsRegular() {
|
|
return "", errors.New("runtime key file must be a regular file")
|
|
}
|
|
if info.Mode().Perm() != 0o400 {
|
|
return "", errors.New("runtime key file permissions must be 0400")
|
|
}
|
|
body, err := io.ReadAll(io.LimitReader(file, maxAPIKeyFileBytes+1))
|
|
if err != nil {
|
|
return "", fmt.Errorf("read runtime key file: %w", err)
|
|
}
|
|
if len(body) > maxAPIKeyFileBytes {
|
|
return "", errors.New("runtime key file is too large")
|
|
}
|
|
key := strings.TrimSpace(string(body))
|
|
if key == "" {
|
|
return "", errors.New("runtime key file is empty")
|
|
}
|
|
return key, nil
|
|
}
|