3825859da6
build / build (push) Successful in 2m20s
PP 增加受控凭据更新、真实登录与目录检查,待验证修改不会替换生效配置;扫描与取件共享生效修订。 钉钉来源向导、校验与登记共用 DWS 授权,支持中途恢复与换账号保护,取消旧扫码自动回落。 认证审计复用已有来源审计页,配置响应不回传密码;补齐运行镜像脚本与权限。 验证:make verify 退出 0;PP/DWS/httpapi 目标 race 回归通过;隔离 HTTP 服务验证 PP 协议及扫描/取件消费者。 尚未部署;真实 PP 凭据与目录可用性在测试站部署后验证。
213 lines
8.5 KiB
Go
213 lines
8.5 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"knowledge-governance/internal/dingtalkdiscovery"
|
|
"knowledge-governance/internal/governance"
|
|
"knowledge-governance/internal/jobs"
|
|
postgresstore "knowledge-governance/internal/platform/postgres"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type dwsSourceError struct {
|
|
Status int
|
|
Code, Hint string
|
|
}
|
|
|
|
func (e *dwsSourceError) Error() string { return e.Hint }
|
|
func writeDWSSourceError(w http.ResponseWriter, err error) {
|
|
var e *dwsSourceError
|
|
if errors.As(err, &e) {
|
|
writeJSON(w, e.Status, map[string]string{"error": e.Code, "hint": e.Hint})
|
|
return
|
|
}
|
|
writeJSON(w, 500, map[string]string{"error": "dws_source_failed", "hint": "读取钉钉来源失败,请重试"})
|
|
}
|
|
func availableDWS(ctx context.Context, config Config) (dingtalkdiscovery.Result, error) {
|
|
identity := ""
|
|
if config.DingtalkSources == nil {
|
|
return dingtalkdiscovery.Result{}, &dwsSourceError{501, "dws_unwired", "DWS 来源服务未接通"}
|
|
}
|
|
if reader, ok := config.DingtalkSources.(interface {
|
|
ReadDWSMetadata(context.Context) (dingtalkdiscovery.CredentialStatus, error)
|
|
}); ok {
|
|
status, err := reader.ReadDWSMetadata(ctx)
|
|
if err != nil || !status.StatusKnown {
|
|
return dingtalkdiscovery.Result{}, &dwsSourceError{503, "dws_status_unavailable", "暂时无法读取 DWS 授权状态,请重试"}
|
|
}
|
|
identity = status.Identity
|
|
if !status.Authenticated && !status.RefreshTokenValid {
|
|
return dingtalkdiscovery.Result{}, &dwsSourceError{401, "dws_login_required", "DWS 未登录或授权已失效,请重新授权"}
|
|
}
|
|
}
|
|
result, err := config.DingtalkSources.ListAvailableDingtalkSources(ctx)
|
|
if err != nil {
|
|
if errors.Is(err, dingtalkdiscovery.ErrCredentials) {
|
|
return result, &dwsSourceError{401, "dws_login_required", "DWS 授权已失效,请重新授权"}
|
|
}
|
|
if errors.Is(err, dingtalkdiscovery.ErrPermission) {
|
|
return result, &dwsSourceError{403, "dws_permission_denied", "当前 DWS 账号缺少目录读取权限"}
|
|
}
|
|
if errors.Is(err, dingtalkdiscovery.ErrUnavailable) {
|
|
return result, &dwsSourceError{503, "dws_unavailable", "DWS 目录暂时无法读取,请检查连接后重试"}
|
|
}
|
|
return result, err
|
|
}
|
|
if identity != "" {
|
|
reader := config.DingtalkSources.(interface {
|
|
ReadDWSMetadata(context.Context) (dingtalkdiscovery.CredentialStatus, error)
|
|
})
|
|
fresh, e := reader.ReadDWSMetadata(ctx)
|
|
if e != nil {
|
|
return result, &dwsSourceError{503, "dws_status_unavailable", "无法确认当前账号,请重试"}
|
|
}
|
|
if fresh.Identity != identity {
|
|
return result, &dwsSourceError{409, "dws_identity_changed", "DWS 账号或组织已变化,请重新确认来源"}
|
|
}
|
|
}
|
|
result.Identity = identity
|
|
if config.Store != nil {
|
|
if page, e := config.Store.ListSourceConnections(ctx); e == nil {
|
|
seen := map[string]bool{}
|
|
for _, c := range page.Items {
|
|
seen[c.ID] = true
|
|
}
|
|
for i, kb := range result.KnowledgeBases {
|
|
in := postgresstore.RegisterDingtalkSpaceInput{RootKey: "space-" + kb.SpaceID}
|
|
result.KnowledgeBases[i].Registered = seen[in.ConnectionID()]
|
|
}
|
|
for i, space := range result.DriveSpaces {
|
|
in := postgresstore.RegisterDingtalkSpaceInput{RootKey: "space-" + space.SpaceID}
|
|
result.DriveSpaces[i].Registered = seen[in.ConnectionID()]
|
|
}
|
|
}
|
|
}
|
|
if registry, ok := config.Store.(interface {
|
|
RegisteredDingtalkSpaceIDs(context.Context) (map[string]bool, error)
|
|
}); ok {
|
|
ids, e := registry.RegisteredDingtalkSpaceIDs(ctx)
|
|
if e != nil {
|
|
return result, &dwsSourceError{503, "dws_registration_state_unavailable", "无法核对已接入空间,请重试"}
|
|
}
|
|
for i, kb := range result.KnowledgeBases {
|
|
result.KnowledgeBases[i].Registered = ids[kb.SpaceID]
|
|
}
|
|
for i, space := range result.DriveSpaces {
|
|
result.DriveSpaces[i].Registered = ids[space.SpaceID]
|
|
}
|
|
}
|
|
return result, nil
|
|
}
|
|
func resolveDWS(result dingtalkdiscovery.Result, raw string) (postgresstore.RegisterDingtalkSpaceInput, error) {
|
|
spaceID := extractDingtalkSpaceID(raw)
|
|
if spaceID == "" {
|
|
return postgresstore.RegisterDingtalkSpaceInput{}, &dwsSourceError{400, "invalid_space_url", "请输入有效的钉钉空间地址"}
|
|
}
|
|
for _, base := range result.KnowledgeBases {
|
|
if base.SpaceID == spaceID {
|
|
return postgresstore.RegisterDingtalkSpaceInput{RootKey: "space-" + spaceID, Name: base.Name, RootID: spaceID, SpaceID: spaceID}, nil
|
|
}
|
|
}
|
|
for _, space := range result.DriveSpaces {
|
|
if space.SpaceID == spaceID && space.RootFolderID != "" {
|
|
return postgresstore.RegisterDingtalkSpaceInput{RootKey: "space-" + spaceID, Name: space.Name, RootID: space.RootFolderID, SpaceID: spaceID}, nil
|
|
}
|
|
}
|
|
if result.KnowledgeNotice != "" || result.DriveNotice != "" {
|
|
return postgresstore.RegisterDingtalkSpaceInput{}, &dwsSourceError{503, "dws_scope_incomplete", "当前目录结果不完整,无法确认所选空间;请检查相应目录权限后重试"}
|
|
}
|
|
return postgresstore.RegisterDingtalkSpaceInput{}, &dwsSourceError{409, "dws_space_not_visible", "当前 DWS 账号已看不到所选空间,请刷新范围或核对账号权限"}
|
|
}
|
|
func registerDWSOnlySpace(w http.ResponseWriter, r *http.Request, config Config, raw, operationID string) {
|
|
if extractDingtalkSpaceID(raw) == "" {
|
|
writeDWSSourceError(w, &dwsSourceError{400, "invalid_space_url", "请输入有效的钉钉空间地址"})
|
|
return
|
|
}
|
|
ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second)
|
|
defer cancel()
|
|
result, err := availableDWS(ctx, config)
|
|
if err != nil {
|
|
writeDWSSourceError(w, err)
|
|
return
|
|
}
|
|
if wanted := r.Header.Get("X-DWS-Identity"); wanted != "" && wanted != result.Identity {
|
|
writeDWSSourceError(w, &dwsSourceError{409, "dws_identity_changed", "DWS 账号或组织已变化,请重新确认来源"})
|
|
return
|
|
}
|
|
input, err := resolveDWS(result, raw)
|
|
if err != nil {
|
|
writeDWSSourceError(w, err)
|
|
return
|
|
}
|
|
input.Now = config.Now()
|
|
input.Actor = actorFor(r)
|
|
store, ok := config.Store.(interface {
|
|
RegisterDingtalkSpace(context.Context, postgresstore.RegisterDingtalkSpaceInput) (string, error)
|
|
})
|
|
if !ok {
|
|
writeJSON(w, 503, map[string]string{"error": "registration_unavailable"})
|
|
return
|
|
}
|
|
connectionID, err := store.RegisterDingtalkSpace(ctx, input)
|
|
switch {
|
|
case errors.Is(err, postgresstore.ErrDingtalkSpaceNameTaken):
|
|
writeJSON(w, 409, map[string]string{"error": "space_name_conflict", "hint": "已有同名来源仍在使用:请核对空间身份,必要时先移除旧连接(原去掉来源操作)"})
|
|
return
|
|
case errors.Is(err, postgresstore.ErrDingtalkSpaceIdentityConflict):
|
|
writeJSON(w, 409, map[string]string{"error": "space_identity_conflict", "hint": "登记键已指向另一个空间,请刷新后核对来源"})
|
|
return
|
|
case err != nil:
|
|
writeJSON(w, 500, map[string]string{"error": "space_registration_failed"})
|
|
return
|
|
}
|
|
queued := false
|
|
if queue, ok := config.Store.(interface {
|
|
EnqueueCatalogRefreshWithOperation(context.Context, governance.CatalogSource, string, time.Time) (jobs.Job, bool, error)
|
|
}); ok {
|
|
_, _, err = queue.EnqueueCatalogRefreshWithOperation(ctx, governance.CatalogSource{ConnectionID: connectionID, SourceType: governance.SourceTypeDingTalk, Name: input.Name}, "dws-source-first:"+connectionID, config.Now().UTC())
|
|
queued = err == nil
|
|
}
|
|
writeJSON(w, 201, map[string]any{"data": map[string]any{"connection_id": connectionID, "name": input.Name, "first_scan_queued": queued}})
|
|
}
|
|
func validateDWSSpace(w http.ResponseWriter, r *http.Request, config Config) {
|
|
if !credentialAdmin(w, r, "dingtalk-space") {
|
|
return
|
|
}
|
|
var input struct {
|
|
URLs []string `json:"urls"`
|
|
}
|
|
if decodeJSONStrict(r, &input) != nil || len(input.URLs) == 0 || len(input.URLs) > 100 {
|
|
writeJSON(w, 400, map[string]string{"error": "请选择有效的空间范围"})
|
|
return
|
|
}
|
|
ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second)
|
|
defer cancel()
|
|
result, err := availableDWS(ctx, config)
|
|
if err != nil {
|
|
writeDWSSourceError(w, err)
|
|
return
|
|
}
|
|
if wanted := r.Header.Get("X-DWS-Identity"); wanted != "" && wanted != result.Identity {
|
|
writeDWSSourceError(w, &dwsSourceError{409, "dws_identity_changed", "DWS 账号或组织已变化,请重新确认来源"})
|
|
return
|
|
}
|
|
for _, raw := range input.URLs {
|
|
if _, err := resolveDWS(result, raw); err != nil {
|
|
writeDWSSourceError(w, err)
|
|
return
|
|
}
|
|
}
|
|
writeJSON(w, 200, map[string]any{"data": map[string]any{"valid": true, "checked_at": config.Now().UTC(), "count": len(input.URLs)}})
|
|
}
|
|
func validDingtalkURL(raw string) bool {
|
|
if !strings.Contains(raw, "://") {
|
|
return true
|
|
}
|
|
u, err := url.Parse(raw)
|
|
return err == nil && u.Scheme == "https" && u.Host == "alidocs.dingtalk.com" && u.User == nil && u.Fragment == ""
|
|
}
|