-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Secrets and GroupSecrets endpoints and models
- Loading branch information
Showing
22 changed files
with
921 additions
and
92 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from backend.serializers.UUIDSerializer import UUIDSerializer | ||
from backend.serializers.SecretSerializer import SecretSerializer | ||
|
||
|
||
class GroupSecretSerializer: | ||
@staticmethod | ||
def serialize(model): | ||
group_secret = {} | ||
group_secret["id"] = model.id | ||
group_secret["group_id"] = model.group.id | ||
group_secret["secret"] = SecretSerializer.serialize(model.secret) | ||
group_secret["uuid"] = UUIDSerializer.serialize(model.uuid) | ||
|
||
return group_secret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from backend.serializers.UUIDSerializer import UUIDSerializer | ||
|
||
|
||
class SecretSerializer: | ||
@staticmethod | ||
def serialize(model): | ||
secret = {} | ||
secret["id"] = model.id | ||
secret["description"] = model.description | ||
secret["tenant_id"] = model.tenant_id | ||
secret["owner"] = model.owner | ||
secret["sk_secret_name"] = model.sk_secret_name | ||
secret["uuid"] = UUIDSerializer.serialize(model.uuid) | ||
|
||
return secret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import uuid | ||
|
||
from typing import Dict | ||
|
||
from backend.models import Credentials, Secret | ||
from backend.conf.constants import SECRETS_TENANT, TAPIS_SERVICE_ACCOUNT, SECRETS_TENANT | ||
from backend.services.TapisServiceAPIGateway import TapisServiceAPIGateway | ||
from backend.services.Service import Service | ||
from backend.views.http.secrets import ReqSecret | ||
|
||
|
||
class CredentialsService(Service): | ||
def __init__(self): | ||
self.tapis_service_api_gateway = TapisServiceAPIGateway() | ||
Service.__init__(self) | ||
|
||
def save(self, owner: str, data: Dict[str, str]): | ||
service_client = self.tapis_service_api_gateway.get_client() | ||
|
||
sk_secret_name = f"tapis+workflows+{owner}+{uuid.uuid4()}" | ||
try: | ||
service_client.sk.writeSecret( | ||
secretType="user", | ||
secretName=sk_secret_name, | ||
user=TAPIS_SERVICE_ACCOUNT, | ||
tenant=SECRETS_TENANT, | ||
data=data, | ||
_tapis_set_x_headers_from_service=True | ||
) | ||
except Exception as e: | ||
raise e | ||
|
||
credentials = Credentials.objects.create(sk_id=sk_secret_name, owner=owner) | ||
|
||
return credentials | ||
|
||
def delete(self, sk_secret_name: str): | ||
service_client = self.tapis_service_api_gateway.get_client() | ||
|
||
service_client.sk.deleteSecret( | ||
secretType="user", | ||
secretName=sk_secret_name, | ||
user=TAPIS_SERVICE_ACCOUNT, | ||
tenant=SECRETS_TENANT, | ||
versions=[], | ||
_tapis_set_x_headers_from_service=True | ||
) | ||
|
||
credentials = Credentials.objects.filter(sk_id=sk_secret_name).first() | ||
if credentials is not None: | ||
credentials.delete() | ||
|
||
def get(self, sk_secret_name: str): | ||
if Credentials.objects.filter(sk_id=sk_secret_name).exists(): | ||
return Credentials.objects.filter(sk_id=sk_secret_name)[0] | ||
|
||
return None | ||
|
||
def get_secret(self, sk_secret_name: str): | ||
service_client = self.tapis_service_api_gateway.get_client() | ||
|
||
try: | ||
res = service_client.sk.readSecret( | ||
secretType="user", | ||
secretName=sk_secret_name, | ||
user=TAPIS_SERVICE_ACCOUNT, | ||
tenant=SECRETS_TENANT, | ||
version=0, | ||
_tapis_set_x_headers_from_service=True | ||
) | ||
|
||
return res.secretMap.__dict__ | ||
except Exception as e: | ||
return None # TODO catch network error | ||
|
||
def _format_secret_name(self, secret_name: str): | ||
return secret_name.replace(" ", "-") | ||
|
||
service = CredentialsService() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,55 @@ | ||
import uuid | ||
|
||
from typing import Dict | ||
|
||
from backend.models import Credentials | ||
from backend.models import Secret | ||
from backend.conf.constants import SECRETS_TENANT, TAPIS_SERVICE_ACCOUNT, SECRETS_TENANT | ||
from backend.services.TapisServiceAPIGateway import TapisServiceAPIGateway | ||
from backend.services.Service import Service | ||
from backend.views.http.secrets import ReqCreateSecret | ||
|
||
|
||
class SecretService(Service): | ||
def __init__(self): | ||
self.tapis_service_api_gateway = TapisServiceAPIGateway() | ||
Service.__init__(self) | ||
|
||
def save(self, owner: str, data: Dict[str, str]): | ||
def create(self, tenant_id, owner, req_secret: ReqCreateSecret): | ||
service_client = self.tapis_service_api_gateway.get_client() | ||
|
||
sk_id = f"workflows+{owner}+{uuid.uuid4()}" | ||
sk_secret_name = f"tapis+{tenant_id}+workflows+{owner}+{req_secret.id}" | ||
try: | ||
service_client.sk.writeSecret( | ||
secretType="user", | ||
secretName=sk_id, | ||
secretName=sk_secret_name, | ||
user=TAPIS_SERVICE_ACCOUNT, | ||
tenant=SECRETS_TENANT, | ||
data=data, | ||
data=req_secret.data, | ||
_tapis_set_x_headers_from_service=True | ||
) | ||
|
||
return Secret.objects.create( | ||
id=req_secret.id, | ||
description=req_secret.description, | ||
sk_secret_name=sk_secret_name, | ||
owner=owner, | ||
tenant_id=tenant_id | ||
) | ||
except Exception as e: | ||
raise e | ||
|
||
credentials = Credentials.objects.create(sk_id=sk_id, owner=owner) | ||
|
||
return credentials | ||
|
||
def delete(self, sk_id: str): | ||
def delete(self, secret_id, tenant_id, owner): | ||
service_client = self.tapis_service_api_gateway.get_client() | ||
|
||
service_client.sk.deleteSecret( | ||
secret = Secret.objects.filter(secret_id=secret_id, tenant_id=tenant_id, owner=owner).first() | ||
if secret is not None: | ||
secret.delete() | ||
|
||
service_client.sk.destroySecret( | ||
secretType="user", | ||
secretName=sk_id, | ||
secretName=secret.sk_secret_name, | ||
user=TAPIS_SERVICE_ACCOUNT, | ||
tenant=SECRETS_TENANT, | ||
versions=[], | ||
_tapis_set_x_headers_from_service=True | ||
) | ||
|
||
credentials = Credentials.objects.filter(sk_id=sk_id).first() | ||
if credentials is not None: | ||
credentials.delete() | ||
|
||
def get(self, sk_id: str): | ||
if Credentials.objects.filter(sk_id=sk_id).exists(): | ||
return Credentials.objects.filter(sk_id=sk_id)[0] | ||
|
||
return None | ||
|
||
def get_secret(self, sk_id: str): | ||
service_client = self.tapis_service_api_gateway.get_client() | ||
|
||
try: | ||
res = service_client.sk.readSecret( | ||
secretType="user", | ||
secretName=sk_id, | ||
user=TAPIS_SERVICE_ACCOUNT, | ||
tenant=SECRETS_TENANT, | ||
version=0, | ||
_tapis_set_x_headers_from_service=True | ||
) | ||
|
||
return res.secretMap.__dict__ | ||
except Exception as e: | ||
return None # TODO catch network error | ||
|
||
def _format_secret_name(self, secret_name: str): | ||
return secret_name.replace(" ", "-") | ||
|
||
service = SecretService() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.