通过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
 
相关推荐
测试开发Kevin5 小时前
详解Jenkins Pipeline 中git 命令的使用方法
运维·jenkins
leblancAndSherry6 小时前
Gitlab + Jenkins 实现 CICD
linux·运维·docker·kubernetes·gitlab·jenkins
三劫散仙1 天前
kubernetes jenkins pipeline优化拉取大仓库性能指定分支+深度
容器·kubernetes·jenkins
zmm04203 天前
Could not get unknown property ‘mUser‘ for Credentials [username: null]
jenkins·gradle
keson要进步3 天前
CICD实战(一) -----Jenkins的下载与安装
运维·ci/cd·centos·自动化·jenkins
新兴AI民工4 天前
windows上的visual studio2022的项目使用jenkins自动打包
windows·jenkins·visual studio
风早君4 天前
jenkins集成gitlab发布到远程服务器
服务器·gitlab·jenkins
Thanks_ks4 天前
SpringBoot 自动化部署实战:CI/CD 整合方案与避坑指南
pipeline·jenkins·springboot·自动化部署·gitlab ci/cd·ci/cd 实战·docker 容器化
爱宇阳4 天前
使用 Docker Compose 部署 Jenkins(LTS 版)持续集成环境
ci/cd·docker·jenkins
π大星星️4 天前
Jenkins 工作流程
运维·jenkins