Saturday, December 9, 2017

Azure ARM Template for Service Health

There have been a few changes to how Azure is surfacing health data for the platform itself.  For more information on a new service in preview, please see this link.  Monitoring the platform for health issues is an important part of any overall monitoring strategy.

In the examples, they provide a walk through of how to enable this service using the portal.  This post expands on that by providing an ARM template to do the same thing (in this case an email alert).

You can apply this to any of your subs.

Enjoy!

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "actionGroups_name": {
            "defaultValue": "SubHealth",
            "type": "String"
        },
        "activityLogAlerts_name": {
            "defaultValue": "ServiceHealthActivityLogAlert",
            "type": "String"
        },
        "emailAddress":{
            "type":"string"
        }
    },
    "variables": {
        "alertScope":"[concat('/','subscriptions','/',subscription().subscriptionId)]"
    },
    "resources": [
        {
            "comments": "Action Group",
            "type": "microsoft.insights/actionGroups",
            "name": "[parameters('actionGroups_name')]",
            "apiVersion": "2017-04-01",
            "location": "Global",
            "tags": {},
            "scale": null,
            "properties": {
                "groupShortName": "[parameters('actionGroups_name')]",
                "enabled": true,
                "emailReceivers": [
                    {
                        "name": "[parameters('actionGroups_name')]",
                        "emailAddress": "[parameters('emailAddress')]"
                    }
                ],
                "smsReceivers": [],
                "webhookReceivers": []
            },
            "dependsOn": []
        },
        {
            "comments": "Service Health Activity Log Alert",
            "type": "microsoft.insights/activityLogAlerts",
            "name": "[parameters('activityLogAlerts_name')]",
            "apiVersion": "2017-04-01",
            "location": "Global",
            "tags": {},
            "scale": null,
            "properties": {
                "scopes": [
                    "[variables('alertScope')]"
                ],
                "condition": {
                    "allOf": [
                        {
                            "field": "category",
                            "equals": "ServiceHealth"
                        },
                        {
                            "field": "properties.incidentType",
                            "equals": "Incident"
                        }
                    ]
                },
                "actions": {
                    "actionGroups": [
                        {
                            "actionGroupId": "[resourceId('microsoft.insights/actionGroups', parameters('actionGroups_name'))]",
                            "webhookProperties": {}
                        }
                    ]
                },
                "enabled": true,
                "description": ""
            },
            "dependsOn": [
                "[resourceId('microsoft.insights/actionGroups', parameters('actionGroups_name'))]"
            ]
        }
    ]
}


Tuesday, December 5, 2017

Azure SQL Database Firewall Rules

Today I had a use case where I wanted to grant external access to one of my Azure SQL databases that was hosted in an elastic pool along with other client databases. 

If you are unaware, in Azure SQL, there are two types of firewalls.  The first is at the server level, and applies to all databases hosted on a particular server.  The second is a "database" firewall, where rules are configured within a given database an only applies to that database.  The second method is particularly useful when using Azure SQL and opting for a "database-contained" approach. 

You can read more about the different database firewall options and the order of operations here.

In any event, database contained firewall rules fits my need as I want to grant access to only a single database on a particular server.

To view the firewall rules currently in place:
select * from sys.database_firewall_rules

To add a new rule:
execute sp_set_firewall_rule @name = 'fwrule' @start_ip_address = '<ip>', @end_ip_address  = '<ip>'

To remove a rule:
execute sp_delete_database_firewall_rule @name = 'fwrule'

One key thing to note is that database firewall rules do not appear on the Azure portal, and are probably not reviewed in either Azure Security Center, Threat Detection, or otherwise.  As far as I can tell, these events are also not part of SQL diagnostics.  The only service that might capture them is the audit, but those would need to be reviewed manually.

Remember to actively review your databases for these types of changes and limit who can make them.

Thursday, November 23, 2017

Change UPN Script for Azure AD Operations

In a lot of Azure deployments, synchronizing identity plays a key part of the overall delivery.  This allows one to utilize on-premises accounts/passwords/etc when logging into Azure, and makes overall management easier.

In some client cases, their internal active directory structure might utilize a domain name that is not routeable on the internet.  This poses a problem, particularly with office 365 integration.  Clients want to log in with their .com address, lets say, but AD Connect ends up syncing a .local address instead.

The fix to this can be quite simple, and generally involves setting the user UPN to the correct external address.  Doing this on an existing domain can be a little painful.  There are some scripts out there that do this, but I wanted to write my own and include some powershell features such as -whatif.

Here is the script, enjoy!


[CmdletBinding(SupportsShouldProcess=$true)]
param(
    [Parameter(Mandatory=$true,HelpMessage="The old suffix to look for")]
    [string]$oldSuffix,
    [Parameter(Mandatory=$true,HelpMessage="The new suffix to set")]
    [string]$newSuffix,
    [Parameter(Mandatory=$true,HelpMessage="The ou to filter for")]
    [string]$ou,
    [Parameter(Mandatory=$true,HelpMessage="The server to target")]
    [string]$server
)

Import-Module ActiveDirectory 
"Oldsuffix: $oldSuffix"
"Newsuffix: $newSuffix"
"ou: $ou"
"server: $server"

$users = Get-AdUser -Filter "*" -SearchBase "$ou"

if (-not $users){
    "Found no users with specified filter"
    exit    
}

foreach ($user in $users){
    "===== Processing {0}" -f $user.UserPrincipalName
    
    if (-not ($user.UserPrincipalName -like ("*$oldSuffix"))){
        "Users UPN suffix does not match oldsuffix, skipping.."
        continue
    }

    if ($user.UserPrincipalName -like ("*$newSuffix")){
        "User is already set correctly, skipping..."
        continue
    }
    
     
    if ($PSCmdlet.ShouldProcess("Updating user")){
        $newUpn = $user.UserPrincipalName.Replace($oldSuffix,$newSuffix)
        $user | Set-ADUser -server $server -UserPrincipalName $newUpn
        "Changed suffix"
    } else {
        "Would have replaced suffix"
    }
}