修复HTTP验证测试并按实际签发配置复核项目权限
This commit is contained in:
1 parent
16224fa1f5
commit
201a272562
10 files changed
+221
-2
No files matched your search
@@ -0,0 +1,25 @@
|
||||
package certificate
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/domain"
|
||||
)
|
||||
|
||||
type issueAuthorizerContextKey struct{}
|
||||
|
||||
// IssueAuthorizer 检查签发实际使用的配置快照,而不是先前读取的验证方式。
|
||||
type IssueAuthorizer func(context.Context, domain.Domain) error
|
||||
|
||||
// WithIssueAuthorizer 为用户发起的工作流绑定实时授权;系统续期不使用用户会话。
|
||||
func WithIssueAuthorizer(ctx context.Context, authorize IssueAuthorizer) context.Context {
|
||||
return context.WithValue(ctx, issueAuthorizerContextKey{}, authorize)
|
||||
}
|
||||
|
||||
func authorizeIssueSnapshot(ctx context.Context, item domain.Domain) error {
|
||||
authorize, _ := ctx.Value(issueAuthorizerContextKey{}).(IssueAuthorizer)
|
||||
if authorize == nil {
|
||||
return nil
|
||||
}
|
||||
return authorize(ctx, item)
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package certificate
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/domain"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/httpchallenge"
|
||||
)
|
||||
|
||||
func TestServiceIssueAuthorizationBeforeSideEffects(t *testing.T) {
|
||||
for _, kind := range []string{domain.ChallengeDNS01, domain.ChallengeHTTP01} {
|
||||
t.Run(kind, func(t *testing.T) {
|
||||
item := domain.Domain{ID: "domain-1", Name: "example.com", CertificateType: domain.CertificateSingle, ChallengeType: kind}
|
||||
repo := &serviceRepository{item: item}
|
||||
service := NewService(certificateTestConfig(t.TempDir()), repo, nil, nil, serviceAccounts{})
|
||||
service.SetHTTPChallenges(httpchallenge.NewStore())
|
||||
denied := errors.New("拒绝当前签发配置")
|
||||
calls := 0
|
||||
ctx := WithIssueAuthorizer(t.Context(), func(_ context.Context, actual domain.Domain) error {
|
||||
calls++
|
||||
if actual.ID != item.ID || actual.ChallengeType != kind {
|
||||
t.Fatal("授权未使用实际读取的配置")
|
||||
}
|
||||
return denied
|
||||
})
|
||||
if _, err := service.Issue(ctx, item.ID); !errors.Is(err, denied) || calls != 1 {
|
||||
t.Fatal("授权错误未传播或授权调用次数错误:", err, calls)
|
||||
}
|
||||
if repo.updateCalls != 0 {
|
||||
t.Fatal("拒绝授权仍修改证书状态")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -158,6 +158,9 @@ func (s *Service) Issue(ctx context.Context, domainID string) (IssueResult, erro
|
||||
if err != nil {
|
||||
return IssueResult{}, err
|
||||
}
|
||||
if err := authorizeIssueSnapshot(ctx, item); err != nil {
|
||||
return IssueResult{}, fmt.Errorf("签发配置权限校验失败:%w", err)
|
||||
}
|
||||
providerID := ""
|
||||
var lease *httpchallenge.Lease
|
||||
if challengeType == domain.ChallengeHTTP01 {
|
||||
|
||||
Reference in new issue
Block a user