Skip to content

Commit eabf899

Browse files
Rasmus Oscar Welanderglpatcern
authored andcommitted
Added status code handler
1 parent 7d86ebc commit eabf899

File tree

7 files changed

+233
-188
lines changed

7 files changed

+233
-188
lines changed

src/auth.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
44
Authors: Rasmus Welander, Diogo Castro, Giuseppe Lo Presti.
55
6-
Last updated: 01/08/2024
6+
Last updated: 19/08/2024
77
"""
88

99
import grpc
1010
import jwt
1111
import datetime
1212
import logging
13-
import cs3.gateway.v1beta1.gateway_api_pb2 as gw
13+
from cs3.gateway.v1beta1.gateway_api_pb2 import AuthenticateRequest
1414
from cs3.auth.registry.v1beta1.registry_api_pb2 import ListAuthProvidersRequest
1515
from cs3.gateway.v1beta1.gateway_api_pb2_grpc import GatewayAPIStub
1616
from cs3.rpc.v1beta1.code_pb2 import CODE_OK
@@ -67,8 +67,8 @@ def get_token(self) -> tuple[str, str]:
6767
the credentials have expired.
6868
6969
:return tuple: A tuple containing the header key and the token.
70-
May throw AuthenticationException (token expired, or failed to authenticate)
71-
or SecretNotSetException (neither token or client secret was set).
70+
:raises: AuthenticationException (token expired, or failed to authenticate)
71+
:raises: SecretNotSetException (neither token or client secret was set)
7272
"""
7373

