|
| 1 | +/* |
| 2 | +Copyright © 2019 The Falco Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package cmd |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + "path" |
| 23 | + |
| 24 | + "github.com/falcosecurity/falcoctl/pkg/kernelmoduleloader" |
| 25 | + "github.com/kris-nova/logger" |
| 26 | + "github.com/spf13/cobra" |
| 27 | + "github.com/spf13/viper" |
| 28 | + "k8s.io/cli-runtime/pkg/genericclioptions" |
| 29 | +) |
| 30 | + |
| 31 | +// ModuleOptions represents the `install module` command options |
| 32 | +type ModuleOptions struct { |
| 33 | + genericclioptions.IOStreams |
| 34 | + falcoVersion string |
| 35 | + falcoModulePath string |
| 36 | + falcoModuleFile string |
| 37 | + falcoModuleURL string |
| 38 | + falcoModuleRepo string |
| 39 | +} |
| 40 | + |
| 41 | +// Validate validates the `install module` command options |
| 42 | +func (o ModuleOptions) Validate(c *cobra.Command, args []string) error { |
| 43 | + if len(o.falcoVersion) == 0 { |
| 44 | + return fmt.Errorf("missing Falco version: specify it via FALCOCTL_FALCO_VERSION env variable or via --falco-version flag") |
| 45 | + } |
| 46 | + return nil |
| 47 | +} |
| 48 | + |
| 49 | +// NewModuleOptions instantiates the `install module` command options |
| 50 | +func NewModuleOptions(streams genericclioptions.IOStreams) CommandOptions { |
| 51 | + o := &ModuleOptions{ |
| 52 | + IOStreams: streams, |
| 53 | + } |
| 54 | + o.falcoVersion = viper.GetString("falco-version") // FALCOCTL_FALCO_VERSION env var |
| 55 | + o.falcoModulePath = viper.GetString("falco-module-path") // FALCOCTL_FALCO_MODULE_PATH env var |
| 56 | + if len(o.falcoModulePath) == 0 { |
| 57 | + o.falcoModulePath = "/" // default |
| 58 | + } |
| 59 | + o.falcoModuleFile = viper.GetString("falco-module-file") // FALCOCTL_FALCO_MODULE_FILE env var |
| 60 | + if len(o.falcoModuleFile) == 0 { |
| 61 | + o.falcoModuleFile = "falco-module.ko" // default |
| 62 | + } |
| 63 | + o.falcoModuleURL = viper.GetString("falco-module-url") // FALCOCTL_FALCO_MODULE_URL env var |
| 64 | + o.falcoModuleRepo = viper.GetString("falco-module-repo") // FALCOCTL_FALCO_MODULE_REPO env var |
| 65 | + if len(o.falcoModuleRepo) == 0 { |
| 66 | + o.falcoModuleRepo = "https://s3.amazonaws.com/download.draios.com/stable/sysdig-module-binaries/" // default |
| 67 | + } |
| 68 | + return o |
| 69 | +} |
| 70 | + |
| 71 | +// InstallModule creates the `install module` command |
| 72 | +func InstallModule(streams genericclioptions.IOStreams) *cobra.Command { |
| 73 | + o := NewModuleOptions(streams).(*ModuleOptions) |
| 74 | + |
| 75 | + cmd := &cobra.Command{ |
| 76 | + Use: "module", |
| 77 | + DisableFlagsInUseLine: true, |
| 78 | + Short: "Install the Falco module locally", |
| 79 | + Long: `Download and install the Falco module locally`, |
| 80 | + PreRun: func(cmd *cobra.Command, args []string) { |
| 81 | + if err := o.Validate(cmd, args); err != nil { |
| 82 | + logger.Critical("%s", err) |
| 83 | + os.Exit(1) |
| 84 | + } |
| 85 | + }, |
| 86 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 87 | + falcoModuleFullpath := path.Join(o.falcoModulePath, o.falcoModuleFile) |
| 88 | + falcoConfigHash, err := kernelmoduleloader.GetKernelConfigHash() |
| 89 | + if err != nil { |
| 90 | + logger.Critical("Error getting Kernel Config Hash: %s", err) |
| 91 | + return err |
| 92 | + } |
| 93 | + falcoKernelRelease, err := kernelmoduleloader.GetKernelRelease() |
| 94 | + if err != nil { |
| 95 | + logger.Critical("Error getting Kernel Version: %s", err) |
| 96 | + return err |
| 97 | + } |
| 98 | + |
| 99 | + logger.Always("FALCO_VERSION: %s", o.falcoVersion) |
| 100 | + logger.Always("FALCO_MODULE_URL: %s", o.falcoModuleURL) |
| 101 | + logger.Always("FALCO_MODULE_REPO: %s", o.falcoModuleRepo) |
| 102 | + logger.Always("KERNEL_VERSION: %s", falcoKernelRelease) |
| 103 | + logger.Always("KERNEL_CONFIG_HASH: %s", falcoConfigHash) |
| 104 | + |
| 105 | + // if FALCO_MODULE_URL not set, build it |
| 106 | + if o.falcoModuleURL == "" { |
| 107 | + o.falcoModuleURL = fmt.Sprintf("%sfalco-module-%s-x86_64-%s-%s.ko", o.falcoModuleRepo, o.falcoVersion, falcoKernelRelease, falcoConfigHash) |
| 108 | + } |
| 109 | + |
| 110 | + // fetch module |
| 111 | + err = kernelmoduleloader.FetchModule(o.falcoModuleURL, falcoModuleFullpath) |
| 112 | + if err != nil { |
| 113 | + logger.Critical("Error fetching module: %s", err) |
| 114 | + return err |
| 115 | + } |
| 116 | + |
| 117 | + // load module |
| 118 | + // TODO(ducy): Need to implement removal of module, retry loop, and timeout |
| 119 | + err = kernelmoduleloader.LoadModule(falcoModuleFullpath) |
| 120 | + if err != nil { |
| 121 | + logger.Critical("Error loading module: %s", err) |
| 122 | + return err |
| 123 | + } |
| 124 | + |
| 125 | + return nil |
| 126 | + }, |
| 127 | + } |
| 128 | + |
| 129 | + // TODO(fntlnz, leodido): validation |
| 130 | + cmd.Flags().StringVar(&o.falcoVersion, "falco-version", o.falcoVersion, "The falco version for which to download the module") |
| 131 | + cmd.Flags().StringVar(&o.falcoModulePath, "falco-module-path", o.falcoModulePath, "The path where to download the falco module") |
| 132 | + cmd.Flags().StringVar(&o.falcoModuleFile, "falco-module-file", o.falcoModuleFile, "The name of the falco module file") |
| 133 | + cmd.Flags().StringVar(&o.falcoModuleURL, "falco-module-url", o.falcoModuleURL, "The direct URL where to download the falco module from, alternative to the repo, not the default, this skips the search since a direct url is provided") |
| 134 | + cmd.Flags().StringVar(&o.falcoModuleRepo, "falco-module-repo", o.falcoModuleRepo, "The URL of the s3 repo where to search for the module") |
| 135 | + return cmd |
| 136 | +} |
0 commit comments