-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcreate-eventhub.ps1
66 lines (56 loc) · 3.39 KB
/
create-eventhub.ps1
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
60
61
62
63
64
65
66
$resourceGroupName = "azsec-rg"
$location = "westus"
$eventHubNameSpaceName = "azsec-eventhub"
$windowsEventHubName = "windows_vm"
$linuxEventHubName = "linux_vm"
$windowsAuthorizationRuleName = "windows-policy"
$linuxAuthorizationRuleName = "linux-policy"
# Create Event Hub Namespace
New-AzureRmEventHubNamespace -ResourceGroupName $resourceGroupName `
-NamespaceName $eventHubNameSpaceName `
-SkuName "Standard"
-Location $location
# Create Windows Event Hub
New-AzureRmEventHub -ResourceGroupName $resourceGroupName `
-NamespaceName $eventHubNameSpaceName
-EventHubName $windowsEventHubName `
-MessageRetentionInDays 3
# Create Linux Event Hub
New-AzureRmEventHub -ResourceGroupName $resourceGroupName `
-NamespaceName $eventHubNameSpaceName
-EventHubName $linuxEventHubName `
-MessageRetentionInDays 3
# Create Windows Authorization Rule (Policy)
New-AzureRmEventHubAuthorizationRule -ResourceGroupName $resourceGroupName `
-NamespaceName $eventHubNameSpaceName `
-EventHubName $windowsEventHubName `
-AuthorizationRuleName $windowsAuthorizationRuleName `
-Rights @("Listen","Send")
# Create Linux Authorization Rule (Policy)
New-AzureRmEventHubAuthorizationRule -ResourceGroupName $resourceGroupName `
-NamespaceName $eventHubNameSpaceName `
-EventHubName $linuxEventHubName `
-AuthorizationRuleName $linuxAuthorizationRuleName `
-Rights @("Listen","Send")
# Get Windows Event Hub Shared Access Key
Get-AzureRmEventHubKey -ResourceGroupName $resourceGroupName `
-NamespaceName $eventHubNameSpaceName `
-EventHubName $windowsEventHubName `
-AuthorizationRuleName $windowsAuthorizationRuleName
# Get Windows Event Hub Shared Access Key
$linuxSharedAccessKey = Get-AzureRmEventHubKey -ResourceGroupName $resourceGroupName `
-NamespaceName $eventHubNameSpaceName `
-EventHubName $linuxEventHubName `
-AuthorizationRuleName $linuxAuthorizationRuleName
# Get Linux Event Hub SAS URL
[Reflection.Assembly]::LoadWithPartialName("System.Web")| out-null
$uri = "https//" + $eventHubNameSpaceName + ".servicebus.windows.net/" + $linuxEventHubName
$linuxSharedAccessKey = $linuxSharedAccessKey
$expires = ([DateTimeOffset]::Now.ToUnixTimeSeconds())+180
$signatureString=[System.Web.HttpUtility]::UrlEncode($uri)+ "`n" + [string]$expires
$HMAC = New-Object System.Security.Cryptography.HMACSHA256
$HMAC.key = [Text.Encoding]::ASCII.GetBytes($linuxSharedAccessKey)
$signature = $HMAC.ComputeHash([Text.Encoding]::ASCII.GetBytes($signatureString))
$signature = [Convert]::ToBase64String($Signature)
$sasUrl = "SharedAccessSignature sr=" + [System.Web.HttpUtility]::UrlEncode($uri) + "&sig=" + [System.Web.HttpUtility]::UrlEncode($signature) + "&se=" + $Expires + "&skn=" + $linuxEventHubName
Write-Host -ForegroundColor Yellow "Your Linux Event Hub SAS URL is:" $sasUrl