added create / compare hashes and notify on miss-match for MinIO backups;

This commit is contained in:
Marco Zeisler 2020-12-06 16:48:50 +01:00
parent 7eb728968a
commit 776c144f3f
3 changed files with 57 additions and 25 deletions

View File

@ -0,0 +1,24 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="IoTClient" type="PythonConfigurationType" factoryName="Python">
<module name="AIC" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<envs>
<env name="PYTHONUNBUFFERED" value="1" />
</envs>
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/iotclient" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/iotclient/iot_client.py" />
<option name="PARAMETERS" value="" />
<option name="SHOW_COMMAND_LINE" value="false" />
<option name="EMULATE_TERMINAL" value="false" />
<option name="MODULE_MODE" value="false" />
<option name="REDIRECT_INPUT" value="false" />
<option name="INPUT_FILE" value="" />
<method v="2" />
</configuration>
</component>

View File

@ -1,3 +1,5 @@
from typing import Union
import requests import requests
from django.conf import settings from django.conf import settings
@ -31,7 +33,7 @@ class MinioService:
:param filename: filename :param filename: filename
:return: bytes representation of the file or None on error :return: bytes representation of the file or None on error
""" """
file_bytes: bytes file_bytes: Union[bytes, None] = None
try: try:
url = settings.AWS_HOST + filename url = settings.AWS_HOST + filename
response = requests.get(url, stream=True) response = requests.get(url, stream=True)

View File

@ -1,16 +1,13 @@
import hashlib
import logging
import json import json
import logging
from app_be.services.dropboxservice import DropboxService from app_be.services.dropboxservice import DropboxService
from app_be.services.mongodbservice import MongoDBService
from app_be.services.minioservice import MinioService
from app_be.services.hashservice import create_sha512 from app_be.services.hashservice import create_sha512
from app_be.services.minioservice import MinioService
from app_be.services.mongodbservice import MongoDBService
from app_be.services.wrapperservice import WrapperService from app_be.services.wrapperservice import WrapperService
from app_be.views.mongo_db import MongoManager
from django.http import JsonResponse
from django.http import HttpRequest from django.http import HttpRequest
from django.http import JsonResponse
from rest_framework.decorators import api_view from rest_framework.decorators import api_view
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -31,7 +28,6 @@ class ImageEndpoint:
logger.debug('Image GET all call: {}'.format(request)) logger.debug('Image GET all call: {}'.format(request))
print(request) print(request)
metadata = MongoDBService.getAll() metadata = MongoDBService.getAll()
print(metadata) print(metadata)
@ -42,30 +38,42 @@ class ImageEndpoint:
def image_api_get_single(request, identifier): def image_api_get_single(request, identifier):
logger.debug('Image GET single call: {}'.format(request)) logger.debug('Image GET single call: {}'.format(request))
# get metadata from MongoDB
metadata = MongoDBService.getSingle(identifier) metadata = MongoDBService.getSingle(identifier)
print(metadata) if not metadata:
return JsonResponse({'Result': 'Error - could not find any metadata in mongoDB.',
'id': identifier}, status=404, safe=False)
logger.debug('Mongo Meta: {}'.format(metadata))
# get stored SHA512 hash
stored_hash = metadata.get('sha512', '')
logger.debug('Sorted SHA512: {}'.format(stored_hash))
# get image bytes from Dropbox
dropbox_image_bytes = DropboxService.read_file(metadata['filename']) dropbox_image_bytes = DropboxService.read_file(metadata['filename'])
#minio_image_bytes = MinioService.read_file(metadata['filename'])
if dropbox_image_bytes is None: if dropbox_image_bytes is None:
return JsonResponse({'Result': 'Error - could not find image in dropbox.', 'id': identifier}, status=404, return JsonResponse({'Result': 'Error - could not find image in dropbox.', 'id': identifier}, status=404,
safe=False) safe=False)
stored_hash = metadata.get('sha512', '') # calc dropbox image hash
print(stored_hash)
actual_dropbox_hash = create_sha512(dropbox_image_bytes) actual_dropbox_hash = create_sha512(dropbox_image_bytes)
logger.debug('Actual Dropbox SHA512: {}'.format(actual_dropbox_hash))
if stored_hash != actual_dropbox_hash: if stored_hash != actual_dropbox_hash:
return JsonResponse('Stored hash does not match generated one! ' return JsonResponse('Stored hash does not match generated one! '
'stored: {} actual: {}'.format(stored_hash, actual_dropbox_hash), safe=False) 'stored: {} actual: {}'.format(stored_hash, actual_dropbox_hash), safe=False)
# TODO - check hash of MinIO image, too # get image bytes from MinIO
# actual_minio_hash = '' # create_sha512(minio_image_bytes) minio_image_bytes = MinioService.read_file(metadata['filename'])
# if stored_hash != actual_minio_hash: if minio_image_bytes is None:
# return JsonResponse('Stored hash does not match generated one! ' return JsonResponse({'Result': 'Error - could not find image in minIO.', 'id': identifier}, status=404,
# 'stored: {} actual: {}'.format(stored_hash, actual_minio_hash), safe=False) safe=False)
# calc minIO image hash
actual_minio_hash = create_sha512(minio_image_bytes)
logger.debug('Actual MinIO SHA512: {}'.format(actual_minio_hash))
if stored_hash != actual_minio_hash:
return JsonResponse('Stored hash does not match generated one! '
'stored: {} actual: {}'.format(stored_hash, actual_minio_hash), safe=False)
payload = { payload = {
'id': identifier, 'id': identifier,
@ -102,9 +110,7 @@ class ImageEndpoint:
logger.debug('Image DELETE single call: {}'.format(request)) logger.debug('Image DELETE single call: {}'.format(request))
resp = MongoDBService.deleteSingle(identifier) resp = MongoDBService.deleteSingle(identifier)
print(resp) print(resp)
return JsonResponse({'Result': 'success1'}, safe=False) return JsonResponse({'Result': 'success1'}, safe=False)