Azure Introduction
Azure Pricing
Azure Threats
Enable Blob Storage Lifecycle Management
More Info:
Ensure that Azure Blob Storage service has a lifecycle management policy configured.
Risk Level
Medium
Address
Security
Compliance Standards
SOC2, PCIDSS
Triage and Remediation
Remediation
Sure, here are the step by step instructions to enable Blob Storage Lifecycle Management in Azure using Azure console:
- Open the Azure portal and navigate to the storage account that needs to be remediated.
- In the left-hand menu, click on “Lifecycle management” under the “Blob service” section.
- Click on the ”+ Add” button to create a new lifecycle management rule.
- In the “Name” field, give a name to the rule.
- In the “Prefix matches” field, enter the prefix of the blob names that you want to apply the rule to. You can also leave it blank to apply the rule to all blobs.
- In the “Blob types” field, select the type of blobs that you want to apply the rule to.
- In the “Action” field, select the action that you want to perform on the blobs that match the rule. For example, you can choose to delete or move the blobs to a different storage tier.
- In the “Days after last modification” field, enter the number of days after which the action should be performed on the blobs.
- Click on the “Review + create” button to review the rule details.
- Once you have reviewed the details, click on the “Create” button to create the rule.
That’s it! The lifecycle management rule has been created and will be applied to the blobs that match the rule criteria.
To remediate the misconfiguration of enabling Blob Storage Lifecycle Management in Azure using Azure CLI, follow these steps:
-
Open the Azure CLI on your local machine or cloud shell.
-
Login to your Azure account using the following command:
az login
-
Once you are logged in, set the subscription that you want to work with using the following command:
az account set --subscription <subscription_id>
Replace
<subscription_id>
with the ID of the subscription that you want to use. -
Next, create a policy definition that will enable Blob Storage Lifecycle Management using the following command:
az policy definition create --name 'Enable Blob Storage Lifecycle Management' --mode All --rules 'https://raw.githubusercontent.com/Azure/azure-policy/master/samples/Storage/storage-account-enable-blob-lifecycle-management/azurepolicy.rules.json' --params 'https://raw.githubusercontent.com/Azure/azure-policy/master/samples/Storage/storage-account-enable-blob-lifecycle-management/azurepolicy.parameters.json'
-
Assign the policy definition to the scope where you want to enable Blob Storage Lifecycle Management using the following command:
az policy assignment create --name 'Enable Blob Storage Lifecycle Management' --scope <scope> --policy "Enable Blob Storage Lifecycle Management"
Replace
<scope>
with the scope where you want to enable Blob Storage Lifecycle Management. This can be a resource group, subscription, or management group. -
Verify that the policy is applied correctly by checking the compliance status using the following command:
az policy state list --resource-type Microsoft.Storage/storageAccounts --query "[?policyDefinitionAction=='deny'].{name:policyDefinitionName,description:policyDefinitionDescription,compliant:isCompliant}"
This command will show you the compliance status of the policy. If it is compliant, then Blob Storage Lifecycle Management is enabled.
That’s it! You have now remediated the misconfiguration of enabling Blob Storage Lifecycle Management in Azure using Azure CLI.
To remediate the misconfiguration of enabling Blob Storage Lifecycle Management in Azure using Python, you can follow the below steps:
Step 1: Install the Azure Blob Storage library for Python using the command pip install azure-storage-blob
.
Step 2: Import the necessary libraries in your Python script.
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, BlobProperties, ContentSettings
Step 3: Authenticate with Azure using your Azure Storage Account name and access key.
account_name = '<your-storage-account-name>'
account_key = '<your-storage-account-key>'
blob_service_client = BlobServiceClient(account_url=f"https://{account_name}.blob.core.windows.net", credential=account_key)
Step 4: Get the container client object for which you want to enable the Blob Storage Lifecycle Management.
container_client = blob_service_client.get_container_client('<your-container-name>')
Step 5: Define the Blob Storage Lifecycle Management policy using the set_blob_lifecycle()
method of the container client object. The policy should be defined in a JSON format.
lifecycle_policy = {
"rules": [
{
"name": "Delete blob",
"type": "Lifecycle",
"definition": {
"filters": {
"blobTypes": [
"blockBlob"
],
"prefixMatch": [
"logs/"
]
},
"actions": {
"baseBlob": {
"delete": {
"daysAfterModificationGreaterThan": 30
}
}
}
}
}
]
}
container_client.set_blob_lifecycle(lifecycle_policy)
Step 6: Run the Python script to enable Blob Storage Lifecycle Management for the specified container.
After running the above Python script, the Blob Storage Lifecycle Management will be enabled for the specified container in Azure.