通过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
 
相关推荐
终端行者3 天前
Jenkins 流水线 Pipeline 声明式语法基础 入门----上
运维·jenkins
终端行者3 天前
Jenkins流水线Pipeline声明式语法基础入门----下
运维·jenkins·cicd
lifewange3 天前
Jenkins Windows MSI 安装包完整安装教程
windows·servlet·jenkins
marsh02063 天前
35 openclawCQRS模式应用:分离读写操作提升性能
运维·ai·jenkins·编程·技术
Oliver_LaVine4 天前
java项目启动报错:CreateProcess error=206, 文件名或扩展名太长
java·linux·jenkins
白毛大侠4 天前
Elasticsearch 核心概念解析:从倒排索引到字段存储
大数据·elasticsearch·jenkins
斌味代码4 天前
Elasticsearch 性能调优实战:内存管理、连接池与查询优化全指南
大数据·elasticsearch·jenkins
HoneyMoose4 天前
Jenkins Cloudflare 部署提示错误
java·servlet·jenkins
HoneyMoose5 天前
Jenkins pipeline 构建提示 packageManager 配置
运维·jenkins