internal/loggingProviders/azureloganalytics.provider.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236

$FunctionDefinitions = {
    function Export-DataToAzure {
        <#
        .SYNOPSIS
            Function to send logging data to an Azure Workspace
 
        .DESCRIPTION
            This function is the main function that takes a PSFMessage object to log in an Azure workspace via Rest API call.
 
        .PARAMETER Message
            This is a PSFMessage object that will be converted to serialized to Json injected to an Azure workspace
 
        .EXAMPLE
            Export-DataToAzure -Message $objectToProcess
 
        .NOTES
            # Adapted from https://docs.microsoft.com/en-us/azure/log-analytics/log-analytics-data-collector-api
            Rest API documentation: https://docs.microsoft.com/en-us/rest/api/azure/
            Azure Monitor HTTP Data Collector API: https://docs.microsoft.com/en-us/azure/azure-monitor/platform/data-collector-api#request-body
 
            Azure Monitor Data collection API Constrains
            --------------------------------------------
            1. Maximum of 30 MB per post to Azure Monitor Data Collector API. This is a size limit for a single post. If the data from a single post that exceeds 30 MB, you should split the data up to smaller sized chunks and send them concurrently.
            2. Maximum of 32 KB limit for field values. If the field value is greater than 32 KB, the data will be truncated.
            3. Recommended maximum number of fields for a given type is 50. This is a practical limit from a usability and search experience perspective.
            4. A table in a Log Analytics workspace only supports up to 500 columns (referred to as a field in this article).
            5. The maximum number of characters for the column name is 500.
 
            Notes on Azure workspace table
            ------------------------------
            The table in the Azure workspace will be the LogType specified in PSFConfig. The default is 'Message'
            When looking at the tables in the Azure workspace they will always have _CL appended to them. _CL stands for (for Custom Log)
            In the final table output in the Azure workspace each property imported to the table will have its own column
            and they will be specified by the property type that was inserted to the table.
            Each Azure workspace column name will be suffixed with the data type - _d for double, _b for boolean, _s for string, etc.
 
            How to register this provider
            -----------------------------
            Set-PSFLoggingProvider -Name AzureLogAnalytics -InstanceName YourInstanceName -WorkspaceId "AzureWorkspaceId" -SharedKey "AzureWorkspaceSharedKey" -LogType "Message" -enabled $True
        #>

        
        [cmdletbinding()]
        param (
            [parameter(Mandatory = $True)]
            $Message
        )
        
        begin {
            # Grab the default configuration values for the logging provider
            $WorkspaceID = Get-ConfigValue -Name 'WorkspaceId'
            $SharedKey = Get-ConfigValue -Name 'SharedKey'
            $LogType = Get-ConfigValue -Name 'LogType'
        }
        
        process {
            # Create a custom PSObject and convert it to a Json object using UTF8 encoding
            $loggingMessage = [PSCustomObject][ordered]@{
                Message = $Message.LogMessage
                Timestamp = $Message.TimeStamp.ToUniversalTime()
                Level   = $Message.Level.ToString()
                Tags    = $Message.Tags
                Data = $Message.Data
                ComputerName = $Message.ComputerName
                Runspace = $Message.Runspace
                UserName = $Message.UserName
                ModuleName = $Message.ModuleName
                FunctionName = $Message.FunctionName
                File    = $Message.File
                CallStack = $Message.CallStack
                TargetObject = $Message.TargetObject
                ErrorRecord = $Message.ErrorRecord
            }
            
            $bodyAsJson = ConvertTo-Json $loggingMessage
            $body = [System.Text.Encoding]::UTF8.GetBytes($bodyAsJson)
            
            $restMethod = "POST"
            $restContentType = "application/json"
            $restResource = "/api/logs"
            $date = [DateTime]::UtcNow.ToString("r")
            $contentLength = $body.Length
            
            $signatureArgs = @{
                WorkspaceID        = $WorkspaceID
                SharedKey        = $SharedKey
                DateAndTime        = $date
                ContentLength   = $contentLength
                RestMethod        = $restMethod
                RestContentType = $restContentType
                RestResource    = $restResource
            }
            
            # Generate a signature needed to gain access to the Azure workspace
            $signature = Get-LogSignature @signatureArgs
            
            # RestAPI headers
            $headers = @{
                "Authorization"           = $signature
                "Log-Type"               = $logType
                "x-ms-date"               = $date
                "time-generated-field" = "TimeStamp"
            }
            
            try {
                $uri = "https://$($WorkspaceID).ods.opinsights.azure.com$($restResource)?api-version=2016-04-01"
                $webResponse = Invoke-WebRequest -Uri $uri -Method $restMethod -ContentType $restContentType -Headers $headers -Body $body -UseBasicParsing
                switch ($webResponse.StatusCode) {
                    '400' {
                        switch ($webResponse.StatusDescription) {
                            'InactiveCustomer' { throw "Sucessful Post to Azure Workspace" }
                            'InvalidApiVersion' { throw "The API version that you specified was not recognized by the service." }
                            'InvalidCustomerId' { throw "The workspace ID specified is invalid." }
                            'InvalidDataFormat' { throw "Invalid JSON was submitted. The response body might contain more information about how to resolve the error." }
                            'InvalidLogType' { throw "The log type specified contained special characters or numerics." }
                            'MissingApiVersion' { throw "The API version wasn't specified." }
                            'MissingContentType' { throw "The content type wasn't specified." }
                            'MissingLogType' { throw "The required value log type wasn't specified." }
                            'UnsupportedContentType' { throw "The content type was not set to application/json." }
                        }
                    }
                    
                    '403' { throw "The service failed to authenticate the request. Verify that the workspace ID and connection key are valid." }
                    '404' { throw "Either the URL provided is incorrect, or the request is too large." }
                    '429' { throw "The service is experiencing a high volume of data from your account. Please retry the request later." }
                    '500' { throw "The service encountered an internal error. Please retry the request." }
                    '503' { throw "The service currently is unavailable to receive requests. Please retry your request." }
                }
            }
            catch { throw }
        }
    }
    
    function Get-LogSignature {
        <#
    .SYNOPSIS
        Function for computing a signature to connect to the Azure workspace
 
    .DESCRIPTION
        This function will compute a signature that will be used to connect to the Azure workspace in order to save logging data.
 
    .PARAMETER WorkspaceID
        WorkspaceID is the unique identifer for the Log Analytics workspace, and Signature is a Hash-based Message Authentication Code (HMAC) constructed from the request and computed by using the SHA256 algorithm, and then encoded using Base64 encoding.
 
    .PARAMETER SharedKey
        This is the Azure workspace shared key.
 
    .PARAMETER DateAndTime
        The name of a field in the data that contains the timestamp of the data item. If you specify a field then its contents are used for TimeGenerated. If this field isn't specified, the default for TimeGenerated is the time that the message is ingested. The contents of the message field should follow the ISO 8601 format YYYY-MM-DDThh:mm:ssZ.
 
    .PARAMETER ContentLength
        The content length of the object being injected to the Azure workspace table
 
    .PARAMETER RestMethod
        Rest Method being used in the connection.
 
    .PARAMETER RestContentType
        Rest content type being used in the connection.
 
    .PARAMETER RestResource
        The API resource name: /api/logs.
 
    .EXAMPLE
        Get-LogSignature @inParameters
 
    .NOTES
        Any request to the Log analytics HTTP Data Collector API must include the Authorization header.
        To authenticate a request, you must sign the request with either the primary or secondary key for the workspace that is making the request and pass that signature as part of the request.
    #>

        
        [cmdletbinding()]
        param (
            $WorkspaceID,
            
            $SharedKey,
            
            $DateAndTime,
            
            $ContentLength,
            
            $RestMethod,
            
            $RestContentType,
            
            $RestResource
        )
        
        process {
            $xHeaders = "x-ms-date:" + $DateAndTime
            $stringToHash = $RestMethod + "`n" + $ContentLength + "`n" + $RestContentType + "`n" + $xHeaders + "`n" + $RestResource
            $bytesToHash = [Text.Encoding]::UTF8.GetBytes($stringToHash)
            $keyBytes = [Convert]::FromBase64String($sharedKey)
            $sha256 = New-Object System.Security.Cryptography.HMACSHA256
            $sha256.Key = $keyBytes
            $computedHash = $sha256.ComputeHash($bytesToHash)
            $encodedHash = [Convert]::ToBase64String($computedHash)
            $authorization = 'SharedKey {0}:{1}' -f $WorkspaceID, $encodedHash
            return $authorization
        }
    }
}

