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

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

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

  1. import json
  2. import boto3
  3. import os
  4. from boto3.dynamodb.conditions import Key
  5. def lambda_handler(event, context):
  6. print('##Below Event occured')
  7. eventName = event['Records'][0]['eventName']
  8. request_status = event['Records'][0]['dynamodb']['NewImage'
  9. ]['request_status']['S']
  10. account_id = event['Records'][0]['dynamodb']['NewImage'
  11. ]['account_id']['S']
  12. if eventName == "INSERT" and request_status == "Approved":
  13. print("Condition ran Successfully")
  14. script = """
  15. curl -X POST -is -u USERNAME:PASSWORD \
  16. -H 'Content-Type: application/json' \
  17. https://api.bitbucket.org/2.0/repositories/workspace/repo/pipelines/ \
  18. -d '
  19. {
  20. "target": {
  21. "type": "pipeline_ref_target",
  22. "ref_type": "branch",
  23. "ref_name": "master",
  24. "selector": {
  25. "type": "custom",
  26. "pattern": "create-aws-account"
  27. }
  28. },
  29. "variables": [
  30. {
  31. "key": "account_id",
  32. "value": "000000011",
  33. "secured": true
  34. }
  35. ]
  36. }'
  37. """
  38. os.system(script)
krcsximq

krcsximq1#

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

  1. import json
  2. script = f"""
  3. curl -X POST -is -u USERNAME:PASSWORD \
  4. -H 'Content-Type: application/json' \
  5. https://api.bitbucket.org/2.0/repositories/workspace/repo/pipelines/ \
  6. -d '
  7. {{
  8. "target": {{
  9. "type": "pipeline_ref_target",
  10. "ref_type": "branch",
  11. "ref_name": "master",
  12. "selector": {{
  13. "type": "custom",
  14. "pattern": "create-aws-account"
  15. }}
  16. }},
  17. "variables": [{{
  18. "key": {json.dumps(str(account_id))},
  19. "value": "000000011",
  20. "secured": true
  21. }}]
  22. }}'
  23. """

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

展开查看全部

相关问题