From 7d41ca1bcd92174b9a82298fda6375601ec4914b Mon Sep 17 00:00:00 2001 From: LArkema Date: Sat, 2 Dec 2023 11:55:34 -0800 Subject: [PATCH] Replace single API key with multiple to choose from --- .github/workflows/compile-sketches.yml | 14 ++++++++++---- .../DCTransistor-Bidirectional.ino | 5 ++++- DCTransistor-Bidirectional/config.h | 8 +++++--- DCTransistor/DCTransistor.ino | 5 ++++- DCTransistor/config.h | 8 +++++--- 5 files changed, 28 insertions(+), 12 deletions(-) diff --git a/.github/workflows/compile-sketches.yml b/.github/workflows/compile-sketches.yml index 5b36d5f..7a5fa26 100644 --- a/.github/workflows/compile-sketches.yml +++ b/.github/workflows/compile-sketches.yml @@ -60,8 +60,11 @@ jobs: echo "version=$next_version" >> $GITHUB_OUTPUT - name: Inject API Key run: | - sed -i -E "/SECRET_WMATA_API_KEY/s/[0-9a-f]{32}/${{ secrets.WMATA_API_KEY }}/" ${CONFIG_FILE} - sed -i -E "/SECRET_WMATA_API_KEY/s/[0-9a-f]{32}/${{ secrets.WMATA_API_KEY }}/" ${BI_CONFIG_FILE} + for config in ${CONFIG_FILE} ${BI_CONFIG_FILE}; do + sed -i -E "/SECRET_WMATA_API_KEY_0/s/[0-9a-f]{32}/${{ secrets.WMATA_API_KEY_0 }}/" ${config} + sed -i -E "/SECRET_WMATA_API_KEY_1/s/[0-9a-f]{32}/${{ secrets.WMATA_API_KEY_1 }}/" ${config} + sed -i -E "/SECRET_WMATA_API_KEY_2/s/[0-9a-f]{32}/${{ secrets.WMATA_API_KEY_2 }}/" ${config}; + done - name: Install Arduino Cli uses: arduino/setup-arduino-cli@v1.1.2 - name: Install boards and libraries @@ -83,8 +86,11 @@ jobs: gzip -k -f ${BI_BIN_NAME} - name: Remove WMATA API Key run: | - sed -i "s/${{ secrets.WMATA_API_KEY }}/0123456789abcdef0123456789abcdef/" ${CONFIG_FILE} - sed -i "s/${{ secrets.WMATA_API_KEY }}/0123456789abcdef0123456789abcdef/" ${BI_CONFIG_FILE} + for config in ${CONFIG_FILE} ${BI_CONFIG_FILE}; do + sed -i "s/${{ secrets.WMATA_API_KEY_0 }}/0123456789abcdef0123456789abcdef/" ${config} + sed -i "s/${{ secrets.WMATA_API_KEY_1 }}/123456789abcdef0123456789abcdef0/" ${config} + sed -i "s/${{ secrets.WMATA_API_KEY_2 }}/2123456789abcdef0123456789abcdef/" ${config}; + done - name: Commit changes to repo run: | if [[ $(git status) != *"nothing to commit, working tree clean"* ]]; then diff --git a/DCTransistor-Bidirectional/DCTransistor-Bidirectional.ino b/DCTransistor-Bidirectional/DCTransistor-Bidirectional.ino index 3690f7a..38710de 100644 --- a/DCTransistor-Bidirectional/DCTransistor-Bidirectional.ino +++ b/DCTransistor-Bidirectional/DCTransistor-Bidirectional.ino @@ -25,6 +25,8 @@ uint8 data_failure_count; //Count failures getting live data StaticJsonDocument train_pos_filter; const uint16_t json_size = JSON_DOC_SIZE; //space to allocate for data FROM WMATA API. Change to 3072 without Line Code, 2048 for just CircuitIDs +//Array of different API keys associated with different "default tier" products on the WMATA developer page. +String wmata_api_keys[3] = {SECRET_WMATA_API_KEY_0, SECRET_WMATA_API_KEY_1, SECRET_WMATA_API_KEY_2}; //Create objects representing each line TrainLine* redline = new TrainLine(NUM_RD_STATIONS, rstations_0, rstations_1, RD_HEX_COLOR, rd_led_array_0, rd_led_array_1); @@ -142,7 +144,8 @@ void loop() { #endif } - https.addHeader("api_key", SECRET_WMATA_API_KEY); + //Use one of three WMATA API Keys to stay under usage quota. Actuall randomness not important, just variance in key usage. + https.addHeader("api_key", wmata_api_keys[random(3)]); /* Flawfinder: ignore */ //Request train data from server. If unsuccessful, set LED red. If successful, deserialize the JSON data returned by the API int httpCode = https.GET(); diff --git a/DCTransistor-Bidirectional/config.h b/DCTransistor-Bidirectional/config.h index 7dde97e..f7fc2c8 100644 --- a/DCTransistor-Bidirectional/config.h +++ b/DCTransistor-Bidirectional/config.h @@ -19,14 +19,16 @@ #include //Version string. Changes with every software version -#define VERSION "1.1.2" +#define VERSION "1.1.3" /* * USER CONFIGURATION VALUES */ -//If using own WMATA API key, enter here. Otherwise, one built into downloaded binary -#define SECRET_WMATA_API_KEY "0123456789abcdef0123456789abcdef" +//If using own WMATA API key, enter here (can set same key for every value). Otherwise, one built into downloaded binary +#define SECRET_WMATA_API_KEY_0 "0123456789abcdef0123456789abcdef" +#define SECRET_WMATA_API_KEY_1 "123456789abcdef0123456789abcdef0" +#define SECRET_WMATA_API_KEY_2 "2123456789abcdef0123456789abcdef" //Whether or not to check for automatic updates every time board powers on (turning to false may break board when web TLS certificates expire) #define AUTOUPDATE true diff --git a/DCTransistor/DCTransistor.ino b/DCTransistor/DCTransistor.ino index 912a62d..8f35abf 100644 --- a/DCTransistor/DCTransistor.ino +++ b/DCTransistor/DCTransistor.ino @@ -25,6 +25,8 @@ uint8_t data_failure_count; //Count API failures / empty responses StaticJsonDocument train_pos_filter; const uint16_t json_size = JSON_DOC_SIZE; //space to allocate for data FROM WMATA API. Change to 3072 without Line Code, 2048 for just CircuitIDs +//Array of different API keys associated with different "default tier" products on the WMATA developer page. +String wmata_api_keys[3] = {SECRET_WMATA_API_KEY_0, SECRET_WMATA_API_KEY_1, SECRET_WMATA_API_KEY_2}; //Create objects representing each line TrainLine* redline = new TrainLine(NUM_RD_STATIONS, rstations_0, rstations_1, RD_HEX_COLOR, rd_led_array); @@ -127,7 +129,8 @@ void loop() { #endif } - https.addHeader("api_key", SECRET_WMATA_API_KEY); + //Use one of three WMATA API Keys to stay under usage quota. Actuall randomness not important, just variance in key usage. + https.addHeader("api_key", wmata_api_keys[random(3)]); /* Flawfinder: ignore */ //Request train data from server. If unsuccessful, set LED red. If successful, deserialize the JSON data returned by the API int httpCode = https.GET(); diff --git a/DCTransistor/config.h b/DCTransistor/config.h index 0c55e14..209f98b 100644 --- a/DCTransistor/config.h +++ b/DCTransistor/config.h @@ -19,14 +19,16 @@ #include //Version string. Changes with every software version -#define VERSION "1.1.2" +#define VERSION "1.1.3" /* * USER CONFIGURATION VALUES */ -//If using own WMATA API key, enter here. Otherwise, one built into downloaded binary -#define SECRET_WMATA_API_KEY "0123456789abcdef0123456789abcdef" +//If using own WMATA API key, enter here (can set same key for every value). Otherwise, one built into downloaded binary +#define SECRET_WMATA_API_KEY_0 "0123456789abcdef0123456789abcdef" +#define SECRET_WMATA_API_KEY_1 "123456789abcdef0123456789abcdef0" +#define SECRET_WMATA_API_KEY_2 "2123456789abcdef0123456789abcdef" //Whether or not to check for automatic updates every time board powers on (turning to false may break board when web TLS certificates expire) #define AUTOUPDATE true