From fec4a9e7cd28b234ace3adf0611987abdeee9ea6 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Wed, 12 Apr 2023 16:33:12 +0200 Subject: [PATCH] Enable CORS on localosmosis (backport #4891) (#4896) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Enable CORS on localosmosis (#4891) Closes: [#4879](https://github.com/osmosis-labs/osmosis/issues/4879) ## What is the purpose of the change Enables cors on localOsmosis for: - rpc - api - gRPC-web ## Brief Changelog - Enable cors by default on localosmosis ## Testing and Verifying Start localosmosis: ```bash make localnet-start ``` Check file changes: ```bash ❯ cat ~/.osmosisd-local/config/config.toml | grep cors cors_allowed_headers = ["Origin", "Accept", "Content-Type", "X-Requested-With", "X-Server-Time", "Accept-Encoding"] cors_allowed_methods = ["HEAD", "GET", "POST", "DELETE", "OPTIONS", "PATCH", "PUT"] cors_allowed_origins = ["*"] ``` ```bash ❯ cat ~/.osmosisd-local/config/app.toml | grep cors enabled-unsafe-cors = true enable-unsafe-cors = true ``` ## Documentation and Release Note - Does this pull request introduce a new feature or user-facing behavior changes? no - Is a relevant changelog entry added to the `Unreleased` section in `CHANGELOG.md`? yes - How is the feature or change documented? not applicable (cherry picked from commit 3b2e1680b239490051fc0a9d5f8c6e6f4dcc50bf) # Conflicts: # CHANGELOG.md # tests/localosmosis/scripts/setup.sh * Update CHANGELOG.md * Align setup.sh * Update create_two_asset_pool() --------- Co-authored-by: Niccolo Raspa <6024049+niccoloraspa@users.noreply.github.com> Co-authored-by: Niccolo Raspa --- tests/localosmosis/scripts/setup.sh | 56 ++++++++++++------- .../state_export/scripts/start.sh | 19 +++++++ 2 files changed, 56 insertions(+), 19 deletions(-) diff --git a/tests/localosmosis/scripts/setup.sh b/tests/localosmosis/scripts/setup.sh index ef8a00d2dd8..647abf7b2bb 100755 --- a/tests/localosmosis/scripts/setup.sh +++ b/tests/localosmosis/scripts/setup.sh @@ -105,6 +105,7 @@ add_genesis_accounts () { } edit_config () { + # Remove seeds dasel put string -f $CONFIG_FOLDER/config.toml '.p2p.seeds' '' @@ -112,38 +113,54 @@ edit_config () { dasel put string -f $CONFIG_FOLDER/config.toml '.rpc.laddr' "tcp://0.0.0.0:26657" } -create_two_asset_pool () { - # Create default pool - substring='code: 0' - COUNTER=0 - while [ $COUNTER -lt 15 ]; do - string=$(osmosisd tx gamm create-pool --pool-file=$1 --from pools --chain-id=$CHAIN_ID --home $OSMOSIS_HOME --keyring-backend=test -b block --yes 2>&1) - if [ "$string" != "${string%"$substring"*}" ]; then - echo "create two asset pool: successful" - break - else - let COUNTER=COUNTER+1 - sleep 0.5 - fi - done +enable_cors () { + + # Enable cors on RPC + dasel put string -f $CONFIG_FOLDER/config.toml -v "*" '.rpc.cors_allowed_origins.[]' + dasel put string -f $CONFIG_FOLDER/config.toml -v "Accept-Encoding" '.rpc.cors_allowed_headers.[]' + dasel put string -f $CONFIG_FOLDER/config.toml -v "DELETE" '.rpc.cors_allowed_methods.[]' + dasel put string -f $CONFIG_FOLDER/config.toml -v "OPTIONS" '.rpc.cors_allowed_methods.[]' + dasel put string -f $CONFIG_FOLDER/config.toml -v "PATCH" '.rpc.cors_allowed_methods.[]' + dasel put string -f $CONFIG_FOLDER/config.toml -v "PUT" '.rpc.cors_allowed_methods.[]' + + # Enable unsafe cors and swagger on the api + dasel put bool -f $CONFIG_FOLDER/app.toml -v "true" '.api.swagger' + dasel put bool -f $CONFIG_FOLDER/app.toml -v "true" '.api.enabled-unsafe-cors' + + # Enable cors on gRPC Web + dasel put bool -f $CONFIG_FOLDER/app.toml -v "true" '.grpc-web.enable-unsafe-cors' } -create_three_asset_pool () { - # Create three asset pool +run_with_retries() { + cmd=$1 + success_msg=$2 + substring='code: 0' COUNTER=0 + while [ $COUNTER -lt 15 ]; do - string=$(osmosisd tx gamm create-pool --pool-file=nativeDenomThreeAssetPool.json --from pools --chain-id=$CHAIN_ID --home $OSMOSIS_HOME --keyring-backend=test -b block --yes 2>&1) + string=$(eval $cmd 2>&1) + echo $string + if [ "$string" != "${string%"$substring"*}" ]; then - echo "create three asset pool: successful" + echo "$success_msg" break else - let COUNTER=COUNTER+1 + COUNTER=$((COUNTER+1)) sleep 0.5 fi done } +# Define the functions using the new function +create_two_asset_pool() { + run_with_retries "osmosisd tx gamm create-pool --pool-file=$1 --from pools --chain-id=$CHAIN_ID --home $OSMOSIS_HOME --keyring-backend=test -b block --fees 5000uosmo --yes" "create two asset pool: successful" +} + +create_three_asset_pool() { + run_with_retries "osmosisd tx gamm create-pool --pool-file=nativeDenomThreeAssetPool.json --from pools --chain-id=$CHAIN_ID --home $OSMOSIS_HOME --keyring-backend=test -b block --fees 5000uosmo --gas 900000 --yes" "create three asset pool: successful" +} + if [[ ! -d $CONFIG_FOLDER ]] then echo $MNEMONIC | osmosisd init -o --chain-id=$CHAIN_ID --home $OSMOSIS_HOME --recover $MONIKER @@ -151,6 +168,7 @@ then edit_genesis add_genesis_accounts edit_config + enable_cors fi osmosisd start --home $OSMOSIS_HOME & diff --git a/tests/localosmosis/state_export/scripts/start.sh b/tests/localosmosis/state_export/scripts/start.sh index ffcade0f681..bbf826788d4 100755 --- a/tests/localosmosis/state_export/scripts/start.sh +++ b/tests/localosmosis/state_export/scripts/start.sh @@ -33,6 +33,24 @@ edit_config () { dasel put string -f $CONFIG_FOLDER/config.toml '.rpc.laddr' "tcp://0.0.0.0:26657" } +enable_cors () { + + # Enable cors on RPC + dasel put string -f $CONFIG_FOLDER/config.toml -v "*" '.rpc.cors_allowed_origins.[]' + dasel put string -f $CONFIG_FOLDER/config.toml -v "Accept-Encoding" '.rpc.cors_allowed_headers.[]' + dasel put string -f $CONFIG_FOLDER/config.toml -v "DELETE" '.rpc.cors_allowed_methods.[]' + dasel put string -f $CONFIG_FOLDER/config.toml -v "OPTIONS" '.rpc.cors_allowed_methods.[]' + dasel put string -f $CONFIG_FOLDER/config.toml -v "PATCH" '.rpc.cors_allowed_methods.[]' + dasel put string -f $CONFIG_FOLDER/config.toml -v "PUT" '.rpc.cors_allowed_methods.[]' + + # Enable unsafe cors and swagger on the api + dasel put bool -f $CONFIG_FOLDER/app.toml -v "true" '.api.swagger' + dasel put bool -f $CONFIG_FOLDER/app.toml -v "true" '.api.enabled-unsafe-cors' + + # Enable cors on gRPC Web + dasel put bool -f $CONFIG_FOLDER/app.toml -v "true" '.grpc-web.enable-unsafe-cors' +} + if [[ ! -d $CONFIG_FOLDER ]] then @@ -67,6 +85,7 @@ then --prune-ibc edit_config + enable_cors fi osmosisd start --home $OSMOSIS_HOME --x-crisis-skip-assert-invariants