Skip to content

Commit

Permalink
Update gRPC client to send credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
sevein committed Jul 4, 2024
1 parent 1255d1a commit 8cbae66
Show file tree
Hide file tree
Showing 12 changed files with 20 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/dashboard/src/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
client_local = threading.local()


def get_client(user_id, client_class=None):
def get_client(user, client_class=None):
"""Return the client for communicating with the MCPServer."""
if not getattr(client_local, "client", None):
client_class = client_class or CCPClient
lang = get_language() or "en"
client_local.client = client_class(user_id, lang)
client_local.client = client_class(user, lang)

return client_local.client

Expand Down
8 changes: 4 additions & 4 deletions src/dashboard/src/client/ccp.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,9 @@ class CCPClient(Client):
supported yet, we choose to make use of the gRPC protocol.
"""

def __init__(self, user_id, lang):
self.user_id = str(user_id)
def __init__(self, user, lang):
self.username = user.username
self.api_key = user.api_key.key
self.lang = lang
self.channel = insecure_channel(
target=settings.GEARMAN_SERVER,
Expand All @@ -136,7 +137,7 @@ def __init__(self, user_id, lang):
self.channel,
header_adder_interceptor(
[
("user_id", self.user_id),
("authorization", f"apikey {self.username}:{self.api_key}"),
("lang", self.lang),
],
),
Expand All @@ -145,7 +146,6 @@ def __init__(self, user_id, lang):

def _tx(self, translations):
tx = translations.tx

return tx.get(self.lang, tx.get("en", ""))

def approve_job(self, job_id, choice):
Expand Down
4 changes: 2 additions & 2 deletions src/dashboard/src/client/mcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ class GearmanClient(gearman.GearmanClient):
class MCPClient(Client):
"""Handles communication with MCPServer."""

def __init__(self, user_id, lang):
def __init__(self, user, lang):
self.server = settings.GEARMAN_SERVER
self.user_id = user_id
self.user_id = user.id
self.lang = lang

def _rpc_sync_call(self, ability, data=None, timeout=INFLIGHT_POLL_TIMEOUT):
Expand Down
2 changes: 1 addition & 1 deletion src/dashboard/src/components/administration/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ def __init__(self, *args, **kwargs):

def load_processing_config_fields(self, user):
"""Obtain processing fields and available choices from MCPServer."""
client = get_client(user.id)
client = get_client(user)
self.processing_fields = client.get_processing_config_fields()

# Override labels with translations in this form.
Expand Down
6 changes: 3 additions & 3 deletions src/dashboard/src/components/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ def approve_transfer(request):
# Append a slash to complete the directory path.
db_transfer_path = os.path.join(watched_path, "")
try:
client = get_client(request.user.id)
client = get_client(request.user)
unit_uuid = client.approve_transfer_by_path(db_transfer_path, transfer_type)
except Exception as err:
msg = "Unable to start the transfer."
Expand Down Expand Up @@ -547,7 +547,7 @@ def reingest_approve(request):
if sip_uuid is None:
return _error_response('"uuid" is required.')
try:
client = get_client(request.user.id)
client = get_client(request.user)
client.approve_partial_reingest(sip_uuid)
except Exception as err:
msg = "Unable to approve the partial reingest."
Expand Down Expand Up @@ -839,7 +839,7 @@ def _package_create(request):
if processing_config is not None:
kwargs["processing_config"] = processing_config
try:
client = get_client(request.user.id)
client = get_client(request.user)
id_ = client.create_package(*args, **kwargs)
except Exception as err:
msg = "Package cannot be created"
Expand Down
2 changes: 1 addition & 1 deletion src/dashboard/src/components/ingest/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def post(self, request):
def ingest_status(request, uuid=None):
response = {"objects": {}, "mcp": False}
try:
client = get_client(request.user.id)
client = get_client(request.user)
response["objects"] = client.get_sips_status()
except Exception:
pass
Expand Down
2 changes: 1 addition & 1 deletion src/dashboard/src/components/ingest/views_as.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ def complete_matching(request, uuid):
if request.method != "POST":
return HttpResponse(status=405)
try:
client = get_client(request.user.id)
client = get_client(request.user)
client.execute_package(
uuid,
# Microservice: Upload DIP
Expand Down
4 changes: 2 additions & 2 deletions src/dashboard/src/components/mcp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
def execute(request):
result = ""
if request.POST.get("uuid"):
client = get_client(request.user.id)
client = get_client(request.user)
result = client.approve_job(
request.POST.get("uuid"), request.POST.get("choice", "")
)
return HttpResponse(result, content_type="text/plain")


def list(request):
client = get_client(request.user.id)
client = get_client(request.user)
jobs = etree.XML(client.list_jobs_awaiting_approval())
response = ""
if 0 < len(jobs):
Expand Down
2 changes: 1 addition & 1 deletion src/dashboard/src/components/transfer/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def component(request, uuid):
def status(request, uuid=None):
response = {"objects": {}, "mcp": False}
try:
client = get_client(request.user.id)
client = get_client(request.user)
response["objects"] = client.get_transfers_status()
except Exception:
logger.error("Failed to load packages status.", exc_info=True)
Expand Down
2 changes: 1 addition & 1 deletion src/dashboard/src/components/unit/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def microservices(request, unit_type, unit_uuid):
:param unit_uuid: UUID of the Transfer or SIP
"""
client = get_client(request.user.id)
client = get_client(request.user)
resp = client.get_package_status(unit_uuid)
return render(
request,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def loop(self, *args, **options):
admin_user = self.admin_user()
if not admin_user:
raise CommandError("Cannot find a superuser.")
client = get_client(admin_user.id)
client = get_client(admin_user)

while True:
self.success("Fetching packages awaiting decisions...")
Expand Down
2 changes: 1 addition & 1 deletion src/dashboard/src/main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def home(request):

# TODO: hide removed elements
def status(request):
client = get_client(request.user.id)
client = get_client(request.user)
xml = etree.XML(client.list_jobs_awaiting_approval())

sip_count = len(
Expand Down

0 comments on commit 8cbae66

Please sign in to comment.