|
| 1 | +package clisetup |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/AlecAivazis/survey/v2" |
| 10 | + "github.com/fatih/color" |
| 11 | + "github.com/hexops/gotextdiff" |
| 12 | + "github.com/hexops/gotextdiff/myers" |
| 13 | + "github.com/hexops/gotextdiff/span" |
| 14 | + "github.com/spf13/cobra" |
| 15 | + |
| 16 | + "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/args" |
| 17 | + "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/clisetup/setup" |
| 18 | +) |
| 19 | + |
| 20 | +type acquisitionFlags struct { |
| 21 | + acquisDir string |
| 22 | +} |
| 23 | + |
| 24 | +func (f *acquisitionFlags) bind(cmd *cobra.Command) { |
| 25 | + flags := cmd.Flags() |
| 26 | + flags.StringVar(&f.acquisDir, "acquis-dir", "", "Directory for the acquisition configuration") |
| 27 | +} |
| 28 | + |
| 29 | +func (cli *cliSetup) newInstallAcquisitionCmd() *cobra.Command { |
| 30 | + var dryRun bool |
| 31 | + |
| 32 | + f := acquisitionFlags{} |
| 33 | + |
| 34 | + cmd := &cobra.Command{ |
| 35 | + Use: "install-acquisition [setup_file]", |
| 36 | + Short: "Generate acquisition configuration from a setup file", |
| 37 | + Long: `Generate acquisition configuration from a setup file. |
| 38 | +
|
| 39 | +This command reads a setup.yaml specification (typically generated by 'cscli setup detect') |
| 40 | +and creates one acquisition file for each listed service. |
| 41 | +By default the files are placed in the acquisition directory, |
| 42 | +which you can override with --acquis-dir.`, |
| 43 | + Example: `# detect running services, create a setup file |
| 44 | +cscli setup detect > setup.yaml |
| 45 | +
|
| 46 | +# write configuration files in acquis.d |
| 47 | +cscli setup install-acquisition setup.yaml |
| 48 | +
|
| 49 | +# write files to a specific directory |
| 50 | +cscli setup install-acquisition --acquis-dir /path/to/acquis.d |
| 51 | +
|
| 52 | +# dry-run to preview what would be created |
| 53 | +cscli setup install-acquisition setup.yaml --dry-run |
| 54 | +`, |
| 55 | + Args: args.ExactArgs(1), |
| 56 | + DisableAutoGenTag: true, |
| 57 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 58 | + inputReader, err := maybeStdinFile(args[0]) |
| 59 | + if err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + |
| 63 | + stup, err := setup.ParseSetupYAML(inputReader, true, cli.cfg().Cscli.Color != "no") |
| 64 | + if err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + |
| 68 | + return cli.acquisition(stup.CollectAcquisitionSpecs(), f.acquisDir, false, dryRun) |
| 69 | + }, |
| 70 | + } |
| 71 | + |
| 72 | + f.bind(cmd) |
| 73 | + |
| 74 | + flags := cmd.Flags() |
| 75 | + flags.BoolVar(&dryRun, "dry-run", false, "simulate the installation without making any changes") |
| 76 | + |
| 77 | + return cmd |
| 78 | +} |
| 79 | + |
| 80 | +func colorizeDiff(diff string) string { |
| 81 | + var b strings.Builder |
| 82 | + |
| 83 | + for line := range strings.SplitSeq(diff, "\n") { |
| 84 | + switch { |
| 85 | + case strings.HasPrefix(line, "+") && !strings.HasPrefix(line, "+++"): |
| 86 | + b.WriteString(color.GreenString(line) + "\n") |
| 87 | + case strings.HasPrefix(line, "-") && !strings.HasPrefix(line, "---"): |
| 88 | + b.WriteString(color.RedString(line) + "\n") |
| 89 | + case strings.HasPrefix(line, "@@"): |
| 90 | + b.WriteString(color.New(color.Bold, color.FgCyan).Sprint(line) + "\n") |
| 91 | + default: |
| 92 | + b.WriteString(line + "\n") |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return b.String() |
| 97 | +} |
| 98 | + |
| 99 | +func shouldOverwrite(path string, newContent []byte) (bool, error) { |
| 100 | + oldContent, err := os.ReadFile(path) |
| 101 | + if err != nil { |
| 102 | + // File doesn't exist or unreadable, assume overwrite |
| 103 | + return true, nil //nolint: nilerr |
| 104 | + } |
| 105 | + |
| 106 | + if err := VerifyChecksum(bytes.NewReader(oldContent)); err == nil { |
| 107 | + // Valid checksum, safe to overwrite silently |
| 108 | + return true, nil |
| 109 | + } |
| 110 | + |
| 111 | + // Invalid or missing checksum, show diff and ask |
| 112 | + edits := myers.ComputeEdits(span.URIFromPath(path), string(oldContent), string(newContent)) |
| 113 | + diff := gotextdiff.ToUnified(path, path, string(oldContent), edits) |
| 114 | + |
| 115 | + fmt.Fprintln(os.Stdout, color.YellowString("This file was modified after being generated by 'cscli setup'. The following changes will be made:\n")) |
| 116 | + |
| 117 | + fmt.Fprint(os.Stdout, colorizeDiff(fmt.Sprintf("%s", diff))) |
| 118 | + |
| 119 | + var overwrite bool |
| 120 | + |
| 121 | + prompt := &survey.Confirm{ |
| 122 | + Message: "Do you want to overwrite with the new version?", |
| 123 | + Default: false, |
| 124 | + } |
| 125 | + |
| 126 | + if err := survey.AskOne(prompt, &overwrite); err != nil { |
| 127 | + return false, fmt.Errorf("prompt failed: %w", err) |
| 128 | + } |
| 129 | + |
| 130 | + fmt.Fprintln(os.Stdout) |
| 131 | + |
| 132 | + return overwrite, nil |
| 133 | +} |
| 134 | + |
| 135 | +// processAcquisitionSpec handles the creation of a single acquisition file. |
| 136 | +// |
| 137 | +// It includes an header with the appropriate checksum. |
| 138 | +// In dry-run, prints the content to stdout instead of writing to disk. |
| 139 | +// In interactive mode, it prompts the user before overwriting an existing file unless it's pristine. |
| 140 | +func (cli *cliSetup) processAcquisitionSpec(spec setup.AcquisitionSpec, toDir string, interactive, dryRun bool) error { |
| 141 | + if spec.Datasource == nil { |
| 142 | + return nil |
| 143 | + } |
| 144 | + |
| 145 | + path, err := spec.Path(toDir) |
| 146 | + if err != nil { |
| 147 | + return err |
| 148 | + } |
| 149 | + |
| 150 | + content, err := spec.ToYAML() |
| 151 | + if err != nil { |
| 152 | + return err |
| 153 | + } |
| 154 | + |
| 155 | + if dryRun { |
| 156 | + _, _ = fmt.Fprintln(os.Stdout, "(dry run) "+path+"\n"+color.BlueString(string(content))) |
| 157 | + return nil |
| 158 | + } |
| 159 | + |
| 160 | + contentWithHeader := spec.AddHeader(content) |
| 161 | + |
| 162 | + if interactive { |
| 163 | + ok, err := shouldOverwrite(path, contentWithHeader) |
| 164 | + if err != nil { |
| 165 | + return err |
| 166 | + } |
| 167 | + |
| 168 | + if !ok { |
| 169 | + fmt.Fprintln(os.Stdout, "skipped "+path) |
| 170 | + return nil |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + fmt.Fprintln(os.Stdout, "creating "+path) |
| 175 | + |
| 176 | + writer, err := spec.Open(toDir) |
| 177 | + if err != nil { |
| 178 | + return err |
| 179 | + } |
| 180 | + defer writer.Close() |
| 181 | + |
| 182 | + _, err = writer.Write(contentWithHeader) |
| 183 | + if err != nil { |
| 184 | + return fmt.Errorf("writing acquisition to %q: %w", path, err) |
| 185 | + } |
| 186 | + |
| 187 | + return nil |
| 188 | +} |
| 189 | + |
| 190 | +func (cli *cliSetup) acquisition(acquisitionSpecs []setup.AcquisitionSpec, toDir string, interactive bool, dryRun bool) error { |
| 191 | + cfg := cli.cfg() |
| 192 | + |
| 193 | + if toDir == "" { |
| 194 | + toDir = cfg.Crowdsec.AcquisitionDirPath |
| 195 | + } |
| 196 | + |
| 197 | + if toDir == "" { |
| 198 | + return fmt.Errorf("no acquisition directory specified, please use --acquis-dir or set crowdsec_services.acquisition_dir in %q", cfg.FilePath) |
| 199 | + } |
| 200 | + |
| 201 | + for _, spec := range acquisitionSpecs { |
| 202 | + if err := cli.processAcquisitionSpec(spec, toDir, interactive, dryRun); err != nil { |
| 203 | + return err |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + return nil |
| 208 | +} |
0 commit comments