feat(auth): accounts, sessions, RBAC and server-stamped actors
上线账号权限管理(后端): - 迁移 000032:users + user_sessions(token 只存 sha256,服务端可即时撤销) - bcrypt 密码、8 次失败锁 15 分钟、统一 401 错误防用户名枚举 - 三角色 viewer/engineer/admin;HttpOnly+SameSite=Lax 会话 cookie,滑动续期 - 强制改密锁定:must_change_password 会话只能走 auth 端点;首登 dobotai - bootstrap:users 表为空时创建 admin(初始密码 dobotai,强制改密) - 生产双闸授权/回滚/失败重试收敛为 admin 角色;AuthorizedBy 用会话身份 - 全部业务写端点删除手填 actor 字段(operator_ref/reviewer_ref/created_by/ sealed_by/confirmed_by),服务端从会话注入;auth 关闭的开发 socket 回退 system - 成员管理 API:列表/创建/改角色/启停/重置密码,最后一名 active admin 保护 - RBAC 矩阵纯函数契约测试 + 中间件行为测试(登录/锁定/角色/审计戳) - 附带修复:GET /api/v1/users 曾被 GET=viewer 规则误放行(测试抓到)
This commit is contained in:
+42
-35
@@ -106,6 +106,7 @@ type Config struct {
|
||||
ProductionReleaseAuthorizerRef string
|
||||
ProductionReleaseAuthorizationToken string
|
||||
NewID func() (string, error)
|
||||
Auth *AuthConfig
|
||||
}
|
||||
|
||||
func NewHandler(config Config) (http.Handler, error) {
|
||||
@@ -344,6 +345,14 @@ func NewHandler(config Config) (http.Handler, error) {
|
||||
mux.HandleFunc("GET /api/v1/job-failures", func(w http.ResponseWriter, r *http.Request) {
|
||||
listJobFailures(w, r, config.Store)
|
||||
})
|
||||
if config.Auth != nil {
|
||||
if config.Auth.Store == nil {
|
||||
return nil, errors.New("auth configuration requires a store")
|
||||
}
|
||||
config.authRoutes(mux)
|
||||
config.userRoutes(mux)
|
||||
return config.Auth.authenticate(mux), nil
|
||||
}
|
||||
return mux, nil
|
||||
}
|
||||
|
||||
@@ -622,7 +631,6 @@ type catalogRouteInput struct {
|
||||
NodeIDs []string `json:"node_ids"`
|
||||
Policy string `json:"policy"`
|
||||
Visibility string `json:"visibility"`
|
||||
OperatorRef string `json:"operator_ref"`
|
||||
Reason string `json:"reason"`
|
||||
}
|
||||
|
||||
@@ -678,8 +686,7 @@ func routeCatalogFiles(w http.ResponseWriter, r *http.Request, config Config) {
|
||||
result, replayed, err := config.Store.RouteCatalogFilesWithOperation(r.Context(), governance.CatalogRouteRequest{
|
||||
ConnectionID: input.ConnectionID, NodeIDs: input.NodeIDs, Visibility: visibility,
|
||||
TargetKnowledgeBaseID: targetKnowledgeBaseID, TargetKnowledgeBaseIDs: targetKnowledgeBaseIDs,
|
||||
OperatorRef: strings.TrimSpace(input.OperatorRef),
|
||||
Reason: strings.TrimSpace(input.Reason), Now: config.Now().UTC(),
|
||||
OperatorRef: actorFor(r), Reason: strings.TrimSpace(input.Reason), Now: config.Now().UTC(),
|
||||
}, operationID)
|
||||
if errors.Is(err, postgresstore.ErrOperationRequestConflict) {
|
||||
writeJSON(w, http.StatusConflict, map[string]string{"error": "Idempotency-Key conflicts with a different request"})
|
||||
@@ -791,7 +798,7 @@ type scopeRuleInput struct {
|
||||
DirectoryPath string `json:"directory_path"`
|
||||
SourceItemID string `json:"source_item_id"`
|
||||
Visibility governance.Visibility `json:"visibility"`
|
||||
OperatorRef string `json:"operator_ref"`
|
||||
|
||||
Reason string `json:"reason"`
|
||||
}
|
||||
|
||||
@@ -828,7 +835,7 @@ func mutateScopeRule(w http.ResponseWriter, r *http.Request, config Config, clea
|
||||
request := governance.ScopeRuleMutation{
|
||||
TargetType: input.TargetType, ConnectionID: strings.TrimSpace(input.ConnectionID),
|
||||
DirectoryPath: input.DirectoryPath, SourceItemID: strings.TrimSpace(input.SourceItemID),
|
||||
Visibility: input.Visibility, OperatorRef: strings.TrimSpace(input.OperatorRef), Reason: strings.TrimSpace(input.Reason),
|
||||
Visibility: input.Visibility, OperatorRef: actorFor(r), Reason: strings.TrimSpace(input.Reason),
|
||||
Clear: clear, Now: config.Now().UTC(),
|
||||
}
|
||||
if clear {
|
||||
@@ -1091,7 +1098,6 @@ func listReviewCandidates(w http.ResponseWriter, r *http.Request, store Store) {
|
||||
type reviewPublicationRequest struct {
|
||||
Decision string `json:"decision"`
|
||||
DecidedVisibility string `json:"decided_visibility"`
|
||||
ReviewerRef string `json:"reviewer_ref"`
|
||||
DecisionCode string `json:"decision_code"`
|
||||
}
|
||||
|
||||
@@ -1107,7 +1113,7 @@ func reviewPublication(w http.ResponseWriter, r *http.Request, config Config) {
|
||||
}
|
||||
review, replayed, err := config.Store.ReviewPublicationWithOperation(r.Context(), governance.ReviewPublicationRequest{
|
||||
PublicationID: r.PathValue("id"), Decision: governance.ReviewState(input.Decision),
|
||||
DecidedVisibility: governance.Visibility(input.DecidedVisibility), ReviewerRef: input.ReviewerRef,
|
||||
DecidedVisibility: governance.Visibility(input.DecidedVisibility), ReviewerRef: actorFor(r),
|
||||
DecisionCode: input.DecisionCode, Now: config.Now().UTC(),
|
||||
}, operationID)
|
||||
switch {
|
||||
@@ -1222,7 +1228,6 @@ func activateUISettings(w http.ResponseWriter, r *http.Request, config Config) {
|
||||
|
||||
type createReleaseBatchRequest struct {
|
||||
Name string `json:"name"`
|
||||
CreatedBy string `json:"created_by"`
|
||||
PublicationIDs []string `json:"publication_ids"`
|
||||
DeletePublicationIDs []string `json:"delete_publication_ids"`
|
||||
ReleaseChangeSetID string `json:"release_change_set_id"`
|
||||
@@ -1239,7 +1244,7 @@ func createReleaseBatch(w http.ResponseWriter, r *http.Request, config Config) {
|
||||
return
|
||||
}
|
||||
batch, replayed, err := config.Store.CreateReleaseBatchWithOperation(r.Context(), governance.CreateReleaseBatchRequest{
|
||||
Name: input.Name, CreatedBy: input.CreatedBy, PublicationIDs: input.PublicationIDs, DeletePublicationIDs: input.DeletePublicationIDs, ReleaseChangeSetID: input.ReleaseChangeSetID, Now: config.Now().UTC(),
|
||||
Name: input.Name, CreatedBy: actorFor(r), PublicationIDs: input.PublicationIDs, DeletePublicationIDs: input.DeletePublicationIDs, ReleaseChangeSetID: input.ReleaseChangeSetID, Now: config.Now().UTC(),
|
||||
}, operationID)
|
||||
switch {
|
||||
case errors.Is(err, postgresstore.ErrOperationRequestConflict):
|
||||
@@ -1262,8 +1267,7 @@ func createReleaseBatch(w http.ResponseWriter, r *http.Request, config Config) {
|
||||
}
|
||||
|
||||
type generateReleaseChangeSetRequest struct {
|
||||
Name string `json:"name"`
|
||||
CreatedBy string `json:"created_by"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
func getReleaseAuditChain(w http.ResponseWriter, r *http.Request, store Store) {
|
||||
@@ -1294,7 +1298,7 @@ func generateReleaseChangeSet(w http.ResponseWriter, r *http.Request, config Con
|
||||
return
|
||||
}
|
||||
set, replayed, err := config.Store.GenerateReleaseChangeSetWithOperation(r.Context(), governance.GenerateReleaseChangeSetRequest{
|
||||
Name: input.Name, CreatedBy: input.CreatedBy, Now: config.Now().UTC(),
|
||||
Name: input.Name, CreatedBy: actorFor(r), Now: config.Now().UTC(),
|
||||
}, operationID)
|
||||
switch {
|
||||
case errors.Is(err, postgresstore.ErrOperationRequestConflict):
|
||||
@@ -1358,16 +1362,14 @@ func confirmReleaseChangeSet(w http.ResponseWriter, r *http.Request, config Conf
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var input struct {
|
||||
ConfirmedBy string `json:"confirmed_by"`
|
||||
}
|
||||
var input struct{}
|
||||
if err := decodeStrictJSON(r, &input); err != nil {
|
||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid confirmation request"})
|
||||
return
|
||||
}
|
||||
id := r.PathValue("id")
|
||||
set, replayed, err := config.Store.ConfirmReleaseChangeSetWithOperation(r.Context(), governance.ConfirmReleaseChangeSetRequest{
|
||||
ChangeSetID: id, ConfirmedBy: input.ConfirmedBy, Now: config.Now().UTC(),
|
||||
ChangeSetID: id, ConfirmedBy: actorFor(r), Now: config.Now().UTC(),
|
||||
}, operationID)
|
||||
switch {
|
||||
case errors.Is(err, postgresstore.ErrOperationRequestConflict):
|
||||
@@ -1389,9 +1391,7 @@ func confirmReleaseChangeSet(w http.ResponseWriter, r *http.Request, config Conf
|
||||
}
|
||||
}
|
||||
|
||||
type sealReleaseBatchRequest struct {
|
||||
SealedBy string `json:"sealed_by"`
|
||||
}
|
||||
type sealReleaseBatchRequest struct{}
|
||||
|
||||
func sealReleaseBatch(w http.ResponseWriter, r *http.Request, config Config) {
|
||||
operationID, ok := requireOperationID(w, r)
|
||||
@@ -1404,7 +1404,7 @@ func sealReleaseBatch(w http.ResponseWriter, r *http.Request, config Config) {
|
||||
return
|
||||
}
|
||||
batch, replayed, err := config.Store.SealReleaseBatchWithOperation(r.Context(), governance.SealReleaseBatchRequest{
|
||||
BatchID: r.PathValue("id"), SealedBy: input.SealedBy, Now: config.Now().UTC(),
|
||||
BatchID: r.PathValue("id"), SealedBy: actorFor(r), Now: config.Now().UTC(),
|
||||
}, operationID)
|
||||
switch {
|
||||
case errors.Is(err, postgresstore.ErrOperationRequestConflict):
|
||||
@@ -1451,7 +1451,7 @@ func authorizeProductionRelease(w http.ResponseWriter, r *http.Request, config C
|
||||
return
|
||||
}
|
||||
execution, replayed, err := config.Store.AuthorizeReleaseBatchWithOperation(r.Context(), governance.AuthorizeReleaseBatchRequest{
|
||||
BatchID: r.PathValue("id"), AuthorizedBy: config.ProductionReleaseAuthorizerRef,
|
||||
BatchID: r.PathValue("id"), AuthorizedBy: productionAuthorizer(r, config),
|
||||
AuthorizationCode: "production_release_authorized", Now: config.Now().UTC(),
|
||||
}, operationID)
|
||||
switch {
|
||||
@@ -1496,7 +1496,7 @@ func authorizeProductionRollback(w http.ResponseWriter, r *http.Request, config
|
||||
return
|
||||
}
|
||||
execution, replayed, err := config.Store.AuthorizeReleaseRollbackWithOperation(r.Context(), governance.AuthorizeReleaseRollbackRequest{
|
||||
PriorExecutionID: r.PathValue("id"), AuthorizedBy: config.ProductionReleaseAuthorizerRef,
|
||||
PriorExecutionID: r.PathValue("id"), AuthorizedBy: productionAuthorizer(r, config),
|
||||
AuthorizationCode: "production_rollback_authorized", Now: config.Now().UTC(),
|
||||
}, operationID)
|
||||
switch {
|
||||
@@ -1560,6 +1560,17 @@ func retryProductionRelease(w http.ResponseWriter, r *http.Request, config Confi
|
||||
writeJSON(w, http.StatusAccepted, map[string]any{"data": safeReleaseExecution(execution), "operation": operation})
|
||||
}
|
||||
|
||||
// productionAuthorizer names the human behind a production double-gate call.
|
||||
// With auth enabled the session identity (an admin, enforced by the RBAC
|
||||
// matrix) is authoritative; the env-declared ref is only the fallback for the
|
||||
// auth-disabled development socket.
|
||||
func productionAuthorizer(r *http.Request, config Config) string {
|
||||
if identity, ok := sessionFrom(r.Context()); ok {
|
||||
return identity.Username
|
||||
}
|
||||
return config.ProductionReleaseAuthorizerRef
|
||||
}
|
||||
|
||||
func productionReleaseAuthorized(r *http.Request, token string) bool {
|
||||
provided := r.Header.Get("Authorization")
|
||||
expected := "Bearer " + token
|
||||
@@ -1700,9 +1711,8 @@ func listTestPublications(w http.ResponseWriter, r *http.Request, store Store) {
|
||||
}
|
||||
|
||||
type confirmChangeSetInput struct {
|
||||
ItemSeqs []int64 `json:"item_seqs"`
|
||||
OperatorRef string `json:"operator_ref"`
|
||||
Reason string `json:"reason"`
|
||||
ItemSeqs []int64 `json:"item_seqs"`
|
||||
Reason string `json:"reason"`
|
||||
}
|
||||
|
||||
func confirmCatalogChangeSet(w http.ResponseWriter, r *http.Request, config Config) {
|
||||
@@ -1719,7 +1729,7 @@ func confirmCatalogChangeSet(w http.ResponseWriter, r *http.Request, config Conf
|
||||
}
|
||||
result, replayed, err := config.Store.ConfirmCatalogChangeSetWithOperation(r.Context(), governance.ConfirmCatalogChangeSetRequest{
|
||||
ChangeSetID: r.PathValue("id"), ItemSeqs: input.ItemSeqs,
|
||||
OperatorRef: input.OperatorRef, Reason: input.Reason,
|
||||
OperatorRef: actorFor(r), Reason: input.Reason,
|
||||
InternalTargetKBID: config.InternalRoutingKnowledgeBaseID, ExternalTargetKBID: config.ExternalRoutingKnowledgeBaseID,
|
||||
Now: config.Now().UTC(),
|
||||
}, operationID)
|
||||
@@ -1743,7 +1753,6 @@ func confirmCatalogChangeSet(w http.ResponseWriter, r *http.Request, config Conf
|
||||
|
||||
type startTestPublicationsInput struct {
|
||||
PublicationIDs []string `json:"publication_ids"`
|
||||
OperatorRef string `json:"operator_ref"`
|
||||
Reason string `json:"reason"`
|
||||
}
|
||||
|
||||
@@ -1757,10 +1766,9 @@ func startTestPublications(w http.ResponseWriter, r *http.Request, config Config
|
||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid processing confirmation"})
|
||||
return
|
||||
}
|
||||
operator := strings.TrimSpace(input.OperatorRef)
|
||||
reason := strings.TrimSpace(input.Reason)
|
||||
if operator == "" || len(operator) > 128 || reason == "" || len(reason) > 500 {
|
||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "operator_ref and reason are required"})
|
||||
if reason == "" || len(reason) > 500 {
|
||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "reason is required"})
|
||||
return
|
||||
}
|
||||
seen := make(map[string]bool, len(input.PublicationIDs))
|
||||
@@ -1773,7 +1781,7 @@ func startTestPublications(w http.ResponseWriter, r *http.Request, config Config
|
||||
}
|
||||
result, replayed, err := config.Store.StartTestPublicationsWithOperation(r.Context(), governance.StartTestPublicationsRequest{
|
||||
PublicationIDs: input.PublicationIDs,
|
||||
OperatorRef: operator,
|
||||
OperatorRef: actorFor(r),
|
||||
Reason: reason,
|
||||
Now: config.Now().UTC(),
|
||||
}, operationID)
|
||||
@@ -1826,10 +1834,9 @@ func retryPublicationsBatch(w http.ResponseWriter, r *http.Request, config Confi
|
||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid retry batch"})
|
||||
return
|
||||
}
|
||||
operator := strings.TrimSpace(input.OperatorRef)
|
||||
reason := strings.TrimSpace(input.Reason)
|
||||
if operator == "" || len(operator) > 128 || reason == "" || len(reason) > 500 {
|
||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "operator_ref and reason are required"})
|
||||
if reason == "" || len(reason) > 500 {
|
||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "reason is required"})
|
||||
return
|
||||
}
|
||||
seen := make(map[string]bool, len(input.PublicationIDs))
|
||||
@@ -1842,7 +1849,7 @@ func retryPublicationsBatch(w http.ResponseWriter, r *http.Request, config Confi
|
||||
}
|
||||
result, replayed, err := config.Store.RetryPublicationsBatchWithOperation(r.Context(), governance.RetryPublicationsBatchRequest{
|
||||
PublicationIDs: input.PublicationIDs,
|
||||
OperatorRef: operator,
|
||||
OperatorRef: actorFor(r),
|
||||
Reason: reason,
|
||||
Now: config.Now().UTC(),
|
||||
}, operationID)
|
||||
|
||||
Reference in New Issue
Block a user