Commit aa396f29 authored by Cédric OLIVIER's avatar Cédric OLIVIER Committed by Pierre Smeyers
Browse files

fix: improve logs when @url@ variable eval fails

parent dd7a0be2
Loading
Loading
Loading
Loading
+26 −4
Original line number Diff line number Diff line
@@ -260,7 +260,29 @@ stages:
      ;;
    @url@*)
      url=$(echo "$value" | cut -c6-)
      if command -v curl > /dev/null
      protocol=$(echo "$url" | cut -d':' -f1)
      if command -v busybox > /dev/null && [ "${protocol}" == "http" ]
      then
        output=$(mktemp)
        decoded=$(mktemp)
        errors=$(mktemp)
        host_port=$(echo "$url" | cut -d'/' -f3)
        host=$(echo "$host_port" | cut -d':' -f1)
        port=$(echo "$host_port" | cut -d':' -s -f2)
        subject=$(echo "$url" | cut -d'/' -f4-)
        cat <(echo -e "GET /${subject} HTTP/1.1\r\nHost: $host\r\n\r\n") <(sleep 1) | nc ${TRACE+-v} "$host" "${port:-80}" > "${output}"
        status_code=$(head -1 "${output}" | cut -d' ' -f2)
        status_message=$(head -1 "${output}" | cut -d' ' -f3-)
        awk 'NR==1,/^\r$/ {next} {printf "%s%s",$0,RT}' "${output}" > "${decoded}"

        if [ "${status_code}" == "200" ]; then
          # shellcheck disable=SC2086
          export ${name}="$(cat ${decoded})"
          log_info "Successfully netcat'd secret \\e[33;1m${name}\\e[0m"
        else
          log_warn "Failed getting secret \\e[33;1m${name}\\e[0m (${status_code} ${status_message}):\\n$(sed 's/^/... /g' "${decoded}")\\n... -----\\n$(sed 's/^/... /g' "${errors}")"
        fi
      elif command -v curl > /dev/null
      then
        decoded=$(mktemp)
        errors=$(mktemp)
@@ -270,19 +292,19 @@ stages:
          export ${name}="$(cat ${decoded})"
          log_info "Successfully curl'd secret \\e[33;1m${name}\\e[0m"
        else
          log_warn "Failed getting secret \\e[33;1m${name}\\e[0m:\\n$(sed 's/^/... /g' "${errors}")"
          log_warn "Failed getting secret \\e[33;1m${name}\\e[0m:\\n$(sed 's/^/... /g' "${decoded}")\\n... -----\\n$(sed 's/^/... /g' "${errors}")"
        fi
      elif command -v wget > /dev/null
      then
        decoded=$(mktemp)
        errors=$(mktemp)
        if wget -T 5 -O "${decoded}" "$url" 2> "${errors}"
        if wget --content-on-error -T 5 -O "${decoded}" "$url" 2> "${errors}"
        then
          # shellcheck disable=SC2086
          export ${name}="$(cat ${decoded})"
          log_info "Successfully wget'd secret \\e[33;1m${name}\\e[0m"
        else
          log_warn "Failed getting secret \\e[33;1m${name}\\e[0m:\\n$(sed 's/^/... /g' "${errors}")"
          log_warn "Failed getting secret \\e[33;1m${name}\\e[0m:\\n$(sed 's/^/... /g' "${decoded}")\\n... -----\\n$(sed 's/^/... /g' "${errors}")"
        fi
      else
        log_warn "Couldn't get secret \\e[33;1m${name}\\e[0m: no http client found"