def prepare_for_scan(user_path, scan_ignore):
    """
    Prepare the files for scanning by collecting their names and contents.
    :param user_path: user path
    :param scan_ignore: list of files to ignore
    :return: list of dictionaries with filenames and their contents for scanning
    """
    API_KEY = os.getenv("GG_API_KEY")
    client = GGClient(api_key=API_KEY)
    if client.health_check(
    ).success:  # check health of the API and the API key used.
        file_list = []
        for name in glob.iglob(user_path, recursive=True):
            if os.path.isdir(name) or os.path.relpath(name, start=user_path)[6:] in scan_ignore \
                    or os.path.basename(name) == 'gg_secret_scanner_results.txt':
                continue
            try:
                with open(name, mode='r', encoding='utf-8') as file:
                    file_list.append({
                        'filename':
                        os.path.relpath(name, start=user_path)[6:],
                        'document':
                        file.read()
                    })
            except Exception:
                # continue if some files could not be open (like images or executables)
                continue
        return file_list
    else:
        print('Invalid API Key or API maintenance.')
Esempio n. 2
0
def test_health_check(client: GGClient):
    health = client.health_check()
    assert health.status_code == 200
    assert health.detail == "Valid API key."
    assert str(health) == "200:Valid API key."
    assert bool(health)
    assert health.success

    assert type(health.to_dict()) == dict
    assert type(health.to_json()) == str
Esempio n. 3
0
def test_health_check(client: GGClient):
    health = client.health_check()
    assert health.status_code == 200
    assert health.detail == "Valid API key."
    assert str(health) == (
        "detail:Valid API key., status_code:200, "
        "app version:1.26.0-rc.4, secrets engine version:2.43.0")
    assert bool(health)
    assert health.success

    assert type(health.to_dict()) == OrderedDict
    assert type(health.to_json()) == str
Esempio n. 4
0
def test_health_check_error(client: GGClient):
    health = client.health_check()
    assert health.status_code == 400
    assert health.detail == "Configuration error."
    assert str(health) == (
        "detail:Configuration error., status_code:400, "
        "app version:1.26.0-rc.4, secrets engine version:2.43.0")
    assert bool(health) is False
    assert health.success is False

    assert type(health.to_dict()) == OrderedDict
    assert type(health.to_json()) == str
Esempio n. 5
0
from requests import codes

from pygitguardian import GGClient

API_KEY = os.getenv("GG_API_KEY")
FILENAME = ".env"
DOCUMENT = """
    import urllib.request
    url = 'http://*****:*****@cake.gitguardian.com/isreal.json'
    response = urllib.request.urlopen(url)
    consume(response.read())"
"""

client = GGClient(api_key=API_KEY)

# Check the health of the API and the API key used.
health_obj = client.health_check()

if health_obj.status_code == codes[r"\o/"]:  # this is 200 but cooler
    try:
        scan_result = client.content_scan(filename=FILENAME, document=DOCUMENT)
    except Exception as exc:
        # Handle exceptions such as schema validation
        traceback.print_exc(2, file=sys.stderr)
        print(str(exc))

    print("Scan results:", scan_result.has_secrets, "-",
          scan_result.policy_break_count)
else:
    print("Invalid API Key")