Matar
Matar

Reputation: 13

Python3 : Azure Key Vault Keys, creating RAS key : TypeError

Following example in :

https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/keyvault/azure-keyvault-keys

whenever I run the below code :

rsa_key = key_client.create_rsa_key("rsa-key-demo", size=2048)
print(rsa_key.name)
print(rsa_key.key_type)

The key will be created in my Azure keyVult, however I always get the following error :

Traceback (most recent call last): File "c:/Users/user/Desktop/azure_keyValut_demo.py", line 152, in rsa_key = key_client.create_rsa_key("rsa-key-demo", size=2048) File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\azure\core\tracing\decorator.py", line 62, in wrapper_use_tracer return func(*args, **kwargs) # type: ignore File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\azure\keyvault\keys_client.py", line 131, in create_rsa_key return self.create_key(name, key_type="RSA-HSM" if hsm else "RSA", **kwargs) File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\azure\core\tracing\decorator.py", line 62, in wrapper_use_tracer return func(*args, **kwargs) # type: ignore File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\azure\keyvault\keys_client.py", line 96, in create_key **kwargs File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\azure\keyvault\keys_shared_generated\v7_0\operations_key_vault_client_operations.py", line 96, in create_key deserialized = self._deserialize('KeyBundle', response) File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\msrest\serialization.py", line 1180, in call data = self._unpack_content(response_data, content_type) File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\msrest\serialization.py", line 1334, in unpack_content return json.loads(data) File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\json_init.py", line 348, in loads 'not {!r}'.format(s.class.name)) TypeError: the JSON object must be str, bytes or bytearray, not 'method'


I didn't quite understand why that exception is being thrown.

I found this on stackoverflow but still couldn't understand what's going on:

Why won't python3 load this json string?


The code run successfully with no issues with Python2.7

The exception will be thrown for all key operations (create, delete, get)

It also happens with all Cryptographic operations

Upvotes: 1

Views: 422

Answers (1)

Jim Xu
Jim Xu

Reputation: 23141

According to my test, if we use msrest under version 0.6.x, we will get the error. My test steps are as below

My requirements.txt

azure-common==1.1.23
azure-core==1.1.1
azure-identity==1.1.0
azure-keyvault-keys==4.0.0
certifi==2019.11.28
cffi==1.13.2
chardet==3.0.4
cryptography==2.8
idna==2.8
isodate==0.6.0
msal==1.0.0
msal-extensions==0.1.3
msrest==0.5.5
oauthlib==3.1.0
portalocker==1.5.2
pycparser==2.19
PyJWT==1.7.1
pywin32==227
requests==2.22.0
requests-oauthlib==1.3.0
six==1.13.0
urllib3==1.25.7

My code

from azure.identity import ClientSecretCredential
from azure.keyvault.keys import KeyClient

tenant_id=''
client_id=''
client_secret=''
credential = ClientSecretCredential(tenant_id, client_id, client_secret)
client = KeyClient("https://testkey08.vault.azure.net/", credential)

key = client.create_rsa_key("rsa-key-demo", size=2048)
print(key.name)
print(key.key_type)

enter image description here

So, please update the merest to 0.6.x. The detailed steps are as below

  1. Create project
python -m venv mytestenv # Might be "python3" or "py -3.6" depending on your Python installation
cd mytestenv
source bin/activate      # Linux shells (Bash, ZSH, etc.)
scripts\activate         # Windows shells (PowerShell, CMD)
  1. install SDK My requirements.txt
azure-common==1.1.23
azure-core==1.1.1
azure-identity==1.1.0
azure-keyvault-keys==4.0.0
certifi==2019.11.28
cffi==1.13.2
chardet==3.0.4
cryptography==2.8
idna==2.8
isodate==0.6.0
msal==1.0.0
msal-extensions==0.1.3
msrest==0.6.10
oauthlib==3.1.0
portalocker==1.5.2
pycparser==2.19
PyJWT==1.7.1
pywin32==227
requests==2.22.0
requests-oauthlib==1.3.0
six==1.13.0
urllib3==1.25.7
xlrd==1.2.0

pip install -r requirements.txt
  1. Code
from azure.identity import ClientSecretCredential
from azure.keyvault.keys import KeyClient

tenant_id=''
client_id=''
client_secret=''
credential = ClientSecretCredential(tenant_id, client_id, client_secret)
client = KeyClient("https://testkey08.vault.azure.net/", credential)

key = client.create_rsa_key("rsa-key-demo", size=2048)
print(key.name)
print(key.key_type)

enter image description here

Upvotes: 0

Related Questions