diff --git a/config/cloudstorage/azure_snippet.txt b/config/cloudstorage/azure_snippet.txt index e510fc356..883701bba 100644 --- a/config/cloudstorage/azure_snippet.txt +++ b/config/cloudstorage/azure_snippet.txt @@ -4,8 +4,8 @@ ckanext.cloudstorage.container_name = ckanext.cloudstorage.driver_options = { "key": "", - "secret": "" -} + "secret": + } ckanext.cloudstorage.use_secure_urls = 1 ckanext.cloudstorage.max_multipart_lifetime = 7 diff --git a/scripts/helper_functions.sh b/scripts/helper_functions.sh index 9b6a8fe18..5c8dcb9d2 100755 --- a/scripts/helper_functions.sh +++ b/scripts/helper_functions.sh @@ -1,6 +1,6 @@ #! /bin/bash -# function to convert a string into a Sed escaped string. +# Function to convert a string into a Sed escaped string. # This string can be used in sed replace command. # arguments: 1 String to be modified with escape chars # return: Modified String with escape chars @@ -18,7 +18,21 @@ function str_to_sed_str(){ # example: # replace_str_in_ckan_ini "replace this" "by this" function replace_str_in_ckan_ini() { - ORIGINAL_STR = "$1" - REPLACEMENT_STR = "$2" + ORIGINAL_STR="$1" + REPLACEMENT_STR="$2" sed -i -r 's/'"$ORIGINAL_STR"'/'"$REPLACEMENT_STR"'/' $CKANINIPATH -} \ No newline at end of file +} + +# replaces a given string with the replacement string inside +# a specified file. +# arguments: string needing replacement, replacement string, file +# return: none +# example: +# replace_str_in_file "replace this" "by this" file +function replace_str_in_file() { + ORIGINAL_STR="$1" + REPLACEMENT_STR="$2" + FILE="$3" + echo $REPLACEMENT_STR + sed -i -r 's/'"$ORIGINAL_STR"'/'"$REPLACEMENT_STR"'/' $FILE +} diff --git a/scripts/setup_cloudstorage.sh b/scripts/setup_cloudstorage.sh index 31ffb50c6..204e5358e 100755 --- a/scripts/setup_cloudstorage.sh +++ b/scripts/setup_cloudstorage.sh @@ -1,4 +1,5 @@ #!/bin/bash +source ./helper_functions.sh # Assume that the sudo password has been exported as an environment variable # export SUDOPASS="mypassword" @@ -29,23 +30,30 @@ pip3 install zope.interface==5.0.0 # Make additional py3 upgrades cd /usr/lib/ckan/default/src/ckanext-cloudstorage git checkout -b ckan2.9 -sed -i -r 's/'"$PYLONSIMPORT"'/'"$PYLONSIMPORT_REPLACEMENT"'/g' $STORAGEPY -sed -i -r 's/'"$PYLONSIMPORT"'/'"$PYLONSIMPORT_REPLACEMENT"'/g' $MULTIPARTPY +replace_str_in_file "$PYLONSIMPORT" "$PYLONSIMPORT_REPLACEMENT" "$STORAGEPY" +replace_str_in_file "$PYLONSIMPORT" "$PYLONSIMPORT_REPLACEMENT" "$MULTIPARTPY" python3 setup.py develop python3 setup.py install # Add cloudstorage to ckan.plugins in ckan.ini -sed -i -r 's/'"$CKANPLUGINS"'/'"$CKANPLUGINS_REPLACEMENT"'/g' $CKANINI +replace_str_in_file "$CKANPLUGINS" "$CKANPLUGINS_REPLACEMENT" "$CKANINI" -# Add Azure params to snippet -sed -i '// r '"$AZUREBLOB"'' $AZURESNIPPET -sed -i '// r '"$AZURECONTAINER"'' $AZURESNIPPET -sed -i '// r '"$AZURESECRET"'' $AZURESNIPPET - -# Add cloudstorage params to ckan.ini +# Add Azure snippet to ckan.ini sed -i '/'"$CKANPLUGINS_REPLACEMENT"'/ r '"$AZURESNIPPET"'' $CKANINI +# Replace Azure placeholders with pre-defined values +replace_str_in_file "" "$AZUREBLOB" "$CKANINI" +replace_str_in_file "" "$AZURECONTAINER" "$CKANINI" +# Escape special characters in Azure secret with str_to_sed_str() +# and duplicate any % chars +AZURESECRET_FORMATTED=$(str_to_sed_str $(sed '/%/s/$/%%/' <<<$AZURESECRET)) +# Store formatted Azure secret in a text file +echo "\"${AZURESECRET_FORMATTED}\"" > secret_snippet.txt +# Add secret to ckan.ini by appending text file +sed -i '/"secret":/ r secret_snippet.txt' $CKANINI +rm secret_snippet.txt + echo $SUDOPASS | sudo -S service supervisor restart deactivate