willthames / kubernetes-validate

Other
45 stars 5 forks source link

Python 2.7 jsonschema.exceptions.RefResolutionError #8

Closed msven closed 2 years ago

msven commented 3 years ago

I ran into the following error with kubernetes_validate version 1.20.0 in an environment that is using python 2.7 (I do realize python 2.7 is not "officially" supported any longer...)

Traceback (most recent call last):
  File "/usr/local/bin/kubernetes-validate", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python2.7/site-packages/kubernetes_validate/__main__.py", line 50, in main
    utils.validate(resource, version, args.strict)
  File "/usr/local/lib/python2.7/site-packages/kubernetes_validate/utils.py", line 77, in validate
    jsonschema.validate(data, schema, resolver=resolver)
  File "/usr/local/lib/python2.7/site-packages/jsonschema/validators.py", line 932, in validate
    error = exceptions.best_match(validator.iter_errors(instance))
  File "/usr/local/lib/python2.7/site-packages/jsonschema/exceptions.py", line 367, in best_match
    best = next(errors, None)
  File "/usr/local/lib/python2.7/site-packages/jsonschema/validators.py", line 328, in iter_errors
    for error in errors:
  File "/usr/local/lib/python2.7/site-packages/jsonschema/_validators.py", line 259, in ref
    scope, resolved = validator.resolver.resolve(ref)
  File "/usr/local/lib/python2.7/site-packages/jsonschema/validators.py", line 766, in resolve
    return url, self._remote_cache(url)
  File "/usr/local/lib/python2.7/site-packages/functools32/functools32.py", line 400, in wrapper
    result = user_function(*args, **kwds)
  File "/usr/local/lib/python2.7/site-packages/jsonschema/validators.py", line 779, in resolve_from_url
    raise exceptions.RefResolutionError(exc)
jsonschema.exceptions.RefResolutionError: <urlopen error [Errno 2] No such file or directory: '/local/lib/python2.7/site-packages/kubernetes_validate/kubernetes-json-schema/v1.21.0-local/_definitions.json'>

The above error was produced with the following command run from the root directory of the repo

docker run --rm -it -v $PWD:/python python:2.7-alpine3.7 /bin/sh -c 'cd python; pip install --upgrade pip; pip install .; kubernetes-validate ./examples/kuard-extra-property.yaml'

Version 1.19.0 does not have this issue. The bug looks to have been introduced by https://github.com/willthames/kubernetes-validate/commit/06dbf5328ffad319c2f34650face591427e2589c#diff-944d057cf1c21162526aada56b570f35793f7569e1085903acd1b14334a0f756 and the following change fixes it

kubernetes-validate$ git diff src/kubernetes_validate/utils.py
diff --git a/src/kubernetes_validate/utils.py b/src/kubernetes_validate/utils.py
index f97c268..c95b769 100644
--- a/src/kubernetes_validate/utils.py
+++ b/src/kubernetes_validate/utils.py
@@ -71,7 +71,7 @@ def validate(data, desired_version, strict=False):
     finally:
         f.close()
     schema_dir = os.path.dirname(os.path.abspath(schema_file))
-    resolver = jsonschema.RefResolver(base_uri='file:///' + schema_dir.replace("\\", "/") + '/', referrer=schema)
+    resolver = jsonschema.RefResolver(base_uri='file://' + schema_dir.replace("\\", "/") + '/', referrer=schema)

     try:
         jsonschema.validate(data, schema, resolver=resolver)

The change also works on python3.

willthames commented 2 years ago

Fixed thanks to @msven's changes in #9 (which were cherry-picked rather than merged)