From 8cbb84c50f2d6ddb10c9e48f71c247cc321c7af3 Mon Sep 17 00:00:00 2001 From: zach rice Date: Sun, 17 Nov 2019 11:56:16 -0600 Subject: [PATCH] fixing tests --- config/default.go | 1 - manager/manager_test.go | 20 +- test_data/test_local_owner_aws_leak.json | 377 ------------------ ...local_repo_one_aws_leak_and_file_leak.json | 13 + test_data/test_local_repo_three_leaks.json | 13 - test_data/test_local_repo_two_leaks.json | 13 - 6 files changed, 32 insertions(+), 405 deletions(-) diff --git a/config/default.go b/config/default.go index 26aeab46..614b1b88 100644 --- a/config/default.go +++ b/config/default.go @@ -71,7 +71,6 @@ title = "gitleaks config" regex = '''(?i)(api_key|apikey|secret)(.{0,20})?['|"][0-9a-zA-Z]{16,45}['|"]''' tags = ["key", "API", "generic"] - [[rules]] description = "Google API key" regex = '''AIza[0-9A-Za-z\\-_]{35}''' diff --git a/manager/manager_test.go b/manager/manager_test.go index 5d55d0a1..d29d0441 100644 --- a/manager/manager_test.go +++ b/manager/manager_test.go @@ -1,8 +1,11 @@ package manager import ( + "crypto/rand" + "fmt" "github.com/zricethezav/gitleaks/config" "github.com/zricethezav/gitleaks/options" + "io" "testing" ) @@ -28,7 +31,10 @@ func TestSendReceiveLeaks(t *testing.T) { m, _ := NewManager(opts, cfg) for i := 0; i < test.leaksToAdd; i++ { - m.SendLeaks(Leak{}) + // we are testing the sync of sending/receiving leaks so we need + // the hash generation in sendLeaks to be unique for each iteration + // so I'm just setting the offender string as a uuid + m.SendLeaks(Leak{Offender: newUUID()}) } got := m.GetLeaks() if len(got) != test.leaksToAdd { @@ -86,3 +92,15 @@ func TestSendReceiveMeta(t *testing.T) { } } } + +// newUUID generates a random UUID according to RFC 4122 +// Ripped from https://play.golang.org/p/4FkNSiUDMg +func newUUID() string { + uuid := make([]byte, 16) + io.ReadFull(rand.Reader, uuid) + // variant bits; see section 4.1.1 + uuid[8] = uuid[8]&^0xc0 | 0x80 + // version 4 (pseudo-random); see section 4.1.3 + uuid[6] = uuid[6]&^0xf0 | 0x40 + return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:]) +} diff --git a/test_data/test_local_owner_aws_leak.json b/test_data/test_local_owner_aws_leak.json index 030ca8c6..24e905cc 100644 --- a/test_data/test_local_owner_aws_leak.json +++ b/test_data/test_local_owner_aws_leak.json @@ -116,19 +116,6 @@ "date": "2019-10-25T13:01:27-04:00", "tags": "key, API, generic" }, - { - "line": "Here's an AWS secret: AKIALALEMEL33243OLIAE", - "offender": "AKIALALEMEL33243OLIA", - "commit": "17471a5fda722a9e423f1a0d3f0d267ea009d41c", - "repo": "test_repo_2", - "rule": "AWS Manager ID", - "commitMessage": "wait this is actually adding an aws secret\n", - "author": "zach rice", - "email": "zricer@protonmail.com", - "file": "secrets.md", - "date": "2019-10-25T13:01:27-04:00", - "tags": "key, AWS" - }, { "line": "\nHere's an AWS secret: AKIALALEMEL33243OLIAE", "offender": "AKIALALEMEL33243OLIA", @@ -245,369 +232,5 @@ "file": "secrets.md", "date": "2019-10-25T13:35:03-04:00", "tags": "key, API, generic" - }, - { - "line": " const AWSKEY = \"AKIALALEMEL33243OLIBE\"", - "offender": "AKIALALEMEL33243OLIB", - "commit": "f61cd8587b7ac1d75a89a0c9af870a2f24c60263", - "repo": "test_repo_3", - "rule": "AWS Manager ID", - "commitMessage": "rm secrets again\n", - "author": "zach rice", - "email": "zricer@protonmail.com", - "file": "secrets.md", - "date": "2019-10-25T13:12:32-04:00", - "tags": "key, AWS" - }, - { - "line": " const AWSSECRET = \"99432bfewaf823ec3294e231\"", - "offender": "SECRET = \"99432bfewaf823ec3294e231\"", - "commit": "f61cd8587b7ac1d75a89a0c9af870a2f24c60263", - "repo": "test_repo_3", - "rule": "Generic Credential", - "commitMessage": "rm secrets again\n", - "author": "zach rice", - "email": "zricer@protonmail.com", - "file": "secrets.md", - "date": "2019-10-25T13:12:32-04:00", - "tags": "key, API, generic" - }, - { - "line": " const AWSKEY = \"AKIALALEMEL33243OLIBE\"", - "offender": "AKIALALEMEL33243OLIB", - "commit": "b2eb34a61c988afd9b4aaa9dd58c8dd7d5f14dba", - "repo": "test_repo_3", - "rule": "AWS Manager ID", - "commitMessage": "adding another one\n", - "author": "zach rice", - "email": "zricer@protonmail.com", - "file": "secrets.md", - "date": "2019-10-25T13:12:08-04:00", - "tags": "key, AWS" - }, - { - "line": " const AWSSECRET = \"99432bfewaf823ec3294e231\"", - "offender": "SECRET = \"99432bfewaf823ec3294e231\"", - "commit": "b2eb34a61c988afd9b4aaa9dd58c8dd7d5f14dba", - "repo": "test_repo_3", - "rule": "Generic Credential", - "commitMessage": "adding another one\n", - "author": "zach rice", - "email": "zricer@protonmail.com", - "file": "secrets.md", - "date": "2019-10-25T13:12:08-04:00", - "tags": "key, API, generic" - }, - { - "line": "Here's an AWS secret: \"AKIALALEMEL33243OLIAE\"", - "offender": "AKIALALEMEL33243OLIA", - "commit": "996865bb912f3bc45898a370a13aadb315014b55", - "repo": "test_repo_3", - "rule": "AWS Manager ID", - "commitMessage": "committing pem\n", - "author": "zach rice", - "email": "zricer@protonmail.com", - "file": "secrets.md", - "date": "2019-10-25T13:07:41-04:00", - "tags": "key, AWS" - }, - { - "line": "Here's an AWS secret: \"AKIALALEMEL33243OLIAE\"", - "offender": "secret: \"AKIALALEMEL33243OLIAE\"", - "commit": "996865bb912f3bc45898a370a13aadb315014b55", - "repo": "test_repo_3", - "rule": "Generic Credential", - "commitMessage": "committing pem\n", - "author": "zach rice", - "email": "zricer@protonmail.com", - "file": "secrets.md", - "date": "2019-10-25T13:07:41-04:00", - "tags": "key, API, generic" - }, - { - "line": "Here's an AWS secret: \"AKIALALEMEL33243OLIAE\"", - "offender": "AKIALALEMEL33243OLIA", - "commit": "17471a5fda722a9e423f1a0d3f0d267ea009d41c", - "repo": "test_repo_3", - "rule": "AWS Manager ID", - "commitMessage": "wait this is actually adding an aws secret\n", - "author": "zach rice", - "email": "zricer@protonmail.com", - "file": "secrets.md", - "date": "2019-10-25T13:01:27-04:00", - "tags": "key, AWS" - }, - { - "line": "Here's an AWS secret: \"AKIALALEMEL33243OLIAE\"", - "offender": "secret: \"AKIALALEMEL33243OLIAE\"", - "commit": "17471a5fda722a9e423f1a0d3f0d267ea009d41c", - "repo": "test_repo_3", - "rule": "Generic Credential", - "commitMessage": "wait this is actually adding an aws secret\n", - "author": "zach rice", - "email": "zricer@protonmail.com", - "file": "secrets.md", - "date": "2019-10-25T13:01:27-04:00", - "tags": "key, API, generic" - }, - { - "line": "Here's an AWS secret: AKIALALEMEL33243OLIAE", - "offender": "AKIALALEMEL33243OLIA", - "commit": "17471a5fda722a9e423f1a0d3f0d267ea009d41c", - "repo": "test_repo_3", - "rule": "AWS Manager ID", - "commitMessage": "wait this is actually adding an aws secret\n", - "author": "zach rice", - "email": "zricer@protonmail.com", - "file": "secrets.md", - "date": "2019-10-25T13:01:27-04:00", - "tags": "key, AWS" - }, - { - "line": "\nHere's an AWS secret: AKIALALEMEL33243OLIAE", - "offender": "AKIALALEMEL33243OLIA", - "commit": "b10b3e2cb320a8c211fda94c4567299d37de7776", - "repo": "test_repo_3", - "rule": "AWS Manager ID", - "commitMessage": "adding aws key\n", - "author": "zach rice", - "email": "zricer@protonmail.com", - "file": "secrets.md", - "date": "2019-10-25T12:58:39-04:00", - "tags": "key, AWS" - }, - { - "line": "const AWSKEY = \"AKIALALEMEL33243OLIAE\"", - "offender": "AKIALALEMEL33243OLIA", - "commit": "cd5eb8bef855f73c46b97b4c088badffdc40ebe9", - "repo": "test_repo_4", - "rule": "AWS Manager ID", - "commitMessage": "rm secrets\n", - "author": "zach rice", - "email": "zricer@protonmail.com", - "file": "secrets.md", - "date": "2019-10-25T13:54:26-04:00", - "tags": "key, AWS" - }, - { - "line": "const AWSSECRET = \"99432bfewaf823ec3294e231\"", - "offender": "SECRET = \"99432bfewaf823ec3294e231\"", - "commit": "cd5eb8bef855f73c46b97b4c088badffdc40ebe9", - "repo": "test_repo_4", - "rule": "Generic Credential", - "commitMessage": "rm secrets\n", - "author": "zach rice", - "email": "zricer@protonmail.com", - "file": "secrets.md", - "date": "2019-10-25T13:54:26-04:00", - "tags": "key, API, generic" - }, - { - "line": "const AWSKEY = \"AKIALALEMEL33243OLIAE\"", - "offender": "AKIALALEMEL33243OLIA", - "commit": "84ac4e80d4dbf2c968b64e9d4005f5079795bb81", - "repo": "test_repo_4", - "rule": "AWS Manager ID", - "commitMessage": "more secrets\n", - "author": "zach rice", - "email": "zricer@protonmail.com", - "file": "secrets.md", - "date": "2019-10-25T13:54:08-04:00", - "tags": "key, AWS" - }, - { - "line": "const AWSSECRET = \"99432bfewaf823ec3294e231\"", - "offender": "SECRET = \"99432bfewaf823ec3294e231\"", - "commit": "84ac4e80d4dbf2c968b64e9d4005f5079795bb81", - "repo": "test_repo_4", - "rule": "Generic Credential", - "commitMessage": "more secrets\n", - "author": "zach rice", - "email": "zricer@protonmail.com", - "file": "secrets.md", - "date": "2019-10-25T13:54:08-04:00", - "tags": "key, API, generic" - }, - { - "line": "AWS secret: \"AKIALALEMEL33243OLIAE\"", - "offender": "AKIALALEMEL33243OLIA", - "commit": "64cfcee9aad1c84581631636bfc54f2050718d1a", - "repo": "test_repo_4", - "rule": "AWS Manager ID", - "commitMessage": "rm secrets\n", - "author": "zach rice", - "email": "zricer@protonmail.com", - "file": "secrets.md", - "date": "2019-10-25T13:36:22-04:00", - "tags": "key, AWS" - }, - { - "line": "AWS secret: \"AKIALALEMEL33243OLIAE\"", - "offender": "secret: \"AKIALALEMEL33243OLIAE\"", - "commit": "64cfcee9aad1c84581631636bfc54f2050718d1a", - "repo": "test_repo_4", - "rule": "Generic Credential", - "commitMessage": "rm secrets\n", - "author": "zach rice", - "email": "zricer@protonmail.com", - "file": "secrets.md", - "date": "2019-10-25T13:36:22-04:00", - "tags": "key, API, generic" - }, - { - "line": "AWS secret: \"AKIALALEMEL33243OLIAE\"", - "offender": "AKIALALEMEL33243OLIA", - "commit": "deea550dd6c7acaf0e59432600593533984a2125", - "repo": "test_repo_4", - "rule": "AWS Manager ID", - "commitMessage": "dev branch\n", - "author": "zach rice", - "email": "zricer@protonmail.com", - "file": "secrets.md", - "date": "2019-10-25T13:35:03-04:00", - "tags": "key, AWS" - }, - { - "line": "AWS secret: \"AKIALALEMEL33243OLIAE\"", - "offender": "secret: \"AKIALALEMEL33243OLIAE\"", - "commit": "deea550dd6c7acaf0e59432600593533984a2125", - "repo": "test_repo_4", - "rule": "Generic Credential", - "commitMessage": "dev branch\n", - "author": "zach rice", - "email": "zricer@protonmail.com", - "file": "secrets.md", - "date": "2019-10-25T13:35:03-04:00", - "tags": "key, API, generic" - }, - { - "line": " const AWSKEY = \"AKIALALEMEL33243OLIBE\"", - "offender": "AKIALALEMEL33243OLIB", - "commit": "f61cd8587b7ac1d75a89a0c9af870a2f24c60263", - "repo": "test_repo_4", - "rule": "AWS Manager ID", - "commitMessage": "rm secrets again\n", - "author": "zach rice", - "email": "zricer@protonmail.com", - "file": "secrets.md", - "date": "2019-10-25T13:12:32-04:00", - "tags": "key, AWS" - }, - { - "line": " const AWSSECRET = \"99432bfewaf823ec3294e231\"", - "offender": "SECRET = \"99432bfewaf823ec3294e231\"", - "commit": "f61cd8587b7ac1d75a89a0c9af870a2f24c60263", - "repo": "test_repo_4", - "rule": "Generic Credential", - "commitMessage": "rm secrets again\n", - "author": "zach rice", - "email": "zricer@protonmail.com", - "file": "secrets.md", - "date": "2019-10-25T13:12:32-04:00", - "tags": "key, API, generic" - }, - { - "line": " const AWSKEY = \"AKIALALEMEL33243OLIBE\"", - "offender": "AKIALALEMEL33243OLIB", - "commit": "b2eb34a61c988afd9b4aaa9dd58c8dd7d5f14dba", - "repo": "test_repo_4", - "rule": "AWS Manager ID", - "commitMessage": "adding another one\n", - "author": "zach rice", - "email": "zricer@protonmail.com", - "file": "secrets.md", - "date": "2019-10-25T13:12:08-04:00", - "tags": "key, AWS" - }, - { - "line": " const AWSSECRET = \"99432bfewaf823ec3294e231\"", - "offender": "SECRET = \"99432bfewaf823ec3294e231\"", - "commit": "b2eb34a61c988afd9b4aaa9dd58c8dd7d5f14dba", - "repo": "test_repo_4", - "rule": "Generic Credential", - "commitMessage": "adding another one\n", - "author": "zach rice", - "email": "zricer@protonmail.com", - "file": "secrets.md", - "date": "2019-10-25T13:12:08-04:00", - "tags": "key, API, generic" - }, - { - "line": "Here's an AWS secret: \"AKIALALEMEL33243OLIAE\"", - "offender": "AKIALALEMEL33243OLIA", - "commit": "996865bb912f3bc45898a370a13aadb315014b55", - "repo": "test_repo_4", - "rule": "AWS Manager ID", - "commitMessage": "committing pem\n", - "author": "zach rice", - "email": "zricer@protonmail.com", - "file": "secrets.md", - "date": "2019-10-25T13:07:41-04:00", - "tags": "key, AWS" - }, - { - "line": "Here's an AWS secret: \"AKIALALEMEL33243OLIAE\"", - "offender": "secret: \"AKIALALEMEL33243OLIAE\"", - "commit": "996865bb912f3bc45898a370a13aadb315014b55", - "repo": "test_repo_4", - "rule": "Generic Credential", - "commitMessage": "committing pem\n", - "author": "zach rice", - "email": "zricer@protonmail.com", - "file": "secrets.md", - "date": "2019-10-25T13:07:41-04:00", - "tags": "key, API, generic" - }, - { - "line": "Here's an AWS secret: \"AKIALALEMEL33243OLIAE\"", - "offender": "AKIALALEMEL33243OLIA", - "commit": "17471a5fda722a9e423f1a0d3f0d267ea009d41c", - "repo": "test_repo_4", - "rule": "AWS Manager ID", - "commitMessage": "wait this is actually adding an aws secret\n", - "author": "zach rice", - "email": "zricer@protonmail.com", - "file": "secrets.md", - "date": "2019-10-25T13:01:27-04:00", - "tags": "key, AWS" - }, - { - "line": "Here's an AWS secret: \"AKIALALEMEL33243OLIAE\"", - "offender": "secret: \"AKIALALEMEL33243OLIAE\"", - "commit": "17471a5fda722a9e423f1a0d3f0d267ea009d41c", - "repo": "test_repo_4", - "rule": "Generic Credential", - "commitMessage": "wait this is actually adding an aws secret\n", - "author": "zach rice", - "email": "zricer@protonmail.com", - "file": "secrets.md", - "date": "2019-10-25T13:01:27-04:00", - "tags": "key, API, generic" - }, - { - "line": "Here's an AWS secret: AKIALALEMEL33243OLIAE", - "offender": "AKIALALEMEL33243OLIA", - "commit": "17471a5fda722a9e423f1a0d3f0d267ea009d41c", - "repo": "test_repo_4", - "rule": "AWS Manager ID", - "commitMessage": "wait this is actually adding an aws secret\n", - "author": "zach rice", - "email": "zricer@protonmail.com", - "file": "secrets.md", - "date": "2019-10-25T13:01:27-04:00", - "tags": "key, AWS" - }, - { - "line": "\nHere's an AWS secret: AKIALALEMEL33243OLIAE", - "offender": "AKIALALEMEL33243OLIA", - "commit": "b10b3e2cb320a8c211fda94c4567299d37de7776", - "repo": "test_repo_4", - "rule": "AWS Manager ID", - "commitMessage": "adding aws key\n", - "author": "zach rice", - "email": "zricer@protonmail.com", - "file": "secrets.md", - "date": "2019-10-25T12:58:39-04:00", - "tags": "key, AWS" } ] diff --git a/test_data/test_local_repo_one_aws_leak_and_file_leak.json b/test_data/test_local_repo_one_aws_leak_and_file_leak.json index c0437d42..edae0d97 100644 --- a/test_data/test_local_repo_one_aws_leak_and_file_leak.json +++ b/test_data/test_local_repo_one_aws_leak_and_file_leak.json @@ -12,6 +12,19 @@ "date": "2019-10-24T10:03:38-04:00", "tags": "" }, + { + "line": "N/A", + "offender": "server.test.py", + "commit": "6557c92612d3b35979bd426d429255b3bf9fab74", + "repo": "test_repo_1", + "rule": "file regex matched(.*)?py$", + "commitMessage": "", + "author": "zach rice", + "email": "zricer@protonmail.com", + "file": "server.test.py", + "date": "2019-10-24T09:29:27-04:00", + "tags": "" + }, { "line": " aws_access_key_id='AKIAIO5FODNN7EXAMPLE',", "offender": "AKIAIO5FODNN7EXAMPLE", diff --git a/test_data/test_local_repo_three_leaks.json b/test_data/test_local_repo_three_leaks.json index 024003a4..a06f1d40 100644 --- a/test_data/test_local_repo_three_leaks.json +++ b/test_data/test_local_repo_three_leaks.json @@ -77,19 +77,6 @@ "date": "2019-10-25T13:01:27-04:00", "tags": "key, AWS" }, - { - "line": "Here's an AWS secret: AKIALALEMEL33243OLIAE", - "offender": "AKIALALEMEL33243OLIA", - "commit": "17471a5fda722a9e423f1a0d3f0d267ea009d41c", - "repo": "test_repo_3", - "rule": "AWS Manager ID", - "commitMessage": "wait this is actually adding an aws secret\n", - "author": "zach rice", - "email": "zricer@protonmail.com", - "file": "secrets.md", - "date": "2019-10-25T13:01:27-04:00", - "tags": "key, AWS" - }, { "line": "\nHere's an AWS secret: AKIALALEMEL33243OLIAE", "offender": "AKIALALEMEL33243OLIA", diff --git a/test_data/test_local_repo_two_leaks.json b/test_data/test_local_repo_two_leaks.json index 187e7afb..53e0af16 100644 --- a/test_data/test_local_repo_two_leaks.json +++ b/test_data/test_local_repo_two_leaks.json @@ -103,19 +103,6 @@ "date": "2019-10-25T13:01:27-04:00", "tags": "key, API, generic" }, - { - "line": "Here's an AWS secret: AKIALALEMEL33243OLIAE", - "offender": "AKIALALEMEL33243OLIA", - "commit": "17471a5fda722a9e423f1a0d3f0d267ea009d41c", - "repo": "test_repo_2", - "rule": "AWS Manager ID", - "commitMessage": "wait this is actually adding an aws secret\n", - "author": "zach rice", - "email": "zricer@protonmail.com", - "file": "secrets.md", - "date": "2019-10-25T13:01:27-04:00", - "tags": "key, AWS" - }, { "line": "\nHere's an AWS secret: AKIALALEMEL33243OLIAE", "offender": "AKIALALEMEL33243OLIA",