通过secret_id和role_id连接Vault

怎么生成secret_id 和role_id 通过这篇文章可以找到:

Jenkins pipeline 怎么连接Vault_jenkinsfile withvault-CSDN博客

当你拥有了secret_id 和role_id,你就可以通过以下代码来进行连接:

bash 复制代码
VaultLoginByApprole(){
  role_id=$1
  secret_id=$2
  export VAULT_ADDR=your vault addr
  export VAULT_NAMESPACE=your namesapce
  export VAULT_TOKEN="$(vault write -ns '$ns' -field=token auth/approle/login role_id="$role_id" secret_id="$secret_id")"

}

Python实现

python 复制代码
import subprocess
import os

def VaultLoginByApprole(role_id, secret_id):
    # Set environment variables
    os.environ['VAULT_ADDR'] = 'your vault addr'
    os.environ['VAULT_NAMESPACE'] = 'your namespace'
    
    # Run the vault write command and get the token
    command = f"vault write -ns {os.environ['VAULT_NAMESPACE']} -field=token auth/approle/login role_id={role_id} secret_id={secret_id}"
    output = subprocess.run(command.split(), capture_output=True, text=True)
    token = output.stdout.strip()
    
    # Set the VAULT_TOKEN environment variable
    os.environ['VAULT_TOKEN'] = token
    
 
python 复制代码
import os
import hvac

def VaultLoginByApprole(role_id, secret_id):
    # Set environment variables
    os.environ['VAULT_ADDR'] = 'your_vault_addr'
    os.environ['VAULT_NAMESPACE'] = 'your_namespace'

    # Create a Vault client
    client = hvac.Client()
    
    # Login using AppRole
    response = client.auth_approle(role_id, secret_id)
    
    # Extract the token from the response
    token = response['auth']['client_token']

    # Set the VAULT_TOKEN environment variable
    os.environ['VAULT_TOKEN'] = token
 
相关推荐
小醉你真好12 小时前
15、Docker Compose 安装ELK + Filebeat单机版
elk·docker·jenkins
菜鸟plus+18 小时前
ElasticSearch
运维·jenkins
要站在顶端20 小时前
Jenkins 使用容器运行自动化任务详细文档
docker·自动化·jenkins
深思慎考1 天前
ElasticSearch与Kibana 入门指南(7.x版本)
大数据·elasticsearch·jenkins
yunson_Liu2 天前
jenkins更新了gitlab后出现报错
运维·gitlab·jenkins
惜.己2 天前
linux中jenkins正常启动外部无法访问
linux·servlet·jenkins
天航星5 天前
Docker 安装 Jenkins
java·运维·jenkins
深思慎考6 天前
【新版】Elasticsearch 8.15.2 完整安装流程(Linux国内镜像提速版)
java·linux·c++·elasticsearch·jenkins·框架
躺着数星星7 天前
Linux中安装es
linux·elasticsearch·jenkins
gb42152878 天前
Spring Boot 2.5集成Elasticsearch(亲测)
spring boot·elasticsearch·jenkins