7474
if not Auth._check_token(self._token):
@@ -81,7 +81,7 @@ def get_token(self) -> tuple[str, str]:
8181
self._log.error("The provided token have expired")
8282
raise AuthenticationException("The credentials have expired")
8383
# Create an authentication request
84-
req = gw.AuthenticateRequest(
84+
req = AuthenticateRequest(
8585
type=self._config.auth_login_type,
8686
client_id=self._config.auth_client_id,
8787
client_secret=self._client_secret,
@@ -90,26 +90,25 @@ def get_token(self) -> tuple[str, str]:
9090
res = self._gateway.Authenticate(req)
9191

9292
if res.status.code != CODE_OK:
93-
self._log.error(
94-
f"Failed to authenticate user {self._config.auth_client_id}, error: {res.status.message}"
95-
)
93+
self._log.error(f"Failed to authenticate user {self._config.auth_client_id}, error: {res.status}")
9694
raise AuthenticationException(
97-
f"Failed to authenticate user {self._config.auth_client_id}, error: {res.status.message}"
95+
f"Failed to authenticate user {self._config.auth_client_id}, error: {res.status}"
9896
)
9997
self._token = res.token
98+
self._log.debug(f'msg="Authenticated user" user="{self._config.auth_client_id}"')
10099
return ("x-access-token", self._token)
101100

102101
def list_auth_providers(self) -> list[str]:
103102
"""
104103
list authentication providers
105104
106105
:return: a list of the supported authentication types
107-
May return ConnectionError (Could not connect to host)
106+
:raises: ConnectionError (Could not connect to host)
108107
"""
109108
try:
110109
res = self._gateway.ListAuthProviders(request=ListAuthProvidersRequest())
111110
if res.status.code != CODE_OK:
112-
self._log.error(f"List auth providers request failed, error: {res.status.message}")
111+
self._log.error(f"List auth providers request failed, error: {res.status}")
113112
raise Exception(res.status.message)
114113
except grpc.RpcError as e:
115114
self._log.error("List auth providers request failed")

src/cs3client.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
Authors: Rasmus Welander, Diogo Castro, Giuseppe Lo Presti.
55
6-
Last updated: 01/08/2024
6+
Last updated: 19/08/2024
77
"""
88

99
import grpc
@@ -14,6 +14,10 @@
1414
from auth import Auth
1515
from file import File
1616
from user import User
17+
from share import Share
18+
from statuscodehandler import StatusCodeHandler
19+
from app import App
20+
from checkpoint import Checkpoint
1721
from config import Config
1822

1923

@@ -39,10 +43,17 @@ def __init__(self, config: ConfigParser, config_category: str, log: logging.Logg
3943
except grpc.FutureTimeoutError as e:
4044
log.error(f'msg="Failed to connect to Reva via GRPC" error="{e}"')
4145
raise TimeoutError("Failed to connect to Reva via GRPC")
46+
4247
self._gateway: cs3gw_grpc.GatewayAPIStub = cs3gw_grpc.GatewayAPIStub(self.channel)
43-
self.auth = Auth(self._config, self._log, self._gateway)
44-
self.file = File(self._config, self._log, self._gateway, self.auth)
45-
self.user = User(self._config, self._log, self._gateway, self.auth)
48+
self._status_code_handler: StatusCodeHandler = StatusCodeHandler(self._log, self._config)
49+
self.auth: Auth = Auth(self._config, self._log, self._gateway)
50+
self.file: File = File(self._config, self._log, self._gateway, self.auth, self._status_code_handler)
51+
self.user: User = User(self._config, self._log, self._gateway, self.auth, self._status_code_handler)
52+
self.app: App = App(self._config, self._log, self._gateway, self.auth, self._status_code_handler)
53+
self.checkpoint: Checkpoint = Checkpoint(
54+
self._config, self._log, self._gateway, self.auth, self._status_code_handler
55+
)
56+
self.share = Share(self._config, self._log, self._gateway, self.auth, self._status_code_handler)
4657

4758
def _create_channel(self) -> grpc.Channel:
4859
"""

src/cs3resource.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
Authors: Rasmus Welander, Diogo Castro, Giuseppe Lo Presti.
55
6-
Last updated: 29/07/2024
6+
Last updated: 19/08/2024
77
"""
88

99
import cs3.storage.provider.v1beta1.resources_pb2 as cs3spr
@@ -94,7 +94,7 @@ def ref(self) -> cs3spr.Reference:
9494
absolute path, relative hybrid path, fully opaque fileid.
9595
9696
:return: The cs3 reference.
97-
May throw ValueError (Invalid Resource)
97+
:raises: ValueError (Invalid Resource)
9898
"""
9999
if self._abs_path:
100100
return cs3spr.Reference(path=self._abs_path)
@@ -123,7 +123,7 @@ def recreate_endpoint_and_file(self) -> dict:
123123
Recreates the endpoint and file reference from the given resource
124124
125125
:return: (dict) {"file": fileref, "endpoint": endpoint}
126-
May throw ValueError (invalid resource)
126+
:raises: ValueError (invalid resource)
127127
"""
128128
endpoint = self._storage_id
129129
if self._space_id:
@@ -139,11 +139,11 @@ def recreate_endpoint_and_file(self) -> dict:
139139
@classmethod
140140
def from_cs3_ref(cls, reference: cs3spr.Reference) -> "Resource":
141141
"""
142-
Alternate constructor that reverses a CS3 reference to obtain a resource.ß
142+
Alternate constructor that reverses a CS3 reference to obtain a resource.
143143
144144
:param reference: The CS3 reference.
145145
:return: Resource object.
146-
May throw ValueError (Invalid reference)
146+
:raises: ValueError (Invalid reference)
147147
"""
148148
rel_path = None
149149
opaque_id = None
@@ -161,7 +161,7 @@ def from_cs3_ref(cls, reference: cs3spr.Reference) -> "Resource":
161161
if reference.path and len(reference.path) > 1:
162162
# It's a relative path (remove the "." in the relative path)
163163
rel_path = reference.path[1:]
164-
# The opaque_id is a parent id since it's a relative pathß
164+
# The opaque_id is a parent id since it's a relative path
165165
parent_id = reference.resource_id.opaque_id
166166
else:
167167
opaque_id = reference.resource_id.opaque_id

src/exceptions/exceptions.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ def __init__(self, message: str = ""):
3838

3939
class FileLockedException(IOError):
4040
"""
41-
Standard error thrown when attempting to overwrite a file/xattr or when
42-
a lock operation cannot be performed because of failed preconditions
41+
Standard error thrown when attempting to overwrite a file/xattr with a mistmatched lock,
42+
or when a lock operation cannot be performed because of failed preconditions
4343
"""
4444

4545
def __init__(self, message: str = ""):
@@ -53,3 +53,12 @@ class UnknownException(Exception):
5353

5454
def __init__(self, message: str = ""):
5555
super().__init__(message)
56+
57+
58+
class AlreadyExistsException(IOError):
59+
"""
60+
Standard error thrown when attempting to create a resource that already exists
61+
"""
62+
63+
def __init__(self, message: str = ""):
64+
super().__init__(message)

0 commit comments

Comments
 (0)