Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a few other allowed chars in tags (: and -) #859

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 16 additions & 27 deletions core/database_arango.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@
import datetime
import json
import logging
import re
import sys
import time
import unicodedata
from typing import TYPE_CHECKING, Any, Iterable, List, Optional, Tuple, Type, TypeVar

if TYPE_CHECKING:
from core.schemas import entity, indicator, observable
from core.schemas.graph import Relationship, TagRelationship
from core.schemas import observable
from core.schemas import entity
from core.schemas import indicator

import requests
from arango import ArangoClient
Expand Down Expand Up @@ -303,41 +299,34 @@ def tag(
) -> TYetiObject:
"""Connects object to tag graph."""
# Import at runtime to avoid circular dependency.
from core.schemas.tag import DEFAULT_EXPIRATION_DAYS, Tag
from core.schemas import tag

expiration_days = expiration_days or DEFAULT_EXPIRATION_DAYS
expiration_days = expiration_days or tag.DEFAULT_EXPIRATION_DAYS

if strict:
self.clear_tags()

extra_tags = set()
for tag_name in tags:
# Attempt to find replacement tag
if normalized:
nfkd_form = unicodedata.normalize("NFKD", tag_name)
nfkd_form.encode("ASCII", "ignore").decode("UTF-8")
tag_name = "".join(
[c for c in nfkd_form if not unicodedata.combining(c)]
)
tag_name = tag_name.strip()
tag_name = re.sub(r"\s+", "_", tag_name).lower()
tag_name = re.sub(r"[^a-zA-Z0-9_]", "", tag_name)
replacements, _ = Tag.filter({"in__replaces": [tag_name]}, count=1)
tag: Optional[Tag] = None
for provided_tag_name in tags:
tag_name = tag.normalize_name(provided_tag_name)
if not tag_name:
raise RuntimeError(f"Cannot tag object with empty tag: '{provided_tag_name}' -> '{tag_name}'")
replacements, _ = tag.Tag.filter({"in__replaces": [tag_name]}, count=1)
new_tag: Optional[tag.Tag] = None

if replacements:
tag = replacements[0]
new_tag = replacements[0]
# Attempt to find actual tag
else:
tag = Tag.find(name=tag_name)
new_tag = tag.Tag.find(name=tag_name)
# Create tag
if not tag:
tag = Tag(name=tag_name).save()
if not new_tag:
new_tag = tag.Tag(name=tag_name).save()

tag_link = self.link_to_tag(tag.name)
self.tags[tag.name] = tag_link
tag_link = self.link_to_tag(new_tag.name)
self.tags[new_tag.name] = tag_link

extra_tags |= set(tag.produces)
extra_tags |= set(new_tag.produces)

extra_tags -= set(tags)
if extra_tags:
Expand Down
12 changes: 12 additions & 0 deletions core/schemas/tag.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import datetime
import re
import unicodedata
from typing import ClassVar

from pydantic import BaseModel, Field
Expand All @@ -11,6 +13,16 @@
def future():
return datetime.timedelta(days=DEFAULT_EXPIRATION_DAYS)

def normalize_name(tag_name: str) -> str:
nfkd_form = unicodedata.normalize("NFKD", tag_name)
nfkd_form.encode("ASCII", "ignore").decode("UTF-8")
tag_name = "".join(
[c for c in nfkd_form if not unicodedata.combining(c)]
)
tag_name = tag_name.strip().lower()
tag_name = re.sub(r"\s+", "_", tag_name)
tag_name = re.sub(r"[^a-zA-Z0-9_:-]", "", tag_name)
return tag_name

class Tag(BaseModel, database_arango.ArangoYetiConnector):
_collection_name: ClassVar[str] = "tags"
Expand Down
6 changes: 3 additions & 3 deletions tests/schemas/tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,18 +171,18 @@ def test_duplicate_name(self):

def test_normalized_tag(self):
"""Tests that a tag can be normalized."""
cases = cases = [
cases = [
("H@ackërS T3st", "hackers_t3st"),
(" SpaCesStartEnd ", "spacesstartend"),
("!!Sp3cial##", "sp3cial"),
("Multi Spaces", "multi_spaces"),
("Multi Spaces After", "multi_spaces_after"),
("Élévation", "elevation"),
("UNDER_score", "under_score"),
("mixCaseMix123", "mixcasemix123"),
("MïxedÁccénts", "mixedaccents"),
("123456", "123456"),
("测试chinese", "chinese"),
("", ""),
("type:some-custom-type", "type:some-custom-type")
]

for cmp, (tag_non_norm, tag_norm) in enumerate(cases):
Expand Down