#region Events
$message_event = {
    param (
        $Message
    )
    
    Export-DataToAzure -Message $Message
}
#endregion Events

# Configuration values for the logging provider
$configuration_Settings = {
    Set-PSFConfig -Module PSFramework -Name 'Logging.AzureLogAnalytics.WorkspaceId' -Value "" -Initialize -Validation 'string' -Description "WorkspaceId for the Azure Workspace we are logging our data objects to."
    Set-PSFConfig -Module PSFramework -Name 'Logging.AzureLogAnalytics.SharedKey' -Value "" -Initialize -Validation 'string' -Description "SharedId for the Azure Workspace we are logging our data objects to."
    Set-PSFConfig -Module PSFramework -Name 'Logging.AzureLogAnalytics.LogType' -Value "Message" -Initialize -Validation 'string' -Description "Log type we will log information to."
}

# Registered parameters for the logging provider.
# ConfigurationDefaultValues are used for all instances of the azure logging provider
$paramRegisterPSFAzureLogAnalyticsProvider = @{
    Name               = "AzureLogAnalytics"
    Version2           = $true
    ConfigurationRoot  = 'PSFramework.Logging.AzureLogAnalytics'
    InstanceProperties = 'WorkspaceId', 'SharedKey', 'LogType'
    MessageEvent       = $message_Event
    ConfigurationSettings = $configuration_Settings
    FunctionDefinitions = $functionDefinitions
    ConfigurationDefaultValues = @{
        LogType = 'Message'
    }
}

# Register the Azure logging provider
Register-PSFLoggingProvider @paramRegisterPSFAzureLogAnalyticsProvider