通过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
 
相关推荐
一点事2 天前
windows:ELK搭建(单机)
windows·elk·jenkins
lcx_defender2 天前
【Docker】Docker部署运行Elasticsearch
elasticsearch·docker·jenkins
海兰3 天前
win11下部署elastic9.0+常见问题及解决方法(续)
运维·jenkins
海鸥813 天前
k8s中Jenkins 配置文件「 更新不了 」
java·kubernetes·jenkins
_运维那些事儿3 天前
GitLab&Jenkins
运维·ci/cd·gitlab·jenkins·devops
一念一花一世界3 天前
Jenkins 太重?试试超轻量开源 CI/CD 工具 Arbess
运维·ci/cd·jenkins·arbess
liux35283 天前
Elasticsearch 8.13.2 单机部署与配置详细指南
大数据·elasticsearch·jenkins
切糕师学AI3 天前
Jenkins Pipeline 小解
运维·servlet·jenkins
BUTCHER54 天前
elasticsearch时间搜索
大数据·elasticsearch·jenkins
Hellc0074 天前
Jenkins 上下游 Job + Docker 镜像部署完整实战(避坑版)
java·docker·jenkins