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

Validate that zowe.config.json file matches schema #192

Merged
merged 37 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
883a0fc
moved the validation logic into ProfileManager
aadityasinha-dotcom Jun 26, 2023
e83fb65
added validation error warning
aadityasinha-dotcom Jun 26, 2023
ffc42e8
added jsonschema exceptions
aadityasinha-dotcom Jun 27, 2023
267f700
fixed indentation
aadityasinha-dotcom Jun 27, 2023
6465b67
raised error
aadityasinha-dotcom Jun 28, 2023
b93d818
fixed test
aadityasinha-dotcom Jun 28, 2023
69d983f
enhanced the validators.py and made some changes to validate schema
aadityasinha-dotcom Jun 29, 2023
f07ebec
fixed tests
aadityasinha-dotcom Jun 29, 2023
8f8d80c
fixed validator if there is not path, fixed raising exceptions for va…
aadityasinha-dotcom Jun 30, 2023
c2ae1ec
moved the validate_schema method to ConfigFile class
aadityasinha-dotcom Jul 6, 2023
6108ebf
changelog
aadityasinha-dotcom Jul 7, 2023
042a4ac
moved the validation logic to init_from_file so the validate_schema m…
aadityasinha-dotcom Jul 9, 2023
708da24
fixed test
aadityasinha-dotcom Jul 10, 2023
19ede33
changes
aadityasinha-dotcom Jul 10, 2023
dd0be07
simplified path_config_json
aadityasinha-dotcom Jul 12, 2023
5902381
added test for valide schema more test will be added soon
aadityasinha-dotcom Jul 17, 2023
8d65e9f
changed the opt_in flag to opt_out
aadityasinha-dotcom Jul 21, 2023
81c4b44
Merge branch 'main' into validate_schema
aadityasinha-dotcom Jul 30, 2023
8a94fdc
fix test
aadityasinha-dotcom Jul 31, 2023
600439a
added test for invalid schema
aadityasinha-dotcom Aug 1, 2023
7b2673b
Merge branch 'main' into validate_schema
aadityasinha-dotcom Aug 5, 2023
e01ffb0
refactored the code
aadityasinha-dotcom Aug 10, 2023
0030a10
added test for invalid schema and updated requirements.txt
aadityasinha-dotcom Aug 11, 2023
8edec11
added test for internet URI
aadityasinha-dotcom Aug 17, 2023
6f34785
did some changes, added unit test for internet URI
aadityasinha-dotcom Aug 21, 2023
cba960c
few changes added file protocol, fixed tests
aadityasinha-dotcom Aug 28, 2023
535ca88
Merge branch 'main' into validate_schema
aadityasinha-dotcom Aug 30, 2023
c0b9d9b
Merge branch 'main' into validate_schema
aadityasinha-dotcom Aug 31, 2023
6917e60
removed verify parameter
aadityasinha-dotcom Sep 4, 2023
fc5aa3a
removed config_type parameter
aadityasinha-dotcom Sep 13, 2023
26e997f
Merge branch 'main' into validate_schema
aadityasinha-dotcom Sep 13, 2023
1ba06a2
Merge branch 'main' into validate_schema
aadityasinha-dotcom Sep 14, 2023
561b370
fix tests
aadityasinha-dotcom Sep 19, 2023
f749d60
attempt to add the entire env/lib dir
zFernand0 Sep 27, 2023
e355a7c
cannot assume that env/lib is the reason why the merging didn't work
zFernand0 Sep 27, 2023
b824ac0
try to load jsonschema
zFernand0 Sep 27, 2023
a7e61a9
Merge branch 'main' into validate_schema
zFernand0 Sep 27, 2023
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
16 changes: 16 additions & 0 deletions src/core/zowe/core_for_zowe_sdk/config_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class ConfigFile:
4. Contents of the file.
4.1 Profiles
4.2 Defaults
4.3 Schema Property
5. Secure Properties associated with the file.
"""

Expand All @@ -77,6 +78,7 @@ class ConfigFile:
_location: Optional[str] = None
profiles: Optional[dict] = None
defaults: Optional[dict] = None
schema_property: Optional[dict] = None
secure_props: Optional[dict] = None
_missing_secure_props: list = field(default_factory=list)

Expand Down Expand Up @@ -120,6 +122,7 @@ def init_from_file(self) -> None:
profile_jsonc = commentjson.load(fileobj)
aadityasinha-dotcom marked this conversation as resolved.
Show resolved Hide resolved

self.profiles = profile_jsonc.get("profiles", {})
self.schema_property = profile_jsonc.get("$schema", None)
aadityasinha-dotcom marked this conversation as resolved.
Show resolved Hide resolved
self.defaults = profile_jsonc.get("defaults", {})

# loading secure props is done in load_profile_properties
Expand Down Expand Up @@ -294,6 +297,19 @@ def load_profile_properties(self, profile_name: str) -> dict:

return props

def load_schema(self) -> str:
aadityasinha-dotcom marked this conversation as resolved.
Show resolved Hide resolved
"""
Find the schema file path from the $schema_property in the config.json
Returns
-------
Str

