Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for conf file and multiplataform paths #3

Merged
merged 4 commits into from
Dec 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 0 additions & 31 deletions config.yml

This file was deleted.

Binary file modified requirements.txt
Binary file not shown.
26 changes: 20 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
#!/usr/bin/env python3

"""The setup script."""

from appdirs import *
from setuptools import setup, find_packages
from colorama import init, Fore, Back, Style
import os

init(autoreset=True)

# Identify OS config default path
os_dirs = AppDirs("IntegrityGuard", "IntegrityGuard")

with open('README.md') as readme_file:
with open('README.rst') as readme_file:
readme = readme_file.read()

with open('HISTORY.md') as history_file:
with open('HISTORY.rst') as history_file:
history = history_file.read()

requirements = ['Click>=7.0', ]
requirements = [ 'appdirs>=1.4.4','watchdog>=1.0.2', 'colorama>=0.4.4' ]

test_requirements = [ ]

Expand All @@ -19,7 +25,7 @@
author_email='[email protected]',
python_requires='>=3.6',
classifiers=[
'Development Status :: 2 - Pre-Alpha',
'Development Status :: 3 - Alpha',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Natural Language :: English',
Expand All @@ -34,6 +40,9 @@
'integrityguard=integrityguard.cli:main',
],
},
data_files=[
(os_dirs.user_config_dir, ['src/helpers/integrityguard.conf'])
],
install_requires=requirements,
license="MIT license",
long_description=readme + '\n\n' + history,
Expand All @@ -47,3 +56,8 @@
version='0.1.1',
zip_safe=False,
)

# Print basic instructions for the user
print(Fore.GREEN + "Success!! See important information below:")
print(Fore.YELLOW + "Default config file path: " + os.path.join(os_dirs.user_config_dir, "integrityguard.conf") )
print(Fore.YELLOW + "Default hashes store path: " + os.path.join(os_dirs.user_data_dir, "hashes.json") )
1 change: 0 additions & 1 deletion src/data/hash

This file was deleted.

21 changes: 13 additions & 8 deletions src/hashreport.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
#!/usr/bin/env python3

import os
from helpers.loadconfig import load_config
from data.hash import hash_file
import json
import pathlib
from helpers.loadconfig import load_config
from data.hash import hash_file
from appdirs import *

# Load configuration
config = load_config()

# Get root path to scan
path = config['targetPath']
path = config['monitor']['target_path']

# Get hash type
hash_type = config['hash_type'].lower()
hash_type = config['hash']['hash_type'].lower()

# Check if the directory is empty
if len( os.listdir(path) ) == 0:
raise ValueError("The directory is empty.")

# Identify OS config default path
os_dirs = AppDirs("IntegrityGuard", "IntegrityGuard")

# Initiate hashes variable
hashes = []

Expand All @@ -30,13 +34,14 @@
try:
getHash = hash_file(file_fullpath, hash_type)
hashes.append({ "path": os.path.abspath(file_fullpath), "hash": getHash })
print(file_fullpath + ";" + getHash)
print(file_fullpath + " > " + getHash)
except ValueError as e:
print("Something went wrong hashing the files. " + e)

# Store hashes
hash_file_path = pathlib.Path(__file__).parent.resolve()
hash_file_path = str(hash_file_path) + "/data/hashes"
hash_file_path = os.path.join(os_dirs.user_data_dir, "hashes.json")
f = open(hash_file_path, "w")
f.write(json.dumps(hashes))
f.close()
f.close()

print("Hashes stored at " + hash_file_path)
24 changes: 24 additions & 0 deletions src/helpers/integrityguard.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Hash algorithm type (MD5, SHA1, SHA224, SHA256, SHA384, and SHA512).
[hash]
hash_type = MD5

# Directory monitoring settings
# Use full path e.g. /var/www/myproject.com/
[monitor]
target_path = ./sample
to_ignore =
./*.log
./ignoreTest/*
log_path =
report_type = JSON

# Notification settings
# Supported methods: API or EMAIL
# Leave empty if a setting is not applicable for your notification option
[notify]
notify_method = API
api_endpoint =
smtp_host =
smtp_user =
smtp_pass =
smtp_port =
29 changes: 21 additions & 8 deletions src/helpers/loadconfig.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
import yaml
import configparser
from appdirs import *
import os

def load_config(configPath=None):
def load_config(config_path=None):

configPath = "./config.yml" if configPath is None else configPath
# Identify OS config default path
dirs = AppDirs("IntegrityGuard", "IntegrityGuard")

with open(configPath, "r") as stream:
try:
return yaml.safe_load(stream)
except yaml.YAMLError as exc:
raise ValueError('Something went wrong loading the config.yml file. ' + exc)
# Define default config file path
config_file = os.path.join(dirs.user_config_dir, "integrityguard.conf")

# Check if the user provided a config path
if config_path != None:
config_file = os.path.abspath(config_path)

# Check if the config file exist
if os.path.exists(config_file) == False:
raise ValueError("The configuration file *" + config_file + "* doesn't exist.")

config = configparser.ConfigParser()
config.read(config_file)

return config