json 구문에서 큰따옴표가 포함된 AWS CLI 명령을 전달할 수 없습니다.

json 구문에서 큰따옴표가 포함된 AWS CLI 명령을 전달할 수 없습니다.

아래 스크립트를 사용하고 있는데 Secret Manager에서 자격 증명을 얻을 수 없기 때문에 구문 오류가 발생합니다. "사용자 이름": "입력한 액세스 키에 대한 AWS CLI 명령", "비밀번호": "입력한 키에 대한 AWS CLI 명령". 누군가가 도움을 줄 수 있다면 좋을 것입니다.

#!/bin/bash

# Send the POST request and capture the response
response=$(curl -k \
-H "Content-Type: application/json" \
-X POST \
-d \
'{
"username":"'aws secretsmanager get-secret-value --region ap-south-1 --secret-id poc | jq --raw-output '.SecretString' | jq -r '.Access_Key''",
"password":"'aws secretsmanager get-secret-value --region ap-south-1 --secret-id poc | jq --raw-output '.SecretString' | jq -r '.Secret_Key''"
}' \
https://<region>/api/v1/authenticate)

답변1

어떻게 해야 할까요? 쉘을 사용하세요여기에 문서가 있습니다:

#!/bin/bash

username=$(aws secretsmanager get-secret-value ... | jq '...')
password=$(aws secretsmanager get-secret-value ... | jq '...')
anotherVariable=foobar

# Send the POST request and capture the response
response=$(
    curl -k \
        -H "Content-Type: application/json" \
        https://<region>/api/v1/authenticate
        -d @/dev/stdin <<EOF
        {
            "username": $username,
            "password": $password,
            "anotherKey": "$anotherVariable"

        }
EOF
)

-X POST사용에는 필요하지 않습니다 -d.


익숙해지면 jq@Kusalananda의 답변을 선택하거나 혼합해 보세요. 그렇지 않다면, 익숙하지 않다면 jq.

답변2

페이로드 데이터의 참조에 문제가 있으며 이러한 명령 파이프라인이 자동으로 실행되지 않습니다.

대신 먼저 JSON 페이로드 문서를 준비한 다음 curl준비된 데이터로 호출하는 것을 고려해 보세요.

#!/bin/sh

# Prepare JSON payload containing username and password.
payload=$(
        aws secretsmanager get-secret-value --region ap-south-1 --secret-id poc |
        jq '.SecretString | fromjson | {username: .Access_Key, password: .Secret_Key}'
)

# Submit the payload to the API and capture the response.
response=$(
        curl --silent \
                -H 'Content-Type: application/json' \
                -d "$payload" \
                "https://{{region}}/api/v1/authenticate"
)

여러 데이터 조각을 JSON 객체로 한 번에 가져올 수 있으므로 AWS 자격 증명을 얻으려면 aws+ 호출을 한 번만 하면 됩니다 . 키 값 은 포함된 JSON 개체이므로 포함된 필드에 액세스하기 전에 표현식에서 이를 디코딩합니다.jqjqSecretStringfromjsonjq

관련 정보