pythonjsondebuggingapache-ranger

Apache-ranger python library JSONDecoder error


So i'm trying to create a policy using the ranger python client, and this is my code (redacted confidential info for security purposes)

from apache_ranger.model.ranger_service import *
from apache_ranger.client.ranger_client import *
from apache_ranger.model.ranger_policy  import *


ranger_url  = 'http://************.com:***'
ranger_auth = ('**********', '**********')

ranger = RangerClient(ranger_url, ranger_auth)

policy           = RangerPolicy()
policy.service   = 'starburst-enterprise'
policy.name      = 'test_policy'
policy.resources = { 'database': RangerPolicyResource({ 'values': ['hive'] }),
                     'table':    RangerPolicyResource({ 'values': ['uber_rides'] }),
                     'column':   RangerPolicyResource({ 'values': ['*'] }) }

allowItem1          = RangerPolicyItem()
allowItem1.groups   = [ 'Data Product Owner' ]
allowItem1.accesses = [ RangerPolicyItemAccess({ 'type': 'select' }) ]

denyItem1          = RangerPolicyItem()
denyItem1.groups    = [ 'Data Product Owner' ]
denyItem1.accesses = [ RangerPolicyItemAccess({ 'type': 'drop' }) ]

policy.policyItems     = [ allowItem1 ]
policy.denyPolicyItems = [ denyItem1 ]

print('Creating policy: name=' + policy.name)

created_policy = ranger.create_policy(policy)

print('    created policy: name=' + created_policy.name + ', id=' + str(created_policy.id))

But i receive this error, When i tried to debug it i found that, the reason it was going for an exception is because the http.post method inside the ranger_clien.py was returning a response (<Response [400]>) that is not handled by any of the if or elifs blocks hence it goes to else block and enters RaiseServiceException(), when i dug deeper i found some comments in the function that gives JSONDecoder error and it said that exception would be raised only if JSON input is not valid or if simplejson is not installed.

Traceback (most recent call last):
  File "c:\Users\j.shimoga.prakash\Desktop\pls work - Copy\test.py", line 37, in <module>
    created_policy = ranger.create_policy(policy)
  File "C:\Users\j.shimoga.prakash\Desktop\pls work - Copy\venv\lib\site-packages\apache_ranger\client\ranger_client.py", line 124, in create_policy
    resp = self.__call_api(RangerClient.CREATE_POLICY, params, policy)
  File "C:\Users\j.shimoga.prakash\Desktop\pls work - Copy\venv\lib\site-packages\apache_ranger\client\ranger_client.py",  
  File "C:\Users\j.shimoga.prakash\Desktop\pls work - Copy\venv\lib\site-packages\simplejson\decoder.py", line 400, in raw_decode
    return self.scan_once(s, idx=_w(s, idx).end())simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Solution

  • Figured out the issue i just had to pass 'https' in ranger_url instead of 'http'