|
| 1 | +package builtin |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + |
| 10 | + securejoin "github.com/cyphar/filepath-securejoin" |
| 11 | + "github.com/xeipuuv/gojsonschema" |
| 12 | + |
| 13 | + kargoapi "github.com/akuity/kargo/api/v1alpha1" |
| 14 | + "github.com/akuity/kargo/internal/yaml" |
| 15 | + "github.com/akuity/kargo/pkg/promotion" |
| 16 | + "github.com/akuity/kargo/pkg/x/promotion/runner/builtin" |
| 17 | +) |
| 18 | + |
| 19 | +// yamlMerger is an implementation of the promotion.StepRunner interface that |
| 20 | +// updates do a merge of the multiple YAML files. |
| 21 | +type yamlMerger struct { |
| 22 | + schemaLoader gojsonschema.JSONLoader |
| 23 | +} |
| 24 | + |
| 25 | +// newYAMLMerger returns an implementation of the promotion.StepRunner interface |
| 26 | +// that updates the values of specified keys in a YAML file. |
| 27 | +func newYAMLMerger() promotion.StepRunner { |
| 28 | + r := &yamlMerger{} |
| 29 | + r.schemaLoader = getConfigSchemaLoader(r.Name()) |
| 30 | + return r |
| 31 | +} |
| 32 | + |
| 33 | +// Name implements the promotion.StepRunner interface. |
| 34 | +func (y *yamlMerger) Name() string { |
| 35 | + return "yaml-merge" |
| 36 | +} |
| 37 | + |
| 38 | +// Run implements the promotion.StepRunner interface. |
| 39 | +func (y *yamlMerger) Run( |
| 40 | + ctx context.Context, |
| 41 | + stepCtx *promotion.StepContext, |
| 42 | +) (promotion.StepResult, error) { |
| 43 | + failure := promotion.StepResult{Status: kargoapi.PromotionStepStatusErrored} |
| 44 | + |
| 45 | + if err := y.validate(stepCtx.Config); err != nil { |
| 46 | + return failure, err |
| 47 | + } |
| 48 | + |
| 49 | + // Convert the configuration into a typed struct |
| 50 | + cfg, err := promotion.ConfigToStruct[builtin.YAMLMergeConfig](stepCtx.Config) |
| 51 | + if err != nil { |
| 52 | + return failure, fmt.Errorf("could not convert config into %s config: %w", y.Name(), err) |
| 53 | + } |
| 54 | + |
| 55 | + return y.run(ctx, stepCtx, cfg) |
| 56 | +} |
| 57 | + |
| 58 | +// validate validates yamlMerger configuration against a JSON schema. |
| 59 | +func (y *yamlMerger) validate(cfg promotion.Config) error { |
| 60 | + return validate(y.schemaLoader, gojsonschema.NewGoLoader(cfg), y.Name()) |
| 61 | +} |
| 62 | + |
| 63 | +func (y *yamlMerger) run( |
| 64 | + _ context.Context, |
| 65 | + stepCtx *promotion.StepContext, |
| 66 | + cfg builtin.YAMLMergeConfig, |
| 67 | +) (promotion.StepResult, error) { |
| 68 | + |
| 69 | + result := promotion.StepResult{Status: kargoapi.PromotionStepStatusSucceeded} |
| 70 | + failure := promotion.StepResult{Status: kargoapi.PromotionStepStatusErrored} |
| 71 | + |
| 72 | + mergedFiles := []string{} // keep track of files actually merged |
| 73 | + |
| 74 | + // Secure join the paths to prevent path traversal attacks. |
| 75 | + yamlData := []string{} |
| 76 | + for _, path := range cfg.InPaths { |
| 77 | + inPath, err := securejoin.SecureJoin(stepCtx.WorkDir, path) |
| 78 | + if err != nil { |
| 79 | + return promotion.StepResult{Status: kargoapi.PromotionStepStatusErrored}, |
| 80 | + fmt.Errorf("could not secure join inPath %q: %w", path, err) |
| 81 | + } |
| 82 | + |
| 83 | + inBytes, err := os.ReadFile(inPath) |
| 84 | + if err != nil { |
| 85 | + // we skip if file does not exist |
| 86 | + if !cfg.Strict && os.IsNotExist(err) { |
| 87 | + continue |
| 88 | + } |
| 89 | + return failure, fmt.Errorf( |
| 90 | + "error reading file %q: %w", |
| 91 | + inPath, |
| 92 | + err, |
| 93 | + ) |
| 94 | + } |
| 95 | + |
| 96 | + // we skip if file is empty |
| 97 | + if len(inBytes) == 0 { |
| 98 | + continue |
| 99 | + } |
| 100 | + |
| 101 | + mergedFiles = append(mergedFiles, path) |
| 102 | + yamlData = append(yamlData, string(inBytes)) |
| 103 | + } |
| 104 | + |
| 105 | + // merge YAML files |
| 106 | + outYAML, err := yaml.MergeYAMLFiles(yamlData) |
| 107 | + |
| 108 | + // write yaml file |
| 109 | + outPath, err := securejoin.SecureJoin(stepCtx.WorkDir, cfg.OutPath) |
| 110 | + if err != nil { |
| 111 | + return promotion.StepResult{Status: kargoapi.PromotionStepStatusErrored}, |
| 112 | + fmt.Errorf("could not secure join outPath %q: %w", cfg.OutPath, err) |
| 113 | + } |
| 114 | + |
| 115 | + if err := os.MkdirAll(filepath.Dir(outPath), 0o700); err != nil { |
| 116 | + return failure, fmt.Errorf("Error creating directory structure %s: %w", filepath.Dir(outPath), err) |
| 117 | + } |
| 118 | + if err = os.WriteFile(outPath, []byte(outYAML), 0o600); err != nil { |
| 119 | + return failure, fmt.Errorf("Error writing to file %s: %w", outPath, err) |
| 120 | + } |
| 121 | + |
| 122 | + // add commit msg |
| 123 | + if commitMsg := y.generateCommitMessage(cfg.OutPath, mergedFiles); commitMsg != "" { |
| 124 | + result.Output = map[string]any{ |
| 125 | + "commitMessage": commitMsg, |
| 126 | + } |
| 127 | + } |
| 128 | + return result, nil |
| 129 | +} |
| 130 | + |
| 131 | +func (y *yamlMerger) generateCommitMessage(path string, fileList []string) string { |
| 132 | + if len(path) <= 1 { |
| 133 | + return "no YAML file merged" |
| 134 | + } |
| 135 | + |
| 136 | + var commitMsg strings.Builder |
| 137 | + _, _ = commitMsg.WriteString(fmt.Sprintf("Merged YAML files to %s\n", path)) |
| 138 | + for _, file := range fileList { |
| 139 | + _, _ = commitMsg.WriteString( |
| 140 | + fmt.Sprintf( |
| 141 | + "\n- %s", |
| 142 | + file, |
| 143 | + ), |
| 144 | + ) |
| 145 | + } |
| 146 | + |
| 147 | + return commitMsg.String() |
| 148 | +} |
0 commit comments