Skip to content

Commit

Permalink
Initial regex command files with table output
Browse files Browse the repository at this point in the history
  • Loading branch information
santoshkal committed Oct 1, 2024
1 parent f626ea5 commit ce38f9a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
14 changes: 7 additions & 7 deletions cmd/regx.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package cmd

import (
"fmt"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -49,12 +47,14 @@ func runRegxSearchCmd(cmd *cobra.Command, args []string) error {
// Perform the regex validation on the resource file
isPass := regx.ScanResourceFile(inputFile, patternConfig.Spec.Pattern)

// Print result: Pass/Fail
if isPass {
fmt.Printf("Pass: No sensitive information found in resource: %v\n", inputFile)
} else {
fmt.Printf("Fail: Sensitive information found in resource: %v\n", inputFile)
// Determine the result and print the table
result := "Pass"
if !isPass {
result = "Fail: Sensitive information found."
}

// Print the metadata and result in a table
regx.PrintResultTable(patternConfig.Metadata, result)

return nil
}
27 changes: 24 additions & 3 deletions pkg/regx/regx.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package regx
import (
"fmt"
"log"
"os"
"regexp"

"github.com/jedib0t/go-pretty/v6/table"
"gopkg.in/yaml.v2"

"github.com/intelops/genval/pkg/utils"
Expand All @@ -19,8 +21,12 @@ type PatternConfig struct {
} `yaml:"spec"`
}

// Metadata contains the details from the policy metadata section
type Metadata struct {
Name string `yaml:"name"`
Name string `yaml:"name"`
Description string `yaml:"description"`
Severity string `yaml:"severity"`
Benchmark string `yaml:"benchmark"`
}

// readYAML reads and parses a YAML file into the provided target structure
Expand Down Expand Up @@ -57,9 +63,24 @@ func ScanResourceFile(resourcePath string, patterns []string) bool {

fmt.Printf("Scanning file: %s\n", resourcePath)
if scanForPattern(string(content), patterns) {
fmt.Printf("The resource [%v] contains sensitive pattern: %v", resourcePath, patterns)
return false // Sensitive pattern found
}
fmt.Printf("No sensitive patterns found in resource: [%v]", resourcePath)
return true // No sensitive patterns found
}

// PrintResultTable prints the metadata and result in a formatted table
func PrintResultTable(metadata Metadata, result string) {
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)

t.AppendHeader(table.Row{"Name", "Description", "Severity", "Benchmark", "Result"})
t.AppendRow([]interface{}{
metadata.Name,
metadata.Description,
metadata.Severity,
metadata.Benchmark,
result,
})

t.Render()
}
10 changes: 10 additions & 0 deletions templates/defaultpolicies/regex/policy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: genval/v1beta1
metadata:
name: test-pattern
description: Checks for sensitive information in the file
severity: Critical
benchmark: xyz
spec:
pattern:
- "password123"
- "token[:=]\\s*['\"]?[a-zA-Z0-9]+['\"]?"

0 comments on commit ce38f9a

Please sign in to comment.