支持阿里云定向只读权限检查并识别RAM资源拒绝
This commit is contained in:
1 parent
aa3b5f17a1
commit
eb44af1f42
5 files changed
+194
-6
No files matched your search
@@ -21,12 +21,20 @@ type dnsPermissionVerifier func(context.Context, string, map[string]string) (dns
|
||||
func newDNSCommand() *cobra.Command {
|
||||
command := &cobra.Command{Use: "dns", Short: "检查指定域名的阿里云 RAM 权限"}
|
||||
checker := dnsprovider.NewAliyunAuthorizationChecker()
|
||||
command.AddCommand(newDNSCheckCommand(checker.Verify))
|
||||
command.AddCommand(newDNSCheckCommand(checker.Verify), newDNSInspectCommand(checker.Inspect))
|
||||
return command
|
||||
}
|
||||
|
||||
func newDNSInspectCommand(inspect dnsPermissionVerifier) *cobra.Command {
|
||||
return newDNSPermissionCommand(inspect, true)
|
||||
}
|
||||
|
||||
// 权限检查不启动应用、续期调度或数据库,也不调用全局域名发现。
|
||||
func newDNSCheckCommand(verify dnsPermissionVerifier) *cobra.Command {
|
||||
return newDNSPermissionCommand(verify, false)
|
||||
}
|
||||
|
||||
func newDNSPermissionCommand(verify dnsPermissionVerifier, readOnly bool) *cobra.Command {
|
||||
var requestedDomain string
|
||||
var confirmWrite bool
|
||||
command := &cobra.Command{
|
||||
@@ -38,7 +46,7 @@ func newDNSCheckCommand(verify dnsPermissionVerifier) *cobra.Command {
|
||||
if !domain.IsValidName(requestedDomain) {
|
||||
return errors.New("必须提供有效的待检查域名")
|
||||
}
|
||||
if !confirmWrite {
|
||||
if !readOnly && !confirmWrite {
|
||||
return errors.New("此检查会写入并删除专用临时 TXT 记录;确认已获目标域名测试授权后,请添加 --confirm-dns-write")
|
||||
}
|
||||
cfg, err := loadConfig()
|
||||
@@ -72,14 +80,19 @@ func newDNSCheckCommand(verify dnsPermissionVerifier) *cobra.Command {
|
||||
if verifyErr != nil {
|
||||
return fmt.Errorf("域名权限检查未通过:%w", verifyErr)
|
||||
}
|
||||
if !report.Verified {
|
||||
if !readOnly && !report.Verified {
|
||||
return errors.New("域名权限检查未通过:未获得写入及清理均成功的证据")
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
command.Flags().StringVar(&requestedDomain, "domain", "", "明确授权检查的单个域名,不枚举全账号域名")
|
||||
command.Flags().BoolVar(&confirmWrite, "confirm-dns-write", false, "确认允许专用临时 TXT 记录写入和精确清理")
|
||||
if readOnly {
|
||||
command.Use = "inspect"
|
||||
command.Short = "只读检查指定托管区,不写入 DNS、不证明可写"
|
||||
} else {
|
||||
command.Flags().BoolVar(&confirmWrite, "confirm-dns-write", false, "确认允许专用临时 TXT 记录写入和精确清理")
|
||||
}
|
||||
_ = command.MarkFlagRequired("domain")
|
||||
return command
|
||||
}
|
||||
@@ -30,6 +30,28 @@ func TestDNSCheckRequiresExplicitWriteConfirmation(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDNSInspectDoesNotRequireWriteOrPromoteReadableToVerified(t *testing.T) {
|
||||
prepareDNSCheckConfig(t)
|
||||
command := newDNSInspectCommand(func(context.Context, string, map[string]string) (dnsprovider.AuthorizationReport, error) {
|
||||
return dnsprovider.AuthorizationReport{
|
||||
Provider: "aliyun", Domains: []dnsprovider.AuthorizationDomain{{Domain: "example.com", Listed: true}},
|
||||
}, nil
|
||||
})
|
||||
var output bytes.Buffer
|
||||
command.SetOut(&output)
|
||||
command.SetArgs([]string{"--domain", "example.com"})
|
||||
if err := command.Execute(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var report dnsprovider.AuthorizationReport
|
||||
if err := json.Unmarshal(output.Bytes(), &report); err != nil || report.Verified || report.Domains[0].Writable {
|
||||
t.Fatalf("只读成功不能升级为写权限通过:%s", output.String())
|
||||
}
|
||||
if command.Flag("confirm-dns-write") != nil {
|
||||
t.Fatal("只读命令不得提供写入开关")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDNSCheckReportsLiveResultWithoutPersistingConfiguration(t *testing.T) {
|
||||
for _, outcome := range []string{"allowed", "denied", "unknown", "unverified"} {
|
||||
t.Run(outcome, func(t *testing.T) {
|
||||
|
||||
@@ -81,6 +81,35 @@ func (c *AliyunAuthorizationChecker) Discover(ctx context.Context, credentials m
|
||||
return report, nil
|
||||
}
|
||||
|
||||
// Inspect 只查询显式指定托管区的读取权限,不逐级探测父域、不写入 DNS,也不证明可写。
|
||||
func (c *AliyunAuthorizationChecker) Inspect(ctx context.Context, requestedDomain string, credentials map[string]string) (AuthorizationReport, error) {
|
||||
requestedDomain = strings.TrimPrefix(normalizeAuthorizationDomain(requestedDomain), "*.")
|
||||
report := AuthorizationReport{Provider: Aliyun, RequestedDomain: requestedDomain, CheckedAt: c.currentTime().UTC()}
|
||||
if !domain.IsValidName(requestedDomain) {
|
||||
return report, fmt.Errorf("待检查托管区格式无效:%s", requestedDomain)
|
||||
}
|
||||
report.Domains = []AuthorizationDomain{{Domain: requestedDomain, Status: "等待读取权限检查"}}
|
||||
dnsClient, err := newAliyunClient(credentials)
|
||||
if err != nil {
|
||||
return report, err
|
||||
}
|
||||
request := new(client.DescribeDomainRecordsRequest).SetDomainName(requestedDomain).SetPageNumber(1).SetPageSize(1)
|
||||
response, err := client.DescribeDomainRecordsWithContext(ctx, dnsClient, request, &dara.RuntimeOptions{})
|
||||
if err != nil {
|
||||
report.Domains[0].Status = "读取未通过,可写性未验证"
|
||||
report.Domains[0].Error = err.Error()
|
||||
report.Diagnosis = DiagnoseAliyunError(err, requestedDomain)
|
||||
return report, fmt.Errorf("阿里云域名读取检查失败:%w", err)
|
||||
}
|
||||
if response == nil || response.Body == nil {
|
||||
return report, errors.New("阿里云未返回有效的域名读取结果")
|
||||
}
|
||||
report.Domains[0].Listed = true
|
||||
report.Domains[0].Status = "可读取,未验证写入和清理权限"
|
||||
// 读取成功必须保持 Verified=false,也不能写入授权缓存。
|
||||
return report, nil
|
||||
}
|
||||
|
||||
// Verify 从目标域名到父级托管区逐级执行最小写入和精确清理。
|
||||
func (c *AliyunAuthorizationChecker) Verify(ctx context.Context, requestedDomain string, credentials map[string]string) (AuthorizationReport, error) {
|
||||
requestedDomain = normalizeAuthorizationDomain(requestedDomain)
|
||||
@@ -383,7 +412,7 @@ func DiagnoseAliyunError(err error, domainName string) string {
|
||||
}
|
||||
msg := err.Error()
|
||||
lowered := strings.ToLower(msg)
|
||||
if strings.Contains(lowered, "forbidden.nopermission") || strings.Contains(lowered, "accessdenied") || strings.Contains(lowered, "not authorized") {
|
||||
if strings.Contains(lowered, "forbidden.nopermission") || strings.Contains(lowered, "forbidden.ram") || strings.Contains(lowered, "accessdenied") || strings.Contains(lowered, "not authorized") {
|
||||
return fmt.Sprintf("当前阿里云 RAM 账号未获得域名 %s 的 DNS 解析权限。请在阿里云 RAM 控制台授予该账号针对 acs:alidns:*:*:domain/%s 的操作权限(Action: alidns:*)", domainName, domainName)
|
||||
}
|
||||
if strings.Contains(lowered, "nosuchdomain") || strings.Contains(lowered, "invaliddomainname.noexist") {
|
||||
|
||||
@@ -128,6 +128,11 @@ func TestDiagnoseAliyunError(t *testing.T) {
|
||||
domain string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
err: errors.New("SDKError: Code: Forbidden.RAM, Message: User not authorized to operate on the specified resource."),
|
||||
domain: "example.com",
|
||||
expected: "当前阿里云 RAM 账号未获得域名 example.com 的 DNS 解析权限。请在阿里云 RAM 控制台授予该账号针对 acs:alidns:*:*:domain/example.com 的操作权限(Action: alidns:*)",
|
||||
},
|
||||
{
|
||||
err: errors.New("SDKError: Code: Forbidden.NoPermission, Message: Specified access key is not authorized."),
|
||||
domain: "solit.cn",
|
||||
@@ -156,4 +161,4 @@ func TestDiagnoseAliyunError(t *testing.T) {
|
||||
t.Errorf("DiagnoseAliyunError(%v, %s) = %q; want %q", tc.err, tc.domain, got, tc.expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user