File: mgmt_test_helper.py

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (59 lines) | stat: -rw-r--r-- 1,805 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from azure.mgmt.msi import ManagedServiceIdentityClient
from azure.mgmt.keyvault import KeyVaultManagementClient
from azure.keyvault.keys import KeyClient


def create_managed_identity(
    msi_client: ManagedServiceIdentityClient, msi_name, rg_name, location
):
    msi = msi_client.user_assigned_identities.create_or_update(
        rg_name, msi_name, {"location": location}
    )
    return msi


def create_key_vault(
    kv_client: KeyVaultManagementClient,
    akv_name,
    rg_name,
    location,
    msi,
    tenant_id,
    object_id,
):
    resource_poller = kv_client.vaults.begin_create_or_update(
        rg_name,
        akv_name,
        {
            "location": location,
            "properties": {
                "sku": {"name": "standard", "family": "A"},
                "tenant_id": tenant_id,
                "enable_soft_delete": True,
                "enable_purge_protection": True,
                "enabled_for_deployment": True,
                "enabled_for_disk_encryption": True,
                "enabled_for_template_deployment": True,
                "access_policies": [
                    {
                        "tenant_id": tenant_id,
                        "object_id": object_id,
                        "permissions": {"keys": ["all"], "secrets": ["all"]},
                    },
                    {
                        "tenant_id": msi.tenant_id,
                        "object_id": msi.principal_id,
                        "permissions": {"keys": ["all"]},
                    },
                ],
            },
        },
    )
    akv = resource_poller.result()
    return akv


def create_key(akv, credential, key_name):
    key_client = KeyClient(akv.properties.vault_uri, credential)
    key = key_client.create_rsa_key(key_name)
    return key