-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #129 from debricked/fingerprinting
File fingerprinting and save to file
- Loading branch information
Showing
24 changed files
with
898 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package fingerprint | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
|
||
"github.com/debricked/cli/internal/file" | ||
"github.com/debricked/cli/internal/fingerprint" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var exclusions = file.DefaultExclusionsFingerprint() | ||
|
||
const ( | ||
ExclusionFlag = "exclusion-fingerprint" | ||
) | ||
|
||
func NewFingerprintCmd(fingerprinter fingerprint.IFingerprint) *cobra.Command { | ||
|
||
short := "Fingerprints files to match against the Debricked knowledge base. [beta feature]" | ||
long := fmt.Sprintf("Fingerprint files for identification in a given path and writes it to %s. [beta feature]\nThis hashes all files and matches them against the Debricked knowledge base.", fingerprint.OutputFileNameFingerprints) | ||
cmd := &cobra.Command{ | ||
Use: "fingerprint [path]", | ||
Short: short, | ||
Hidden: true, | ||
Long: long, | ||
PreRun: func(cmd *cobra.Command, _ []string) { | ||
_ = viper.BindPFlags(cmd.Flags()) | ||
}, | ||
RunE: RunE(fingerprinter), | ||
} | ||
fileExclusionExample := filepath.Join("*", "**.pyc") | ||
dirExclusionExample := filepath.Join("**", "node_modules", "**") | ||
exampleFlags := fmt.Sprintf("-%s \"%s\" -%s \"%s\"", ExclusionFlag, fileExclusionExample, ExclusionFlag, dirExclusionExample) | ||
cmd.Flags().StringArrayVarP(&exclusions, ExclusionFlag, "", exclusions, `The following terms are supported to exclude paths: | ||
Special Terms | Meaning | ||
------------- | ------- | ||
"*" | matches any sequence of non-Separator characters | ||
"/**/" | matches zero or multiple directories | ||
"?" | matches any single non-Separator character | ||
"[class]" | matches any single non-Separator character against a class of characters ([see "character classes"]) | ||
"{alt1,...}" | matches a sequence of characters if one of the comma-separated alternatives matches | ||
Example: | ||
$ debricked files fingerprint . `+exampleFlags) | ||
|
||
viper.MustBindEnv(ExclusionFlag) | ||
|
||
return cmd | ||
} | ||
|
||
func RunE(f fingerprint.IFingerprint) func(_ *cobra.Command, args []string) error { | ||
return func(_ *cobra.Command, args []string) error { | ||
path := "" | ||
if len(args) > 0 { | ||
path = args[0] | ||
} | ||
|
||
output, err := f.FingerprintFiles(path, exclusions) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
err = output.ToFile(fingerprint.OutputFileNameFingerprints) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package fingerprint | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/debricked/cli/internal/fingerprint" | ||
"github.com/debricked/cli/internal/fingerprint/testdata" | ||
"github.com/spf13/viper" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewFingerprintCmd(t *testing.T) { | ||
var f fingerprint.IFingerprint | ||
cmd := NewFingerprintCmd(f) | ||
|
||
commands := cmd.Commands() | ||
nbrOfCommands := 0 | ||
assert.Len(t, commands, nbrOfCommands) | ||
|
||
flags := cmd.Flags() | ||
flagAssertions := map[string]string{} | ||
for name, shorthand := range flagAssertions { | ||
flag := flags.Lookup(name) | ||
assert.NotNil(t, flag) | ||
assert.Equal(t, shorthand, flag.Shorthand) | ||
} | ||
|
||
var flagKeys = []string{ | ||
ExclusionFlag, | ||
} | ||
viperKeys := viper.AllKeys() | ||
for _, flagKey := range flagKeys { | ||
match := false | ||
for _, key := range viperKeys { | ||
if key == flagKey { | ||
match = true | ||
} | ||
} | ||
assert.Truef(t, match, "failed to assert that flag was present: "+flagKey) | ||
} | ||
|
||
} | ||
|
||
func TestRunE(t *testing.T) { | ||
defer func() { | ||
os.Remove(fingerprint.OutputFileNameFingerprints) | ||
}() | ||
fingerprintMock := testdata.NewFingerprintMock() | ||
runE := RunE(fingerprintMock) | ||
|
||
err := runE(nil, []string{"."}) | ||
|
||
assert.NoError(t, err) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.