Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove secrets usage #245

Merged
merged 10 commits into from
Mar 6, 2025
Merged

Conversation

justmobilize
Copy link
Collaborator

@justmobilize justmobilize commented Feb 24, 2025

Remove usage of secrets and more:

  1. change from using secrets.py to os.getenv
  2. switch from ESPSPI_WiFiManager to WiFiManager
  3. update pool, ssl_context setting to use adafruit_connection_manager like all the other examples
  4. Updated some things to use f-strings

@justmobilize justmobilize marked this pull request as ready for review February 24, 2025 23:27
@dhalbert dhalbert requested review from brentru and tyeth February 24, 2025 23:30
@justmobilize
Copy link
Collaborator Author

There are 6 PRs that use ESPSPI_WiFiManager and are the largest user of secrets in examples. This is 1 of them

Copy link
Contributor

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks OK to me but as this is quite a vital library it would be good for @brentru and/or @tyeth to review also.

@@ -61,7 +63,7 @@ def message(client, topic, message):

# Set up a MiniMQTT Client
mqtt_client = MQTT.MQTT(
broker=os.getenv("broker"),
broker="io.adafruit.com",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we stick with broker env var lookup and fall back to io.adafruit.com using the second parameter / default value, as this example isn't really IO related (just a good test server if the user already has an account). Similarly here: https://github.com/adafruit/Adafruit_CircuitPython_MiniMQTT/pull/245/files#diff-afdace9507c758aee40533c5ef2f1cacbfe152f00f601e2fc07571931dfa63f8R16 which is examples/esp32spi/minimqtt_certificate_esp32spi.py line 16.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tyeth , so do:

broker = getenv("broker", "io.adafruit.com")

in this one?

Copy link
Contributor

@tyeth tyeth Feb 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, something like that. think i spotted "broker" in a few other places too (just glancing at the connect lines)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will find all of these and make sure they are updated correctly. Will hold until I get other answers to do the work

@@ -11,12 +11,12 @@

import adafruit_minimqtt.adafruit_minimqtt as MQTT

# Add settings.toml to your filesystem CIRCUITPY_WIFI_SSID and CIRCUITPY_WIFI_PASSWORD keys
# with your WiFi credentials. Add your Adafruit IO username and key as well.
# DO NOT share that file or commit it into Git or other source control.
Copy link
Contributor

@tyeth tyeth Feb 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want these warnings back (line 16), regarding not commiting/sharing settings.toml, as these examples are more intended for new users.
I'm also less of a fan of just getenv and prefer the module qualified version (os.getenv) as it's clearer what's going on for new users (the from syntax is rarely mentioned in learn guides), but personal preference maybe (and we should educate users).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So these are copied from other example. Like Requests just has:

# Get WiFi details, ensure these are setup in settings.toml
ssid = os.getenv("CIRCUITPY_WIFI_SSID")
password = os.getenv("CIRCUITPY_WIFI_PASSWORD")

I did my best to merge them. Happy to update these to have the warnings if wanted.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tyeth, as far as os.getenv vs getenv, I'm good either way. The examples across the libraries are a mix. Let me know what you want here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tyeth I'm happy with what ever you want here. Let me know

Copy link
Contributor

@tyeth tyeth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great. Only a couple of thoughts related to beginners, and using the broker address from os.getenv with a default value of io.adafruit.com wherever broker is used.
I'd quite like to run all of the examples before release, just to be sure, but won't likely get around to it until the end of the week. Equally happy for others to do so instead.

@tyeth
Copy link
Contributor

tyeth commented Feb 26, 2025 via email

port=os.getenv("port"),
username=os.getenv("username"),
password=os.getenv("password"),
broker="io.adafruit.com",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example is actually meant to be optionally another server and port (just as it's simple test we might as well show the optional arg, you can use 1883 for insecure or 8883 for secure), as all it does it test callbacks and no IO specific behaviour.

Comment on lines 96 to 98
broker=broker,
port=broker_port,
socket_pool=pool,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think we want to add the username and password here too, not sure how this ever worked unless it was a broker that doesn't require one (like test.mosquitto.org), but the aio_username used in topic later is now needed so we might as well offer it here too.

Suggested change
broker=broker,
port=broker_port,
socket_pool=pool,
broker=broker,
port=broker_port,
username=aio_username,
password=aio_key,
socket_pool=pool,

@@ -106,14 +105,14 @@ def on_message(client, topic, message):
client.on_subscribe = subscribe
client.on_unsubscribe = unsubscribe
client.on_message = on_message
client.add_topic_callback(secrets["aio_username"] + "/feeds/device.batterylevel", on_battery_msg)
client.add_topic_callback(aio_username + "/feeds/device.batterylevel", on_battery_msg)
Copy link
Contributor

@tyeth tyeth Mar 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This (aio_username) doesn't exist, see comments earlier in file

Comment on lines 17 to 21
ssid = getenv("CIRCUITPY_WIFI_SSID")
password = getenv("CIRCUITPY_WIFI_PASSWORD")
broker = getenv("broker")
broker_port = getenv("broker_port")

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we provide defaults here too for port/broker, in addition to getting the aio_username (and I've grabbed key too)

Suggested change
ssid = getenv("CIRCUITPY_WIFI_SSID")
password = getenv("CIRCUITPY_WIFI_PASSWORD")
broker = getenv("broker")
broker_port = getenv("broker_port")
ssid = getenv("CIRCUITPY_WIFI_SSID")
password = getenv("CIRCUITPY_WIFI_PASSWORD")
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
aio_key = getenv("ADAFRUIT_AIO_KEY")
broker = getenv("broker", "io.adafruit.com")
broker_port = getenv("broker_port", 1883) # Port 1883 insecure, 8883 secure

@justmobilize justmobilize requested review from dhalbert and tyeth March 1, 2025 16:02
@justmobilize
Copy link
Collaborator Author

@tyeth I updated all the places we needed broker, fixed some f-strings, an esp32spi update from moving rssi (to match wifi.radio a while ago) and removed unsecure ports when we are passing in a ssl context.

Comment on lines +14 to +18
ssid = getenv("CIRCUITPY_WIFI_SSID")
password = getenv("CIRCUITPY_WIFI_PASSWORD")
broker = getenv("broker")
username = getenv("username")
paswword = getenv("paswword")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getenv not defined

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tyeth good catch. Fixed.

Ruff didn't catch that, so that's strange (cc @FoamyGuy)

Copy link
Contributor

@tyeth tyeth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just that one missing import, apart from that looks great, thanks for all the hard work!

I can't deny I wanted to suggest the "broker" environment variable be uppercase, but seeing the older fona example use it I guess either we have that as the odd one out or just leave broker alone for now.

@justmobilize justmobilize requested a review from tyeth March 6, 2025 05:12
@justmobilize
Copy link
Collaborator Author

Just that one missing import, apart from that looks great, thanks for all the hard work!

I can't deny I wanted to suggest the "broker" environment variable be uppercase, but seeing the older fona example use it I guess either we have that as the odd one out or just leave broker alone for now.

Happy to update, but the few places in the bundle it exists are all lowercase

@tyeth
Copy link
Contributor

tyeth commented Mar 6, 2025 via email

@Neradoc
Copy link

Neradoc commented Mar 6, 2025

Attempting to use the official ruff plugin for vscode, it refuses to use ruff with this repo and in the logs shows:

E999 Was removed in ruff 0.8.0 (November 2024) because it's always on now (it's syntax errors).
https://github.com/astral-sh/ruff/releases/tag/0.8.0

The cookiecutter pre-commit uses v0.3.4 (and therefore so does this repository).
I can only assume the plugin uses a more recent version.
Ruff changes very quickly it seems. Current is 0.9.9

Note the pre-commit version seemingly matches the ruff version.

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.4

@justmobilize
Copy link
Collaborator Author

@tyeth are you good with the code?

I think we can bring up the ruff issues in discord

@tyeth
Copy link
Contributor

tyeth commented Mar 6, 2025

I haven't seen much traction in discord for some things, probably as there are too many notifications in the circuitpython-dev channel to make it a pleasant experience catching up on all discussions (and my questions being poorly framed or with no definitive answer - plus I should maybe tag people even if occasionally unwanted).

I'm happy to merge this, but I think I want a real issue raised that ruff is not working as expected possibly meaning errors are being introduced without automatic detection.
It would be helpful to have the CI show the changed files as far as pre-commit is concerned, as I can't tell which files it's run ruff against. This makes me think the issue should be raised in https://github.com/adafruit/workflows-circuitpython-libs (?) maybe with this series of commits and CI runs as the trail to follow.
Unfortunately if we merge this then we won't be able to see updated output from potential fixes in https://github.com/adafruit/workflows-circuitpython-libs although surely similar testing is possible.

Also please forgive my unfamiliarity of the circuitpython workings and processes, I normally live in arduino land and keep half an ear on circuitpython only occasionally do a bit of fixing/testing/maintenance (along with some weekend projects).

@tyeth tyeth mentioned this pull request Mar 6, 2025
@justmobilize
Copy link
Collaborator Author

@tyeth I'll dig into the ruff stuff and make sure it comes up next week during in-the-weeds

@tyeth
Copy link
Contributor

tyeth commented Mar 6, 2025

Yeah that's probably best place for it.
I might just poke the rust pre-commit version and re-enable the error to see if it will pick it up...

Plus reorder the ruff format to after fix, to comply with the newest recommendation in 0.9.9 (thanks @Neradoc )
https://github.com/astral-sh/ruff-pre-commit/blob/2c8dce6094fa2b4b668e74f694ca63ceffd38614/README.md?plain=1#L63-L65

@tyeth tyeth self-requested a review March 6, 2025 20:42
@tyeth
Copy link
Contributor

tyeth commented Mar 6, 2025

Okay, @justmobilize this is now the real ruff error messages (some rules were ignored without preview mode being on, and we still must be missing basic rules if I had to add F821 Undefined Name).

I've taken out the class too long one, but the rest might be actually this PR (doesn't look like it though), please take a quick look.
Only tackle what's part of your PR, and do a quick format if possible. We'll leave the rest failing for now (or I'll fix)

@justmobilize
Copy link
Collaborator Author

@tyeth I added ignores for the rest, which are valid for now (although could be cleaned up 1 by 1)

@tyeth
Copy link
Contributor

tyeth commented Mar 6, 2025

@tyeth I added ignores for the rest, which are valid for now (although could be cleaned up 1 by 1)

Hero level work, thank you so much!

Making a mental note to go back and remove those after we clear up the default circuitpython library ruff rules

@tyeth tyeth merged commit c413397 into adafruit:main Mar 6, 2025
1 check passed
@justmobilize justmobilize deleted the remove-secrets-usage branch March 6, 2025 22:30
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Mar 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants