مشاركة عبر


إنشاء صورة واستخدام هوية مُدارة معينة من قِبل المستخدم للوصول إلى الملفات في حساب Azure Storage

ينطبق على: ✔️ أجهزة Linux الظاهرية ✔️ مجموعات مقياس مرنة

توضح هذه المقالة كيفية إنشاء صورة مخصصة باستخدام Azure VM Image Builder. تستخدم الخدمة هوية مدارة معينة من قبل المستخدم للوصول إلى الملفات في حساب تخزين Azure، ويمكنها تحقيق حظر الوصول غير المصادق عليه إلى حساب التخزين.

يدعم Azure VM Image Builder استخدام البرامج النصية ونسخ الملفات من GitHub وحسابات تخزين Azure ومواقع أخرى. إذا كنت ترغب في استخدام المواقع، يجب أن تكون متاحة خارجياً لـ VM Image Builder.

في المثال أدناه، ستقوم بإنشاء مجموعتين من الموارد، سيتم استخدام إحداهما للصورة المخصصة، وستستضيف الأخرى حساب تخزين Azure الذي يحتوي على ملف برنامج نصي. هذا المثال يحاكي سيناريو الحياة الواقعية، حيث قد يكون لديك بيانات اصطناعية للبناء، أو ملفات صور في حسابات تخزين مختلفة. ستقوم بإنشاء هوية معينة من قبل المستخدم ثم منح أذونات قراءة الهوية على ملف البرنامج النصي، ولكنك لن تسمح بالوصول العام إلى الملف. بعد ذلك، ستستخدم أداة تخصيص Shell لتنزيل هذا البرنامج النصي وتشغيله من حساب التخزين.

إنشاء مجموعة موارد

  1. نظرًا إلى أنك ستستخدم بعض المعلومات بشكل متكرر، فقم بإنشاء بعض المتغيرات لتخزين تلك المعلومات.

    # Image resource group name 
    imageResourceGroup=aibmdimsi
    # Storage resource group
    strResourceGroup=aibmdimsistor
    # Location 
    location=WestUS2
    # Name of the image to be created
    imageName=aibCustLinuxImgMsi01
    # Image distribution metadata reference name
    runOutputName=u1804ManImgMsiro
    
  2. قم بإنشاء متغير لمعرف الاشتراك الخاص بك:

    subscriptionID=$(az account show --query id --output tsv)
    
  3. أنشئ مجموعات الموارد لكل من الصورة وتخزين البرنامج النصي:

    # Create a resource group for the image template
    az group create -n $imageResourceGroup -l $location
    # Create a resource group for the script storage
    az group create -n $strResourceGroup -l $location
    
  4. إنشاء هوية يعينها المستخدم، وتعيين الأذونات على مجموعة الموارد:

    يستخدم VM Image Builder هوية المستخدم المتوفرة لإدخال الصورة في مجموعة الموارد. في هذا المثال، تقوم بإنشاء تعريف دور Azure بإجراءات محددة لتوزيع الصورة. ثم يتم تعيين تعريف الدور لهوية المستخدم.

    # Create a user-assigned identity for VM Image Builder to access the storage account where the script is located
    identityName=aibBuiUserId$(date +'%s')
    az identity create -g $imageResourceGroup -n $identityName
    
    # Get an identity ID
    imgBuilderCliId=$(az identity show -g $imageResourceGroup -n $identityName --query clientId -o tsv)
    
    # Get the user-identity URI, which is needed for the template
    imgBuilderId=/subscriptions/$subscriptionID/resourcegroups/$imageResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/$identityName
    
    # Download the preconfigured role definition example
    curl https://raw.githubusercontent.com/azure/azvmimagebuilder/master/solutions/12_Creating_AIB_Security_Roles/aibRoleImageCreation.json -o aibRoleImageCreation.json
    
    # Update the definition
    sed -i -e "s/<subscriptionID>/$subscriptionID/g" aibRoleImageCreation.json
    sed -i -e "s/<rgName>/$imageResourceGroup/g" aibRoleImageCreation.json
    
    # Create role definitions
    az role definition create --role-definition ./aibRoleImageCreation.json
    
    # Grant the role definition to the user-assigned identity
    az role assignment create \
        --assignee $imgBuilderCliId \
        --role "Azure Image Builder Service Image Creation Role" \
        --scope /subscriptions/$subscriptionID/resourceGroups/$imageResourceGroup
    
  5. أنشئ حساب التخزين، وانسخ نموذج البرنامج النصي إليه من GitHub:

    # Script storage account
    scriptStorageAcc=aibstorscript$(date +'%s')
    
    # Script container
    scriptStorageAccContainer=scriptscont$(date +'%s')
    
    # Script URL
    scriptUrl=https://$scriptStorageAcc.blob.core.windows.net/$scriptStorageAccContainer/customizeScript.sh
    
    # Create the storage account and blob in the resource group
    az storage account create -n $scriptStorageAcc -g $strResourceGroup -l $location --sku Standard_LRS
    
    az storage container create -n $scriptStorageAccContainer --fail-on-exist --account-name $scriptStorageAcc
    
    # Copy in an example script from the GitHub repo 
    az storage blob copy start \
        --destination-blob customizeScript.sh \
        --destination-container $scriptStorageAccContainer \
        --account-name $scriptStorageAcc \
        --source-uri https://raw.githubusercontent.com/azure/azvmimagebuilder/master/quickquickstarts/customizeScript.sh
    
  6. امنح VM Image Builder إذناً لإنشاء موارد في مجموعة موارد الصور. تعد القيمة --assignee معرف هوية المستخدم.

    az role assignment create \
        --assignee $imgBuilderCliId \
        --role "Storage Blob Data Reader" \
        --scope /subscriptions/$subscriptionID/resourceGroups/$strResourceGroup/providers/Microsoft.Storage/storageAccounts/$scriptStorageAcc/blobServices/default/containers/$scriptStorageAccContainer 
    

تعديل المثال

قم بتنزيل مثال ملف JSON وتكوينه باستخدام المتغيرات التي أنشأتها مسبقاً.

curl https://raw.githubusercontent.com/azure/azvmimagebuilder/master/quickquickstarts/7_Creating_Custom_Image_using_MSI_to_Access_Storage/helloImageTemplateMsi.json -o helloImageTemplateMsi.json
sed -i -e "s/<subscriptionID>/$subscriptionID/g" helloImageTemplateMsi.json
sed -i -e "s/<rgName>/$imageResourceGroup/g" helloImageTemplateMsi.json
sed -i -e "s/<region>/$location/g" helloImageTemplateMsi.json
sed -i -e "s/<imageName>/$imageName/g" helloImageTemplateMsi.json
sed -i -e "s%<scriptUrl>%$scriptUrl%g" helloImageTemplateMsi.json
sed -i -e "s%<imgBuilderId>%$imgBuilderId%g" helloImageTemplateMsi.json
sed -i -e "s%<runOutputName>%$runOutputName%g" helloImageTemplateMsi.json

إنشاء الصورة

  1. أرسل تكوين الصورة إلى خدمة VM Image Builder:

    az resource create \
        --resource-group $imageResourceGroup \
        --properties @helloImageTemplateMsi.json \
        --is-full-object \
        --resource-type Microsoft.VirtualMachineImages/imageTemplates \
        -n helloImageTemplateMsi01
    
  2. ابدأ بناء الصورة:

    az resource invoke-action \
        --resource-group $imageResourceGroup \
        --resource-type  Microsoft.VirtualMachineImages/imageTemplates \
        -n helloImageTemplateMsi01 \
        --action Run 
    

قد يستغرق الإنشاء حوالي 15 دقيقة للانتهاء.

قم بإنشاء جهاز ظاهري

  1. أنشئ جهازاً ظاهرياً من الصورة:

    az vm create \
    --resource-group $imageResourceGroup \
    --name aibImgVm00 \
    --admin-username aibuser \
    --image $imageName \
    --location $location \
    --generate-ssh-keys
    
  2. بعد إنشاء الجهاز الظاهري، ابدأ جلسة عمل Secure Shell (SSH) معه.

    ssh aibuser@<publicIp>
    

بعد إنشاء اتصال SSH، يجب أن ترى أنه تم تلقي «رسالة اليوم» توضح أن الصورة قد تم تخصيصها:


*******************************************************
**            This VM was built from the:            **
**      !! AZURE VM IMAGE BUILDER Custom Image !!    **
**         You have just been Customized :-)         **
*******************************************************

تفريغ مواردك

إذا لم تعد بحاجة إلى الموارد التي تم إنشاؤها أثناء هذه العملية، يمكنك حذفها عن طريق تشغيل التعليمة البرمجية التالية:


az role definition delete --name "$imageRoleDefName"
```azurecli-interactive
az role assignment delete \
    --assignee $imgBuilderCliId \
    --role "$imageRoleDefName" \
    --scope /subscriptions/$subscriptionID/resourceGroups/$imageResourceGroup
az identity delete --ids $imgBuilderId
az resource delete \
    --resource-group $imageResourceGroup \
    --resource-type Microsoft.VirtualMachineImages/imageTemplates \
    -n helloImageTemplateMsi01
az group delete -n $imageResourceGroup
az group delete -n $strResourceGroup

الخطوات التالية

إذا كان لديك أي مشاكل في استخدام VM Image Builder، فراجع استكشاف أخطاء Azure VM Image Builder وإصلاحها.