microsoftgraph / microsoftgraph/msgraph-sdk-python

Event loop is closed using RemovePasswordPostRequestBody

Đang mở
#1,093 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

priority:p2 type:bug
Ngôn ngữ chính
Python
Star
630
Fork
96
Merge trung bình
15 giờ 20 phút
Pull request đã merge (30 ngày)
3

Mô tả

Describe the bug

I am trying to delete multiple app secrets in Azure. One key is deleted but it does not advance. I can add secrets just fine. I can delete ONE secret typically. But deleting multiple secrets ends with "Event loop is closed".

Expected behavior

I can add secrets just fine. I can delete ONE secret -- typically. But multiple fail as above.

How to reproduce

This code reproduces the problem:

from msgraph import GraphServiceClient
from azure.identity import ClientSecretCredential
from creds import tenant_id, client_id, client_secret

from msgraph.generated.applications.item.remove_password.remove_password_post_request_body import RemovePasswordPostRequestBody

credential = ClientSecretCredential(
    tenant_id=tenant_id,
    client_id=client_id,
    client_secret=client_secret
)

from uuid import UUID

scopes = ['https://graph.microsoft.com/.default']
graph_client = GraphServiceClient(credential, scopes)

async def show_secrets():
    result = await graph_client.applications.by_application_id(OBJECT_ID).get()
    return result

async def delete_older_secrets():
    try:
        for one_secret in str_not_uuids:
            print(f"== {one_secret}")
            if one_secret == "__CURRENT__":
                print("passing current")
                continue 
            request_body = RemovePasswordPostRequestBody(key_id=UUID(one_secret),)
            result = await graph_client.applications.by_application_id(
                "OBJECT_ID"
            ).remove_password.post(request_body)
    except Exception as e:
        print(e)
    else:
        print('ok')


if __name__ == '__main__':
    import asyncio
    import operator
    all_secrets=asyncio.run(show_secrets())
    str_not_uuids=[str(x.key_id) for x in all_secrets.password_credentials]
    asyncio.run(delete_older_secrets())

SDK Version
pip freeze | grep -iE "graph|azure"
azure-common==1.1.28
azure-core==1.32.0
azure-graphrbac==0.61.2
azure-identity==1.19.0
cryptography==44.0.0
microsoft-kiota-authentication-azure==1.7.1
msgraph-beta-sdk==1.17.0
msgraph-core==1.2.0
msgraph-sdk==1.17.0
msrestazure==0.6.4.post1
Latest version known to work for scenario above?

unknown

Known Workarounds

I can use msal in a non asyncio loop:

import msal
import requests

def remove_application_secrets(tenant_id, client_id, client_secret, secret_ids):

    authority = f"[https://login.microsoftonline.com/{tenant_id}](https://login.microsoftonline.com/%7Btenant_id%7D)"
    app = msal.ConfidentialClientApplication(
    client_id,
    authority=authority,
    client_credential=client_secret

)

result = app.acquire_token_for_client(scopes=["[https://graph.microsoft.com/.default"]](https://graph.microsoft.com/.default%22]))

if "access_token" not in result:
    print(result.get("error"))
    print(result.get("error_description"))
    return

# Remove each secret
headers = {
'Authorization': 'Bearer ' + result['access_token'],
'Content-Type': 'application/json'
}


for secret_id in secret_ids:

try:

   url = f"[https://graph.microsoft.com/v1.0/applications/{object_id}/removePassword] 
   (https://graph.microsoft.com/v1.0/applications/%7Bobject_id%7D/removePassword)"

    payload = {"keyId": secret_id}
    response = requests.post(url, headers=headers, json=payload)
    if response.status_code == 204:
    print(f"Successfully removed secret with ID: {secret_id}")

else:

    print(f"Failed to remove secret {secret_id}. Status code: {response.status_code}")
    print(response.text)

except Exception as e:
    print(f"Error removing secret {secret_id}: {str(e)}")
    remove_application_secrets(

client_id=client_id,
tenant_id=tenant_id,
client_secret=client_secret,
secret_ids=['blah1', 'blah2']
)
Debug output

Event loop is closed

Configuration
cat /etc/os-release 
PRETTY_NAME="Ubuntu 24.04.1 LTS"
NAME="Ubuntu"
VERSION_ID="24.04"
VERSION="24.04.1 LTS (Noble Numbat)"
VERSION_CODENAME=noble
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=noble
LOGO=ubuntu-logo
Other information

No response

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng cách chạy luồng asyncio.run sử dụng GraphServiceClient, ClientSecretCredential và remove_password.post từ báo cáo, sau đó kiểm tra cách client và credential quản lý các tài nguyên gắn với event loop qua các lần gọi lặp lại. Hoàn thành khi việc xóa nhiều application secret hoàn tất mà không có "Event loop is closed" và hành vi này được bao phủ bởi một regression test.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
azure, python
Lĩnh vực
api, authentication
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.