|
| 1 | +// Copyright (C) 2026 The Falco Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | +// SPDX-License-Identifier: Apache-2.0 |
| 16 | + |
| 17 | +package plugin |
| 18 | + |
| 19 | +import ( |
| 20 | + "gopkg.in/yaml.v3" |
| 21 | + |
| 22 | + artifactv1alpha1 "github.com/falcosecurity/falco-operator/api/artifact/v1alpha1" |
| 23 | + "github.com/falcosecurity/falco-operator/internal/pkg/artifact" |
| 24 | + "github.com/falcosecurity/falco-operator/internal/pkg/priority" |
| 25 | +) |
| 26 | + |
| 27 | +// PluginConfig is the configuration for a plugin. |
| 28 | +type PluginConfig struct { |
| 29 | + InitConfig map[string]string `yaml:"init_config,omitempty"` |
| 30 | + LibraryPath string `yaml:"library_path"` |
| 31 | + Name string `yaml:"name"` |
| 32 | + OpenParams string `yaml:"open_params,omitempty"` |
| 33 | +} |
| 34 | + |
| 35 | +func (p *PluginConfig) isSame(other *PluginConfig) bool { |
| 36 | + if p.Name != other.Name { |
| 37 | + return false |
| 38 | + } |
| 39 | + // Check if the maps are equal. |
| 40 | + if len(p.InitConfig) != len(other.InitConfig) { |
| 41 | + return false |
| 42 | + } |
| 43 | + // Check if the keys and values are equal. |
| 44 | + for key, value := range p.InitConfig { |
| 45 | + if otherValue, ok := other.InitConfig[key]; !ok || value != otherValue { |
| 46 | + return false |
| 47 | + } |
| 48 | + } |
| 49 | + if p.LibraryPath != other.LibraryPath { |
| 50 | + return false |
| 51 | + } |
| 52 | + if p.OpenParams != other.OpenParams { |
| 53 | + return false |
| 54 | + } |
| 55 | + return true |
| 56 | +} |
| 57 | + |
| 58 | +// PluginsConfig is the configuration for the plugins. |
| 59 | +type PluginsConfig struct { |
| 60 | + Configs []PluginConfig `yaml:"plugins"` |
| 61 | + LoadPlugins []string `yaml:"load_plugins,omitempty"` |
| 62 | +} |
| 63 | + |
| 64 | +func (pc *PluginsConfig) addConfig(plugin *artifactv1alpha1.Plugin) { |
| 65 | + config := PluginConfig{ |
| 66 | + LibraryPath: artifact.Path(plugin.Name, priority.DefaultPriority, artifact.MediumOCI, artifact.TypePlugin), |
| 67 | + Name: plugin.Name, |
| 68 | + } |
| 69 | + |
| 70 | + // If not nil, set the values that are not empty. |
| 71 | + if plugin.Spec.Config != nil { |
| 72 | + if plugin.Spec.Config.InitConfig != nil { |
| 73 | + config.InitConfig = plugin.Spec.Config.InitConfig |
| 74 | + } |
| 75 | + if plugin.Spec.Config.LibraryPath != "" { |
| 76 | + config.LibraryPath = plugin.Spec.Config.LibraryPath |
| 77 | + } |
| 78 | + if plugin.Spec.Config.Name != "" { |
| 79 | + config.Name = plugin.Spec.Config.Name |
| 80 | + } |
| 81 | + if plugin.Spec.Config.OpenParams != "" { |
| 82 | + config.OpenParams = plugin.Spec.Config.OpenParams |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + // Check if the pluginConfig already exists in the list. |
| 87 | + for i, c := range pc.Configs { |
| 88 | + if c.isSame(&config) { |
| 89 | + // Remove the plugin from the list and add the current plugin. |
| 90 | + pc.Configs = append(pc.Configs[:i], pc.Configs[i+1:]...) |
| 91 | + break |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // Add the plugin to the list if it doesn't exist. |
| 96 | + if len(pc.Configs) == 0 { |
| 97 | + pc.Configs = append(pc.Configs, config) |
| 98 | + } else { |
| 99 | + found := false |
| 100 | + for _, c := range pc.Configs { |
| 101 | + if c.Name == plugin.Name { |
| 102 | + found = true |
| 103 | + break |
| 104 | + } |
| 105 | + } |
| 106 | + if !found { |
| 107 | + pc.Configs = append(pc.Configs, config) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + // Check if the plugin is already in the list. |
| 112 | + for _, c := range pc.LoadPlugins { |
| 113 | + if c == plugin.Name { |
| 114 | + return |
| 115 | + } |
| 116 | + } |
| 117 | + pc.LoadPlugins = append(pc.LoadPlugins, plugin.Name) |
| 118 | +} |
| 119 | + |
| 120 | +func (pc *PluginsConfig) removeConfig(plugin *artifactv1alpha1.Plugin) { |
| 121 | + // Check if the pluginConfig already exists in the list. |
| 122 | + for i, c := range pc.Configs { |
| 123 | + if c.Name == plugin.Name { |
| 124 | + // Remove the plugin from the list. |
| 125 | + pc.Configs = append(pc.Configs[:i], pc.Configs[i+1:]...) |
| 126 | + break |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + // Check if the plugin is already in the list. |
| 131 | + for i, c := range pc.LoadPlugins { |
| 132 | + if c == plugin.Name { |
| 133 | + // Remove the plugin from the list. |
| 134 | + pc.LoadPlugins = append(pc.LoadPlugins[:i], pc.LoadPlugins[i+1:]...) |
| 135 | + break |
| 136 | + } |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +func (pc *PluginsConfig) toString() (string, error) { |
| 141 | + // Convert the struct to YAML. |
| 142 | + data, err := yaml.Marshal(pc) |
| 143 | + if err != nil { |
| 144 | + return "", err |
| 145 | + } |
| 146 | + return string(data), nil |
| 147 | +} |
| 148 | + |
| 149 | +func (pc *PluginsConfig) isEmpty() bool { |
| 150 | + return len(pc.Configs) == 0 && len(pc.LoadPlugins) == 0 |
| 151 | +} |
0 commit comments