- boot/ (Java/Maven, JNPF 7.0) — 移除旧的 boot/.git,源码直接纳入仓库
- web/ (Vue 3 + Vite + Ant Design Vue, pnpm)
- docs/, scard/, nax/
- refs/INDEX.md + refs/国标/(其余 refs/* 开源子仓库不纳入)
- 已忽略: connector_data/, jweb/, log/, web/{dist,node_modules,.pnpm}, boot/*/target/, .DS_Store, .idea, .vscode
405 lines
9.1 KiB
Go
405 lines
9.1 KiB
Go
// Policy engine - RBAC/ABAC implementation
|
|
|
|
package policy
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"security-connector/common/types"
|
|
)
|
|
|
|
// PolicyEngine enforces access control policies
|
|
type PolicyEngine struct {
|
|
mu sync.RWMutex
|
|
|
|
// Policies indexed by policy ID
|
|
policies map[string]*types.Policy
|
|
|
|
// Device roles indexed by device ID
|
|
deviceRoles map[string][]string
|
|
|
|
// Audit logger
|
|
auditLog func(*types.AuditEvent)
|
|
}
|
|
|
|
// NewPolicyEngine creates a new policy engine
|
|
func NewPolicyEngine() *PolicyEngine {
|
|
return &PolicyEngine{
|
|
policies: make(map[string]*types.Policy),
|
|
deviceRoles: make(map[string][]string),
|
|
}
|
|
}
|
|
|
|
// SetAuditLogger sets the audit log function
|
|
func (pe *PolicyEngine) SetAuditLogger(logger func(*types.AuditEvent)) {
|
|
pe.auditLog = logger
|
|
}
|
|
|
|
// AddPolicy adds a new policy
|
|
func (pe *PolicyEngine) AddPolicy(policy *types.Policy) error {
|
|
pe.mu.Lock()
|
|
defer pe.mu.Unlock()
|
|
|
|
if policy.PolicyID == "" {
|
|
return fmt.Errorf("policy ID is required")
|
|
}
|
|
|
|
policy.CreatedAt = time.Now()
|
|
pe.policies[policy.PolicyID] = policy
|
|
|
|
return nil
|
|
}
|
|
|
|
// RemovePolicy removes a policy
|
|
func (pe *PolicyEngine) RemovePolicy(policyID string) error {
|
|
pe.mu.Lock()
|
|
defer pe.mu.Unlock()
|
|
|
|
if _, exists := pe.policies[policyID]; !exists {
|
|
return fmt.Errorf("policy not found: %s", policyID)
|
|
}
|
|
|
|
delete(pe.policies, policyID)
|
|
return nil
|
|
}
|
|
|
|
// Evaluate evaluates whether an action should be allowed
|
|
func (pe *PolicyEngine) Evaluate(subject, action, resource string, conditions *types.Conditions) (bool, error) {
|
|
pe.mu.RLock()
|
|
defer pe.mu.RUnlock()
|
|
|
|
// Sort policies by priority (higher first)
|
|
policies := pe.getSortedPolicies()
|
|
|
|
for _, policy := range policies {
|
|
if !policy.Enabled {
|
|
continue
|
|
}
|
|
|
|
// Check if subject matches
|
|
if !pe.subjectMatches(subject, policy.Subjects) {
|
|
continue
|
|
}
|
|
|
|
// Check if resource matches
|
|
if !pe.resourceMatches(resource, policy.Resources) {
|
|
continue
|
|
}
|
|
|
|
// Check if action matches
|
|
if !pe.actionMatches(action, policy.Actions) {
|
|
continue
|
|
}
|
|
|
|
// Check conditions
|
|
if policy.Conditions != nil && conditions != nil {
|
|
if !pe.checkConditions(policy.Conditions, conditions) {
|
|
continue
|
|
}
|
|
}
|
|
|
|
// Policy matches - return its effect
|
|
return policy.Effect == types.PolicyAllow, nil
|
|
}
|
|
|
|
// Default deny if no policy matches
|
|
return false, nil
|
|
}
|
|
|
|
// subjectMatches checks if a subject matches any of the policy subjects
|
|
func (pe *PolicyEngine) subjectMatches(subject string, subjects []string) bool {
|
|
for _, s := range subjects {
|
|
if s == subject || s == "*" {
|
|
return true
|
|
}
|
|
// Check for wildcard patterns like "device:*"
|
|
if strings.HasSuffix(s, "*") && strings.HasPrefix(subject, strings.TrimSuffix(s, "*")) {
|
|
return true
|
|
}
|
|
// Check for group prefix
|
|
if strings.HasPrefix(s, "group:") {
|
|
groupName := strings.TrimPrefix(s, "group:")
|
|
if pe.deviceInGroup(subject, groupName) {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// resourceMatches checks if a resource matches any of the policy resources
|
|
func (pe *PolicyEngine) resourceMatches(resource string, resources []string) bool {
|
|
for _, r := range resources {
|
|
if r == resource || r == "*" {
|
|
return true
|
|
}
|
|
// Check for wildcard patterns
|
|
if strings.HasSuffix(r, "*") {
|
|
prefix := strings.TrimSuffix(r, "*")
|
|
if strings.HasPrefix(resource, prefix) {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// actionMatches checks if an action matches any of the policy actions
|
|
func (pe *PolicyEngine) actionMatches(action string, actions []string) bool {
|
|
for _, a := range actions {
|
|
if a == action || a == "*" {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// checkConditions checks if conditions are satisfied
|
|
func (pe *PolicyEngine) checkConditions(policyConditions, requestConditions *types.Conditions) bool {
|
|
// Check time window
|
|
if policyConditions.TimeWindow != nil && requestConditions != nil {
|
|
if !pe.checkTimeWindow(policyConditions.TimeWindow) {
|
|
return false
|
|
}
|
|
}
|
|
|
|
// Check PCR values
|
|
if len(policyConditions.PCRValues) > 0 && requestConditions != nil {
|
|
if !pe.checkPCRValues(policyConditions.PCRValues, requestConditions.PCRValues) {
|
|
return false
|
|
}
|
|
}
|
|
|
|
// Check key version
|
|
if policyConditions.KeyVersion != nil && requestConditions != nil {
|
|
if requestConditions.KeyVersion == nil {
|
|
return false
|
|
}
|
|
if *requestConditions.KeyVersion < *policyConditions.KeyVersion {
|
|
return false
|
|
}
|
|
}
|
|
|
|
// Check device status
|
|
if len(policyConditions.DeviceStatus) > 0 && requestConditions != nil {
|
|
found := false
|
|
for _, status := range policyConditions.DeviceStatus {
|
|
for _, reqStatus := range requestConditions.DeviceStatus {
|
|
if status == reqStatus {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if found {
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
// checkTimeWindow checks if current time is within the window
|
|
func (pe *PolicyEngine) checkTimeWindow(tw *types.TimeWindow) bool {
|
|
now := time.Now()
|
|
|
|
// Check day of week
|
|
if len(tw.Days) > 0 {
|
|
today := int(now.Weekday())
|
|
found := false
|
|
for _, day := range tw.Days {
|
|
if day == today {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
return false
|
|
}
|
|
}
|
|
|
|
// Parse time windows
|
|
start, err := parseTime(tw.Start)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
end, err := parseTime(tw.End)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
current := now.Hour()*60 + now.Minute()
|
|
return current >= start && current <= end
|
|
}
|
|
|
|
// checkPCRValues checks if PCR values match
|
|
func (pe *PolicyEngine) checkPCRValues(policyPCRs, devicePCRs []string) bool {
|
|
if len(policyPCRs) == 0 {
|
|
return true
|
|
}
|
|
|
|
for i, pcr := range policyPCRs {
|
|
if i >= len(devicePCRs) {
|
|
return false
|
|
}
|
|
if pcr != "*" && pcr != devicePCRs[i] {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
// deviceInGroup checks if a device is in a group
|
|
func (pe *PolicyEngine) deviceInGroup(deviceID, group string) bool {
|
|
roles, exists := pe.deviceRoles[deviceID]
|
|
if !exists {
|
|
return false
|
|
}
|
|
|
|
for _, role := range roles {
|
|
if role == group || strings.HasPrefix(role, "group:"+group) {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
// AssignRole assigns a role to a device
|
|
func (pe *PolicyEngine) AssignRole(deviceID, role string) {
|
|
pe.mu.Lock()
|
|
defer pe.mu.Unlock()
|
|
|
|
pe.deviceRoles[deviceID] = append(pe.deviceRoles[deviceID], role)
|
|
}
|
|
|
|
// RemoveRole removes a role from a device
|
|
func (pe *PolicyEngine) RemoveRole(deviceID, role string) {
|
|
pe.mu.Lock()
|
|
defer pe.mu.Unlock()
|
|
|
|
roles := pe.deviceRoles[deviceID]
|
|
for i, r := range roles {
|
|
if r == role {
|
|
pe.deviceRoles[deviceID] = append(roles[:i], roles[i+1:]...)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
// GetPoliciesForSubject returns all policies applicable to a subject
|
|
func (pe *PolicyEngine) GetPoliciesForSubject(subject string) []*types.Policy {
|
|
pe.mu.RLock()
|
|
defer pe.mu.RUnlock()
|
|
|
|
var applicable []*types.Policy
|
|
for _, policy := range pe.policies {
|
|
if pe.subjectMatches(subject, policy.Subjects) {
|
|
applicable = append(applicable, policy)
|
|
}
|
|
}
|
|
|
|
return applicable
|
|
}
|
|
|
|
// getSortedPolicies returns policies sorted by priority
|
|
func (pe *PolicyEngine) getSortedPolicies() []*types.Policy {
|
|
policies := make([]*types.Policy, 0, len(pe.policies))
|
|
for _, policy := range pe.policies {
|
|
policies = append(policies, policy)
|
|
}
|
|
|
|
// Sort by priority (descending)
|
|
for i := 0; i < len(policies); i++ {
|
|
for j := i + 1; j < len(policies); j++ {
|
|
if policies[j].Priority > policies[i].Priority {
|
|
policies[i], policies[j] = policies[j], policies[i]
|
|
}
|
|
}
|
|
}
|
|
|
|
return policies
|
|
}
|
|
|
|
// DefaultPolicies returns the default RBAC policies
|
|
func DefaultPolicies() []*types.Policy {
|
|
return []*types.Policy{
|
|
{
|
|
PolicyID: "admin_all",
|
|
Name: "Admin has full access",
|
|
Effect: types.PolicyAllow,
|
|
Subjects: []string{"group:admins", "*"},
|
|
Resources: []string{"*"},
|
|
Actions: []string{"*"},
|
|
Priority: 100,
|
|
Enabled: true,
|
|
},
|
|
{
|
|
PolicyID: "device_read",
|
|
Name: "Devices can read their own data",
|
|
Effect: types.PolicyAllow,
|
|
Subjects: []string{"device:*", "*"},
|
|
Resources: []string{"device:*", "*"},
|
|
Actions: []string{"read"},
|
|
Priority: 50,
|
|
Enabled: true,
|
|
},
|
|
{
|
|
PolicyID: "device_write",
|
|
Name: "Devices can write their own data",
|
|
Effect: types.PolicyAllow,
|
|
Subjects: []string{"device:*", "*"},
|
|
Resources: []string{"device:*", "*"},
|
|
Actions: []string{"write"},
|
|
Priority: 40,
|
|
Enabled: true,
|
|
},
|
|
{
|
|
PolicyID: "easytier_join",
|
|
Name: "Devices can join EasyTier network",
|
|
Effect: types.PolicyAllow,
|
|
Subjects: []string{"*"},
|
|
Resources: []string{"easytier:*", "*"},
|
|
Actions: []string{"easytier_join"},
|
|
Priority: 35,
|
|
Enabled: true,
|
|
},
|
|
{
|
|
PolicyID: "k3s_join",
|
|
Name: "Devices can join k3s cluster",
|
|
Effect: types.PolicyAllow,
|
|
Subjects: []string{"*"},
|
|
Resources: []string{"k3s:*", "*"},
|
|
Actions: []string{"k3s_join"},
|
|
Priority: 35,
|
|
Enabled: true,
|
|
},
|
|
{
|
|
PolicyID: "deny_revoke",
|
|
Name: "Deny revoke unless admin",
|
|
Effect: types.PolicyDeny,
|
|
Subjects: []string{"device:*"},
|
|
Resources: []string{"device:revoke"},
|
|
Actions: []string{"revoke"},
|
|
Priority: 30,
|
|
Enabled: true,
|
|
},
|
|
}
|
|
}
|
|
|
|
func parseTime(t string) (int, error) {
|
|
var hour, minute int
|
|
n, err := fmt.Sscanf(t, "%d:%d", &hour, &minute)
|
|
if err != nil || n != 2 {
|
|
return 0, fmt.Errorf("invalid time format: %s", t)
|
|
}
|
|
return hour*60 + minute, nil
|
|
} |