Skip to content

Commit

Permalink
Merge pull request #78 from salsadigitalauorg/feature/plugin-refactor
Browse files Browse the repository at this point in the history
feat: plugin refactor
  • Loading branch information
yusufhm authored Feb 20, 2025
2 parents 0e9a7dd + 6f9ed78 commit 3d1aed7
Show file tree
Hide file tree
Showing 62 changed files with 2,209 additions and 1,924 deletions.
15 changes: 12 additions & 3 deletions cmd/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ output them in the format specified`,
Run: func(cmd *cobra.Command, args []string) {
shipshape.FactsOnly = true
runCmd.Run(cmd, args)
for _, f := range fact.Facts {
for _, f := range fact.Manager().GetPlugins() {
if shouldSkipFact(f) {
continue
}

log.WithFields(log.Fields{
"fact": f.GetName(),
"format": f.GetFormat(),
"data": f.GetData(),
}).Debug("printing collected fact")
fmt.Printf("%s:", f.GetName())
switch f.GetFormat() {

case data.FormatMapListString:
loadedData := data.AsMapListString(f.GetData())
for k, vList := range loadedData {
Expand All @@ -44,6 +44,7 @@ output them in the format specified`,
fmt.Printf(" - %s\n", v)
}
}

case data.FormatMapString:
loadedData := data.AsMapString(f.GetData())
fmt.Println()
Expand All @@ -55,6 +56,7 @@ output them in the format specified`,
fmt.Printf(" %s: %s\n", k, v)
}
}

case data.FormatMapNestedString:
loadedData := data.AsMapNestedString(f.GetData())
fmt.Println()
Expand All @@ -64,10 +66,17 @@ output them in the format specified`,
fmt.Printf(" %s: %s\n", k2, v)
}
}

case data.FormatString:
fmt.Printf(" %s\n", f.GetData())

case data.FormatRaw:
fmt.Println("\n",
output.TabbedMultiline(" ", fmt.Sprintf("%s", f.GetData())))

