Skip to content

Commit

Permalink
fix: Credentials management
Browse files Browse the repository at this point in the history
  • Loading branch information
Dr0p42 committed Jun 18, 2024
1 parent b1f6f51 commit 513ba13
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 18 deletions.
21 changes: 15 additions & 6 deletions naas_python/domains/storage/StorageDomain.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .models.Storage import Storage

from typing import List
from typing import Mapping

from naas_python.domains.storage.StorageSchema import (
IStorageDomain,
Expand All @@ -11,11 +11,11 @@
StorageProviderNotFound
)
class StorageDomain(IStorageDomain):
def __init__(self, adaptor: IStorageAdaptor, storage_provider_adaptors : List[IStorageProviderAdaptor]):
def __init__(self, adaptor: IStorageAdaptor, storage_provider_adaptors : Mapping[str, IStorageProviderAdaptor]):
# List[IStorageProviderAdaptor])
#Map[str : IStorageProviderAdaptor])
self.adaptor = adaptor
self.storage_provider_adaptors = storage_provider_adaptors
self.adaptor : IStorageAdaptor = adaptor
self.storage_provider_adaptors : Mapping[str, IStorageProviderAdaptor] = storage_provider_adaptors

############### API ###############
def create_workspace_storage(self,
Expand Down Expand Up @@ -75,8 +75,8 @@ def create_workspace_storage_credentials(self,
storage_name: Storage.__fields__['name'],
) -> dict:
credentials = self.adaptor.generate_credentials(workspace_id, storage_name)
self.storage_provider.save_naas_credentials(workspace_id, storage_name, credentials)
return dict
self.__get_storage_provider_adaptor(workspace_id=workspace_id, storage_name=storage_name).save_naas_credentials(workspace_id, storage_name, credentials)
return credentials

############### BOTO ###############
def __get_storage_provider(self,
Expand All @@ -86,6 +86,15 @@ def __get_storage_provider(self,
#TODO This function should check in ~.naas/credentials to grab the provider id (s3;azure;gcp;...)
return 's3'

def __get_storage_provider_adaptor(self,
workspace_id: str,
storage_name: Storage.__fields__['name']
) -> IStorageProviderAdaptor:
storage_provider_id = self.__get_storage_provider(workspace_id, storage_name)
if storage_provider_id not in self.storage_provider_adaptors:
raise StorageProviderNotFound(f'Provider "{storage_provider_id}" is not implemented or not loaded.')
return self.storage_provider_adaptors[storage_provider_id]


def post_workspace_storage_object(self,
workspace_id: str,
Expand Down
12 changes: 10 additions & 2 deletions naas_python/domains/storage/StorageSchema.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from abc import ABCMeta, abstractmethod
from logging import getLogger
from typing import List
from typing import Mapping

from naas_models.pydantic.storage_p2p import *
from .models.Storage import Storage, Object
Expand Down Expand Up @@ -48,6 +48,10 @@ def delete_workspace_storage_object(self,
) -> dict:
raise NotImplementedError

@abstractmethod
def generate_credentials(self, workspace_id :str, storage_name: str) -> dict:
raise NotImplementedError

class IStorageProviderAdaptor(metaclass=ABCMeta):

provider_id : str
Expand All @@ -70,10 +74,14 @@ def get_workspace_storage_object(self,
) -> bytes:
raise NotImplementedError

@abstractmethod
def save_naas_credentials(self, workspace_id:str, storage_name:str, credentials:dict)-> str:
raise NotImplementedError

# Domain
class IStorageDomain(metaclass=ABCMeta):
adaptor: IStorageAdaptor
storage_provider_adaptors : List[IStorageProviderAdaptor]
storage_provider_adaptors : Mapping[str, IStorageProviderAdaptor]
# storage_provider_adaptors : Map[str, IStorageProviderAdaptor]
#TODO to be validated

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ def create_workspace_storage_credentials(self,
workspace_id=workspace_id,
storage_name=storage_name
)
print("Credentials created.")
print("Credentials created.")
print(response)

############### BOTO3 ###############
def post_workspace_storage_object(self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@ def create_workspace_storage(self,
workspace_id: str,
storage_name: Storage.__fields__['name']
) -> dict:
_url = f"{self.host}/workspace/{workspace_id}/storage"
_url = f"{self.host}/workspace/{workspace_id}/storage/"

api_response = self.make_api_request(
requests.post,
_url,
payload=json.dumps(
{"storage": {"name": storage_name} }
),
payload={
"storage":
{"name": storage_name}
},
)
return self.__handle_response(api_response)

Expand All @@ -66,7 +67,7 @@ def delete_workspace_storage(self,
def list_workspace_storage(self,
workspace_id: str,
) -> dict:
_url = f"{self.host}/workspace/{workspace_id}/storage"
_url = f"{self.host}/workspace/{workspace_id}/storage/"

api_response = self.make_api_request(
requests.get,
Expand Down Expand Up @@ -111,8 +112,9 @@ def generate_credentials(self, workspace_id :str, storage_name: str) -> dict:
api_response = self.make_api_request(
requests.post,
_url,
payload=json.dumps(
{"name": storage_name}
),
payload={
"name": storage_name
}
,
)
return self.__handle_response(api_response)
2 changes: 1 addition & 1 deletion naas_python/utils/domains_base/secondary/BaseAPIAdaptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def make_api_request(
headers.update({"Authorization": f"Bearer {self.jwt_token()}"})

try:
api_response = method(url, data=payload, headers=headers)
api_response = method(url, data=payload if type(payload) is str else json.dumps(payload), headers=headers)
api_response.raise_for_status()
return api_response

Expand Down

0 comments on commit 513ba13

Please sign in to comment.