【presto】presto使用Java Keystores认证及测试代码

前言

presto 集群使用password插件进行身份认证,及测试代码。如果使用 Kerberos 、 LDAP 身份验证时,必须通过 HTTPS 访问 Presto 协调器。Presto 协调器使用Java Keystore文件进行 TLS 配置。这些密钥是使用keytool生成的,并存储在 Presto 协调器的 Java Keystore 文件中。

实践

  1. 生成keystore.jks 文件
keytool -genkeypair -alias presto -keyalg RSA -keystore keystore.jks

Enter keystore password:
Re-enter new password:
What is your first and last name?
  [Unknown]:  presto-coordinator.example.com
What is the name of your organizational unit?
  [Unknown]:
What is the name of your organization?
  [Unknown]:
What is the name of your City or Locality?
  [Unknown]:
What is the name of your State or Province?
  [Unknown]:
What is the two-letter country code for this unit?
  [Unknown]:
Is CN=presto-coordinator.example.com, OU=Unknown, O=Unknown, L=Unknown, ST=Unknown, C=Unknown correct?
  [no]:  yes

Enter key password for <presto>
        (RETURN if same as keystore password):
  1. 配置config.properties
http-server.authentication.type=PASSWORD
http-server.https.enabled=true
http-server.https.port=8444
http-server.https.keystore.path=etc/keystore.jks
http-server.https.keystore.key=rong360
  1. 创建 etc/password-authenticator.properties
touch etc/password-authenticator.properties
  1. 编辑 etc/password-authenticator.properties
password-authenticator.name=file
file.password-file=/path/to/password.db

密码文件
文件格式
密码文件包含用户名和密码列表,每行一个,用冒号分隔。密码必须使用 bcrypt 或 PBKDF2 安全地散列。

bcrypt 密码以$2y$并且必须使用最低成本8:

test:$2y$10$BqTb8hScP5DfcpmHo5PeyugxHz5Ky/qf3wrpD7SNm8sWuA3VlGqsa
PBKDF2 密码由迭代计数组成,后跟十六进制编码的盐和哈希:

test:1000:5b4240333032306164:f38d165fce8ce42f59d366139ef5d9e1ca1247f0e06e503ee1a611dd9ec40876bb5edb8409f5abe5504aab6628e70cfb3d3a18e99d70357d295002c3d0a308a0

  1. 创建密码文件

可以使用Apache HTTP Server中的htpasswd 实用程序创建使用 bcrypt 格式的密码文件 。必须指定成本,因为 Presto 强制执行的最低成本高于默认值。

创建一个空密码文件以开始:

touch password.db
  1. 添加或更新用户的密码test:
htpasswd -B -C 10 password.db test
  1. jks 转 pem
keytool -importkeystore -srckeystore keystore.jks -destkeystore presto.p12 -srcstoretype jks -deststoretype pkcs12
  1. p12或pfx 转 pem
openssl pkcs12 -nodes -in presto.p12 -out presto.pem

测试代码

配置文件

[presto]
PRESTO_HOST=presto-coordinator.rong360.com
PRESTO_PORT=8444
PRESTO_USER=risk_cjjr_group_m
PRESTO_PASSWD=risk_cjjr_group_m

代码如下:

# -*- coding: utf-8 -*-

import os
import pandas as pd
import prestodb
import configparser

conf = configparser.ConfigParser()
# DATABASE_CONFIGURE_FILE = path_join(os.path.split(os.path.realpath('presto.cfg'))[0],'credentials.cfg')
DATABASE_CONFIGURE_FILE = 'credentials.cfg'
with open(DATABASE_CONFIGURE_FILE, 'r') as cfgfile:
    conf.readfp(cfgfile)

def presto_read_sql(sql):
    conn=prestodb.dbapi.connect(
        host=conf.get('presto', 'PRESTO_HOST'),
        port=conf.get('presto', 'PRESTO_PORT'),
        user=conf.get('presto', 'PRESTO_USER'),
        catalog='hive',
        #schema='trisk',
        http_scheme='https',
        auth=prestodb.auth.BasicAuthentication(conf.get('presto', 'PRESTO_USER'),
                                            conf.get('presto', 'PRESTO_PASSWD')),
    )
    conn._http_session.verify = '/tmp/presto/presto.pem'

    cur = conn.cursor()
    cur.execute(sql)
    rows = cur.fetchall()
    columns = [i[0] for i in cur.description]
    df = pd.DataFrame(rows, columns=columns)
    cur.close()
    print(df)
    return df

if __name__ == '__main__':
    sql = "select * from risk.app_login_reg limit 10"
    presto_read_sql(sql)
    
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值