product-iots/modules/scripts/change-ip.sh

315 lines
14 KiB
Bash
Raw Permalink Normal View History

2017-08-02 03:48:18 +00:00
#!/bin/bash
echo ""
echo "----------------------------------------"
2018-10-08 05:00:26 +00:00
echo "Entgra IoT Server IP configuration tool"
2017-08-02 03:48:18 +00:00
echo "----------------------------------------"
##################################### IP configs related to core ####################################
2017-08-02 03:48:18 +00:00
echo ""
echo ">>> Step 1: Change current IP address of the IoT server"
2017-08-02 03:48:18 +00:00
echo ""
echo "Please enter the IoT Core IP that you need to replace (if you are trying out IoT server for the first time this will be localhost)"
2020-11-03 21:03:14 +00:00
read -r val1;
2017-08-02 03:48:18 +00:00
while [[ -z $val1 ]]; do #if $val1 is a zero length String
echo "Please enter the IoT Core IP that you need to replace (if you are trying out IoT server for the first time this will be localhost)"
2020-11-03 21:03:14 +00:00
read -r val1;
2017-08-02 03:48:18 +00:00
done
echo ""
echo "Please enter your current IP"
2020-11-03 21:03:14 +00:00
read -r val2;
2017-08-02 03:48:18 +00:00
while [[ -z $val2 ]]; do #if $val2 is a zero length String
echo "Please enter your current IP"
2020-11-03 21:03:14 +00:00
read -r val2;
2017-08-02 03:48:18 +00:00
done
echo "--------------------------------------"
2020-11-03 21:03:14 +00:00
echo "All your " + "$val1" + " IP's are replaced with " + "$val2" ;
2017-08-02 03:48:18 +00:00
echo "--------------------------------------"
echo "Changing <IoT_HOME>/conf/carbon.xml"
2020-11-03 21:03:14 +00:00
sed -i -e 's#\(<HostName>\)'"$val1"'\(</HostName>\)#\1'"$val2"'\2#g' ../conf/carbon.xml
sed -i -e 's#\(<MgtHostName>\)'"$val1"'\(</MgtHostName>\)#\1'"$val2"'\2#g' ../conf/carbon.xml
2017-08-02 03:48:18 +00:00
echo "Completed!!"
#--------------------
echo "Changing <IoT_HOME>/conf/identity/sso-idp-config.xml"
2020-11-03 21:03:14 +00:00
sed -i -e 's/'"$val1"'/'"$val2"'/g' ../conf/identity/sso-idp-config.xml
2017-08-02 03:48:18 +00:00
echo "Completed!!"
#--------------------
echo "Changing <IoT_HOME>/conf/iot-api-config.xml"
2020-11-03 21:03:14 +00:00
sed -i -e 's/'"$val1"'/'"$val2"'/g' ../conf/iot-api-config.xml
2017-08-02 03:48:18 +00:00
echo "Completed!!"
#--------------------
echo "Changing <IoT_HOME>/repository/deployment/server/jaggeryapps/devicemgt/app/conf/app-conf.json"
2020-11-03 21:03:14 +00:00
sed -i -e 's/"identityProvider.*/\"identityProviderUrl\"\ :\ "https\:\/\/'"$val2"':9443\/samlsso\"\,/' ../repository/deployment/server/jaggeryapps/devicemgt/app/conf/app-conf.json
sed -i -e 's/"acs.*/\"acs\"\ :\ "https\:\/\/'"$val2"':9443\/devicemgt\/uuf\/sso\/acs\"\,/' ../repository/deployment/server/jaggeryapps/devicemgt/app/conf/app-conf.json
2017-08-02 03:48:18 +00:00
echo "Completed!!"
#--------------------
echo "Changing <IoT_HOME>/repository/deployment/server/jaggeryapps/devicemgt/app/conf/config.json"
sed -i -e 's/"androidAgentDownloadURL.*/\"androidAgentDownloadURL\":\ "https\:\/\/\%iot.manager.host\%\:\%iot.manager.https.port\%\/devicemgt\/public\/cdmf.unit.device.type.android.type-view\/assets\/android-agent.apk\",/' ../repository/deployment/server/jaggeryapps/devicemgt/app/conf/config.json
echo "Completed!!"
2017-08-02 03:48:18 +00:00
#--------------------
echo "Changing <IoT_HOME>/repository/deployment/server/jaggeryapps/api-store/site/conf/site.json"
2020-11-03 21:03:14 +00:00
sed -i -e 's/"identityProvider.*/\"identityProviderURL\"\ :\ "https\:\/\/'"$val2"':9443\/samlsso\"\,/' ../repository/deployment/server/jaggeryapps/api-store/site/conf/site.json
2017-08-02 03:48:18 +00:00
echo "Completed!!"
#--------------------
echo "Changing <IoT_HOME>/conf/api-manager.xml"
if grep -q '<!-- Server URL of the API key manager -->' ../conf/api-manager.xml;
then
echo 'found'
sed -i -e 's|<!-- Server URL of the API key manager -->||' ../conf/api-manager.xml
fi
2020-11-03 21:03:14 +00:00
if grep -q "<ServerURL>https:\/\/\${carbon.local.ip}:\${mgt.transport.https.port}\${carbon.context}services\/<\/ServerURL>" ../conf/api-manager.xml;
2017-08-02 03:48:18 +00:00
then
echo 'found'
2020-11-03 21:03:14 +00:00
sed -i -e "s/<ServerURL>https:\/\/\${carbon.local.ip}:\${mgt.transport.https.port}\${carbon.context}services\/<\/ServerURL>//" ../conf/api-manager.xml
2017-08-02 03:48:18 +00:00
fi
2020-11-03 21:03:14 +00:00
if grep -q "<ServerURL>https:\/\/$val2:\${mgt.transport.https.port}\${carbon.context}services\/<\/ServerURL>" ../conf/api-manager.xml;
2017-08-02 03:48:18 +00:00
then
echo 'found'
2020-11-03 21:03:14 +00:00
sed -i -e "s/<ServerURL>https:\/\/$val2:\${mgt.transport.https.port}\${carbon.context}services\/<\/ServerURL>//" ../conf/api-manager.xml
2017-08-02 03:48:18 +00:00
fi
2020-11-03 21:03:14 +00:00
sed -i -e 's/<APIKeyValidator>/<APIKeyValidator><!-- Server URL of the API key manager --><ServerURL>https:\/\/'"$val2"'\:\$\{mgt\.transport\.https\.port\}\$\{carbon\.context\}services\/<\/ServerURL>/g' ../conf/api-manager.xml
2017-08-02 03:48:18 +00:00
echo "Completed!!"
#--------------------
2020-11-03 21:03:14 +00:00
if grep -q "<RevokeAPIURL>https:\/\/localhost:\${https.nio.port}\/revoke<\/RevokeAPIURL>" ../conf/api-manager.xml;
2017-08-02 03:48:18 +00:00
then
2020-11-03 21:03:14 +00:00
sed -i -e "s|<RevokeAPIURL>https:\/\/localhost:\${https.nio.port}\/revoke<\/RevokeAPIURL>|<RevokeAPIURL>https:\/\/$val2:\${https.nio.port}\/revoke<\/RevokeAPIURL>|" ../conf/api-manager.xml
2017-08-02 03:48:18 +00:00
fi
2020-11-03 21:03:14 +00:00
if grep -q "<RevokeAPIURL>https:\/\/$val1:\${https.nio.port}\/revoke<\/RevokeAPIURL>" ../conf/api-manager.xml;
2017-08-02 03:48:18 +00:00
then
2020-11-03 21:03:14 +00:00
sed -i -e "s|<RevokeAPIURL>https:\/\/$val1:\${https.nio.port}\/revoke<\/RevokeAPIURL>|<RevokeAPIURL>https:\/\/$val2:\${https.nio.port}\/revoke<\/RevokeAPIURL>|" ../conf/api-manager.xml
2017-08-02 03:48:18 +00:00
fi
#--------------------
echo "Changing <IoT_HOME>/conf/etc/webapp-publisher-config.xml"
2020-11-03 21:03:14 +00:00
sed -i -e 's#\(<EnabledUpdateApi>\)false\(</EnabledUpdateApi>\)#\1'"true"'\2#g' ../conf/etc/webapp-publisher-config.xml
2017-08-02 03:48:18 +00:00
echo "Completed!!"
#--------------------
echo "Changing <IoT_HOME>/bin/iot-server.sh"
2020-11-03 21:03:14 +00:00
sed -i -e 's/-Diot.manager.host.*/-Diot.manager.host="'"$val2"'" \\/' ../bin/iot-server.sh
sed -i -e 's/-Diot.core.host.*/-Diot.core.host="'"$val2"'" \\/' ../bin/iot-server.sh
sed -i -e 's/-Diot.keymanager.host.*/-Diot.keymanager.host="'"$val2"'" \\/' ../bin/iot-server.sh
sed -i -e 's/-Diot.gateway.host.*/-Diot.gateway.host="'"$val2"'" \\/' ../bin/iot-server.sh
2017-08-02 03:48:18 +00:00
echo "Completed!!"
#----------------------
echo ""
echo "Changing hostURL of <IoT_HOME>/wso2/broker/conf/broker.xml"
2020-11-03 21:03:14 +00:00
if grep -q '<property name="hostURL">https://'"$val1"':9443/services/OAuth2TokenValidationService</property>' ../wso2/broker/conf/broker.xml;
2017-08-02 03:48:18 +00:00
then
echo "found"
2020-11-03 21:03:14 +00:00
sed -i -e 's|<property name="hostURL">https:\/\/'"$val1"':9443\/services\/OAuth2TokenValidationService</\property>|<property name="hostURL">https:\/\/'"$val2"':9443\/services\/OAuth2TokenValidationService</\property>|' ../wso2/broker/conf/broker.xml
2017-08-02 03:48:18 +00:00
echo "Completed!!"
fi
#------------------------
echo ""
echo "Changing tokenEndpoint of <IoT_HOME>/wso2/broker/conf/broker.xml"
2020-11-03 21:03:14 +00:00
if grep -q '<property name="tokenEndpoint">https:\/\/'"$val1"':8243</\property>' ../wso2/broker/conf/broker.xml;
2017-08-02 03:48:18 +00:00
then
echo "found"
2020-11-03 21:03:14 +00:00
sed -i -e 's|<property name="tokenEndpoint">https:\/\/'"$val1"':8243</\property>|<property name="tokenEndpoint">https:\/\/'"$val2"':8243</\property>|' ../wso2/broker/conf/broker.xml
2017-08-02 03:48:18 +00:00
echo "Completed!!"
fi
#--------------------------
echo ""
echo "Changing deviceMgtServerUrl of <IoT_HOME>/wso2/broker/conf/broker.xml"
2020-11-03 21:03:14 +00:00
if grep -q '<property name="deviceMgtServerUrl">https:\/\/'"$val1"':8243</\property>' ../wso2/broker/conf/broker.xml;
2017-08-02 03:48:18 +00:00
then
echo "found"
2020-11-03 21:03:14 +00:00
sed -i -e 's|<property name="deviceMgtServerUrl">https:\/\/'"$val1"':8243</\property>|<property name="deviceMgtServerUrl">https:\/\/'"$val2"':8243</\property>|' ../wso2/broker/conf/broker.xml
2017-08-02 03:48:18 +00:00
echo "Completed!!"
fi
#--------------------
echo "Changing <IoT_HOME>/wso2/analytics/bin/wso2server.sh"
2020-11-03 21:03:14 +00:00
sed -i -e 's/-Diot.keymanager.host.*/-Diot.keymanager.host="'"$val2"'" \\/' ../wso2/analytics/bin/wso2server.sh
sed -i -e 's/-Diot.gateway.host.*/-Diot.gateway.host="'"$val2"'" \\/' ../wso2/analytics/bin/wso2server.sh
2017-08-02 03:48:18 +00:00
echo "Completed!!"
##################################### IP configs related to broker ####################################
echo ""
echo ""
echo ">>> Step 2: Change current IP address of the IoT Broker"
echo "-------------------------------------------------------"
#--------------------
echo "Changing <IoT_HOME>/wso2/analytics/bin/wso2server.sh"
2020-11-03 21:03:14 +00:00
sed -i -e 's/-Dmqtt.broker.host.*/-Dmqtt.broker.host="'"$val2"'" \\/' ../wso2/analytics/bin/wso2server.sh
2017-08-02 03:48:18 +00:00
echo "Completed!!"
#--------------------
echo "Changing <IoT_HOME>/bin/iot-server.sh"
2020-11-03 21:03:14 +00:00
sed -i -e 's/-Dmqtt.broker.host.*/-Dmqtt.broker.host="'"$val2"'" \\/' ../bin/iot-server.sh
2017-08-02 03:48:18 +00:00
echo "Completed!!"
echo "Changing <IoT_HOME>/wso2/broker/conf/carbon.xml"
2020-11-03 21:03:14 +00:00
sed -i -e 's#\(<HostName>\)'"$val1"'\(</HostName>\)#\1'"$val2"'\2#g' ../wso2/broker/conf/carbon.xml
sed -i -e 's#\(<MgtHostName>\)'"$val1"'\(</MgtHostName>\)#\1'"$val2"'\2#g' ../wso2/broker/conf/carbon.xml
echo "Completed!!"
2017-08-02 03:48:18 +00:00
##################################### IP configs related to analytics ####################################
echo ""
echo ""
echo ">>> Step 3: Change current IP address of the IoT Analytics"
echo "-------------------------------------------------------"
#--------------------
echo "Changing <IoT_HOME>/bin/iot-server.sh"
2020-11-03 21:03:14 +00:00
sed -i -e 's/-Diot.analytics.host.*/-Diot.analytics.host="'"$val2"'" \\/' ../bin/iot-server.sh
2017-08-02 03:48:18 +00:00
echo "Completed!!"
echo "Changing <IoT_HOME>/wso2/analytics/conf/carbon.xml"
2020-11-03 21:03:14 +00:00
sed -i -e 's#\(<HostName>\)'"$val1"'\(</HostName>\)#\1'"$val2"'\2#g' ../wso2/analytics/conf/carbon.xml
sed -i -e 's#\(<MgtHostName>\)'"$val1"'\(</MgtHostName>\)#\1'"$val2"'\2#g' ../wso2/analytics/conf/carbon.xml
echo "Completed!!"
2017-08-02 03:48:18 +00:00
echo ""
echo "-----------------------------------------------"
echo "Generating SSL certificates for the IoT Server"
echo "-----------------------------------------------"
2017-08-02 03:48:18 +00:00
echo ""
B_SUBJ=''
C_SUBJ=''
A_SUBJ=''
2019-12-12 07:37:25 +00:00
SAN_NAMES=''
2017-08-02 03:48:18 +00:00
slash='/'
equal='='
buildSubject(){
2020-11-03 21:03:14 +00:00
if [ "$1" = "CN" ]; then
2017-08-02 03:48:18 +00:00
echo "Please provide Common Name "
2020-11-03 21:03:14 +00:00
read -r val
2017-08-02 03:48:18 +00:00
while [[ -z $val ]]; do #if $val is a zero length String
echo "Common name(your server IP/hostname) cannot be null. Please enter the Common name."
2020-11-03 21:03:14 +00:00
read -r val;
2017-08-02 03:48:18 +00:00
done
2020-11-03 21:03:14 +00:00
if [ -n "$val" ]; then #This is true if $val is not empty (If $val is not a non zero length String)
if [ "$3" = "C" ]; then
2017-08-02 03:48:18 +00:00
C_SUBJ="$C_SUBJ$slash$1$equal$val"
return
2020-11-03 21:03:14 +00:00
elif [ "$3" = "B" ]; then
2017-08-02 03:48:18 +00:00
B_SUBJ="$B_SUBJ$slash$1$equal$val"
return
else
A_SUBJ="$A_SUBJ$slash$1$equal$val"
return
fi
fi
fi
2020-11-03 21:03:14 +00:00
echo "Please provide ""$2"". Press Enter to skip."
read -r val;
if [ -n "$val" ]; then #If $val is not a zero length String; This is same as if[ -n $val]; then
if [ "$3" = "C" ]; then
2017-08-02 03:48:18 +00:00
C_SUBJ="$C_SUBJ$slash$1$equal$val"
return
2020-11-03 21:03:14 +00:00
elif [ "$3" = "B" ]; then
2017-08-02 03:48:18 +00:00
B_SUBJ="$B_SUBJ$slash$1$equal$val"
return
2020-11-03 21:03:14 +00:00
elif [ "$3" = "S" ]; then
2019-12-12 07:37:25 +00:00
SAN_NAMES="DNS:$val$4$SAN_NAMES"
buildSubject 'SAN' 'SAN' 'S' ','
return
2017-08-02 03:48:18 +00:00
else
A_SUBJ="$A_SUBJ$slash$1$equal$val"
return
fi
fi
}
2020-11-03 21:03:14 +00:00
if [ -d "tmp" ]; then
rm -rf tmp
fi
2017-08-02 03:48:18 +00:00
mkdir tmp
2020-11-03 21:03:14 +00:00
2017-08-02 03:48:18 +00:00
echo ''
echo '=======Enter Values for IoT Core SSL Certificate======='
buildSubject 'C' 'Country' 'C'
buildSubject 'ST' 'State' 'C'
buildSubject 'L' 'Location' 'C'
buildSubject 'O' 'Organization' 'C'
buildSubject 'OU' 'Organizational Unit' 'C'
buildSubject 'emailAddress' 'Email Address' 'C'
buildSubject 'CN' 'Common Name' 'C'
2019-12-12 07:37:25 +00:00
buildSubject 'SAN' 'SAN' 'S'
2017-08-02 03:48:18 +00:00
echo ""
2020-11-03 21:03:14 +00:00
echo 'Provided IoT Core SSL Subject : ' "$C_SUBJ"
2017-08-02 03:48:18 +00:00
echo 'If you have a different IoT Core Keystore password please enter it here. Press Enter to use the default password.'
2020-11-03 21:03:14 +00:00
read -r -s password
if [ -n "$password" ]; then
2017-08-02 03:48:18 +00:00
SSL_PASS=$password
else
SSL_PASS="wso2carbon"
fi
echo ""
echo "Generating SSL Certificate for IoT Core"
openssl genrsa -out ./tmp/c.key 4096
2020-11-03 21:03:14 +00:00
openssl req -new -key ./tmp/c.key -out ./tmp/c.csr -subj "$C_SUBJ"
if [ -z "$SAN_NAMES" ]; then
2019-12-12 07:37:25 +00:00
openssl x509 -req -days 730 -in ./tmp/c.csr -signkey ./tmp/c.key -set_serial 044324884 -sha256 -out ./tmp/c.crt
else
2020-11-03 21:03:14 +00:00
openssl x509 -req -extfile <(printf "subjectAltName=%s" "$SAN_NAMES") -days 730 -in ./tmp/c.csr -signkey ./tmp/c.key -set_serial 044324884 -sha256 -out ./tmp/c.crt
2019-12-12 07:37:25 +00:00
fi
2017-08-02 03:48:18 +00:00
echo "Export to PKCS12"
openssl pkcs12 -export -out ./tmp/CKEYSTORE.p12 -inkey ./tmp/c.key -in ./tmp/c.crt -name "wso2carbon" -password pass:$SSL_PASS
echo "Export PKCS12 to JKS"
keytool -importkeystore -srckeystore ./tmp/CKEYSTORE.p12 -srcstoretype PKCS12 -destkeystore ../repository/resources/security/wso2carbon.jks -deststorepass wso2carbon -srcstorepass wso2carbon -noprompt
keytool -importkeystore -srckeystore ./tmp/CKEYSTORE.p12 -srcstoretype PKCS12 -destkeystore ../repository/resources/security/client-truststore.jks -deststorepass wso2carbon -srcstorepass wso2carbon -noprompt
keytool -importkeystore -srckeystore ./tmp/CKEYSTORE.p12 -srcstoretype PKCS12 -destkeystore ../wso2/broker/repository/resources/security/wso2carbon.jks -deststorepass wso2carbon -srcstorepass wso2carbon -noprompt
keytool -importkeystore -srckeystore ./tmp/CKEYSTORE.p12 -srcstoretype PKCS12 -destkeystore ../wso2/broker/repository/resources/security/client-truststore.jks -deststorepass wso2carbon -srcstorepass wso2carbon -noprompt
2017-08-02 03:48:18 +00:00
keytool -importkeystore -srckeystore ./tmp/CKEYSTORE.p12 -srcstoretype PKCS12 -destkeystore ../wso2/analytics/repository/resources/security/wso2carbon.jks -deststorepass wso2carbon -srcstorepass wso2carbon -noprompt
keytool -importkeystore -srckeystore ./tmp/CKEYSTORE.p12 -srcstoretype PKCS12 -destkeystore ../wso2/analytics/repository/resources/security/client-truststore.jks -deststorepass wso2carbon -srcstorepass wso2carbon -noprompt
2017-08-10 01:15:13 +00:00
echo ""
echo "Setting up the public certificate for the default idp"
if hash tac; then
VAR=$(keytool -exportcert -alias wso2carbon -keystore ../repository/resources/security/wso2carbon.jks -rfc -storepass wso2carbon | tail -n +2 | tac | tail -n +2 | tac | tr -cd "[:print:]");
else
VAR=$(keytool -exportcert -alias wso2carbon -keystore ../repository/resources/security/wso2carbon.jks -rfc -storepass wso2carbon | tail -n +2 | tail -r | tail -n +2 | tail -r | tr -cd "[:print:]"); fi
echo ""
echo "Printing certificate"
echo "-----------------------"
2020-11-03 21:03:14 +00:00
echo "$VAR"
sed -i -e 's#<Certificate>.*#<Certificate>'"$VAR"'</Certificate>#g' ../conf/identity/identity-providers/iot_default.xml
echo ""
if [ -e "../conf/identity/identity-providers/iot_default.xml-e" ]; then
echo "IDP temp file exists, hence removing"
rm -f ../conf/identity/identity-providers/iot_default.xml-e
fi
echo ""
echo "Configuration Completed!!!"