首页 > 解决方案 > 如何在 Bash 脚本中调用 Secret

问题描述

所以我知道一种在 Powershell 脚本和 Python 脚本中处理机密的方法。我很好奇是否有办法在 bash 脚本中调用 json、yml 或 json 对象。您将使用什么以及如何在脚本中动态调用它们。

这是脚本:

#!/bin/bash
# ===========================================================
# Created By: Richard Barrett
# Organization: Mirantis
# Department: Customer Success Operation
# Purpose: Send Message to Slack Channel
# Date: 03/20/2020
# ===========================================================
# Use Messages in this command syntax
# curl -X POST -H 'Content-type: application/json' --data '{"text":"BODY"}' <insert_URL>

# Generalt Message:
curl -X POST -H 'Content-type: application/json' --data '{"text":"Please see the following links for Handovers and Change Requests that may impact your shift."}' https://hooks.slack.com/services/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

# Messages for Handover:
curl -X POST -H 'Content-type: application/json' --data '{"text":"Handovers: https://mirantis.my.salesforce.com/00O2S000003g25h"}' <insert_URL>

# Message for All Change Requests:
curl -X POST -H 'Content-type: application/json' --data '{"text":"All Change Requests: https://mirantis.my.salesforce.com/00O2S000004INH1"}' <insert_URL>

# Message for Change Requests in Ready to Execute
# curl -X POST -H 'Content-type: application/json' --data '{"text":"All CRs in Ready to Execute:"}' <insert_URL>

它说我正在通过 slack 插入一个 web-hook 链接。有没有一种类似于python中以下json方法的方法来调用它?

with open('secrets.json','r') as f:
      config = json.load(f)

# Set the webhook_url to the one provided by Slack when you create the webhook at https://my.slack.com/services/new/incoming-webhook/
# webhook_url = 'https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX'
# slack_data = {'text': "BODY"}
webhook_url = (config['slack_config']['slack_target_url'])
slack_message_1={'text': config['slack_messages']['message_1']}
slack_message_2={'text': config['slack_messages']['message_2']}
slack_message_3={'text': config['slack_messages']['message_3']}

我也知道可以制作一个文件并将其作为秘密xml加载到脚本中。Powershell我只需要一些关于如何在 shell 脚本中处理秘密的指导。

标签: linuxbashshellautomationscripting

解决方案


与 Python 等效的 Bashwebhook_url = (config['slack_config']['slack_target_url'])将是webhook_url="$(jq --raw-output .slack_config.slack_target_url secrets.json)". 演示:

$ echo '{"slack_config": {"slack_target_url": "URL"}}' | jq --raw-output .slack_config.slack_target_url
URL

推荐阅读