-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
### Feature or Bugfix - Refactoring ### Detail - detangle StackRepository & StackService & EnvironmentService - Move logic from resolvers and repositories to services - Remove unused params ### Relates - #741 ### Security Please answer the questions below briefly where applicable, or write `N/A`. Based on [OWASP 10](https://owasp.org/Top10/en/). - Does this PR introduce or modify any input fields or queries - this includes fetching data from storage outside the application (e.g. a database, an S3 bucket)? - Is the input sanitized? - What precautions are you taking before deserializing the data you consume? - Is injection prevented by parametrizing queries? - Have you ensured no `eval` or similar functions are used? - Does this PR introduce any functionality or component that requires authorization? - How have you ensured it respects the existing AuthN/AuthZ mechanisms? - Are you logging failed auth attempts? - Are you using or adding any cryptographic features? - Do you use a standard proven implementations? - Are the used keys controlled by the customer? Where are they stored? - Are you introducing any new policies/roles/users? - Have you used the least-privilege principle? How? By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license. --------- Co-authored-by: Sofia Sazonova <[email protected]>
- Loading branch information
1 parent
356a8a7
commit d4819ef
Showing
21 changed files
with
255 additions
and
190 deletions.
There are no files selected for viewing
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
84 changes: 15 additions & 69 deletions
84
backend/dataall/core/stacks/db/keyvaluetag_repositories.py
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,96 +1,42 @@ | ||
import logging | ||
|
||
from dataall.base.context import get_context | ||
from dataall.core.permissions.services.resource_policy_service import ResourcePolicyService | ||
from dataall.core.stacks.db import stack_models as models | ||
from dataall.core.stacks.db.target_type_repositories import TargetType | ||
from dataall.base.db import exceptions | ||
from dataall.core.stacks.db.stack_models import KeyValueTag | ||
from typing import List | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class KeyValueTag: | ||
class KeyValueTagRepository: | ||
@staticmethod | ||
def update_key_value_tags(session, uri: str, data: dict = None) -> [models.KeyValueTag]: | ||
if not uri: | ||
raise exceptions.RequiredParameter('targetUri') | ||
if not data: | ||
raise exceptions.RequiredParameter('data') | ||
if not data.get('targetType'): | ||
raise exceptions.RequiredParameter('targetType') | ||
|
||
context = get_context() | ||
ResourcePolicyService.check_user_resource_permission( | ||
session=session, | ||
username=context.username, | ||
groups=context.groups, | ||
resource_uri=uri, | ||
permission_name=TargetType.get_resource_update_permission_name(data['targetType']), | ||
) | ||
|
||
tag_keys = [tag['key'].lower() for tag in data.get('tags', [])] | ||
if tag_keys and len(tag_keys) != len(set(tag_keys)): | ||
raise exceptions.UnauthorizedOperation( | ||
action='SAVE_KEY_VALUE_TAGS', | ||
message='Duplicate tag keys found. Please note that Tag keys are case insensitive', | ||
) | ||
|
||
tags = [] | ||
session.query(models.KeyValueTag).filter( | ||
models.KeyValueTag.targetUri == uri, | ||
models.KeyValueTag.targetType == data['targetType'], | ||
).delete() | ||
for tag in data.get('tags'): | ||
kv_tag: models.KeyValueTag = models.KeyValueTag( | ||
targetUri=uri, targetType=data['targetType'], key=tag['key'], value=tag['value'], cascade=tag['cascade'] | ||
) | ||
tags.append(kv_tag) | ||
session.add(kv_tag) | ||
|
||
return tags | ||
|
||
@staticmethod | ||
def list_key_value_tags(session, uri, target_type) -> dict: | ||
context = get_context() | ||
ResourcePolicyService.check_user_resource_permission( | ||
session=session, | ||
username=context.username, | ||
groups=context.groups, | ||
resource_uri=uri, | ||
permission_name=TargetType.get_resource_read_permission_name(target_type), | ||
) | ||
return KeyValueTag.find_key_value_tags(session, uri, target_type) | ||
|
||
@staticmethod | ||
def find_key_value_tags(session, target_uri, target_type) -> [models.KeyValueTag]: | ||
def find_key_value_tags(session, target_uri, target_type) -> List[KeyValueTag]: | ||
return ( | ||
session.query(models.KeyValueTag) | ||
session.query(KeyValueTag) | ||
.filter( | ||
models.KeyValueTag.targetUri == target_uri, | ||
models.KeyValueTag.targetType == target_type, | ||
KeyValueTag.targetUri == target_uri, | ||
KeyValueTag.targetType == target_type, | ||
) | ||
.all() | ||
) | ||
|
||
@staticmethod | ||
def find_environment_cascade_key_value_tags(session, target_uri) -> [models.KeyValueTag]: | ||
def find_environment_cascade_key_value_tags(session, target_uri) -> List[KeyValueTag]: | ||
return ( | ||
session.query(models.KeyValueTag) | ||
session.query(KeyValueTag) | ||
.filter( | ||
models.KeyValueTag.targetUri == target_uri, | ||
models.KeyValueTag.targetType == 'environment', | ||
models.KeyValueTag.cascade.is_(True), | ||
KeyValueTag.targetUri == target_uri, | ||
KeyValueTag.targetType == 'environment', | ||
KeyValueTag.cascade.is_(True), | ||
) | ||
.all() | ||
) | ||
|
||
@staticmethod | ||
def delete_key_value_tags(session, target_uri, target_type): | ||
return ( | ||
session.query(models.KeyValueTag) | ||
session.query(KeyValueTag) | ||
.filter( | ||
models.KeyValueTag.targetUri == target_uri, | ||
models.KeyValueTag.targetType == target_type, | ||
KeyValueTag.targetUri == target_uri, | ||
KeyValueTag.targetType == target_type, | ||
) | ||
.delete() | ||
) |
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.