Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Prepare applications for Azure deployment by generating infrastructure code, Dockerfiles, and config files.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/security.md
1# Security Hardening23Secure Azure resources following Zero Trust principles.45## Security Principles671. **Zero Trust** — Never trust, always verify82. **Least Privilege** — Minimum required permissions93. **Defense in Depth** — Multiple security layers104. **Encryption Everywhere** — At rest and in transit1112---1314## Security Services1516| Service | Use When | MCP Tools | CLI |17|---------|----------|-----------|-----|18| Key Vault | Secrets, keys, certificates | `azure__keyvault` | `az keyvault` |19| Managed Identity | Credential-free authentication | — | `az identity` |20| RBAC | Role-based access control | `azure__role` | `az role` |21| Entra ID | Identity and access management | — | `az ad` |22| Defender | Threat protection, security posture | — | `az security` |2324### MCP Tools (Preferred)2526When Azure MCP is enabled:2728**Key Vault:**29- `azure__keyvault` with command `keyvault_list` — List Key Vaults30- `azure__keyvault` with command `keyvault_secret_list` — List secrets31- `azure__keyvault` with command `keyvault_secret_get` — Get secret value32- `azure__keyvault` with command `keyvault_key_list` — List keys33- `azure__keyvault` with command `keyvault_certificate_list` — List certificates3435**RBAC:**36- `azure__role` with command `role_assignment_list` — List role assignments37- `azure__role` with command `role_definition_list` — List role definitions3839### CLI Quick Reference4041```bash42# Key Vault43az keyvault list --output table44az keyvault secret list --vault-name VAULT --output table4546# RBAC47az role assignment list --output table4849# Managed Identity50az identity list --output table51```5253---5455## Identity and Access5657### Checklist5859- [ ] Use managed identities (no credentials in code)60- [ ] Enable MFA for all users61- [ ] Apply least privilege RBAC62- [ ] Use Microsoft Entra ID for authentication63- [ ] SQL Server: Entra-only auth — NEVER generate `administratorLogin` or `administratorLoginPassword` anywhere in Bicep, including inside conditional branches (see [sql-database/auth.md](services/sql-database/auth.md))64- [ ] Review access regularly6566### Managed Identity6768```bash69# App Service70az webapp identity assign --name APP -g RG7172# Container Apps73az containerapp identity assign --name APP -g RG --system-assigned7475# Function App76az functionapp identity assign --name APP -g RG77```7879### Grant Access8081```bash82# Grant Key Vault access83az role assignment create \84--role "Key Vault Secrets User" \85--assignee IDENTITY_PRINCIPAL_ID \86--scope /subscriptions/SUB/resourceGroups/RG/providers/Microsoft.KeyVault/vaults/VAULT87```8889### Permissions Required to Grant Roles9091> ⚠️ **Important**: To assign RBAC roles to identities, you need a role with the `Microsoft.Authorization/roleAssignments/write` permission.9293| Your Role | Permissions | Recommended For |94|-----------|-------------|-----------------|95| **User Access Administrator** | Assign roles (no data access) | ✅ Least privilege for role assignment |96| **Owner** | Full access + assign roles | ❌ More permissions than needed |97| **Custom Role** | Specific permissions including roleAssignments/write | ✅ Fine-grained control |9899**Common Scenario**: Granting Storage Blob Data Owner to a Web App's managed identity100101```bash102# You need User Access Administrator (or Owner) on the Storage Account to run this:103az role assignment create \104--role "Storage Blob Data Owner" \105--assignee WEBAPP_PRINCIPAL_ID \106--scope /subscriptions/SUB/resourceGroups/RG/providers/Microsoft.Storage/storageAccounts/ACCOUNT107```108109If you encounter `AuthorizationFailed` errors when assigning roles, you likely need the User Access Administrator role at the target scope.110111### RBAC Best Practices112113| Role | Use When |114|------|----------|115| Owner | Full access + assign roles |116| Contributor | Full access except IAM |117| Reader | View-only access |118| Key Vault Secrets User | Read secrets only |119| Storage Blob Data Reader | Read blobs only |120121```bash122# Grant minimal role at resource scope123az role assignment create \124--role "Storage Blob Data Reader" \125--assignee PRINCIPAL_ID \126--scope /subscriptions/SUB/resourceGroups/RG/providers/Microsoft.Storage/storageAccounts/ACCOUNT127```128129---130131## Network Security132133### Checklist134135- [ ] Use private endpoints for PaaS services136- [ ] Configure NSGs on all subnets137- [ ] Disable public endpoints where possible138- [ ] Enable DDoS protection139- [ ] Use Azure Firewall or NVA140141### Private Endpoints142143```bash144# Create private endpoint for storage145az network private-endpoint create \146--name myEndpoint -g RG \147--vnet-name VNET --subnet SUBNET \148--private-connection-resource-id STORAGE_ID \149--group-id blob \150--connection-name myConnection151```152153### NSG Rules154155```bash156# Deny all inbound by default, allow only required traffic157az network nsg rule create \158--nsg-name NSG -g RG \159--name AllowHTTPS \160--priority 100 \161--destination-port-ranges 443 \162--access Allow163```164165### Best Practices1661671. **Default deny** — Block all traffic by default, allow only required1682. **Segment networks** — Use subnets and NSGs to isolate workloads1693. **Private endpoints** — Use for all PaaS services in production1704. **Service endpoints** — Alternative to private endpoints for simpler scenarios1715. **Azure Firewall** — Centralize egress traffic control172173---174175## Data Protection176177### Checklist178179- [ ] Enable encryption at rest (default for most Azure services)180- [ ] Use TLS 1.2+ for transit181- [ ] Store secrets in Key Vault182- [ ] Enable soft delete for Key Vault183- [ ] Use customer-managed keys (CMK) for sensitive data184185### Key Vault Security186187```bash188# Enable soft delete and purge protection189az keyvault update \190--name VAULT -g RG \191--enable-soft-delete true \192--enable-purge-protection true193194# Enable RBAC permission model195az keyvault update \196--name VAULT -g RG \197--enable-rbac-authorization true198```199200### Best Practices2012021. **Never store secrets in code** — Use Key Vault or managed identity2032. **Rotate secrets regularly** — Set expiration dates and automate rotation2043. **Enable soft delete** — Protect against accidental deletion2054. **Enable purge protection** — Prevent permanent deletion during retention2065. **Use RBAC for Key Vault** — Prefer over access policies2076. **Customer-managed keys** — For sensitive data requiring key control208209---210211## Monitoring and Defender212213### Checklist214215- [ ] Enable Microsoft Defender for Cloud216- [ ] Configure diagnostic logging217- [ ] Set up security alerts218- [ ] Enable audit logging219220### Microsoft Defender for Cloud221222```bash223# Enable Defender plans224az security pricing create \225--name VirtualMachines \226--tier Standard227```228229### Security Assessment230231Use Microsoft Defender for Cloud for:232- Security score233- Recommendations234- Compliance assessment235- Threat detection236237### Best Practices2382391. **Enable Defender** — For all production workloads2402. **Review security score** — Address high-priority recommendations2413. **Configure alerts** — Set up notifications for security events2424. **Diagnostic logs** — Enable for all resources, send to Log Analytics2435. **Audit logging** — Track administrative actions and access244245---246247## Azure Identity SDK248249All Azure SDKs use their language's Identity library for credential-free authentication. Use `DefaultAzureCredential` for **local development only**; in production, use `ManagedIdentityCredential` or another deterministic credential — see [auth-best-practices.md](auth-best-practices.md). Rust uses `DeveloperToolsCredential` as it doesn't have a `DefaultAzureCredential` equivalent.250251| Language | Package | Install |252|----------|---------|---------|253| .NET | `Azure.Identity` | `dotnet add package Azure.Identity` |254| Java | `azure-identity` | Maven: `com.azure:azure-identity` |255| JavaScript | `@azure/identity` | `npm install @azure/identity` |256| Python | `azure-identity` | `pip install azure-identity` |257| Go | `azidentity` | `go get github.com/Azure/azure-sdk-for-go/sdk/azidentity` |258| Rust | `azure_identity` | `cargo add azure_identity` |259260For Key Vault SDK examples, see: [Key Vault Reference](services/key-vault/README.md)261262For Storage SDK examples, see: [Storage Reference](services/storage/README.md)263264---265266## Further Reading267268- [Key Vault documentation](https://learn.microsoft.com/azure/key-vault/general/overview)269- [Managed identities documentation](https://learn.microsoft.com/azure/active-directory/managed-identities-azure-resources/overview)270- [Azure RBAC documentation](https://learn.microsoft.com/azure/role-based-access-control/overview)271- [Microsoft Defender for Cloud](https://learn.microsoft.com/azure/defender-for-cloud/defender-for-cloud-introduction)272