solarwinds / OrionSDK

SDK for the SolarWinds Orion platform, including tools, documentation, and samples in PowerShell, C#, Go, Perl, and Java.
https://thwack.com/OrionSDK
Apache License 2.0
400 stars 142 forks source link

Syntax error for Copy Query As / curl (cmd || bash) #334

Open kmsigma opened 1 year ago

kmsigma commented 1 year ago

When a query is copied using the Edit / Copy Query As / Curl (Cmd) or Edit / Copy Query As / Curl (Bash), it does not contain surrounding quotes. This is necessary for any query that contains parenthesis. For this example, I've seen issues with functions like GETUTCDATE() or parenthetical clauses WHERE ( This AND THAT )

Query Used

SELECT TOP 10 Caption
      , IP
      , CONCAT(Caption,'"/"',IP) AS [ComboName]
      , GETUTCDATE() AS [RightNow]
FROM Orion.Nodes
WHERE IP LIKE '192.168.%.%'

As Produced

Copy Query As / Curl (cmd)

curl.exe -k -u MyUsername:MyC0mpl3xP@ssw0rd https://solarwinds.domain.local:17778/SolarWinds/InformationService/v3/Json/Query?query=SELECT+TOP+1+Caption%2c+IP%2c+CONCAT(Caption%2c+%27%22%2f%22%27%2c+IP)+AS+%5bComboName%5d%2c+GETUTCDATE()+AS+%5bRightNow%5d+FROM+Orion.Nodes+WHERE+IP+LIKE+%27192.168.%25.%25%27

Copy Query As / Curl (bash)

curl -k -u MyUsername:MyC0mpl3xP@ssw0rd curl -k -u MyUsername:MyC0mpl3xP@ssw0rd https://solarwinds.domain.local:17778/SolarWinds/InformationService/v3/Json/Query?query=SELECT+TOP+1+Caption%2c+IP%2c+CONCAT(Caption%2c+%27%22%2f%22%27%2c+IP)+AS+%5bComboName%5d%2c+GETUTCDATE()+AS+%5bRightNow%5d+FROM+Orion.Nodes+WHERE+IP+LIKE+%27192.168.%25.%25%27

Results when executed in selected shell

Copy Query As / Curl (cmd)

Using Command Prompt as Shell (cmd.exe)

C:\Users\LocalUser> curl.exe -k -u MyUsername:MyC0mpl3xP@ssw0rd https://solarwinds.domain.local:17778/SolarWinds/InformationService/v3/Json/Query?query=SELECT+TOP+1+Caption%2c+IP%2c+CONCAT(Caption%2c+%27%22%2f%22%27%2c+IP)+AS+%5bComboName%5d%2c+GETUTCDATE()+AS+%5bRightNow%5d+FROM+Orion.Nodes+WHERE+IP+LIKE+%27192.168.%25.%25%27
{"results":[{"Caption":"KMS-NAS","IP":"192.168.21.200","ComboName":"KMS-NAS\"/\"192.168.21.200","RightNow":"2023-03-29T16:22:28.5000000"}]}

Returns results as expected.

Using Windows PowerShell as Shell (powershell.exe)

PS C:\Users\LocalUser> curl.exe -k -u MyUsername:MyC0mpl3xP@ssw0rd https://solarwinds.domain.local:17778/SolarWinds/InformationService/v3/Json/Query?query=SELECT+TOP+1+Caption%2c+IP%2c+CONCAT(Caption%2c+%27%22%2f%22%27%2c+IP)+AS+%5bComboName%5d%2c+GETUTCDATE()+AS+%5bRightNow%5d+FROM+Orion.Nodes+WHERE+IP+LIKE+%27192.168.%25.%25%27
At line:1 char:152
+ ... v3/Json/Query?query=SELECT+TOP+1+Caption%2c+IP%2c+GETUTCDATE()+AS+%5b ...
+                                                                  ~
An expression was expected after '('.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ExpectedExpression

Generates a parsing error.

Using PowerShell as Shell (pwsh.exe on Windows) and (pwsh on *NIX)

PS C:\Users\LocalUser> curl.exe -k -u MyUsername:MyC0mpl3xP@ssw0rd https://solarwinds.domain.local:17778/SolarWinds/InformationService/v3/Json/Query?query=SELECT+TOP+1+Caption%2c+IP%2c+CONCAT(Caption%2c+%27%22%2f%22%27%2c+IP)+AS+%5bComboName%5d%2c+GETUTCDATE()+AS+%5bRightNow%5d+FROM+Orion.Nodes+WHERE+IP+LIKE+%27192.168.%25.%25%27
ParserError:
Line |
   1 |  … v3/Json/Query?query=SELECT+TOP+1+Caption%2c+IP%2c+GETUTCDATE()+AS+%5b …
     |                                                                 ~
     | An expression was expected after '('.

Generates a parsing error.

Copy Query As / Curl (bash)

username@LINUXHOST:~ $ curl -k -u curl -k -u MyUsername:MyC0mpl3xP@ssw0rd https://solarwinds.domain.local:17778/SolarWinds/InformationService/v3/Json/Query?query=SELECT+TOP+1+Caption%2c+IP%2c+CONCAT(Caption%2c+%27%22%2f%22%27%2c+IP)+AS+%5bComboName%5d%2c+GETUTCDATE()+AS+%5bRightNow%5d+FROM+Orion.Nodes+WHERE+IP+LIKE+%27192.168.%25.%25%27
-bash: syntax error near unexpected token `('

Generates a parsing error.

Possible Solution

Surround the entire URI with single quotes (') or double-quotes (") so that the native parser reads it as a single element. This should not be an issue with the query converter because the query has already been URL enocoded which strips ' and ".