Project

General

Profile

« Previous | Next » 

Revision a0cdac0b

Added by Vincent MEMBRÉ over 6 years ago

Fixes #11161: Share file from shared folder using relay rest api

View differences:

rudder-server-relay/SOURCES/relay-api/relay_api/shared_files.py
import os
import datetime
import requests
import hashlib
# disable ssl warning on rudder connection in all the possible ways
try:
......
SIGNATURE_FORMAT="header=rudder-signature-v1"
METADATA_EXTENSION=".metadata"
BLOCKSIZE = 65536
SHARED_FOLDER="/var/rudder/configuration-repository/shared-files"
# convert a byte string to hexadecimal representation
toHex = lambda x:"".join([hex(ord(c))[2:].zfill(2) for c in x])
......
fd.close()
raise ValueError("ERROR invalid storage: " + line)
def get_shared_folder_hash(file_path, hasher):
with open(file_path, 'rb') as afile:
buf = afile.read(BLOCKSIZE)
while len(buf) > 0:
hasher.update(buf)
buf = afile.read(BLOCKSIZE)
return hasher.hexdigest()
# =====================
# Manage PUT API call
......
if req.status_code == 404:
return False
raise ValueError("ERROR from server:" + str(req.status_code))
# ======================
# Share folder HEAD API call
# ======================
# Parameters:
# - file_name Name of the file in shared folder
# - file_hash The hash to compare with, can be None or Empty
# - hash_type hash algorithm
# Returns correponding return code, 404 (file does not exists), 304 (hash is the same, not modified), 200 (file is different or no Hash sent, download)
# 500 if hash_type is invalid
def shared_folder_head(file_name, file_hash, hash_type):
# where to find file
file_path = os.path.join(SHARED_FOLDER, file_name)
# check if the file and signature exist
if not os.path.isfile(file_path):
return 404
else:
if file_hash is None or file_hash == "":
return 200
try:
hasher = hashlib.new(hash_type)
except:
return 500
# check hash
hash_value = get_shared_folder_hash(file_path, hasher)
if hash_value == file_hash:
return 304
else:
return 200
rudder-server-relay/SOURCES/relay-api/relay_api/views.py
from relay_api import app
from relay_api.shared_files import shared_files_put, shared_files_head, shared_files_put_forward, shared_files_head_forward
from relay_api.shared_files import shared_files_put, shared_files_head, shared_files_put_forward, shared_files_head_forward, shared_folder_head
from relay_api.remote_run import remote_run_generic
from relay_api.common import *
......
return format_error(e, API_DEBUGINFO)
@app.route("/shared-folder/<path:file_name>", methods=["HEAD"])
def head_shared_folder(file_name):
try:
hash_type = request.args["hash_type"]
if hash_type is None:
hash_type = "sha256"
file_hash = request.args["hash"]
return_code = shared_folder_head(file_name, file_hash, hash_type)
return format_response("", return_code)
except Exception as e:
print(traceback.format_exc())
return format_error(e, API_DEBUGINFO)
@app.route('/shared-files/<string:target_uuid>/<string:source_uuid>/<string:file_id>', methods=['HEAD'])
def head_file(target_uuid, source_uuid, file_id):
try:
rudder-server-relay/SOURCES/rudder-apache-relay-ssl.conf
Include /opt/rudder/etc/rudder-networks-24.conf
</IfVersion>
</Directory>
<Location /rudder/relay-api/shared-folder>
SSLVerifyClient require
SSLUserName SSL_CLIENT_S_DN_CN
SSLRequireSSL
<IfVersion < 2.4>
Order allow,deny
Include /opt/rudder/etc/rudder-networks.conf
</IfVersion>
<IfVersion >= 2.4>
Include /opt/rudder/etc/rudder-networks-24.conf
</IfVersion>
</Location>
RewriteCond %{REQUEST_METHOD} =GET
RewriteRule /rudder/relay-api/shared-folder/(.*) /var/rudder/configuration-repository/shared-files/$1

Also available in: Unified diff