Azurite https SSL 証明書の検証エラー
ローカルとAzure上で、Azure Storageに接続する際、同じコードにしたいので
DefaultAzureCredentialでAzuriteに接続したいと思った
test.py
config = os.environ
credential = DefaultAzureCredential()
storage_account_url = config["STORAGE_ACCOUNT_URL"]
blob_service = BlobServiceClient(storage_account_url, credential)
containers = blob_service.list_containers(include_metadata=True)
for container in containers:
logging.info(f"{container['name']} {container['metadata']}")
STORAGE_ACCOUNT_URL=https://127.0.0.1:10000/devstoreaccount1
Azuriteをhttpsで起動する
READMEにある様に、mkcertを使って、証明書の発行に必要なルート証明書を作成し、Azurite に使用できる派生証明書を作成して、それらを使ってAzuriteをhttpsで起動した
https://github.com/Azure/Azurite?tab=readme-ov-file#pem
brew install mkcert
mkcert -install
mkcert 127.0.0.1
Created a new certificate valid for the following names 📜
- "127.0.0.1"
The certificate is at "./127.0.0.1.pem" and the key at "./127.0.0.1-key.pem" ✅
It will expire on 18 October 2026 🗓
azurite --location .azurite --debug azurite.log --cert ./127.0.0.1.pem --key ./127.0.0.1-key.pem --oauth basic
test.pyを実行すると、HTTPS リクエストの SSL 証明書の検証エラーが発生した
error.txt
Exception: ServiceRequestError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1007)
どうやらpyenvだと、mkcert -installでインストールしたルート証明を見てくれてない・・・
% python
Python 3.10.13 (main, Oct 23 2023, 13:08:08) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import certifi
>>> certifi.where()
'/Users/********/.anyenv/envs/pyenv/versions/3.10.13/lib/python3.10/site-packages/certifi/cacert.pem'
% mkcert -CAROOT
/Users/********/Library/Application Support/mkcert
% cd "/Users/********/Library/Application Support/mkcert"
% ls
rootCA-key.pem rootCA.pem
環境変数にセットして、再度実行したら解決した
.zshrc
REQUESTS_CA_BUNDLE="/Users/********/Library/Application Support/mkcert/rootCA.pem"