| 1 |
#!/usr/bin/env python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
"""Tool to validate artifact definitions.""" |
| 4 |
|
| 5 |
import argparse |
| 6 |
import glob |
| 7 |
import logging |
| 8 |
import os |
| 9 |
import sys |
| 10 |
|
| 11 |
from artifacts import definitions |
| 12 |
from artifacts import errors |
| 13 |
from artifacts import reader |
| 14 |
from artifacts import registry |
| 15 |
|
| 16 |
|
| 17 |
class ArtifactDefinitionsValidator(object): |
| 18 |
"""Artifact definitions validator.""" |
| 19 |
|
| 20 |
LEGACY_PATH = os.path.join('data', 'legacy.yaml') |
| 21 |
|
| 22 |
_MACOS_PRIVATE_SUB_PATHS = ('etc', 'tftpboot', 'tmp', 'var') |
| 23 |
|
| 24 |
_SUPPORTED_POSIX_USERS_VARIABLES = [ |
| 25 |
'%%users.homedir%%'] |
| 26 |
|
| 27 |
_SUPPORTED_WINDOWS_ENVIRONMENT_VARIABLES = [ |
| 28 |
'%%environ_allusersappdata%%', |
| 29 |
'%%environ_allusersprofile%%', |
| 30 |
'%%environ_programdata%%', |
| 31 |
'%%environ_programfiles%%', |
| 32 |
'%%environ_programfilesx86%%', |
| 33 |
'%%environ_systemdrive%%', |
| 34 |
'%%environ_systemroot%%', |
| 35 |
'%%environ_windir%%'] |
| 36 |
|
| 37 |
_SUPPORTED_WINDOWS_USERS_VARIABLES = [ |
| 38 |
'%%users.appdata%%', |
| 39 |
'%%users.localappdata%%', |
| 40 |
'%%users.sid%%', |
| 41 |
'%%users.temp%%', |
| 42 |
'%%users.username%%', |
| 43 |
'%%users.userprofile%%'] |
| 44 |
|
| 45 |
def __init__(self): |
| 46 |
"""Initializes an artifact definitions validator.""" |
| 47 |
super(ArtifactDefinitionsValidator, self).__init__() |
| 48 |
self._artifact_registry = registry.ArtifactDefinitionsRegistry() |
| 49 |
self._artifact_registry_key_paths = set() |
| 50 |
|
| 51 |
def _CheckGlobstarInPathSegment( |
| 52 |
self, filename, artifact_definition, path, path_segment): |
| 53 |
"""Checks if a globstar in a path segment is valid. |
| 54 |
|
| 55 |
Args: |
| 56 |
filename (str): name of the artifacts definition file. |
| 57 |
artifact_definition (ArtifactDefinition): artifact definition. |
| 58 |
path (str): path of which the path segment originated. |
| 59 |
path_segment (str): path segment to validate. |
| 60 |
|
| 61 |
Returns: |
| 62 |
bool: True if the globstar is valid. |
| 63 |
""" |
| 64 |
if not path_segment.startswith('**'): |
| 65 |
logging.warning(( |
| 66 |
f'Unuspported globstar with prefix: {path_segment:s} for path: ' |
| 67 |
f'{path:s} defined by artifact definition: ' |
| 68 |
f'{artifact_definition.name:s} in file: {filename:s}')) |
| 69 |
return False |
| 70 |
|
| 71 |
if len(path_segment) > 2: |
| 72 |
try: |
| 73 |
recursion_depth = int(path_segment[2:], 10) |
| 74 |
except (TypeError, ValueError): |
| 75 |
logging.warning(( |
| 76 |
f'Unuspported globstar with suffix: {path_segment:s} for path: ' |
| 77 |
f'{path:s} defined by artifact definition: ' |
| 78 |
f'{artifact_definition.name:s} in file: {filename:s}')) |
| 79 |
return False |
| 80 |
|
| 81 |
if recursion_depth <= 0 or recursion_depth > 10: |
| 82 |
logging.warning(( |
| 83 |
f'Globstar with unsupported recursion depth: {path_segment:s} for ' |
| 84 |
f'path: {path:s} defined by artifact definition: ' |
| 85 |
f'{artifact_definition.name:s} in file: {filename:s}')) |
| 86 |
return False |
| 87 |
|
| 88 |
return True |
| 89 |
|
| 90 |
def _CheckMacOSPaths(self, filename, artifact_definition, paths): |
| 91 |
"""Checks if the paths are valid MacOS paths. |
| 92 |
|
| 93 |
Args: |
| 94 |
filename (str): name of the artifacts definition file. |
| 95 |
artifact_definition (ArtifactDefinition): artifact definition. |
| 96 |
paths (list[str]): paths to validate. |
| 97 |
|
| 98 |
Returns: |
| 99 |
bool: True if the MacOS paths is valid. |
| 100 |
""" |
| 101 |
result = True |
| 102 |
|
| 103 |
paths_with_private = [] |
| 104 |
paths_with_symbolic_link_to_private = [] |
| 105 |
|
| 106 |
for path in paths: |
| 107 |
path_lower = path.lower() |
| 108 |
path_segments = path_lower.split('/') |
| 109 |
if not path_segments: |
| 110 |
logging.warning(( |
| 111 |
f'Empty path defined by artifact definition: ' |
| 112 |
f'{artifact_definition.name:s} in file: {filename:s}')) |
| 113 |
result = False |
| 114 |
|
| 115 |
elif len(path_segments) == 1: |
| 116 |
continue |
| 117 |
|
| 118 |
elif path_segments[1] in self._MACOS_PRIVATE_SUB_PATHS: |
| 119 |
paths_with_symbolic_link_to_private.append(path) |
| 120 |
|
| 121 |
elif path_segments[1] == 'private' and len(path_segments) >= 2: |
| 122 |
if path_segments[2] in self._MACOS_PRIVATE_SUB_PATHS: |
| 123 |
paths_with_private.append(path) |
| 124 |
|
| 125 |
else: |
| 126 |
logging.warning(( |
| 127 |
f'Unsupported private path: {path:s} defined by artifact ' |
| 128 |
f'definition: {artifact_definition.name:s} in file: ' |
| 129 |
f'{filename:s}')) |
| 130 |
result = False |
| 131 |
|
| 132 |
has_globstar = False |
| 133 |
for path_segment in path_segments: |
| 134 |
if '**' in path_segment: |
| 135 |
if has_globstar: |
| 136 |
logging.warning(( |
| 137 |
f'Unsupported path: {path:s} with multiple globstars defined ' |
| 138 |
f'by artifact definition: {artifact_definition.name:s} in ' |
| 139 |
f'file: {filename:s}')) |
| 140 |
result = False |
| 141 |
break |
| 142 |
|
| 143 |
has_globstar = True |
| 144 |
if not self._CheckGlobstarInPathSegment( |
| 145 |
filename, artifact_definition, path, path_segment): |
| 146 |
result = False |
| 147 |
|
| 148 |
if has_globstar and path.endswith('/'): |
| 149 |
logging.warning(( |
| 150 |
f'Unsupported path: {path:s} with globstar and trailing path ' |
| 151 |
f'separator defined by artifact definition: ' |
| 152 |
f'{artifact_definition.name:s} in file: {filename:s}')) |
| 153 |
result = False |
| 154 |
|
| 155 |
for private_path in paths_with_private: |
| 156 |
symbolic_link = private_path[8:] |
| 157 |
if symbolic_link not in paths_with_symbolic_link_to_private: |
| 158 |
logging.warning(( |
| 159 |
f'Missing symbolic link: {symbolic_link:s} for path: ' |
| 160 |
f'{private_path:s} defined by artifact definition: ' |
| 161 |
f'{artifact_definition.name:s} in file: {filename:s}')) |
| 162 |
result = False |
| 163 |
|
| 164 |
for path in paths_with_symbolic_link_to_private: |
| 165 |
private_path = f'/private{path:s}' |
| 166 |
if private_path not in paths_with_private: |
| 167 |
logging.warning(( |
| 168 |
f'Missing path: {private_path:s} for symbolic link: {path:s} ' |
| 169 |
f'defined by artifact definition: {artifact_definition.name:s} in ' |
| 170 |
f'file: {filename:s}')) |
| 171 |
result = False |
| 172 |
|
| 173 |
return result |
| 174 |
|
| 175 |
def _CheckPath(self, filename, artifact_definition, source, path): |
| 176 |
"""Checks if a path is valid. |
| 177 |
|
| 178 |
Args: |
| 179 |
filename (str): name of the artifacts definition file. |
| 180 |
artifact_definition (ArtifactDefinition): artifact definition. |
| 181 |
source (SourceType): source definition. |
| 182 |
path (str): path to validate. |
| 183 |
|
| 184 |
Returns: |
| 185 |
bool: True if the path is valid. |
| 186 |
""" |
| 187 |
result = True |
| 188 |
|
| 189 |
path_segments = path.split(source.separator) |
| 190 |
|
| 191 |
has_globstar = False |
| 192 |
for path_segment in path_segments: |
| 193 |
if '**' in path_segment: |
| 194 |
if has_globstar: |
| 195 |
logging.warning(( |
| 196 |
f'Unsupported path: {path:s} with multiple globstars defined by ' |
| 197 |
f'artifact definition: {artifact_definition.name:s} in file: ' |
| 198 |
f'{filename:s}')) |
| 199 |
result = False |
| 200 |
break |
| 201 |
|
| 202 |
has_globstar = True |
| 203 |
if not self._CheckGlobstarInPathSegment( |
| 204 |
filename, artifact_definition, path, path_segment): |
| 205 |
result = False |
| 206 |
|
| 207 |
if has_globstar and path.endswith(source.separator): |
| 208 |
logging.warning(( |
| 209 |
f'Unsupported path: {path:s} with globstar and trailing path ' |
| 210 |
f'separator defined by artifact definition: ' |
| 211 |
f'{artifact_definition.name:s} in file: {filename:s}')) |
| 212 |
result = False |
| 213 |
|
| 214 |
return result |
| 215 |
|
| 216 |
def _CheckWindowsPath(self, filename, artifact_definition, source, path): |
| 217 |
"""Checks if a path is a valid Windows path. |
| 218 |
|
| 219 |
Args: |
| 220 |
filename (str): name of the artifacts definition file. |
| 221 |
artifact_definition (ArtifactDefinition): artifact definition. |
| 222 |
source (SourceType): source definition. |
| 223 |
path (str): path to validate. |
| 224 |
|
| 225 |
Returns: |
| 226 |
bool: True if the Windows path is valid. |
| 227 |
""" |
| 228 |
result = True |
| 229 |
|
| 230 |
number_of_forward_slashes = path.count('/') |
| 231 |
number_of_backslashes = path.count('\\') |
| 232 |
if (number_of_forward_slashes < number_of_backslashes and |
| 233 |
source.separator != '\\'): |
| 234 |
logging.warning(( |
| 235 |
f'Incorrect path separator: {source.separator:s} in path: {path:s} ' |
| 236 |
f'defined by artifact definition: {artifact_definition.name:s} in ' |
| 237 |
f'file: {filename:s}')) |
| 238 |
result = False |
| 239 |
|
| 240 |
if source.separator != '\\': |
| 241 |
return result |
| 242 |
|
| 243 |
path_lower = path.lower() |
| 244 |
path_segments = path_lower.split(source.separator) |
| 245 |
if not path_segments: |
| 246 |
logging.warning(( |
| 247 |
f'Empty path defined by artifact definition: ' |
| 248 |
f'{artifact_definition.name:s} in file: {filename:s}')) |
| 249 |
result = False |
| 250 |
|
| 251 |
elif path_segments[0].startswith('%%users.') and path_segments[0] not in ( |
| 252 |
'%%users.appdata%%', '%%users.homedir%%', '%%users.localappdata%%', |
| 253 |
'%%users.temp%%', '%%users.username%%', '%%users.userprofile%%'): |
| 254 |
logging.warning(( |
| 255 |
f'Unsupported "{path_segments[0]:s}" in path: {path:s} defined by ' |
| 256 |
f'artifact definition: {artifact_definition.name:s} in file: ' |
| 257 |
f'{filename:s}')) |
| 258 |
result = False |
| 259 |
|
| 260 |
elif path_segments[0] == '%%users.homedir%%': |
| 261 |
logging.warning(( |
| 262 |
f'Replace "%%users.homedir%%" by "%%users.userprofile%%" in path: ' |
| 263 |
f'{path:s} defined by artifact definition: ' |
| 264 |
f'{artifact_definition.name:s} in file: {filename:s}')) |
| 265 |
result = False |
| 266 |
|
| 267 |
elif path_lower.startswith('%%users.userprofile%%\\appdata\\local\\'): |
| 268 |
logging.warning(( |
| 269 |
f'Replace "%%users.userprofile%%\\AppData\\Local" by ' |
| 270 |
f'"%%users.localappdata%%" in path: {path:s} defined by artifact ' |
| 271 |
f'definition: {artifact_definition.name:s} in file: {filename:s}')) |
| 272 |
result = False |
| 273 |
|
| 274 |
elif path_lower.startswith('%%users.userprofile%%\\appdata\\roaming\\'): |
| 275 |
logging.warning(( |
| 276 |
f'Replace "%%users.userprofile%%\\AppData\\Roaming" by ' |
| 277 |
f'"%%users.appdata%%" in path: {path:s} defined by artifact ' |
| 278 |
f'definition: {artifact_definition.name:s} in file: {filename:s}')) |
| 279 |
result = False |
| 280 |
|
| 281 |
elif path_lower.startswith('%%users.userprofile%%\\application data\\'): |
| 282 |
logging.warning(( |
| 283 |
f'Replace "%%users.userprofile%%\\Application Data" by ' |
| 284 |
f'"%%users.appdata%%" in path: {path:s} defined by artifact ' |
| 285 |
f'definition: {artifact_definition.name:s} in file: {filename:s}')) |
| 286 |
result = False |
| 287 |
|
| 288 |
elif path_lower.startswith( |
| 289 |
'%%users.userprofile%%\\local settings\\application data\\'): |
| 290 |
logging.warning(( |
| 291 |
f'Replace "%%users.userprofile%%\\Local Settings\\Application Data" ' |
| 292 |
f'by "%%users.localappdata%%" in path: {path:s} defined by artifact ' |
| 293 |
f'definition: {artifact_definition.name:s} in file: {filename:s}')) |
| 294 |
result = False |
| 295 |
|
| 296 |
has_globstar = False |
| 297 |
for path_segment in path_segments: |
| 298 |
if path_segment.startswith('%%') and path_segment.endswith('%%'): |
| 299 |
if (path_segment.startswith('%%environ_') and |
| 300 |
path_segment not in self._SUPPORTED_WINDOWS_ENVIRONMENT_VARIABLES): |
| 301 |
result = False |
| 302 |
logging.warning(( |
| 303 |
f'Artifact definition: {artifact_definition.name:s} in file: ' |
| 304 |
f'{filename:s} contains Windows path that contains an ' |
| 305 |
f'unuspported environment variable: "{path_segment:s}".')) |
| 306 |
|
| 307 |
elif (path_segment.startswith('%%users.') and |
| 308 |
path_segment not in self._SUPPORTED_WINDOWS_USERS_VARIABLES): |
| 309 |
result = False |
| 310 |
logging.warning(( |
| 311 |
f'Artifact definition: {artifact_definition.name:s} in file: ' |
| 312 |
f'{filename:s} contains Windows path that contains an ' |
| 313 |
f'unsupported users variable: "{path_segment:s}". ')) |
| 314 |
|
| 315 |
elif '**' in path_segment: |
| 316 |
if has_globstar: |
| 317 |
logging.warning(( |
| 318 |
f'Unsupported path: {path:s} with multiple globstars defined by ' |
| 319 |
f'artifact definition: {artifact_definition.name:s} in file: ' |
| 320 |
f'{filename:s}')) |
| 321 |
result = False |
| 322 |
break |
| 323 |
|
| 324 |
has_globstar = True |
| 325 |
if not self._CheckGlobstarInPathSegment( |
| 326 |
filename, artifact_definition, path, path_segment): |
| 327 |
result = False |
| 328 |
|
| 329 |
if has_globstar and path.endswith(source.separator): |
| 330 |
logging.warning(( |
| 331 |
f'Unsupported path: {path:s} with globstar and trailing path ' |
| 332 |
f'separator defined by artifact definition: ' |
| 333 |
f'{artifact_definition.name:s} in file: {filename:s}')) |
| 334 |
result = False |
| 335 |
|
| 336 |
return result |
| 337 |
|
| 338 |
def _CheckWindowsRegistryKeyPath( |
| 339 |
self, filename, artifact_definition, key_path): |
| 340 |
"""Checks if a path is a valid Windows Registry key path. |
| 341 |
|
| 342 |
Args: |
| 343 |
filename (str): name of the artifacts definition file. |
| 344 |
artifact_definition (ArtifactDefinition): artifact definition. |
| 345 |
key_path (str): Windows Registry key path to validate. |
| 346 |
|
| 347 |
Returns: |
| 348 |
bool: True if the Windows Registry key path is valid. |
| 349 |
""" |
| 350 |
result = True |
| 351 |
key_path_segments = key_path.lower().split('\\') |
| 352 |
|
| 353 |
if key_path_segments[0] == '%%current_control_set%%': |
| 354 |
result = False |
| 355 |
logging.warning(( |
| 356 |
f'Artifact definition: {artifact_definition.name:s} in file: ' |
| 357 |
f'{filename:s} contains Windows Registry key path that starts with ' |
| 358 |
f'%%CURRENT_CONTROL_SET%%. Replace %%CURRENT_CONTROL_SET%% with ' |
| 359 |
f'HKEY_LOCAL_MACHINE\\System\\CurrentControlSet')) |
| 360 |
|
| 361 |
for segment_index, key_path_segment in enumerate(key_path_segments): |
| 362 |
if key_path_segment.startswith('%%') and key_path_segment.endswith('%%'): |
| 363 |
if (segment_index == 1 and key_path_segment == '%%users.sid%%' and |
| 364 |
key_path_segments[0] == 'hkey_users'): |
| 365 |
continue |
| 366 |
|
| 367 |
if key_path_segment.startswith('%%environ_'): |
| 368 |
result = False |
| 369 |
logging.warning(( |
| 370 |
f'Artifact definition: {artifact_definition.name:s} in file: ' |
| 371 |
f'{filename:s} contains Windows Registry key path that contains ' |
| 372 |
f'an environment variable: "{key_path_segment:s}". Usage of ' |
| 373 |
f'environment variables in key paths is not encouraged at this ' |
| 374 |
f'time.')) |
| 375 |
|
| 376 |
elif key_path_segment.startswith('%%users.'): |
| 377 |
result = False |
| 378 |
logging.warning(( |
| 379 |
f'Artifact definition: {artifact_definition.name:s} in file: ' |
| 380 |
f'{filename:s} contains Windows Registry key path that contains ' |
| 381 |
f'a users variable: "{key_path_segment:s}". Usage of users ' |
| 382 |
f'variables in key paths, except for ' |
| 383 |
f'"HKEY_USERS\\%%users.sid%%", is not encouraged at this time.')) |
| 384 |
|
| 385 |
return result |
| 386 |
|
| 387 |
def _HasDuplicateRegistryKeyPaths( |
| 388 |
self, filename, artifact_definition, source): |
| 389 |
"""Checks if Registry key paths are not already defined by other artifacts. |
| 390 |
|
| 391 |
Note that at the moment this function will only find exact duplicate |
| 392 |
Registry key paths. |
| 393 |
|
| 394 |
Args: |
| 395 |
filename (str): name of the artifacts definition file. |
| 396 |
artifact_definition (ArtifactDefinition): artifact definition. |
| 397 |
source (SourceType): source definition. |
| 398 |
|
| 399 |
Returns: |
| 400 |
bool: True if the Registry key paths defined by the source type |
| 401 |
are used in other artifacts. |
| 402 |
""" |
| 403 |
result = False |
| 404 |
intersection = self._artifact_registry_key_paths.intersection( |
| 405 |
set(source.keys)) |
| 406 |
if intersection: |
| 407 |
duplicate_key_paths = '\n'.join(intersection) |
| 408 |
logging.warning(( |
| 409 |
f'Artifact definition: {artifact_definition.name:s} in file: ' |
| 410 |
f'{filename:s} has duplicate Registry key paths:\n' |
| 411 |
f'{duplicate_key_paths:s}')) |
| 412 |
result = True |
| 413 |
|
| 414 |
self._artifact_registry_key_paths.update(source.keys) |
| 415 |
return result |
| 416 |
|
| 417 |
def CheckDirectory(self, path): |
| 418 |
"""Validates the artifacts definition in a specific directory. |
| 419 |
|
| 420 |
Args: |
| 421 |
path (str): path of the directory containing the artifacts definition |
| 422 |
files. |
| 423 |
|
| 424 |
Returns: |
| 425 |
bool: True if the file contains valid artifacts definitions. |
| 426 |
""" |
| 427 |
for filename in glob.glob(os.path.join(path, '*.yaml')): |
| 428 |
result = self.CheckFile(filename) |
| 429 |
if not result: |
| 430 |
break |
| 431 |
|
| 432 |
return result |
| 433 |
|
| 434 |
def CheckFile(self, filename): |
| 435 |
"""Validates the artifacts definition in a specific file. |
| 436 |
|
| 437 |
Args: |
| 438 |
filename (str): name of the artifacts definition file. |
| 439 |
|
| 440 |
Returns: |
| 441 |
bool: True if the file contains valid artifacts definitions. |
| 442 |
""" |
| 443 |
result = True |
| 444 |
artifact_reader = reader.YamlArtifactsReader() |
| 445 |
|
| 446 |
try: |
| 447 |
for artifact_definition in artifact_reader.ReadFile(filename): |
| 448 |
try: |
| 449 |
self._artifact_registry.RegisterDefinition(artifact_definition) |
| 450 |
except KeyError: |
| 451 |
logging.warning(( |
| 452 |
f'Duplicate artifact definition: {artifact_definition.name:s} in ' |
| 453 |
f'file: {filename:s}')) |
| 454 |
result = False |
| 455 |
|
| 456 |
artifact_definition_supports_macos = ( |
| 457 |
definitions.SUPPORTED_OS_DARWIN in ( |
| 458 |
artifact_definition.supported_os)) |
| 459 |
artifact_definition_supports_windows = ( |
| 460 |
definitions.SUPPORTED_OS_WINDOWS in ( |
| 461 |
artifact_definition.supported_os)) |
| 462 |
|
| 463 |
macos_paths = [] |
| 464 |
|
| 465 |
for source in artifact_definition.sources: |
| 466 |
if source.type_indicator == definitions.TYPE_INDICATOR_DIRECTORY: |
| 467 |
logging.warning(( |
| 468 |
f'Use of deprecated source type: DIRECTORY in artifact ' |
| 469 |
f'definition: {artifact_definition.name:s} in file: ' |
| 470 |
f'{filename:s}')) |
| 471 |
|
| 472 |
if source.type_indicator in ( |
| 473 |
definitions.TYPE_INDICATOR_DIRECTORY, |
| 474 |
definitions.TYPE_INDICATOR_FILE, definitions.TYPE_INDICATOR_PATH): |
| 475 |
|
| 476 |
if (definitions.SUPPORTED_OS_DARWIN in source.supported_os or ( |
| 477 |
artifact_definition_supports_macos and |
| 478 |
not source.supported_os)): |
| 479 |
if source.separator != '/': |
| 480 |
logging.warning(( |
| 481 |
f'Use of unsupported path segment separator in artifact ' |
| 482 |
f'definition: {artifact_definition.name:s} in file: ' |
| 483 |
f'{filename:s}')) |
| 484 |
|
| 485 |
macos_paths.extend(source.paths) |
| 486 |
|
| 487 |
elif (artifact_definition_supports_windows or |
| 488 |
definitions.SUPPORTED_OS_WINDOWS in source.supported_os): |
| 489 |
for path in source.paths: |
| 490 |
if not self._CheckWindowsPath( |
| 491 |
filename, artifact_definition, source, path): |
| 492 |
result = False |
| 493 |
|
| 494 |
else: |
| 495 |
for path in source.paths: |
| 496 |
if not self._CheckPath( |
| 497 |
filename, artifact_definition, source, path): |
| 498 |
result = False |
| 499 |
|
| 500 |
elif source.type_indicator == ( |
| 501 |
definitions.TYPE_INDICATOR_WINDOWS_REGISTRY_KEY): |
| 502 |
|
| 503 |
# Exempt the legacy file from duplicate checking because it has |
| 504 |
# duplicates intentionally. |
| 505 |
if (filename != self.LEGACY_PATH and |
| 506 |
self._HasDuplicateRegistryKeyPaths( |
| 507 |
filename, artifact_definition, source)): |
| 508 |
result = False |
| 509 |
|
| 510 |
for key_path in source.keys: |
| 511 |
if not self._CheckWindowsRegistryKeyPath( |
| 512 |
filename, artifact_definition, key_path): |
| 513 |
result = False |
| 514 |
|
| 515 |
elif source.type_indicator == ( |
| 516 |
definitions.TYPE_INDICATOR_WINDOWS_REGISTRY_VALUE): |
| 517 |
|
| 518 |
for key_value_pair in source.key_value_pairs: |
| 519 |
if not self._CheckWindowsRegistryKeyPath( |
| 520 |
filename, artifact_definition, key_value_pair['key']): |
| 521 |
result = False |
| 522 |
|
| 523 |
if macos_paths: |
| 524 |
if not self._CheckMacOSPaths( |
| 525 |
filename, artifact_definition, macos_paths): |
| 526 |
result = False |
| 527 |
|
| 528 |
except errors.FormatError as exception: |
| 529 |
logging.warning( |
| 530 |
f'Unable to validate file: {filename:s} with error: {exception!s}') |
| 531 |
result = False |
| 532 |
|
| 533 |
return result |
| 534 |
|
| 535 |
def GetUndefinedArtifacts(self): |
| 536 |
"""Retrieves the names of undefined artifacts used by artifact groups. |
| 537 |
|
| 538 |
Returns: |
| 539 |
set[str]: undefined artifacts names. |
| 540 |
""" |
| 541 |
return self._artifact_registry.GetUndefinedArtifacts() |
| 542 |
|
| 543 |
|
| 544 |
def Main(): |
| 545 |
"""The main program function. |
| 546 |
|
| 547 |
Returns: |
| 548 |
bool: True if successful or False if not. |
| 549 |
""" |
| 550 |
args_parser = argparse.ArgumentParser( |
| 551 |
description='Validates an artifact definitions file.') |
| 552 |
|
| 553 |
args_parser.add_argument( |
| 554 |
'definitions', nargs='?', action='store', metavar='PATH', default=None, |
| 555 |
help=('path of the file or directory that contains the artifact ' |
| 556 |
'definitions.')) |
| 557 |
|
| 558 |
options = args_parser.parse_args() |
| 559 |
|
| 560 |
if not options.definitions: |
| 561 |
print('Source value is missing.') |
| 562 |
print('') |
| 563 |
args_parser.print_help() |
| 564 |
print('') |
| 565 |
return False |
| 566 |
|
| 567 |
if not os.path.exists(options.definitions): |
| 568 |
print(f'No such file or directory: {options.definitions:s}') |
| 569 |
print('') |
| 570 |
return False |
| 571 |
|
| 572 |
validator = ArtifactDefinitionsValidator() |
| 573 |
|
| 574 |
if os.path.isdir(options.definitions): |
| 575 |
print(f'Validating definitions in: {options.definitions:s}/*.yaml') |
| 576 |
result = validator.CheckDirectory(options.definitions) |
| 577 |
|
| 578 |
elif os.path.isfile(options.definitions): |
| 579 |
print(f'Validating definitions in: {options.definitions:s}') |
| 580 |
result = validator.CheckFile(options.definitions) |
| 581 |
|
| 582 |
if not result: |
| 583 |
print('FAILURE') |
| 584 |
return False |
| 585 |
|
| 586 |
print('SUCCESS') |
| 587 |
return True |
| 588 |
|
| 589 |
|
| 590 |
if __name__ == '__main__': |
| 591 |
if not Main(): |
| 592 |
sys.exit(1) |
| 593 |
else: |
| 594 |
sys.exit(0) |