Skip to content

Commit

Permalink
Use placeholder as default if field left empty and is required (truff…
Browse files Browse the repository at this point in the history
…lesecurity#1642)

* Use placeholder as default if field left empty and is required

Co-authored-by: mcastorina <[email protected]>

* Drop unused func

* uncomment azure source

* update wording

---------

Co-authored-by: mcastorina <[email protected]>
  • Loading branch information
2 people authored and Phoenix591 committed Oct 27, 2023
1 parent 7ae86e9 commit 498d9a0
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 53 deletions.
7 changes: 4 additions & 3 deletions pkg/tui/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strings"

"github.com/muesli/reflow/truncate"
"github.com/trufflesecurity/trufflehog/v3/pkg/tui/components/textinputs"
)

// TruncateString is a convenient wrapper around truncate.TruncateString.
Expand All @@ -14,11 +15,11 @@ func TruncateString(s string, max int) string {
return truncate.StringWithTail(s, uint(max), "…")
}

func SummarizeSource(keys []string, inputs map[string]string, labels map[string]string) string {
func SummarizeSource(keys []string, inputs map[string]textinputs.Input, labels map[string]string) string {
summary := strings.Builder{}
for _, key := range keys {
if inputs[key] != "" {
summary.WriteString("\t" + labels[key] + ": " + inputs[key] + "\n")
if inputs[key].Value != "" {
summary.WriteString("\t" + labels[key] + ": " + inputs[key].Value + "\n")
}
}

Expand Down
17 changes: 14 additions & 3 deletions pkg/tui/components/textinputs/textinputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,22 @@ type InputConfig struct {
Placeholder string
}

func (m Model) GetInputs() map[string]string {
inputs := make(map[string]string)
type Input struct {
Value string
IsDefault bool
}

func (m Model) GetInputs() map[string]Input {
inputs := make(map[string]Input)

for i, input := range m.inputs {
inputs[m.configs[i].Key] = input.Value()
isDefault := false
value := input.Value()
if value == "" && m.configs[i].Required {
isDefault = true
value = input.Placeholder
}
inputs[m.configs[i].Key] = Input{Value: value, IsDefault: isDefault}
}

return inputs
Expand Down
21 changes: 11 additions & 10 deletions pkg/tui/pages/source_configure/trufflehog_configure.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package source_configure

import (
"runtime"
"strconv"
"strings"

Expand Down Expand Up @@ -49,7 +50,7 @@ func GetTrufflehogConfiguration() truffleCmdModel {
Key: "concurrency",
Required: false,
Help: "Number of concurrent workers.",
Placeholder: "1",
Placeholder: strconv.Itoa(runtime.NumCPU()),
}

return truffleCmdModel{textinputs.New([]textinputs.InputConfig{jsonOutput, verification, verifiedResults, excludeDetectors, concurrency}).SetSkip(true)}
Expand All @@ -59,25 +60,25 @@ func (m truffleCmdModel) Cmd() string {
var command []string
inputs := m.GetInputs()

if isTrue(inputs["json"]) {
if isTrue(inputs["json"].Value) {
command = append(command, "--json")
}

if isTrue(inputs["no-verification"]) {
if isTrue(inputs["no-verification"].Value) {
command = append(command, "--no-verification")
}

if isTrue(inputs["only-verified"]) {
if isTrue(inputs["only-verified"].Value) {
command = append(command, "--only-verified")
}

if inputs["exclude_detectors"] != "" {
cmd := "--exclude-detectors=" + strings.ReplaceAll(inputs["exclude_detectors"], " ", "")
if inputs["exclude_detectors"].Value != "" {
cmd := "--exclude-detectors=" + strings.ReplaceAll(inputs["exclude_detectors"].Value, " ", "")
command = append(command, cmd)
}

if inputs["concurrency"] != "" {
command = append(command, "--concurrency="+inputs["concurrency"])
if inputs["concurrency"].Value != "" {
command = append(command, "--concurrency="+inputs["concurrency"].Value)
}

return strings.Join(command, " ")
Expand All @@ -90,8 +91,8 @@ func (m truffleCmdModel) Summary() string {
inputs := m.GetInputs()
labels := m.GetLabels()
for _, key := range keys {
if inputs[key] != "" {
summary.WriteString("\t" + labels[key] + ": " + inputs[key] + "\n")
if inputs[key].Value != "" {
summary.WriteString("\t" + labels[key] + ": " + inputs[key].Value + "\n")
}
}

Expand Down
1 change: 1 addition & 0 deletions pkg/tui/pages/source_select/source_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func New(c common.Common) *SourceSelect {
OssItem("GCS (Google Cloud Storage)", "Scan a Google Cloud Storage instance."),
// Enterprise sources.
EnterpriseItem("Artifactory", "Scan JFrog Artifactory packages."),
EnterpriseItem("Azure Repos", "Scan Microsoft Azure repositories."),
EnterpriseItem("BitBucket", "Scan Atlassian's Git-based source code repository hosting service."),
EnterpriseItem("Buildkite", "Scan Buildkite, a CI/CD platform."),
EnterpriseItem("Confluence", "Scan Atlassian's web-based wiki and knowledge base."),
Expand Down
5 changes: 1 addition & 4 deletions pkg/tui/sources/circleci/circleci.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ func (m circleCiCmdModel) Cmd() string {
command = append(command, "trufflehog", "circleci")

inputs := m.GetInputs()

if inputs["token"] != "" {
command = append(command, "--token="+inputs["token"])
}
command = append(command, "--token="+inputs["token"].Value)

return strings.Join(command, " ")
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/tui/sources/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ func (m dockerCmdModel) Cmd() string {
command = append(command, "trufflehog", "docker")

inputs := m.GetInputs()
vals := inputs["images"]
vals := inputs["images"].Value

if vals != "" {
images := strings.Fields(vals)
for _, image := range images {
Expand Down
5 changes: 1 addition & 4 deletions pkg/tui/sources/filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ func (m fsModel) Cmd() string {
command = append(command, "trufflehog", "filesystem")

inputs := m.GetInputs()

if inputs["path"] != "" {
command = append(command, inputs["path"])
}
command = append(command, inputs["path"].Value)

return strings.Join(command, " ")
}
Expand Down
11 changes: 5 additions & 6 deletions pkg/tui/sources/gcs/gcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ type gcsCmdModel struct {
func GetFields() gcsCmdModel {
projectId := textinputs.InputConfig{
Label: "Project ID",
Key: "project_id",
Key: "project-id",
Required: true,
Placeholder: "my-project",
Placeholder: "trufflehog-testing",
}

return gcsCmdModel{textinputs.New([]textinputs.InputConfig{projectId})}
Expand All @@ -27,9 +27,8 @@ func (m gcsCmdModel) Cmd() string {
command = append(command, "trufflehog", "gcs")

inputs := m.GetInputs()
if inputs["project_id"] != "" {
command = append(command, "--project_id="+inputs["project_id"])
}

command = append(command, "--project-id="+inputs["project-id"].Value)

command = append(command, "--cloud-environment")
return strings.Join(command, " ")
Expand All @@ -39,6 +38,6 @@ func (m gcsCmdModel) Summary() string {
inputs := m.GetInputs()
labels := m.GetLabels()

keys := []string{"project_id"}
keys := []string{"project-id"}
return common.SummarizeSource(keys, inputs, labels)
}
7 changes: 3 additions & 4 deletions pkg/tui/sources/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ func GetFields() gitCmdModel {
uri := textinputs.InputConfig{
Label: "Git URI",
Key: "uri",
Help: "file:// for local git repos",
Required: true,
Placeholder: "[email protected]:trufflesecurity/trufflehog.git.",
Placeholder: "[email protected]:trufflesecurity/trufflehog.git",
}

return gitCmdModel{textinputs.New([]textinputs.InputConfig{uri})}
Expand All @@ -28,9 +29,7 @@ func (m gitCmdModel) Cmd() string {

inputs := m.GetInputs()

if inputs["uri"] != "" {
command = append(command, inputs["uri"])
}
command = append(command, inputs["uri"].Value)

return strings.Join(command, " ")
}
Expand Down
32 changes: 23 additions & 9 deletions pkg/tui/sources/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,55 @@ func GetFields() githubCmdModel {
org := textinputs.InputConfig{
Label: "Organization",
Key: "org",
Required: false,
Required: true,
Help: "GitHub organization to scan.",
Placeholder: "trufflesecurity",
}

repo := textinputs.InputConfig{
Label: "Repository",
Key: "repo",
Required: false,
Required: true,
Help: "GitHub repo to scan.",
Placeholder: "https://github.com/trufflesecurity/test_keys",
}

return githubCmdModel{textinputs.New([]textinputs.InputConfig{org, repo})}
}

// Handle default values since GitHub flags are OR operations
func (m githubCmdModel) GetSpecialInputs() map[string]textinputs.Input {
inputs := m.GetInputs()
if inputs["org"].IsDefault != inputs["repo"].IsDefault {
if inputs["org"].IsDefault {
delete(inputs, "org")
}
if inputs["repo"].IsDefault {
delete(inputs, "repo")
}
}

return inputs
}

func (m githubCmdModel) Cmd() string {
var command []string
command = append(command, "trufflehog", "github")
inputs := m.GetSpecialInputs()

inputs := m.GetInputs()

if inputs["org"] != "" {
command = append(command, "--org="+inputs["org"])
if inputs["org"].Value != "" {
command = append(command, "--org="+inputs["org"].Value)
}

if inputs["repo"] != "" {
command = append(command, "--repo="+inputs["repo"])
if inputs["repo"].Value != "" {
command = append(command, "--repo="+inputs["repo"].Value)
}

return strings.Join(command, " ")
}

func (m githubCmdModel) Summary() string {
inputs := m.GetInputs()
inputs := m.GetSpecialInputs()
labels := m.GetLabels()

keys := []string{"org", "repo"}
Expand Down
4 changes: 1 addition & 3 deletions pkg/tui/sources/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ func (m gitlabCmdModel) Cmd() string {

inputs := m.GetInputs()

if inputs["token"] != "" {
command = append(command, "--token="+inputs["token"])
}
command = append(command, "--token="+inputs["token"].Value)

return strings.Join(command, " ")
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/tui/sources/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func GetFields() s3CmdModel {
Label: "S3 bucket name(s)",
Key: "buckets",
Required: true,
Placeholder: "my-bucket-name",
Placeholder: "truffletestbucket",
Help: "Buckets to scan. Separate by space if multiple.",
}

Expand All @@ -28,7 +28,7 @@ func (m s3CmdModel) Cmd() string {
command = append(command, "trufflehog", "s3")

inputs := m.GetInputs()
vals := inputs["buckets"]
vals := inputs["buckets"].Value
if vals != "" {
buckets := strings.Fields(vals)
for _, bucket := range buckets {
Expand Down
6 changes: 2 additions & 4 deletions pkg/tui/sources/syslog/syslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,8 @@ func (m syslogCmdModel) Cmd() string {
syslogKeys := [5]string{"address", "protocol", "cert", "key", "format"}

for _, key := range syslogKeys {
if inputs[key] != "" {
flag := "--" + key + "=" + inputs[key]
command = append(command, flag)
}
flag := "--" + key + "=" + inputs[key].Value
command = append(command, flag)
}

return strings.Join(command, " ")
Expand Down

0 comments on commit 498d9a0

Please sign in to comment.