From c822531fde57b8bf6c3e7d111ab9e7bbf94cd58c Mon Sep 17 00:00:00 2001 From: "David J. Allen" Date: Fri, 20 Sep 2024 16:50:45 -0600 Subject: [PATCH 1/9] Added check to remove duplicates in 'inspect' cmd --- cmd/inspect.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/cmd/inspect.go b/cmd/inspect.go index 2635b70..735fa7b 100644 --- a/cmd/inspect.go +++ b/cmd/inspect.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/OpenCHAMI/configurator/pkg/generator" + "github.com/OpenCHAMI/configurator/pkg/util" "github.com/rodaine/table" "github.com/rs/zerolog/log" "github.com/spf13/cobra" @@ -26,11 +27,16 @@ var inspectCmd = &cobra.Command{ return strings.ToUpper(fmt.Sprintf(format, vals...)) } - // TODO: remove duplicate args from CLI + // remove duplicate clean paths from CLI + paths := make([]string, len(args)) + for _, path := range args { + paths = append(paths, filepath.Clean(path)) + } + paths = util.RemoveDuplicates(paths) // load specific plugins from positional args var generators = make(map[string]generator.Generator) - for _, path := range args { + for _, path := range paths { err := filepath.Walk(path, func(path string, info fs.FileInfo, err error) error { if err != nil { return err From 67854029286fe1eb7f551246e7da5c12eb0ae451 Mon Sep 17 00:00:00 2001 From: "David J. Allen" Date: Fri, 20 Sep 2024 16:51:18 -0600 Subject: [PATCH 2/9] Removed vars from fetch cmd --- cmd/fetch.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/cmd/fetch.go b/cmd/fetch.go index ef7e16e..c2ba4e5 100644 --- a/cmd/fetch.go +++ b/cmd/fetch.go @@ -13,12 +13,6 @@ import ( "github.com/spf13/cobra" ) -var ( - accessToken string - remoteHost string - remotePort int -) - var fetchCmd = &cobra.Command{ Use: "fetch", Short: "Fetch a config file from a remote instance of configurator", From 41c0e24c06ee10ff8a5301ef183c1adbcc07b931 Mon Sep 17 00:00:00 2001 From: "David J. Allen" Date: Fri, 20 Sep 2024 16:52:00 -0600 Subject: [PATCH 3/9] Changed logic for RunTargets in 'generate' cmd --- cmd/generate.go | 75 ++++++++++++++++++++++++++++++------------------- 1 file changed, 46 insertions(+), 29 deletions(-) diff --git a/cmd/generate.go b/cmd/generate.go index 15c5e02..390f564 100644 --- a/cmd/generate.go +++ b/cmd/generate.go @@ -12,12 +12,14 @@ import ( configurator "github.com/OpenCHAMI/configurator/pkg" "github.com/OpenCHAMI/configurator/pkg/generator" "github.com/OpenCHAMI/configurator/pkg/util" + "github.com/rs/zerolog/log" "github.com/spf13/cobra" ) var ( tokenFetchRetries int - pluginPaths []string + templatePaths []string + pluginPath string cacertPath string ) @@ -27,8 +29,6 @@ var generateCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { // make sure that we have a token present before trying to make request if config.AccessToken == "" { - // TODO: make request to check if request will need token - // check if OCHAMI_ACCESS_TOKEN env var is set if no access token is provided and use that instead accessToken := os.Getenv("ACCESS_TOKEN") if accessToken != "" { @@ -42,16 +42,10 @@ var generateCmd = &cobra.Command{ } // use cert path from cobra if empty - // TODO: this needs to be checked for the correct desired behavior if config.CertPath == "" { config.CertPath = cacertPath } - // use config plugins if none supplied via CLI - if len(pluginPaths) <= 0 { - pluginPaths = append(pluginPaths, config.PluginDirs...) - } - // show config as JSON and generators if verbose if verbose { b, err := json.MarshalIndent(config, "", " ") @@ -61,8 +55,22 @@ var generateCmd = &cobra.Command{ fmt.Printf("%v\n", string(b)) } - RunTargets(&config, args, targets...) + // run all of the target recursively until completion if provided + if len(targets) > 0 { + RunTargets(&config, args, targets...) + } else { + if pluginPath == "" { + fmt.Printf("no plugin path specified") + return + } + // run generator.Generate() with just plugin path and templates provided + generator.Generate(&config, generator.Params{ + PluginPath: pluginPath, + TemplatePaths: templatePaths, + }) + + } }, } @@ -75,22 +83,20 @@ var generateCmd = &cobra.Command{ func RunTargets(config *configurator.Config, args []string, targets ...string) { // generate config with each supplied target for _, target := range targets { - params := generator.Params{ - Args: args, - PluginPaths: pluginPaths, - Target: target, - Verbose: verbose, - } - outputBytes, err := generator.GenerateWithTarget(config, params) + outputBytes, err := generator.GenerateWithTarget(config, generator.Params{ + Args: args, + PluginPath: pluginPath, + Target: target, + Verbose: verbose, + }) if err != nil { - fmt.Printf("failed to generate config: %v\n", err) + log.Error().Err(err).Msg("failed to generate config") os.Exit(1) } - outputMap := generator.ConvertContentsToString(outputBytes) - // if we have more than one target and output is set, create configs in directory var ( + outputMap = generator.ConvertContentsToString(outputBytes) targetCount = len(targets) templateCount = len(outputMap) ) @@ -110,16 +116,16 @@ func RunTargets(config *configurator.Config, args []string, targets ...string) { for _, contents := range outputBytes { err := os.WriteFile(outputPath, contents, 0o644) if err != nil { - fmt.Printf("failed to write config to file: %v", err) + log.Error().Err(err).Msg("failed to write config to file") os.Exit(1) } - fmt.Printf("wrote file to '%s'\n", outputPath) + log.Info().Msgf("wrote file to '%s'\n", outputPath) } } else if outputPath != "" && targetCount > 1 || templateCount > 1 { // write multiple files in directory using template name err := os.MkdirAll(filepath.Clean(outputPath), 0o755) if err != nil { - fmt.Printf("failed to make output directory: %v\n", err) + log.Error().Err(err).Msg("failed to make output directory") os.Exit(1) } for path, contents := range outputBytes { @@ -127,15 +133,17 @@ func RunTargets(config *configurator.Config, args []string, targets ...string) { cleanPath := fmt.Sprintf("%s/%s", filepath.Clean(outputPath), filename) err := os.WriteFile(cleanPath, contents, 0o755) if err != nil { - fmt.Printf("failed to write config to file: %v\n", err) + log.Error().Err(err).Msg("failed to write config to file") os.Exit(1) } - fmt.Printf("wrote file to '%s'\n", cleanPath) + log.Info().Msgf("wrote file to '%s'\n", cleanPath) } } // remove any targets that are the same as current to prevent infinite loop - nextTargets := util.CopyIf(config.Targets[target].RunTargets, func(t string) bool { return t != target }) + nextTargets := util.CopyIf(config.Targets[target].RunTargets, func(nextTarget string) bool { + return nextTarget != target + }) // ...then, run any other targets that the current target has RunTargets(config, args, nextTargets...) @@ -143,11 +151,20 @@ func RunTargets(config *configurator.Config, args []string, targets ...string) { } func init() { - generateCmd.Flags().StringSliceVar(&targets, "target", []string{}, "set the target configs to make") - generateCmd.Flags().StringSliceVar(&pluginPaths, "plugins", []string{}, "set the generator plugins directory path") + generateCmd.Flags().StringSliceVar(&targets, "target", []string{}, "set the targets to run pre-defined config") + generateCmd.Flags().StringSliceVar(&templatePaths, "template", []string{}, "set the paths for the Jinja 2 templates to use") + generateCmd.Flags().StringVar(&pluginPath, "plugin", "", "set the generator plugin path") generateCmd.Flags().StringVarP(&outputPath, "output", "o", "", "set the output path for config targets") - generateCmd.Flags().StringVar(&cacertPath, "ca-cert", "", "path to CA cert. (defaults to system CAs)") + generateCmd.Flags().StringVar(&cacertPath, "cacert", "", "path to CA cert. (defaults to system CAs)") generateCmd.Flags().IntVar(&tokenFetchRetries, "fetch-retries", 5, "set the number of retries to fetch an access token") + generateCmd.Flags().StringVar(&remoteHost, "host", "http://localhost", "set the remote host") + generateCmd.Flags().IntVar(&remotePort, "port", 80, "set the remote port") + + // requires either 'target' by itself or 'plugin' and 'templates' together + // generateCmd.MarkFlagsOneRequired("target", "plugin") + generateCmd.MarkFlagsMutuallyExclusive("target", "plugin") + generateCmd.MarkFlagsMutuallyExclusive("target", "template") + generateCmd.MarkFlagsRequiredTogether("plugin", "template") rootCmd.AddCommand(generateCmd) } From b488c32195b765f0a65659192fa872a1c84c98a6 Mon Sep 17 00:00:00 2001 From: "David J. Allen" Date: Fri, 20 Sep 2024 16:52:31 -0600 Subject: [PATCH 4/9] Updated vars in 'root' cmd --- cmd/root.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index cbbfab7..26ccba7 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -10,11 +10,14 @@ import ( ) var ( - configPath string - config configurator.Config - verbose bool - targets []string - outputPath string + configPath string + config configurator.Config + verbose bool + targets []string + outputPath string + accessToken string + remoteHost string + remotePort int ) var rootCmd = &cobra.Command{ From e044d4b5edaeeccdeb1a77694a2d5fec64228625 Mon Sep 17 00:00:00 2001 From: "David J. Allen" Date: Fri, 20 Sep 2024 16:53:30 -0600 Subject: [PATCH 5/9] Changed from using multiple plugin paths to just one --- cmd/serve.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/cmd/serve.go b/cmd/serve.go index ee54f6c..f7e4401 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -35,11 +35,6 @@ var serveCmd = &cobra.Command{ } } - // use config plugins if none supplied via CLI - if len(pluginPaths) <= 0 { - pluginPaths = append(pluginPaths, config.PluginDirs...) - } - // show config as JSON and generators if verbose if verbose { b, err := json.MarshalIndent(config, "", " ") @@ -60,15 +55,19 @@ var serveCmd = &cobra.Command{ Retries: config.Server.Jwks.Retries, }, GeneratorParams: generator.Params{ - Args: args, - PluginPaths: pluginPaths, + Args: args, + PluginPath: pluginPath, // Target: target, // NOTE: targets are set via HTTP requests (ex: curl http://configurator:3334/generate?target=dnsmasq) Verbose: verbose, }, } + + // start listening with the server err := server.Serve() if errors.Is(err, http.ErrServerClosed) { - fmt.Printf("Server closed.") + if verbose { + fmt.Printf("Server closed.") + } } else if err != nil { fmt.Errorf("failed to start server: %v", err) os.Exit(1) @@ -79,7 +78,7 @@ var serveCmd = &cobra.Command{ func init() { serveCmd.Flags().StringVar(&config.Server.Host, "host", config.Server.Host, "set the server host") serveCmd.Flags().IntVar(&config.Server.Port, "port", config.Server.Port, "set the server port") - serveCmd.Flags().StringSliceVar(&pluginPaths, "plugins", nil, "set the generator plugins directory path") + serveCmd.Flags().StringVar(&pluginPath, "plugin", "", "set the generator plugins directory path") serveCmd.Flags().StringVar(&config.Server.Jwks.Uri, "jwks-uri", config.Server.Jwks.Uri, "set the JWKS url to fetch public key") serveCmd.Flags().IntVar(&config.Server.Jwks.Retries, "jwks-fetch-retries", config.Server.Jwks.Retries, "set the JWKS fetch retry count") rootCmd.AddCommand(serveCmd) From b922dbdbdac38361512652751e728193c0f27e30 Mon Sep 17 00:00:00 2001 From: "David J. Allen" Date: Fri, 20 Sep 2024 16:54:25 -0600 Subject: [PATCH 6/9] Removed VerifyClaims --- pkg/auth.go | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/pkg/auth.go b/pkg/auth.go index 58aea04..c857102 100644 --- a/pkg/auth.go +++ b/pkg/auth.go @@ -8,26 +8,9 @@ import ( "slices" "github.com/OpenCHAMI/jwtauth/v5" - "github.com/lestrrat-go/jwx/jwk" + "github.com/lestrrat-go/jwx/v2/jwk" ) -func VerifyClaims(testClaims []string, r *http.Request) (bool, error) { - // extract claims from JWT - _, claims, err := jwtauth.FromContext(r.Context()) - if err != nil { - return false, fmt.Errorf("failed to get claims(s) from token: %v", err) - } - - // verify that each one of the test claims are included - for _, testClaim := range testClaims { - _, ok := claims[testClaim] - if !ok { - return false, fmt.Errorf("failed to verify claim(s) from token: %s", testClaim) - } - } - return true, nil -} - func VerifyScope(testScopes []string, r *http.Request) (bool, error) { // extract the scopes from JWT var scopes []string @@ -112,3 +95,7 @@ func FetchPublicKeyFromURL(url string) (*jwtauth.JWTAuth, error) { return tokenAuth, nil } + +func LoadAccessToken() { + +} From 601089672c7d441b1ed8730dcd65905f7fb631e8 Mon Sep 17 00:00:00 2001 From: "David J. Allen" Date: Fri, 20 Sep 2024 16:55:31 -0600 Subject: [PATCH 7/9] Changed how plugins and tempates are loaded --- pkg/generator/generator.go | 264 ++++++++++++++++++++++++++----------- 1 file changed, 188 insertions(+), 76 deletions(-) diff --git a/pkg/generator/generator.go b/pkg/generator/generator.go index 27a032e..9a00545 100644 --- a/pkg/generator/generator.go +++ b/pkg/generator/generator.go @@ -3,7 +3,7 @@ package generator import ( "bytes" "fmt" - "maps" + "io/fs" "os" "path/filepath" "plugin" @@ -17,6 +17,7 @@ import ( type Mappings map[string]any type FileMap map[string][]byte type FileList [][]byte +type Template []byte // Generator interface used to define how files are created. Plugins can // be created entirely independent of the main driver program. @@ -29,11 +30,12 @@ type Generator interface { // Params defined and used by the "generate" subcommand. type Params struct { - Args []string - PluginPaths []string - Generators map[string]Generator - Target string - Verbose bool + Args []string + Generators map[string]Generator + TemplatePaths []string + PluginPath string + Target string + Verbose bool } // Converts the file outputs from map[string][]byte to map[string]string. @@ -51,13 +53,13 @@ func LoadFiles(paths ...string) (FileMap, error) { for _, path := range paths { expandedPaths, err := filepath.Glob(path) if err != nil { - return nil, fmt.Errorf("failed to glob path: %v", err) + return nil, fmt.Errorf("failed to glob path: %w", err) } for _, expandedPath := range expandedPaths { info, err := os.Stat(expandedPath) if err != nil { fmt.Println(err) - return nil, fmt.Errorf("failed to stat file or directory: %v", err) + return nil, fmt.Errorf("failed to stat file or directory: %w", err) } // skip any directories found if info.IsDir() { @@ -65,7 +67,7 @@ func LoadFiles(paths ...string) (FileMap, error) { } b, err := os.ReadFile(expandedPath) if err != nil { - return nil, fmt.Errorf("failed to read file: %v", err) + return nil, fmt.Errorf("failed to read file: %w", err) } outputs[expandedPath] = b @@ -81,19 +83,19 @@ func LoadPlugin(path string) (Generator, error) { if isDir, err := util.IsDirectory(path); err == nil && isDir { return nil, nil } else if err != nil { - return nil, fmt.Errorf("failed to test if path is directory: %v", err) + return nil, fmt.Errorf("failed to test if plugin path is directory: %w", err) } // try and open the plugin p, err := plugin.Open(path) if err != nil { - return nil, fmt.Errorf("failed to open plugin: %v", err) + return nil, fmt.Errorf("failed to open plugin: %w", err) } // load the "Generator" symbol from plugin symbol, err := p.Lookup("Generator") if err != nil { - return nil, fmt.Errorf("failed to look up symbol at path '%s': %v", path, err) + return nil, fmt.Errorf("failed to look up symbol at path '%s': %w", path, err) } // assert that the plugin loaded has a valid generator @@ -111,45 +113,123 @@ func LoadPlugin(path string) (Generator, error) { func LoadPlugins(dirpath string, opts ...util.Option) (map[string]Generator, error) { // check if verbose option is supplied var ( - gens = make(map[string]Generator) - params = util.GetParams(opts...) + generators = make(map[string]Generator) + params = util.ToDict(opts...) ) - items, _ := os.ReadDir(dirpath) - for _, item := range items { - if item.IsDir() { - subitems, _ := os.ReadDir(item.Name()) - for _, subitem := range subitems { - if !subitem.IsDir() { - gen, err := LoadPlugin(subitem.Name()) - if err != nil { - fmt.Printf("failed to load generator in directory '%s': %v\n", item.Name(), err) - continue - } - if verbose, ok := params["verbose"].(bool); ok { - if verbose { - fmt.Printf("-- found plugin '%s'\n", item.Name()) - } - } - gens[gen.GetName()] = gen - } + // + err := filepath.Walk(dirpath, func(path string, info fs.FileInfo, err error) error { + // skip trying to load generator plugin if directory or error + if info.IsDir() || err != nil { + return nil + } + + // load the generator plugin from current path + gen, err := LoadPlugin(path) + if err != nil { + return fmt.Errorf("failed to load generator in directory '%s': %w", path, err) + } + + // show the plugins found if verbose flag is set + if params.GetVerbose() { + fmt.Printf("-- found plugin '%s'\n", gen.GetName()) + } + + // map each generator plugin by name for lookup + generators[gen.GetName()] = gen + return nil + }) + + if err != nil { + return nil, fmt.Errorf("failed to walk directory: %w", err) + } + + // items, _ := os.ReadDir(dirpath) + // for _, item := range items { + // if item.IsDir() { + // subitems, _ := os.ReadDir(item.Name()) + // for _, subitem := range subitems { + // if !subitem.IsDir() { + // gen, err := LoadPlugin(subitem.Name()) + // if err != nil { + // fmt.Printf("failed to load generator in directory '%s': %v\n", item.Name(), err) + // continue + // } + // if verbose, ok := params["verbose"].(bool); ok { + // if verbose { + // fmt.Printf("-- found plugin '%s'\n", item.Name()) + // } + // } + // gens[gen.GetName()] = gen + // } + // } + // } else { + // gen, err := LoadPlugin(dirpath + item.Name()) + // if err != nil { + // fmt.Printf("failed to load plugin: %v\n", err) + // continue + // } + // if verbose, ok := params["verbose"].(bool); ok { + // if verbose { + // fmt.Printf("-- found plugin '%s'\n", dirpath+item.Name()) + // } + // } + // gens[gen.GetName()] = gen + // } + // } + + return generators, nil +} + +func LoadTemplate(path string) (Template, error) { + // skip loading template if path is a directory with no error + if isDir, err := util.IsDirectory(path); err == nil && isDir { + return nil, nil + } else if err != nil { + return nil, fmt.Errorf("failed to test if template path is directory: %w", err) + } + + // try and read the contents of the file + // NOTE: we don't care if this is actually a Jinja template + // or not...at least for now. + return os.ReadFile(path) +} + +func LoadTemplates(paths []string, opts ...util.Option) (map[string]Template, error) { + var ( + templates = make(map[string]Template) + params = util.ToDict(opts...) + ) + + for _, path := range paths { + err := filepath.Walk(path, func(path string, info fs.FileInfo, err error) error { + // skip trying to load generator plugin if directory or error + if info.IsDir() || err != nil { + return nil } - } else { - gen, err := LoadPlugin(dirpath + item.Name()) + + // load the contents of the template + template, err := LoadTemplate(path) if err != nil { - fmt.Printf("failed to load plugin: %v\n", err) - continue + return fmt.Errorf("failed to load generator in directory '%s': %w", path, err) } - if verbose, ok := params["verbose"].(bool); ok { - if verbose { - fmt.Printf("-- found plugin '%s'\n", dirpath+item.Name()) - } + + // show the templates loaded if verbose flag is set + if params.GetVerbose() { + fmt.Printf("-- loaded tempalte '%s'\n", path) } - gens[gen.GetName()] = gen + + // map each template by the path it was loaded from + templates[path] = template + return nil + }) + + if err != nil { + return nil, fmt.Errorf("failed to walk directory: %w", err) } } - return gens, nil + return templates, nil } // Option to specify "target" in parameter map. This is used to set which generator @@ -171,6 +251,31 @@ func WithType(_type string) util.Option { } } +// Option to the plugin to load +func WithPlugin(path string) util.Option { + return func(p util.Params) { + if p != nil { + plugin, err := LoadPlugin(path) + if err != nil { + return + } + p["plugin"] = plugin + } + } +} + +func WithTemplates(paths []string) util.Option { + return func(p util.Params) { + if p != nil { + templates, err := LoadTemplates(paths) + if err != nil { + + } + p["templates"] = templates + } + } +} + // Option to a specific client to include in implementing plugin generator.Generate(). // // NOTE: This may be changed to pass some kind of client interface as an argument in @@ -217,13 +322,13 @@ func ApplyTemplates(mappings Mappings, contents ...[]byte) (FileList, error) { // load jinja template from file t, err := gonja.FromBytes(b) if err != nil { - return nil, fmt.Errorf("failed to read template from file: %v", err) + return nil, fmt.Errorf("failed to read template from file: %w", err) } // execute/render jinja template b := bytes.Buffer{} if err = t.Execute(&b, data); err != nil { - return nil, fmt.Errorf("failed to execute: %v", err) + return nil, fmt.Errorf("failed to execute: %w", err) } outputs = append(outputs, b.Bytes()) } @@ -243,13 +348,13 @@ func ApplyTemplateFromFiles(mappings Mappings, paths ...string) (FileMap, error) // load jinja template from file t, err := gonja.FromFile(path) if err != nil { - return nil, fmt.Errorf("failed to read template from file: %v", err) + return nil, fmt.Errorf("failed to read template from file: %w", err) } // execute/render jinja template b := bytes.Buffer{} if err = t.Execute(&b, data); err != nil { - return nil, fmt.Errorf("failed to execute: %v", err) + return nil, fmt.Errorf("failed to execute: %w", err) } outputs[path] = b.Bytes() } @@ -257,6 +362,23 @@ func ApplyTemplateFromFiles(mappings Mappings, paths ...string) (FileMap, error) return outputs, nil } +// Generate() is the main function to generate a collection of files and returns them as a map. +// This function only expects a path to a plugin and paths to a collection of templates to +// be used. This function will only load the plugin on-demand and fetch resources as needed. +func Generate(config *configurator.Config, params Params) (FileMap, error) { + var ( + gen Generator + client = configurator.NewSmdClient() + ) + + return gen.Generate( + config, + WithPlugin(params.PluginPath), + WithTemplates(params.TemplatePaths), + WithClient(client), + ) +} + // Main function to generate a collection of files as a map with the path as the key and // the contents of the file as the value. This function currently expects a list of plugin // paths to load all plugins within a directory. Then, each plugin's generator.GenerateWithTarget() @@ -269,8 +391,7 @@ func ApplyTemplateFromFiles(mappings Mappings, paths ...string) (FileMap, error) func GenerateWithTarget(config *configurator.Config, params Params) (FileMap, error) { // load generator plugins to generate configs or to print var ( - generators = make(map[string]Generator) - client = configurator.NewSmdClient( + client = configurator.NewSmdClient( configurator.WithHost(config.SmdClient.Host), configurator.WithPort(config.SmdClient.Port), configurator.WithAccessToken(config.AccessToken), @@ -278,41 +399,32 @@ func GenerateWithTarget(config *configurator.Config, params Params) (FileMap, er ) ) - // load all plugins from supplied arguments - for _, path := range params.PluginPaths { - if params.Verbose { - fmt.Printf("loading plugins from '%s'\n", path) - } - plugins, err := LoadPlugins(path) - if err != nil { - fmt.Printf("failed to load plugins: %v\n", err) - err = nil - continue - } - - // add loaded generator plugins to set - maps.Copy(generators, plugins) + // check if a target is supplied + if len(params.Args) == 0 && params.Target == "" { + return nil, fmt.Errorf("must specify a target") } - // copy all generators supplied from arguments - maps.Copy(generators, params.Generators) + // load target information from config + target, ok := config.Targets[params.Target] + if !ok { + return nil, fmt.Errorf("target not found in config") + } - // show available targets then exit - if len(params.Args) == 0 && params.Target == "" { - for g := range generators { - fmt.Printf("-- found generator plugin \"%s\"\n", g) - } - return nil, nil + // if plugin path specified from CLI, use that instead + if params.PluginPath != "" { + target.PluginPath = params.PluginPath + } + + // only load the plugin needed for this target + generator, err := LoadPlugin(target.PluginPath) + if err != nil { + return nil, fmt.Errorf("failed to load plugin: %w", err) } // run the generator plugin from target passed - gen := generators[params.Target] - if gen == nil { - return nil, fmt.Errorf("invalid generator target (%s)", params.Target) - } - return gen.Generate( + return generator.Generate( config, - WithTarget(gen.GetName()), + WithTarget(generator.GetName()), WithClient(client), ) } From 751a2facdbea29d882a5d37f9c2f950b717cc6c8 Mon Sep 17 00:00:00 2001 From: "David J. Allen" Date: Fri, 20 Sep 2024 16:56:15 -0600 Subject: [PATCH 8/9] Minor changes to util functions --- pkg/util/params.go | 19 ++++++++++++++++--- pkg/util/util.go | 11 +++++++++-- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/pkg/util/params.go b/pkg/util/params.go index 6b342cd..322d852 100644 --- a/pkg/util/params.go +++ b/pkg/util/params.go @@ -11,7 +11,7 @@ type Params map[string]any type Option func(Params) // Extract all parameters from the options passed as map[string]any. -func GetParams(opts ...Option) Params { +func ToDict(opts ...Option) Params { params := Params{} for _, opt := range opts { opt(params) @@ -45,8 +45,8 @@ func WithDefault[T any](v T) Option { } } -// Syntactic sugar generic function to get parameter from util.Params. -func Get[T any](params Params, key string, opts ...Option) *T { +// Sugary generic function to get parameter from util.Params. +func Get[T any](params Params, key string) *T { if v, ok := params[key].(T); ok { return &v } @@ -55,3 +55,16 @@ func Get[T any](params Params, key string, opts ...Option) *T { } return nil } + +func GetOpt[T any](opts []Option, key string) *T { + return Get[T](ToDict(opts...), "required_claims") +} + +func (p Params) GetVerbose() bool { + if verbose, ok := p["verbose"].(bool); ok { + return verbose + } + + // default setting + return false +} diff --git a/pkg/util/util.go b/pkg/util/util.go index fd0daa2..fc53b67 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -2,12 +2,14 @@ package util import ( "bytes" + "cmp" "crypto/tls" "fmt" "io" "net/http" "os" "os/exec" + "slices" "strings" ) @@ -28,7 +30,7 @@ func IsDirectory(path string) (bool, error) { // This returns an *os.FileInfo type fileInfo, err := os.Stat(path) if err != nil { - return false, fmt.Errorf("failed to stat path: %v", err) + return false, fmt.Errorf("failed to stat path (%s): %v", path, err) } // IsDir is short for fileInfo.Mode().IsDir() @@ -63,7 +65,7 @@ func MakeRequest(url string, httpMethod string, body []byte, headers map[string] // NOTE: This currently requires git to be installed. // TODO: Change how this is done to not require executing a command. func GitCommit() string { - c := exec.Command("git", "rev-parse", "HEAD") + c := exec.Command("git", "rev-parse", "--short=8", "HEAD") stdout, err := c.Output() if err != nil { return "" @@ -80,6 +82,11 @@ func RemoveIndex[T comparable](s []T, index int) []T { return append(ret, s[index+1:]...) } +func RemoveDuplicates[T cmp.Ordered](s []T) []T { + slices.Sort(s) + return slices.Compact(s) +} + // General function to copy elements from slice if condition is true. func CopyIf[T comparable](s []T, condition func(t T) bool) []T { var f = make([]T, 0) From cb73258a84db24ea8a24801fbae597f8284a815e Mon Sep 17 00:00:00 2001 From: "David J. Allen" Date: Fri, 20 Sep 2024 16:56:46 -0600 Subject: [PATCH 9/9] Minor changes to error format in dhcpd plugin --- pkg/generator/plugins/dhcpd/dhcpd.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/generator/plugins/dhcpd/dhcpd.go b/pkg/generator/plugins/dhcpd/dhcpd.go index a736064..1a37b9c 100644 --- a/pkg/generator/plugins/dhcpd/dhcpd.go +++ b/pkg/generator/plugins/dhcpd/dhcpd.go @@ -37,7 +37,7 @@ func (g *Dhcpd) Generate(config *configurator.Config, opts ...util.Option) (gene if client != nil { eths, err = client.FetchEthernetInterfaces(opts...) if err != nil { - return nil, fmt.Errorf("failed to fetch ethernet interfaces with client: %v", err) + return nil, fmt.Errorf("failed to fetch ethernet interfaces with client: %w", err) } }