diff --git a/agent/configmgr/git.go b/agent/configmgr/git.go index 9ba6bb0..de7fa1e 100644 --- a/agent/configmgr/git.go +++ b/agent/configmgr/git.go @@ -192,7 +192,10 @@ func (gc *gitConfigManager) processSelector(file *object.File, cfg config.Config return nil, err } - // Check for matching selector + // Use a set (map) to store unique policy paths + policyPathsSet := make(map[string]struct{}) + + // Iterate through all selectors and collect all matching ones for selectorName, entry := range selectors { matches := true for key, value := range entry.Selector { @@ -201,21 +204,28 @@ func (gc *gitConfigManager) processSelector(file *object.File, cfg config.Config break } } - if matches { gc.logger.Info("Selector matched", zap.String("selector", selectorName)) - policyPaths := make([]string, 0) - for _, policy := range entry.Policies { + for pName, policy := range entry.Policies { if policy.Enabled != nil && !*policy.Enabled { continue } - policyPaths = append(policyPaths, policy.Path) + if _, exists := policyPathsSet[policy.Path]; exists { + gc.logger.Warn("Policy path already exists", zap.String("selector", selectorName), + zap.String("policy", pName), zap.String("path", policy.Path)) + } + policyPathsSet[policy.Path] = struct{}{} } - return policyPaths, nil } } - return nil, nil + // Convert map keys to a slice + var policyPaths []string + for path := range policyPathsSet { + policyPaths = append(policyPaths, path) + } + + return policyPaths, nil } func (gc *gitConfigManager) schedule(cfg config.Config, backends map[string]backend.Backend) { diff --git a/docs/configs/git.md b/docs/configs/git.md index cefe263..3cc2536 100644 --- a/docs/configs/git.md +++ b/docs/configs/git.md @@ -42,17 +42,18 @@ The Orb Agent requires the Git repository containing its policies to have the fo . ├── .git ├── selector.yaml -├── dir2 -│   ├── newpolicy.yaml -│   └── dir3 -│   └── newpolicy2.yaml -└── folder1 - └── policy1.yaml +├── policy1.yaml +├── folder2 +│   ├── policy2.yaml +│   └── folder3 +│   └── policy3.yaml +└── folder4 + └── policy4.yaml ``` ### selector.yaml The `selector.yaml` file must include the `selector` and `policies` sections: - - `selector`: Defines key-value pairs (agent labels) used to identify agents + - `selector`: Defines key-value pairs that identify agents based on their labels. If the selector is empty, it matches all agents. - `policies`: Specifies policy file paths and their enabled or disabled state. If the `enabled` field is not provided, the policy is enabled by default @@ -64,8 +65,8 @@ agent_selector_1: policies: policy1: path: policy1.yaml - policy2: - enabled: false + policy2: + enabled: false path: folder2/policy2.yaml agent_selector_2: selector: @@ -73,8 +74,12 @@ agent_selector_2: pop: nyc02 policies: policy1: - enabled: true - path: policy1.yaml - policy3: - path: folder3/policy3.yaml + enabled: true + path: policy1.yaml + policy3: + path: folder2/folder3/policy3.yaml +agent_selector_matches_all: + selector: + policies: + path: folder4/policy4.yaml ``` \ No newline at end of file