()
| 269 | } |
| 270 | |
| 271 | func (pm *PolicyManager) load() { |
| 272 | globalPath := pm.configPath |
| 273 | globalRules := []Rule{} |
| 274 | |
| 275 | if _, err := os.Stat(globalPath); os.IsNotExist(err) { |
| 276 | pm.defaultRules() |
| 277 | globalRules = pm.Rules |
| 278 | } else { |
| 279 | data, err := os.ReadFile(globalPath) //#nosec G304 -- path supplied by user/agent through validated tool surface (boundary check upstream) |
| 280 | if err != nil { |
| 281 | pm.logger.Warn("Failed to read security policy", zap.Error(err)) |
| 282 | } else { |
| 283 | var pf policyFile |
| 284 | if err := json.Unmarshal(data, &pf); err != nil { |
| 285 | pm.logger.Warn("Failed to parse security policy", zap.Error(err)) |
| 286 | } else { |
| 287 | globalRules = pf.Rules |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | localPath := findLocalPolicyPath() |
| 293 | if localPath == "" { |
| 294 | pm.Rules = globalRules |
| 295 | pm.localPath = "" |
| 296 | pm.mergeLocal = false |
| 297 | return |
| 298 | } |
| 299 | |
| 300 | localData, err := os.ReadFile(localPath) //#nosec G304 -- path supplied by user/agent through validated tool surface (boundary check upstream) |
| 301 | if err != nil { |
| 302 | pm.logger.Warn("Failed to read local security policy", zap.Error(err)) |
| 303 | pm.Rules = globalRules |
| 304 | pm.localPath = "" |
| 305 | pm.mergeLocal = false |
| 306 | return |
| 307 | } |
| 308 | |
| 309 | var local policyFile |
| 310 | if err := json.Unmarshal(localData, &local); err != nil { |
| 311 | pm.logger.Warn("Failed to parse local security policy", zap.Error(err)) |
| 312 | pm.Rules = globalRules |
| 313 | pm.localPath = "" |
| 314 | pm.mergeLocal = false |
| 315 | return |
| 316 | } |
| 317 | |
| 318 | if local.Merge { |
| 319 | pm.Rules = mergeRules(globalRules, local.Rules) |
| 320 | } else { |
| 321 | pm.Rules = local.Rules |
| 322 | } |
| 323 | |
| 324 | // Se existe policy local, persistir alterações nela. |
| 325 | pm.configPath = localPath |
| 326 | pm.localPath = localPath |
| 327 | pm.mergeLocal = local.Merge |
| 328 | } |
no test coverage detected