default:
fmt.Println(" collect not yet implemented for", f.GetFormat())
log.WithField("data", fmt.Sprintf("%s", f.GetData())).Warn("collect not yet implemented for this format")
fmt.Println(" collect not yet implemented for", f.GetFormat())
}
fmt.Println()
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ var configListPluginsCmd = &cobra.Command{
Long: `List all available plugins that can be used in shipshape`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Connection plugins:")
for _, p := range connection.RegistryKeys() {
for _, p := range connection.Manager().GetFactoriesKeys() {
fmt.Println(" - " + p)
}

fmt.Println("\nFact plugins:")
for _, p := range fact.RegistryKeys() {
for _, p := range fact.Manager().GetFactoriesKeys() {
fmt.Println(" - " + p)
}

fmt.Println("\nAnalyse plugins:")
for _, p := range analyse.RegistryKeys() {
for _, p := range analyse.Manager().GetFactoriesKeys() {
fmt.Println(" - " + p)
}

Expand Down
10 changes: 0 additions & 10 deletions cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,10 @@ func main() {
}
gen.BreachType(breachTypes)
break
case "connection-plugin":
if len(plugins) == 0 {
log.Fatal("connection-plugin missing flags; plugin is required")
}
gen.ConnectionPlugin(plugins)
break
case "fact-plugin":
if len(plugins) == 0 {
log.Fatal("fact-plugin missing flags; plugin is required")
}
if pkg == "" {
log.Fatal("fact-plugin missing flags; package is required")
}
gen.FactPlugin(plugins, pkg, enableEnvResolver)
gen.FactRegistry(pkg)
break
case "analyse-plugin":
Expand Down
10 changes: 0 additions & 10 deletions cmd/gen/analyseplugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ func AnalysePlugin(plugins []string) {
log.Println("Generating analyse plugin funcs -", strings.Join(plugins, ","))

tmplPath := filepath.Join("..", "..", "pkg", "analyse", "templates", "analyseplugin.go.tmpl")
tmplTestPath := filepath.Join("..", "..", "pkg", "analyse", "templates", "analyseplugin_test.go.tmpl")

for _, p := range plugins {
pluginFile := strings.ToLower(p) + "_gen.go"
Expand All @@ -21,14 +20,5 @@ func AnalysePlugin(plugins []string) {
}

templateToFile(tmplPath, struct{ Plugin string }{p}, pluginFullFilePath)

// Test file.
pluginTestFile := strings.ToLower(p) + "_gen_test.go"
pluginFullTestFilePath := filepath.Join(getScriptPath(), "..", "..", "pkg", "analyse", pluginTestFile)
if err := os.Remove(pluginTestFile); err != nil && !os.IsNotExist(err) {
log.Fatalln(err)
}

templateToFile(tmplTestPath, struct{ Plugin string }{p}, pluginFullTestFilePath)
}
}
46 changes: 0 additions & 46 deletions cmd/gen/connectionplugin.go

This file was deleted.

27 changes: 0 additions & 27 deletions cmd/gen/factplugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,8 @@ package gen
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
)

func FactPlugin(plugins []string, pkg string, envResolver bool) {
log.Printf("Generating fact plugin funcs for package %s: %s\n", pkg, strings.Join(plugins, ","))

tmplPath := filepath.Join("..", "..", "pkg", "fact", "templates", "factplugin.go.tmpl")

for _, p := range plugins {
pluginFile := strings.ToLower(p) + "_gen.go"
pluginDir := filepath.Join(getScriptPath(), "..", "..", "pkg", "fact")
if pkg != "fact" {
pluginDir = filepath.Join(pluginDir, pkg)
}
pluginFullFilePath := filepath.Join(pluginDir, pluginFile)
if err := os.Remove(pluginFullFilePath); err != nil && !os.IsNotExist(err) {
log.Fatalln(err)
}

templateToFile(tmplPath, struct {
Package string
Plugin string
EnvResolver bool
}{Package: pkg, Plugin: p, EnvResolver: envResolver}, pluginFullFilePath)
}
}

// FactRegistry adds the Facters for a package to the registry.
func FactRegistry(pkg string) {
log.Println("Updating Fact plugins registry - adding", pkg)
Expand Down
3 changes: 2 additions & 1 deletion examples/regex-not-match.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ collect:
yaml:key:
input: extension-file
path: module.lagoon_logs
ignore-not-found: true

analyse:
lagoon-logs-check:
regex:not-match:
description: Lagoon logs module is not enabled
input: module-lagoon_logs
pattern: "^1$"
pattern: "^0$"
20 changes: 5 additions & 15 deletions pkg/analyse/allowedlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,11 @@ import (

"github.com/salsadigitalauorg/shipshape/pkg/breach"
"github.com/salsadigitalauorg/shipshape/pkg/data"
"github.com/salsadigitalauorg/shipshape/pkg/fact"
"github.com/salsadigitalauorg/shipshape/pkg/result"
"github.com/salsadigitalauorg/shipshape/pkg/utils"
)

type AllowedList struct {
// Common fields.
Id string `yaml:"name"`
Description string `yaml:"description"`
InputName string `yaml:"input"`
Severity string `yaml:"severity"`
breach.BreachTemplate `yaml:"breach-format"`
Result result.Result
Remediation interface{} `yaml:"remediation"`
input fact.Facter

// Plugin fields.
BaseAnalyser `yaml:",inline"`
PackageMatch string `yaml:"package-match"`
pkgRegex *regexp.Regexp
Allowed []string `yaml:"allowed"`
Expand All @@ -36,10 +24,12 @@ type AllowedList struct {
//go:generate go run ../../cmd/gen.go analyse-plugin --plugin=AllowedList --package=analyse

func init() {
Registry["allowed:list"] = func(id string) Analyser { return NewAllowedList(id) }
Manager().RegisterFactory("allowed:list", func(id string) Analyser {
return NewAllowedList(id)
})
}

func (p *AllowedList) PluginName() string {
func (p *AllowedList) GetName() string {
return "allowed:list"
}

Expand Down
Loading

0 comments on commit 3d1aed7

Please sign in to comment.