hue_clean_duplicate_permissions.sh 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #!/bin/bash
  2. # Licensed to Cloudera, Inc. under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. Cloudera, Inc. licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #Clean up duplicate Hue permissions
  18. #parse command line arguments
  19. parse_arguments()
  20. {
  21. # Test that we're using compatible getopt version.
  22. getopt -T > /dev/null
  23. if [[ $? -ne 4 ]]; then
  24. echo "Incompatible getopt version."
  25. exit 1
  26. fi
  27. # Parse short and long option parameters.
  28. OVERRIDE=
  29. VERBOSE=
  30. DESKTOP_DEBUG=false
  31. GETOPT=`getopt -n $0 -o o,v,h \
  32. -l override,verbose,help \
  33. -- "$@"`
  34. eval set -- "$GETOPT"
  35. while true;
  36. do
  37. case "$1" in
  38. -o|--override)
  39. OVERRIDE=true
  40. shift
  41. ;;
  42. -v|--verbose)
  43. VERBOSE=true
  44. shift
  45. ;;
  46. --)
  47. shift
  48. break
  49. ;;
  50. *)
  51. usage
  52. exit 1
  53. ;;
  54. esac
  55. done
  56. #
  57. }
  58. usage()
  59. {
  60. cat << EOF
  61. usage: $0 [options]
  62. Removes all duplicate permissions
  63. OPTIONS
  64. -o|--override Allow script to run as non-root, must set HUE_CONF_DIR manually before running
  65. -v|--verbose Enable verbose logging
  66. -h|--help Show this message.
  67. EOF
  68. }
  69. debug()
  70. {
  71. if [[ ! -z $VERBOSE ]]
  72. then
  73. echo "$1" >> ${LOG_FILE}
  74. fi
  75. }
  76. main()
  77. {
  78. parse_arguments "$@"
  79. SCRIPT_DIR="$( cd -P "$( dirname "$0" )" && pwd )"
  80. PYTHONPATH=${SCRIPT_DIR}/lib:${PYTHONPATH}
  81. export SCRIPT_DIR PYTHONPATH
  82. #SET IMPORTANT ENV VARS
  83. if [[ -z ${HUE_CONF_DIR} ]]
  84. then
  85. if [ -d "/var/run/cloudera-scm-agent/process" ]
  86. then
  87. HUE_CONF_DIR="/var/run/cloudera-scm-agent/process/`ls -1 /var/run/cloudera-scm-agent/process | grep HUE_SERVER | sort -n | tail -1 `"
  88. else
  89. HUE_CONF_DIR="/etc/hue/conf"
  90. fi
  91. export HUE_CONF_DIR
  92. fi
  93. if [[ ! ${USER} =~ .*root* ]]
  94. then
  95. if [[ -z ${OVERRIDE} ]]
  96. then
  97. echo "Script must be run as root: exiting"
  98. exit 1
  99. fi
  100. else
  101. if [[ $(ps -ef | grep "[h]ue runc" | awk '{print }') ]]
  102. then
  103. DESKTOP_LOG_DIR=$(strings /proc/$(ps -ef | grep "[h]ue runc" | awk '{print }' | awk '{print $2}')/environ | grep DESKTOP_LOG_DIR | awk -F\= '{print $2}')
  104. fi
  105. fi
  106. if [[ -z ${DESKTOP_LOG_DIR} ]]
  107. then
  108. DESKTOP_LOG_DIR=${HUE_CONF_DIR}/logs
  109. fi
  110. if [[ ! -f ${DESKTOP_LOG_DIR} ]]
  111. then
  112. mkdir -p ${DESKTOP_LOG_DIR}
  113. fi
  114. LOG_FILE=${DESKTOP_LOG_DIR}/`basename "$0" | awk -F\. '{print $1}'`.log
  115. LOG_ROTATE_SIZE=10 #MB before rotating, size in MB before rotating log to .1
  116. LOG_ROTATE_COUNT=5 #number of log files, so 20MB max
  117. PARCEL_DIR=/opt/cloudera/parcels/CDH
  118. if [ ! -d "/usr/lib/hadoop" ]
  119. then
  120. CDH_HOME=$PARCEL_DIR
  121. else
  122. CDH_HOME=/usr
  123. fi
  124. if [ -d "${CDH_HOME}/lib/hue/build/env/bin" ]
  125. then
  126. COMMAND="${CDH_HOME}/lib/hue/build/env/bin/hue shell"
  127. TEST_COMMAND="${CDH_HOME}/lib/hue/build/env/bin/hue dbshell"
  128. else
  129. COMMAND="${CDH_HOME}/share/hue/build/env/bin/hue shell"
  130. TEST_COMMAND="${CDH_HOME}/share/hue/build/env/bin/hue dbshell"
  131. fi
  132. ORACLE_ENGINE_CHECK=$(grep engine ${HUE_CONF_DIR}/hue* | grep -i oracle)
  133. if [[ ! -z ${ORACLE_ENGINE_CHECK} ]]
  134. then
  135. if [[ -z ${ORACLE_HOME} ]]
  136. then
  137. ORACLE_PARCEL=/opt/cloudera/parcels/ORACLE_INSTANT_CLIENT/instantclient_11_2
  138. if [[ -d ${ORACLE_PARCEL} ]]
  139. then
  140. ORACLE_HOME=${ORACLE_PARCEL}
  141. LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${ORACLE_HOME}
  142. export LD_LIBRARY_PATH ORACLE_HOME
  143. fi
  144. fi
  145. if [[ -z ${ORACLE_HOME} ]]
  146. then
  147. echo "It looks like you are using Oracle as your backend"
  148. echo "ORACLE_HOME must be set to the correct Oracle client"
  149. echo "before running this script"
  150. exit 1
  151. fi
  152. fi
  153. HUE_IGNORE_PASSWORD_SCRIPT_ERRORS=1
  154. if [[ -z ${HUE_DATABASE_PASSWORD} ]]
  155. then
  156. echo "CDH 5.5 and above requires that you set the environment variable:"
  157. echo "HUE_DATABASE_PASSWORD=<dbpassword>"
  158. exit 1
  159. fi
  160. PGPASSWORD=${HUE_DATABASE_PASSWORD}
  161. export CDH_HOME COMMAND HUE_IGNORE_PASSWORD_SCRIPT_ERRORS PGPASSWORD
  162. debug "Validating DB connectivity"
  163. # echo "COMMAND: echo \"from django.db import connection; cursor = connection.cursor(); cursor.execute('select count(*) from auth_user')\" | ${TEST_COMMAND}" | tee -a ${LOG_FILE}
  164. # echo "from django.db import connection; cursor = connection.cursor(); cursor.execute('select count(*) from auth_user')" | ${TEST_COMMAND} | tee -a ${LOG_FILE}
  165. QUIT_COMMAND="quit"
  166. PG_ENGINE_CHECK=$(grep engine ${HUE_CONF_DIR}/hue* | grep -i postgres)
  167. if [[ ! -z ${PG_ENGINE_CHECK} ]]
  168. then
  169. QUIT_COMMAND='\q'
  170. fi
  171. # echo "Running echo ${QUIT_COMMAND} | ${TEST_COMMAND}"
  172. # echo ${QUIT_COMMAND} | ${TEST_COMMAND}
  173. if [[ $? -ne 0 ]]
  174. then
  175. echo "HUE_DATABASE_PASSWORD is incorrect. Please check CM: http://${HOSTNAME}:7180/api/v5/cm/deployment and search for HUE_SERVER and database to find correct password"
  176. exit 1
  177. fi
  178. debug "Running ${COMMAND}"
  179. ${COMMAND} >> /dev/null 2>&1 <<EOF
  180. from useradmin.models import HuePermission, GroupPermission
  181. from datetime import date, timedelta
  182. import desktop.conf
  183. import logging
  184. import logging.handlers
  185. import sys
  186. LOGFILE="${LOG_FILE}"
  187. log = logging.getLogger('')
  188. log.setLevel(logging.INFO)
  189. format = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
  190. fh = logging.handlers.RotatingFileHandler(LOGFILE, maxBytes = (1048576*logrotatesize), backupCount = backupcount)
  191. fh.setFormatter(format)
  192. log.addHandler(fh)
  193. log.info('HUE_CONF_DIR: ${HUE_CONF_DIR}')
  194. log.info("DB Engine: %s" % desktop.conf.DATABASE.ENGINE.get())
  195. log.info("DB Name: %s" % desktop.conf.DATABASE.NAME.get())
  196. log.info("DB User: %s" % desktop.conf.DATABASE.USER.get())
  197. log.info("DB Host: %s" % desktop.conf.DATABASE.HOST.get())
  198. log.info("DB Port: %s" % str(desktop.conf.DATABASE.PORT.get()))
  199. log.info("Removing duplicate Hue permissions")
  200. apps = []
  201. for perm in HuePermission.objects.all():
  202. apps.append(perm.app)
  203. for app in sorted(set(apps)):
  204. log.info("")
  205. log.info("Working on app: %s" % app)
  206. perms = HuePermission.objects.filter(app=app)
  207. for perm in perms:
  208. permsdistinct = HuePermission.objects.filter(app=app, description = perm.description)
  209. print "len: %s: app: %s: description: %s: id: %s" % (len(permsdistinct), perm.app, perm.description, perm.id)
  210. if len(permsdistinct) > 1:
  211. try:
  212. group = GroupPermission.objects.get(hue_permission=perm)
  213. if group.group_id != 1:
  214. log.info("Cleaning up Hue permission for only default group: %s" % perm.__dict__)
  215. HuePermission.objects.filter(app=app, id = perm.id).delete()
  216. except:
  217. pass
  218. for app in sorted(set(apps)):
  219. log.info("")
  220. log.info("Working on app because no special perms assigned: %s" % app)
  221. perms = HuePermission.objects.filter(app=app)
  222. for perm in perms:
  223. permsdistinct = HuePermission.objects.filter(app=app, description = perm.description)
  224. if len(permsdistinct) > 1:
  225. log.info("Cleaning up Hue permission: %s" % perm.__dict__)
  226. HuePermission.objects.filter(app=app, id = perm.id).delete()
  227. EOF
  228. echo ""
  229. echo "Logs can be found in ${DESKTOP_LOG_DIR}"
  230. unset PGPASSWORD
  231. }
  232. main "$@"