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

背景

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

手動設置

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

通過step自動設置

手動設置需要在每次集羣啓動後都設置一遍,如果需要頻繁啓動集羣,就很不方便,因此可以在啓動集羣的時候使用step來自動設置。
具體來說,AWS提供了一個script-runner.jar,它的路徑是s3://<YOUR-AWS-REGION>.elasticmapreduce/libs/script-runner/script-runner.jar,在啓動集羣的時候,添加這個jar包作爲step,指定一個S3上的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的代碼如下:

#!/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腳本寫的很簡陋,理解就好。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章