AWS實戰 - EMR Zeppelin啓用用戶驗證

背景

默認狀況下,運行在emr上的zeppelin是未開啓用戶驗證的,這意味着集羣安全組內的任何人均可以訪問zeppelin,並在上面運行代碼,這無疑是不安全的。咱們經過必定的設置爲zeppelin開啓用戶驗證。html

手動設置

參考文檔Apache Shiro authentication for Apache Zeppelinssh到已經啓動的集羣的主節點進行設置。python

經過step自動設置

手動設置須要在每次集羣啓動後都設置一遍,若是須要頻繁啓動集羣,就很不方便,所以能夠在啓動集羣的時候使用step來自動設置。
具體來講,AWS提供了一個script-runner.jar,它的路徑是s3://<YOUR-AWS-REGION>.elasticmapreduce/libs/script-runner/script-runner.jar,在啓動集羣的時候,添加這個jar包做爲step,指定一個S3上的shell腳本做爲參數,就能夠在啓動集羣后立刻運行這個腳本了。具體命令以下:shell

aws emr create-cluster --name "Test cluster" ...(省略部分參數) --steps Type=CUSTOM_JAR,Name=CustomJAR,ActionOnFailure=CONTINUE,Jar=s3://<YOUR-AWS-REGION>.elasticmapreduce/libs/script-runner/script-runner.jar,Args=["s3://mybucket/script-path/add_password.sh mypassword"]

該命令啓動一個名爲Test cluster的集羣,併爲zeppelinadmin用戶加上mypassword做爲密碼。add_password.sh的代碼以下:apache

#!/bin/bash

password=$1

if test -z "$password"
then 
    exit 1
fi

aws s3 cp s3://mybucket/script-path/add_password.py /home/hadoop/add_password.py

sudo /usr/bin/python3  /home/hadoop/add_password.py $password

sudo stop zeppelin

sudo start zeppelin

exit 0

能夠看到,腳本首先從s3上下載一個python腳原本完成設置密碼的操做,而後再重啓zeppelin使其生效。對應的add_password.py代碼以下安全

#!/usr/bin/python3
import os
import time
import sys


password = str(sys.argv[1]).strip()


with open("/etc/zeppelin/conf/shiro.ini", "r") as f:
    lines = f.readlines()
with open("/etc/zeppelin/conf/shiro.ini", "w") as f:
    for line in lines:
        if line.startswith('admin ='):
            f.write('admin = {}\n'.format(password))
        elif line.startswith('user'):
            continue
        elif line.startswith('/** = anon'):
            f.write('#/** = anon\n')
        elif line.startswith('#/** = authc'):
            f.write('/** = authc\n')
        else:
            f.write(line)


os.popen('cp /etc/zeppelin/conf/zeppelin-site.xml.template /etc/zeppelin/conf/zeppelin-site.xml')
time.sleep(1)
with open("/etc/zeppelin/conf/zeppelin-site.xml", "r") as f:
    lines = f.readlines()
with open("/etc/zeppelin/conf/zeppelin-site.xml", "w") as f:
    for i, line in enumerate(lines):
        if i > 0 and 'zeppelin.anonymous.allowed' in lines[i-1].strip() and line.strip() == '<value>true</value>':
            f.write(line.replace('true', 'false'))
        else:
            f.write(line)

這個python腳本寫的很簡陋,理解就好。bash

相關文章
相關標籤/搜索