| 568 | } |
| 569 | |
| 570 | func parseCreationRuleForFile(conf *configFile, confPath, filePath string, kmsEncryptionContext map[string]*string) (*Config, error) { |
| 571 | // If config file doesn't contain CreationRules (it's empty or only contains DestionationRules), assume it does not exist |
| 572 | if conf.CreationRules == nil { |
| 573 | return nil, nil |
| 574 | } |
| 575 | |
| 576 | configDir, err := filepath.Abs(filepath.Dir(confPath)) |
| 577 | if err != nil { |
| 578 | return nil, err |
| 579 | } |
| 580 | |
| 581 | // compare file path relative to path of config file |
| 582 | filePath = strings.TrimPrefix(filePath, configDir+string(filepath.Separator)) |
| 583 | |
| 584 | var rule *creationRule |
| 585 | |
| 586 | for _, r := range conf.CreationRules { |
| 587 | if r.PathRegex == "" { |
| 588 | rule = &r |
| 589 | break |
| 590 | } |
| 591 | reg, err := regexp.Compile(r.PathRegex) |
| 592 | if err != nil { |
| 593 | return nil, fmt.Errorf("can not compile regexp: %w", err) |
| 594 | } |
| 595 | if reg.MatchString(filePath) { |
| 596 | rule = &r |
| 597 | break |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | if rule == nil { |
| 602 | return nil, fmt.Errorf("error loading config: no matching creation rules found") |
| 603 | } |
| 604 | |
| 605 | config, err := configFromRule(rule, kmsEncryptionContext) |
| 606 | if err != nil { |
| 607 | return nil, err |
| 608 | } |
| 609 | |
| 610 | return config, nil |
| 611 | } |
| 612 | |
| 613 | // LoadCreationRuleForFile load the configuration for a given SOPS file from the config file at confPath. A kmsEncryptionContext |
| 614 | // should be provided for configurations that do not contain key groups, as there's no way to specify context inside |