修复HTTP验证测试并按实际签发配置复核项目权限
This commit is contained in:
1 parent
16224fa1f5
commit
201a272562
10 files changed
+221
-2
No files matched your search
@@ -103,7 +103,7 @@ func TestHTTPTaskAuthorizationRetainsProjectPermissions(t *testing.T) {
|
||||
if (calls == 1) != want {
|
||||
t.Fatalf("HTTP 签发项目授权错误:%d %s", w.Code, w.Body.String())
|
||||
}
|
||||
err := server.authorizeCertificateTaskExecution(t.Context(), repository.Task{DomainID: "domain-1", ProjectID: "project-a", ScopeType: repository.ScopeProject, OperatorID: "actor", TaskType: "certificate_issue"}, repository.TaskAttempt{})
|
||||
err := server.authorizeCertificateTaskExecution(t.Context(), repository.Task{DomainID: "domain-1", HistoryScope: repository.HistoryScope{ProjectID: "project-a", ScopeType: repository.ScopeProject}, OperatorID: "actor", TaskType: "certificate_issue"}, repository.TaskAttempt{})
|
||||
if (err == nil) != want {
|
||||
t.Fatal("执行前授权与验证方式不匹配:", err)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/certificate"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/deployment"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/domain"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/httpchallenge"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/repository"
|
||||
)
|
||||
|
||||
var errSnapshotAccountReached = errors.New("测试已通过授权并到达账户阶段")
|
||||
|
||||
type snapshotTestAccounts struct{}
|
||||
|
||||
func (snapshotTestAccounts) LoadOrCreate(context.Context, string, string) (*certificate.AccountUser, error) {
|
||||
return nil, errSnapshotAccountReached
|
||||
}
|
||||
|
||||
func (snapshotTestAccounts) Save(context.Context, string, *certificate.AccountUser) error {
|
||||
return errors.New("本测试不应保存账户")
|
||||
}
|
||||
|
||||
// 在前置授权和实际签发读取之间确定性切换快照,不依赖不稳定的调度时序。
|
||||
func TestHTTPIssueReauthorizesActualSnapshot(t *testing.T) {
|
||||
for _, async := range []bool{false, true} {
|
||||
for _, change := range []string{"unchanged", "dns", "legacy_dns", "project", "domain", "certificate_revoked", "deployment_revoked"} {
|
||||
t.Run(map[bool]string{false: "sync", true: "async"}[async]+"/"+change, func(t *testing.T) {
|
||||
initial := domain.Domain{ID: "domain-1", Name: "example.com", CertificateType: domain.CertificateSingle, ChallengeType: domain.ChallengeHTTP01, ResourceScope: domain.ResourceScope{ProjectID: "default"}}
|
||||
visible := &stubDomainRepository{loaded: initial}
|
||||
actual := &stubDomainRepository{loaded: initial}
|
||||
var server *Server
|
||||
var issueErr error
|
||||
started := false
|
||||
operation := func(ctx context.Context, id string) (certificate.IssueResult, deployment.Report, error) {
|
||||
started = true
|
||||
switch change {
|
||||
case "dns":
|
||||
actual.loaded.ChallengeType = domain.ChallengeDNS01
|
||||
case "legacy_dns":
|
||||
actual.loaded.ChallengeType = ""
|
||||
case "project":
|
||||
actual.loaded.ProjectID = "another-project"
|
||||
case "domain":
|
||||
actual.loaded.ID = "another-domain"
|
||||
}
|
||||
service := certificate.NewService(server.config, actual, nil, nil, snapshotTestAccounts{})
|
||||
service.SetHTTPChallenges(httpchallenge.NewStore())
|
||||
result, err := service.Issue(ctx, id)
|
||||
issueErr = err
|
||||
return result, deployment.Report{}, err
|
||||
}
|
||||
if async {
|
||||
var cleanup func()
|
||||
server, _, _, cleanup = newTaskAPITestServer(t, operation)
|
||||
defer cleanup()
|
||||
} else {
|
||||
server = New(Dependencies{Config: readyTestConfig(t.TempDir()), Repository: visible, IssueCertificate: operation})
|
||||
}
|
||||
server.projectResources = projectResourceReaderStub{domains: visible}
|
||||
server.projectMembers = permissionProjectMemberRepository{allow: func(permission domain.ProjectPermission) error {
|
||||
if permission == domain.PermissionDNSCredentialUse || started && (change == "certificate_revoked" && permission == domain.PermissionCertificateManage ||
|
||||
change == "deployment_revoked" && permission == domain.PermissionDeploymentExecute) {
|
||||
return repository.ErrProjectAccessDenied
|
||||
}
|
||||
return nil
|
||||
}}
|
||||
response := httptest.NewRecorder()
|
||||
serveAdminHTTP(server, response, httptest.NewRequest(http.MethodPost, "/api/v1/domains/domain-1/issue", nil))
|
||||
if async {
|
||||
submittedTask(t, response)
|
||||
waitTaskWorkersForTest(t, server)
|
||||
}
|
||||
want := repository.ErrProjectAccessDenied
|
||||
if change == "unchanged" {
|
||||
want = errSnapshotAccountReached
|
||||
}
|
||||
if !started || !errors.Is(issueErr, want) {
|
||||
t.Fatalf("实际签发配置未正确重新授权:started=%v err=%v,响应=%d %s", started, issueErr, response.Code, response.Body.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHTTPIssueSnapshotRetainsRetryPermission(t *testing.T) {
|
||||
item := domain.Domain{ID: "domain-1", Name: "example.com", CertificateType: domain.CertificateSingle, ChallengeType: domain.ChallengeHTTP01, ResourceScope: domain.ResourceScope{ProjectID: "default"}}
|
||||
server := New(Dependencies{Config: readyTestConfig(t.TempDir()), ProjectMembers: permissionProjectMemberRepository{allow: func(permission domain.ProjectPermission) error {
|
||||
if permission == domain.PermissionTaskRetry {
|
||||
return repository.ErrProjectAccessDenied
|
||||
}
|
||||
return nil
|
||||
}}})
|
||||
service := certificate.NewService(server.config, &stubDomainRepository{loaded: item}, nil, nil, snapshotTestAccounts{})
|
||||
service.SetHTTPChallenges(httpchallenge.NewStore())
|
||||
ctx := server.withCertificateIssueAuthorization(t.Context(), "actor", item.ID, "default", true)
|
||||
if _, err := service.Issue(ctx, item.ID); !errors.Is(err, repository.ErrProjectAccessDenied) {
|
||||
t.Fatal("重试授权未在实际签发快照处生效:", err)
|
||||
}
|
||||
}
|
||||
@@ -778,7 +778,11 @@ func (s *Server) authorizeSynchronousCertificateIssue(c *gin.Context) bool {
|
||||
s.writeProjectResourceError(c, err)
|
||||
return false
|
||||
}
|
||||
return s.authorizeIssueChallenge(c, resource)
|
||||
if !s.authorizeIssueChallenge(c, resource) {
|
||||
return false
|
||||
}
|
||||
c.Request = c.Request.WithContext(s.withCertificateIssueAuthorization(c.Request.Context(), c.GetString(authenticatedActorKey), resource.ID, resource.ResourceScope.Normalize().ProjectID, false))
|
||||
return true
|
||||
}
|
||||
|
||||
func (s *Server) issueDomain(c *gin.Context) {
|
||||
|
||||
@@ -273,6 +273,9 @@ func (s *Server) runCertificateOperation(ctx context.Context, task repository.Ta
|
||||
if s.finishCancelledTask(task, attempt, executionCtx) {
|
||||
return
|
||||
}
|
||||
if task.TaskType == "certificate_issue" {
|
||||
executionCtx = s.withCertificateIssueAuthorization(executionCtx, task.OperatorID, task.DomainID, task.ProjectID, attempt.AttemptNumber > 1)
|
||||
}
|
||||
result, issueErr := operation(executionCtx, task.DomainID)
|
||||
certificateResult := result.Certificate
|
||||
var deploymentReport deployment.Report
|
||||
|
||||
@@ -3,10 +3,38 @@ package httpapi
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/certificate"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/domain"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/repository"
|
||||
)
|
||||
|
||||
// 把操作者与项目绑定到实际签发快照,防止授权后修改验证方式绕过 DNS 权限。
|
||||
func (s *Server) withCertificateIssueAuthorization(ctx context.Context, actor, domainID, projectID string, retry bool) context.Context {
|
||||
return certificate.WithIssueAuthorizer(ctx, func(ctx context.Context, item domain.Domain) error {
|
||||
authorizer, ok := s.projectMembers.(repository.ProjectAuthorizer)
|
||||
if !ok || actor == "" || projectID == "" || item.ID != domainID || item.ResourceScope.Normalize().ProjectID != projectID {
|
||||
return repository.ErrProjectAccessDenied
|
||||
}
|
||||
kind, err := domain.NormalizeChallengeType(item.ChallengeType, append([]string{item.Name}, item.SANs...))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
permissions := []domain.ProjectPermission{domain.PermissionCertificateManage, domain.PermissionDeploymentExecute}
|
||||
if kind == domain.ChallengeDNS01 {
|
||||
permissions = append(permissions, domain.PermissionDNSCredentialUse)
|
||||
}
|
||||
if retry {
|
||||
permissions = append(permissions, domain.PermissionTaskRetry)
|
||||
}
|
||||
for _, permission := range permissions {
|
||||
if err := authorizer.AuthorizeProject(ctx, actor, projectID, permission); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// 排队期间权限和资源归属可能变化;历史快照只用于审计,不作为授权凭证。
|
||||
// 只检查本次即将执行的操作,后台自动续期不从此 HTTP 任务入口执行。
|
||||
func (s *Server) authorizeCertificateTaskExecution(ctx context.Context, task repository.Task, attempt repository.TaskAttempt) error {
|
||||
|
||||
Reference in new issue
Block a user