Skip to content

Commit

Permalink
Enable CORS on localosmosis (backport #4891) (#4896)
Browse files Browse the repository at this point in the history
* Enable CORS on localosmosis (#4891)

Closes: [#4879](#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 3b2e168)

# Conflicts:
#	CHANGELOG.md
#	tests/localosmosis/scripts/setup.sh

* Update CHANGELOG.md

* Align setup.sh

* Update create_two_asset_pool()

---------

Co-authored-by: Niccolo Raspa <[email protected]>
Co-authored-by: Niccolo Raspa <[email protected]>
  • Loading branch information
3 people authored Apr 12, 2023
1 parent 8eab695 commit fec4a9e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 19 deletions.
56 changes: 37 additions & 19 deletions tests/localosmosis/scripts/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -105,52 +105,70 @@ add_genesis_accounts () {
}

edit_config () {

# Remove seeds
dasel put string -f $CONFIG_FOLDER/config.toml '.p2p.seeds' ''

# Expose the rpc
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
install_prerequisites
edit_genesis
add_genesis_accounts
edit_config
enable_cors
fi

osmosisd start --home $OSMOSIS_HOME &
Expand Down
19 changes: 19 additions & 0 deletions tests/localosmosis/state_export/scripts/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -67,6 +85,7 @@ then
--prune-ibc

edit_config
enable_cors
fi

osmosisd start --home $OSMOSIS_HOME --x-crisis-skip-assert-invariants

0 comments on commit fec4a9e

Please sign in to comment.