如何在python lambda函数中的curl命令中将参数传递给json?

nqwrtyyt  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(377)

如何传递的值 account_id 由下面的python lambda函数获取到curl命令中的json值,用于调用bitbucket端点api代码:

import json
import boto3
import os
from boto3.dynamodb.conditions import Key

def lambda_handler(event, context):

    print('##Below Event occured')
    eventName = event['Records'][0]['eventName']
    request_status = event['Records'][0]['dynamodb']['NewImage'
            ]['request_status']['S']
    account_id = event['Records'][0]['dynamodb']['NewImage'
            ]['account_id']['S']

    if eventName == "INSERT" and request_status == "Approved":
        print("Condition ran Successfully")
        script = """
        curl -X POST -is -u USERNAME:PASSWORD \
        -H 'Content-Type: application/json' \
        https://api.bitbucket.org/2.0/repositories/workspace/repo/pipelines/ \
        -d '
        {
            "target": { 
            "type": "pipeline_ref_target",
            "ref_type": "branch",
            "ref_name": "master",
            "selector": {
            "type": "custom",
            "pattern": "create-aws-account"
        }
        },
        "variables": [
            {
            "key": "account_id",
            "value": "000000011",
            "secured": true
            }
            ]
          }'

        """
        os.system(script)
krcsximq

krcsximq1#

正如@charles duffy所建议的,这种方法具有潜在的危险性,并且会受到恶意命令的影响。不鼓励使用与未知用户输入连接的字符串运行命令。
使用f字符串:

import json

script = f"""
curl -X POST -is -u USERNAME:PASSWORD \
-H 'Content-Type: application/json' \
https://api.bitbucket.org/2.0/repositories/workspace/repo/pipelines/ \
-d '
{{
    "target": {{
        "type": "pipeline_ref_target",
        "ref_type": "branch",
        "ref_name": "master",
        "selector": {{
            "type": "custom",
            "pattern": "create-aws-account"
        }}
    }},
    "variables": [{{
        "key": {json.dumps(str(account_id))},
        "value": "000000011",
        "secured": true
    }}]
}}'
"""

哪里 {variable_name} 是要插入的值的占位符,并且 {{}} 是用来逃避大括号的。

相关问题