check-safety/check_rbac_test.go
Alex Dunmow 482e4f9d43 fix(check2): skip RBAC check for the definitions-only core SDK
Check 2 (RPC methods registered in RBAC interceptor) fired FAIL on core,
flagging all 926 shared procedures as "NOT in MethodRoles". Core is the
shared Go SDK: it carries the canonical proto (blockninja.v1, orchestrator.v1,
helpdesk.v1) but serves nothing and has no interceptor, so there is no
MethodRoles map to match against and every procedure looked missing.

RBAC is genuinely enforced where these procedures are actually served, and
check 2 already validates it there: cms covers blockninja.v1 (green) and
orchestrator covers orchestrator.v1 (0 missing). The abi.v1 protos declare no
services, and helpdesk.v1 is unmounted definitions. So scanning the SDK in
isolation is a not-applicable result, not a finding.

Detect the core SDK target (no serving package prefix, no interceptor.go,
module path == core) and SKIP check 2 for it instead of folding its shared
procedures into the missing-set. Serving backends (cms/orchestrator) and
plugins are untouched — the guard short-circuits the moment a target has a
package prefix or an interceptor.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-04 09:28:19 +08:00

65 lines
2.3 KiB
Go

package main
import (
"os"
"path/filepath"
"testing"
)
// writeModule writes a minimal go.mod declaring modulePath under dir.
func writeModule(t *testing.T, dir, modulePath string) {
t.Helper()
if err := os.WriteFile(filepath.Join(dir, "go.mod"), []byte("module "+modulePath+"\n\ngo 1.23\n"), 0o644); err != nil {
t.Fatalf("write go.mod: %v", err)
}
}
func TestIsDefinitionsOnlySDKTarget(t *testing.T) {
t.Run("core SDK: proto but no interceptor, no serving prefix -> skipped", func(t *testing.T) {
dir := t.TempDir()
writeModule(t, dir, coreSDKModulePath)
if !isDefinitionsOnlySDKTarget(backendScanTarget{root: dir}) {
t.Fatal("expected core SDK target to be definitions-only")
}
})
t.Run("serving backend with package prefix -> not skipped", func(t *testing.T) {
dir := t.TempDir()
writeModule(t, dir, coreSDKModulePath) // even the core module path must not skip if it declares a serving prefix
target := backendScanTarget{root: dir, allowedPackagePrefixes: []string{"blockninja."}}
if isDefinitionsOnlySDKTarget(target) {
t.Fatal("a target with a serving package prefix must never be treated as definitions-only")
}
})
t.Run("backend carrying an RBAC interceptor -> not skipped", func(t *testing.T) {
dir := t.TempDir()
writeModule(t, dir, coreSDKModulePath)
rbacDir := filepath.Join(dir, "internal", "rbac")
if err := os.MkdirAll(rbacDir, 0o755); err != nil {
t.Fatalf("mkdir: %v", err)
}
if err := os.WriteFile(filepath.Join(rbacDir, "interceptor.go"), []byte("package rbac\n"), 0o644); err != nil {
t.Fatalf("write interceptor: %v", err)
}
if isDefinitionsOnlySDKTarget(backendScanTarget{root: dir}) {
t.Fatal("a target with a serving RBAC interceptor must never be treated as definitions-only")
}
})
t.Run("some other SDK module -> not skipped", func(t *testing.T) {
dir := t.TempDir()
writeModule(t, dir, "git.dev.alexdunmow.com/block/cms")
if isDefinitionsOnlySDKTarget(backendScanTarget{root: dir}) {
t.Fatal("only the core SDK module is definitions-only; other modules must be checked")
}
})
t.Run("no go.mod -> not skipped", func(t *testing.T) {
dir := t.TempDir()
if isDefinitionsOnlySDKTarget(backendScanTarget{root: dir}) {
t.Fatal("a target without a resolvable module must not be treated as definitions-only")
}
})
}