fix(netbird): extract plain_token from PAT response and add OAuth2 PKCE token fallback
ansible-rus-central / deploy (push) Successful in 20s
ansible-rus-central / deploy (push) Successful in 20s
This commit is contained in:
1 parent
324327ee24
commit
11b6539a7b
1 file changed
+47
-9
@@ -112,7 +112,7 @@
|
|||||||
route_created=0
|
route_created=0
|
||||||
|
|
||||||
setup_req=$(curl "${CURL_OPTS[@]}" "${API_BASE}/instance" | jq -r '.setup_required')
|
setup_req=$(curl "${CURL_OPTS[@]}" "${API_BASE}/instance" | jq -r '.setup_required')
|
||||||
bootstrap_token=""
|
bootstrap_auth_header=""
|
||||||
|
|
||||||
if [ "${setup_req}" = "true" ]; then
|
if [ "${setup_req}" = "true" ]; then
|
||||||
setup_payload=$(jq -n \
|
setup_payload=$(jq -n \
|
||||||
@@ -128,6 +128,7 @@
|
|||||||
echo "Не удалось получить personal_access_token из /api/setup" >&2
|
echo "Не удалось получить personal_access_token из /api/setup" >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
bootstrap_auth_header="Authorization: Token ${bootstrap_token}"
|
||||||
admin_created=1
|
admin_created=1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -142,34 +143,71 @@
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
if [ "${token_valid}" -eq 0 ]; then
|
if [ "${token_valid}" -eq 0 ]; then
|
||||||
if [ -z "${bootstrap_token}" ]; then
|
if [ -z "${bootstrap_auth_header}" ]; then
|
||||||
echo "Сервер уже инициализирован (setup_required=false), но действительный NETBIRD_API_TOKEN в Infisical отсутствует." >&2
|
verifier="dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk"
|
||||||
|
challenge="E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM"
|
||||||
|
redirect_uri="https://{{ netbird_domain }}/nb-auth"
|
||||||
|
auth_url="https://{{ netbird_domain }}/oauth2/auth?client_id=netbird-dashboard&redirect_uri=${redirect_uri}&response_type=code&scope=openid+profile+email+groups&code_challenge=${challenge}&code_challenge_method=S256&state=ansible"
|
||||||
|
|
||||||
|
login_page=$(curl -sLk --resolve "{{ netbird_domain }}:443:127.0.0.1" "${auth_url}")
|
||||||
|
login_action=$(printf '%s' "${login_page}" | grep -oE 'action="[^"]+"' | head -n 1 | cut -d'"' -f2 | sed 's/&/\&/g')
|
||||||
|
if [ -z "${login_action}" ]; then
|
||||||
|
echo "Не удалось получить форму входа OAuth2 для выпуска токена" >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
users_json=$(curl "${CURL_OPTS[@]}" -H "Authorization: Token ${bootstrap_token}" "${API_BASE}/users")
|
redir_loc=$(curl -sSk --resolve "{{ netbird_domain }}:443:127.0.0.1" -o /dev/null -w '%{redirect_url}' \
|
||||||
|
-X POST "https://{{ netbird_domain }}${login_action}" \
|
||||||
|
--data-urlencode "login=${NB_ADMIN_EMAIL}" \
|
||||||
|
--data-urlencode "password=${NB_ADMIN_PASSWORD}")
|
||||||
|
|
||||||
|
if printf '%s' "${redir_loc}" | grep -q '/oauth2/approval'; then
|
||||||
|
redir_loc=$(curl -sSk --resolve "{{ netbird_domain }}:443:127.0.0.1" -o /dev/null -w '%{redirect_url}' "${redir_loc}")
|
||||||
|
fi
|
||||||
|
|
||||||
|
auth_code=$(printf '%s' "${redir_loc}" | sed -n 's/.*[?&]code=\([^&]*\).*/\1/p')
|
||||||
|
if [ -z "${auth_code}" ]; then
|
||||||
|
echo "Не удалось получить authorization code через OAuth2 (проверьте NETBIRD_ADMIN_EMAIL / NETBIRD_ADMIN_PASSWORD)" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
token_resp=$(curl "${CURL_OPTS[@]}" -X POST "https://{{ netbird_domain }}/oauth2/token" \
|
||||||
|
--data-urlencode "grant_type=authorization_code" \
|
||||||
|
--data-urlencode "client_id=netbird-dashboard" \
|
||||||
|
--data-urlencode "redirect_uri=${redirect_uri}" \
|
||||||
|
--data-urlencode "code=${auth_code}" \
|
||||||
|
--data-urlencode "code_verifier=${verifier}")
|
||||||
|
jwt_token=$(printf '%s' "${token_resp}" | jq -r '.access_token // .id_token // empty')
|
||||||
|
if [ -z "${jwt_token}" ]; then
|
||||||
|
echo "Не удалось обменять authorization code на JWT токен" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
bootstrap_auth_header="Authorization: Bearer ${jwt_token}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
users_json=$(curl "${CURL_OPTS[@]}" -H "${bootstrap_auth_header}" "${API_BASE}/users")
|
||||||
svc_user_id=$(printf '%s' "${users_json}" | jq -r '.[] | select(.is_service_user == true and .name == "infra-automation") | .id' | head -n 1)
|
svc_user_id=$(printf '%s' "${users_json}" | jq -r '.[] | select(.is_service_user == true and .name == "infra-automation") | .id' | head -n 1)
|
||||||
|
|
||||||
if [ -z "${svc_user_id}" ]; then
|
if [ -z "${svc_user_id}" ]; then
|
||||||
user_payload=$(jq -n '{name: "infra-automation", role: "admin", auto_groups: [], is_service_user: true}')
|
user_payload=$(jq -n '{name: "infra-automation", role: "admin", auto_groups: [], is_service_user: true}')
|
||||||
svc_user_resp=$(curl "${CURL_OPTS[@]}" -X POST "${API_BASE}/users" \
|
svc_user_resp=$(curl "${CURL_OPTS[@]}" -X POST "${API_BASE}/users" \
|
||||||
-H "Authorization: Token ${bootstrap_token}" \
|
-H "${bootstrap_auth_header}" \
|
||||||
-H "Content-Type: application/json" \
|
-H "Content-Type: application/json" \
|
||||||
-d "${user_payload}")
|
-d "${user_payload}")
|
||||||
svc_user_id=$(printf '%s' "${svc_user_resp}" | jq -r '.id')
|
svc_user_id=$(printf '%s' "${svc_user_resp}" | jq -r '.id')
|
||||||
fi
|
fi
|
||||||
|
|
||||||
old_tokens=$(curl "${CURL_OPTS[@]}" -H "Authorization: Token ${bootstrap_token}" "${API_BASE}/users/${svc_user_id}/tokens" || echo "[]")
|
old_tokens=$(curl "${CURL_OPTS[@]}" -H "${bootstrap_auth_header}" "${API_BASE}/users/${svc_user_id}/tokens" || echo "[]")
|
||||||
for old_tid in $(printf '%s' "${old_tokens}" | jq -r '.[] | select(.name == "infisical-ci-token") | .id'); do
|
for old_tid in $(printf '%s' "${old_tokens}" | jq -r '.[] | select(.name == "infisical-ci-token") | .id'); do
|
||||||
curl "${CURL_OPTS[@]}" -X DELETE -H "Authorization: Token ${bootstrap_token}" "${API_BASE}/users/${svc_user_id}/tokens/${old_tid}" >/dev/null || true
|
curl "${CURL_OPTS[@]}" -X DELETE -H "${bootstrap_auth_header}" "${API_BASE}/users/${svc_user_id}/tokens/${old_tid}" >/dev/null || true
|
||||||
done
|
done
|
||||||
|
|
||||||
pat_payload=$(jq -n '{name: "infisical-ci-token", expires_in: 365}')
|
pat_payload=$(jq -n '{name: "infisical-ci-token", expires_in: 365}')
|
||||||
pat_resp=$(curl "${CURL_OPTS[@]}" -X POST "${API_BASE}/users/${svc_user_id}/tokens" \
|
pat_resp=$(curl "${CURL_OPTS[@]}" -X POST "${API_BASE}/users/${svc_user_id}/tokens" \
|
||||||
-H "Authorization: Token ${bootstrap_token}" \
|
-H "${bootstrap_auth_header}" \
|
||||||
-H "Content-Type: application/json" \
|
-H "Content-Type: application/json" \
|
||||||
-d "${pat_payload}")
|
-d "${pat_payload}")
|
||||||
active_token=$(printf '%s' "${pat_resp}" | jq -r '.plain_text // empty')
|
active_token=$(printf '%s' "${pat_resp}" | jq -r '.plain_token // .plain_text // empty')
|
||||||
if [ -z "${active_token}" ]; then
|
if [ -z "${active_token}" ]; then
|
||||||
echo "Не удалось выпустить сервисный токен через /api/users/${svc_user_id}/tokens" >&2
|
echo "Не удалось выпустить сервисный токен через /api/users/${svc_user_id}/tokens" >&2
|
||||||
exit 1
|
exit 1
|
||||||
|
|||||||
Reference in new issue
Block a user