| 1 |
import copy |
| 2 |
import json |
| 3 |
from pathlib import Path |
| 4 |
from typing import List, Dict |
| 5 |
from urllib.parse import urlparse |
| 6 |
|
| 7 |
from azure.kusto.data.helpers import get_string_tail_lower_case |
| 8 |
from azure.kusto.data.security import _is_local_address |
| 9 |
from .exceptions import KustoClientInvalidConnectionStringException |
| 10 |
from .helpers import load_bundled_json |
| 11 |
|
| 12 |
|
| 13 |
class MatchRule: |
| 14 |
def __init__(self, suffix, exact): |
| 15 |
self.suffix = suffix.lower() |
| 16 |
self.exact = exact |
| 17 |
|
| 18 |
|
| 19 |
class FastSuffixMatcher: |
| 20 |
def __init__(self, rules: List[MatchRule]): |
| 21 |
self._suffix_length = min(len(rule.suffix) for rule in rules) |
| 22 |
_processed_rules: Dict[str, List] = {} |
| 23 |
for rule in rules: |
| 24 |
suffix = get_string_tail_lower_case(rule.suffix, self._suffix_length) |
| 25 |
if suffix not in _processed_rules: |
| 26 |
_processed_rules[suffix] = [] |
| 27 |
_processed_rules[suffix].append(rule) |
| 28 |
|
| 29 |
self.rules = _processed_rules |
| 30 |
|
| 31 |
def is_match(self, candidate): |
| 32 |
if len(candidate) < self._suffix_length: |
| 33 |
return False |
| 34 |
|
| 35 |
_match_rules = self.rules.get(get_string_tail_lower_case(candidate, self._suffix_length)) |
| 36 |
if _match_rules: |
| 37 |
for rule in _match_rules: |
| 38 |
if candidate.lower().endswith(rule.suffix): |
| 39 |
if len(candidate) == len(rule.suffix) or not rule.exact: |
| 40 |
return True |
| 41 |
|
| 42 |
return False |
| 43 |
|
| 44 |
|
| 45 |
def create_fast_suffix_matcher_from_existing(rules: List[MatchRule], existing: FastSuffixMatcher) -> FastSuffixMatcher: |
| 46 |
if existing is None or len(existing.rules) == 0: |
| 47 |
return FastSuffixMatcher(rules) |
| 48 |
|
| 49 |
if not rules: |
| 50 |
return existing |
| 51 |
|
| 52 |
return FastSuffixMatcher([*copy.deepcopy(rules), *(v for item in existing.rules.values() for v in item)]) |
| 53 |
|
| 54 |
|
| 55 |
class KustoTrustedEndpoints: |
| 56 |
def __init__(self): |
| 57 |
self._matchers = { |
| 58 |
k: FastSuffixMatcher( |
| 59 |
[*(MatchRule(suffix, False) for suffix in v["AllowedKustoSuffixes"]), *(MatchRule(hostname, True) for hostname in v["AllowedKustoHostnames"])] |
| 60 |
) |
| 61 |
for (k, v) in _well_known_kusto_endpoints_data["AllowedEndpointsByLogin"].items() |
| 62 |
} |
| 63 |
|
| 64 |
self._additional_matcher = None |
| 65 |
self._override_matcher = None |
| 66 |
|
| 67 |
def set_override_matcher(self, matcher): |
| 68 |
self._override_matcher = matcher |
| 69 |
|
| 70 |
def add_trusted_hosts(self, rules, replace): |
| 71 |
if rules is None or not rules: |
| 72 |
if replace: |
| 73 |
self._additional_matcher = None |
| 74 |
return |
| 75 |
|
| 76 |
self._additional_matcher = create_fast_suffix_matcher_from_existing(rules, None if replace else self._additional_matcher) |
| 77 |
|
| 78 |
def validate_trusted_endpoint(self, endpoint: str, login_endpoint: str): |
| 79 |
hostname = urlparse(endpoint).hostname |
| 80 |
self.validate_hostname_is_trusted(hostname if hostname is not None else endpoint, login_endpoint) |
| 81 |
|
| 82 |
def validate_hostname_is_trusted(self, hostname: str, login_endpoint: str): |
| 83 |
if _is_local_address(hostname): |
| 84 |
return |
| 85 |
if self._override_matcher is not None: |
| 86 |
if self._override_matcher(hostname): |
| 87 |
return |
| 88 |
else: |
| 89 |
matcher = self._matchers.get(login_endpoint.lower()) |
| 90 |
if matcher is not None and matcher.is_match(hostname): |
| 91 |
return |
| 92 |
|
| 93 |
matcher = self._additional_matcher |
| 94 |
if matcher is not None and matcher.is_match(hostname): |
| 95 |
return |
| 96 |
|
| 97 |
raise KustoClientInvalidConnectionStringException( |
| 98 |
f"Can't communicate with '{hostname}' as this hostname is currently not trusted; please see " f"https://aka.ms/kustotrustedendpoints" |
| 99 |
) |
| 100 |
|
| 101 |
def set_override_policy(self, matcher): |
| 102 |
self._override_matcher = matcher |
| 103 |
|
| 104 |
|
| 105 |
_well_known_kusto_endpoints_data = load_bundled_json("wellKnownKustoEndpoints.json") |
| 106 |
well_known_kusto_endpoints = KustoTrustedEndpoints() |