Skip to content

Instantly share code, notes, and snippets.

@mikekistler
Last active January 24, 2023 00:45
Show Gist options
  • Save mikekistler/9a5550d669310d8dd2cf6c372c45696c to your computer and use it in GitHub Desktop.
Save mikekistler/9a5550d669310d8dd2cf6c372c45696c to your computer and use it in GitHub Desktop.
Directions for running Restler with the test Proxy to capture service traffic

Restler + Test Recording Proxy

This gist will describe how to run Restler with the Test Recording Proxy to capture service traffic that that later be validated by the Swagger Accuracy tooling.

We'll use the Azure App Configuration service in this example but the concepts should generalize to any Azure service.

Clone and build Restler

git clone https://github.com/microsoft/restler-fuzzer.git
restler_bin=~/bin/restler
mkdir -p $restler_bin
# Need --python because it can’t find “python” — I use an alias that it did not understand.
python ./build-restler.py --dest_dir $restler_bin --python_path /opt/homebrew/bin/python3

Install test proxy

dotnet tool update azure.sdk.tools.testproxy --global --add-source https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-net/nuget/v3/index.json --version "1.0.0-dev*"

This will install to ~/.dotnet/tools. You need to add this to your path if not done previously.

Create the initial Restler config and dict files

Lets try Azure Databricks.

specs=$(find /Users/mikekistler/Projects/Azure/azure-rest-api-specs/specification/databricks/resource-manager/Microsoft.Databricks/preview/2022-04-01-preview -type f -depth 1)
dotnet $restler_bin/restler/Restler.dll generate_config --specs ${=specs}

Add values to restler_custom_payload of dict.json

vars='{
   "subscriptionId": ["my-subscription-id"],
   "resourceGroupName": ["my-resource-group"],
   "location": ["my-location"]
}'
jq ".restler_custom_payload = $vars" restlerConfig/dict.json > dict.json

Also set:

    "api-version": [
      "2022-04-01-preview"
    ]

Update engineSettings.json

Add the host and port for the test-proxy to the RESTler engine settings.  This will send traffic to the recorder.

jq '.host = "localhost" | .target_port = 5000 | .no_ssl: true' restlerConfig/engine_settings.json > engine_settings.json

Update config.json

We need to update config.json CustomDictionaryFilePath to point to our modified dict.json and AnnotationFilePath to point to our annotations.json:

jq '.CustomDictionaryFilePath = "dict.json" | del(.AnnotationFilePath) | .DataFuzzing = false' restlerConfig/config.json > config.json

Compile with the base configuration

Compile using this base configuration. This is just to make sure there are no issues to this point.

dotnet $restler_bin/restler/Restler.dll compile config.json

Start the test proxy

In a separate terminal window/tab, start the test proxy with the location to save recordings.

test-proxy --storage-location c:\recordings

From your test session window, start a recording session with a request to the proxy. The response of this request will contain an "x-recording-id" header. Save the value in recording_id variable.

body='{ "x-recording-file": "databricks" }'
recording_id=$(curl -X POST -s -D - -d $body http://localhost:5000/Record/Start | grep 'x-recording-id' | awk '{print $2}' | sed 's/\r$//')

Update the Restler custom dictionary with test-proxy headers

header='{                                                 
  "x-recording-upstream-base-uri": ["https://management.azure.com"],
  "x-recording-id": ["'${recording_id}'"],
  "x-recording-mode": ["record"]
}'
jq ".restler_custom_payload_header = $header" dict.json > tmp.json && mv tmp.json dict.json 

And recompile to incorporate these changes.

dotnet $restler_bin/restler/Restler.dll compile config.json

Run the tests

Now you are ready to run the Test phase of Restler.

dotnet $restler_bin/restler/Restler.dll test --dictionary_file Compile/dict.json --grammar_file Compile/grammar.py --settings Compile/engine_settings.json --token_refresh_command "bash $PWD/getToken.sh" --token_refresh_interval 60

Stop the recording

Send the following request to the test proxy:

curl -X POST -D - -H  "x-recording-id: ${recording_id}" -H 'Content-Length: 0' http://localhost:5000/Record/Stop

Upload the Recording

Add assets.json for recording to azure-rest-api-specs

Update the recording

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment