<commit_before>#!/bin/bash


if [[ -z "$SOTA_NTP_SKIP" ]]; then
  while [[ "$(timedatectl status | grep NTP)" != "NTP synchronized: yes" ]]; do
    echo "Waiting for NTP sync..."
    sleep 5
  done
fi


set -xeuo pipefail

device_id="${SOTA_DEVICE_ID-$(ifconfig -a | grep -oE '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}' | head -n1)}"
hardware_id="${SOTA_HARDWARE_ID-$(cat /etc/hostname)}"
primary_serial=$(python -c "import random; print(random.randint(10**12, 10**13-1))")
cert_dir="${SOTA_CERT_DIR-/usr/local/etc/sota}"
mkdir -p "$cert_dir" && cd "$cert_dir"

in_reg="${1-credentials}" # input registration credentials file prefix
out_dev="${2-device}"     # output device credentials file prefix
out_ca="${3-ca}"          # output ca certificates file prefix
out_pri="${4-primary}"    # output primary ecu file prefix
in_ecus="${5-ecus}"       # input secondary ecus file


main() {
  register_device
  register_ecus
  fetch_metadata root director
  fetch_metadata root repo
  generate_toml
}

register_device() {
  echo "Registering device: $device_id"
  [ ! -f "$in_reg.p12"  ] && { echo "Missing '$in_reg.p12' in $PWD"; exit 1; }
  [   -f "$out_dev.p12" ] && { echo "Already provisioned '$out_dev.p12' in $PWD"; exit 0; }

  openssl pkcs12 -in "$in_reg.p12" -out "$in_reg.pem" -nodes -passin pass:""
  openssl pkcs12 -in "$in_reg.p12" -cacerts -nokeys -passin pass:"" 2>/dev/null \
    | sed -n '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > "$out_ca.crt"
  curl -vvf --cacert "$out_ca.crt" --cert "$in_reg.pem" "$SOTA_GATEWAY_URI/devices" \
    -H 'Content-Type: application/json' \
    -d '{"deviceId":"'"$device_id"'","ttl":365}' \
    -o "$out_dev.p12"
  echo "Wrote device bundle to $cert_dir/$out_dev.p12"

  openssl pkcs12 -in "$out_dev.p12" -out "$out_dev.pem" -nodes -passin pass:""
  echo "Wrote device certificate to $cert_dir/$out_dev.pem"
  openssl pkcs12 -in "$out_dev.p12" -cacerts -nokeys -passin pass:"" 2>/dev/null \
    | sed -n '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > "$out_ca.crt"
  echo "Wrote certificate authority file to $cert_dir/$out_ca.crt"
  openssl pkcs12 -in "$out_dev.p12" -clcerts -nokeys -passin pass:"" 2>/dev/null \
    | sed -n '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > "$out_dev.crt"
  echo "Wrote client certificate to $cert_dir/$out_dev.crt"
}

register_ecus() {
  echo "Generating RSA keypair for Primary ECU"
  openssl genpkey -algorithm RSA -out "$out_pri.der" -outform DER -pkeyopt rsa_keygen_bits:2048
  openssl rsa -pubout -in "$out_pri.der" -inform DER -out "$out_pri.pub"
  pubkey=$(sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/\\n/g' < "$out_pri.pub")<commit_msg>PRO-3452 Only wait for NTP if first prov attempt fails<commit_after>#!/bin/bash

set -xeuo pipefail

device_id="${SOTA_DEVICE_ID-$(ifconfig -a | grep -oE '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}' | head -n1)}"
hardware_id="${SOTA_HARDWARE_ID-$(cat /etc/hostname)}"
primary_serial=$(python -c "import random; print(random.randint(10**12, 10**13-1))")
cert_dir="${SOTA_CERT_DIR-/usr/local/etc/sota}"
mkdir -p "$cert_dir" && cd "$cert_dir"

in_reg="${1-credentials}" # input registration credentials file prefix
out_dev="${2-device}"     # output device credentials file prefix
out_ca="${3-ca}"          # output ca certificates file prefix
out_pri="${4-primary}"    # output primary ecu file prefix
in_ecus="${5-ecus}"       # input secondary ecus file


main() {
  register_device || { wait_for_ntp && register_device; }
  register_ecus
  fetch_metadata root director
  fetch_metadata root repo
  generate_toml
}

function wait_for_ntp() {
  while [[ "$(timedatectl status | grep NTP)" != "NTP synchronized: yes" ]]; do
    sleep 5
    echo "Waiting for NTP sync..."
  done
}

register_device() {
  echo "Registering device: $device_id"
  [ ! -f "$in_reg.p12"  ] && { echo "Missing '$in_reg.p12' in $PWD"; exit 1; }
  [   -f "$out_dev.p12" ] && { echo "Already provisioned '$out_dev.p12' in $PWD"; exit 0; }

  openssl pkcs12 -in "$in_reg.p12" -out "$in_reg.pem" -nodes -passin pass:""
  openssl pkcs12 -in "$in_reg.p12" -cacerts -nokeys -passin pass:"" 2>/dev/null \
    | sed -n '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > "$out_ca.crt"
  curl -vvf --cacert "$out_ca.crt" --cert "$in_reg.pem" "$SOTA_GATEWAY_URI/devices" \
    -H 'Content-Type: application/json' \
    -d '{"deviceId":"'"$device_id"'","ttl":365}' \
    -o "$out_dev.p12" \
    || return 1
  echo "Wrote device bundle to $cert_dir/$out_dev.p12"

  openssl pkcs12 -in "$out_dev.p12" -out "$out_dev.pem" -nodes -passin pass:""
  echo "Wrote device certificate to $cert_dir/$out_dev.pem"
  openssl pkcs12 -in "$out_dev.p12" -cacerts -nokeys -passin pass:"" 2>/dev/null \
    | sed -n '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > "$out_ca.crt"
  echo "Wrote certificate authority file to $cert_dir/$out_ca.crt"
  openssl pkcs12 -in "$out_dev.p12" -clcerts -nokeys -passin pass:"" 2>/dev/null \
    | sed -n '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > "$out_dev.crt"
  echo "Wrote client certificate to $cert_dir/$out_dev.crt"
}

register_ecus() {
  echo "Generating RSA keypair for Primary ECU"
  openssl genpkey -algorithm RSA -out "$out_pri.der" -outform DER -pkeyopt rsa_keygen_bits:2048
  openssl rsa -pubout -in "$out_pri.der" -inform DER -out "$out_pri.pub"
  pubkey=$(sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/\\n/g' < "$out_pri.pub")
{'fraction_non_alphanumeric': 0.18157944100543988, 'fraction_numerical': 0.025323579065841307, 'mean_word_length': 3.71858407079646, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1992830', 'n_tokens_mistral': 2278, 'n_tokens_neox': 2107, 'n_words': 507}