file_path to the schema property
"""

file_path = self.schema_property
return file_path

def load_secure_props(self) -> None:
"""
load secure_props stored for the given config file
Expand Down
53 changes: 53 additions & 0 deletions src/core/zowe/core_for_zowe_sdk/profile_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@

import os.path
import warnings
import jsonschema
from typing import Optional

from .config_file import ConfigFile, Profile
from .validators import validate_config_json
from .custom_warnings import (
ConfigNotFoundWarning,
ProfileNotFoundWarning,
Expand Down Expand Up @@ -169,12 +171,57 @@ def get_profile(
)
finally:
return cfg_profile

@staticmethod
def load_schema(
cfg: ConfigFile,
path_config_json: str,
opt_in: Optional[bool] = True,
) -> str:
"""
Get the $schema_property from the config and load the schema

Returns
-------
file_path to the $schema property
"""

path_schema_json = None
try:
path_schema_json = cfg.load_schema()
if path_schema_json is None: # check if the $schema property is not defined
warnings.warn(
f"$schema property could not found"
)

# validate the $schema property
if path_schema_json and opt_in:
validate_config_json(path_config_json, path_schema_json)
aadityasinha-dotcom marked this conversation as resolved.
Show resolved Hide resolved
except jsonschema.ValidationError as exc:
raise Exception(
f"Instance was invalid under the provided $schema property, {exc}"
)
except jsonschema.FormatError:
raise Exception(
f"Validating a format {path_config_json} failed for {path_schema_json}"
)
except jsonschema.UndefinedTypeCheck as exc:
raise Exception(
f"A type checker was asked to check a type it did not have registered, {exc}"
)
except jsonschema.UnknownType:
raise Exception(
f"Unknown type is found in {path_schema_json}"
)
finally:
aadityasinha-dotcom marked this conversation as resolved.
Show resolved Hide resolved
return path_schema_json

def load(
self,
profile_name: Optional[str] = None,
profile_type: Optional[str] = None,
check_missing_props: bool = True,
opt_in: Optional[bool] = True,
) -> dict:
"""Load connection details from a team config profile.
Returns
Expand Down Expand Up @@ -209,6 +256,7 @@ def load(
"Global Config": self.global_config,
}
profile_props: dict = {}
schema_path = None

missing_secure_props = [] # track which secure props were not loaded

Expand All @@ -224,6 +272,11 @@ def load(
) # Define profile name that will be merged from other layers
profile_props = {**profile_loaded.data, **profile_props}

# Loading $schema property from project config
if config_type in ("Project Config"):
path_config_json = cfg._location + "/zowe.config.json"
schema_path = self.load_schema(cfg, path_config_json, opt_in)
aadityasinha-dotcom marked this conversation as resolved.
Show resolved Hide resolved

missing_secure_props.extend(profile_loaded.missing_secure_props)

if i == 1 and profile_props:
Expand Down