forked from tidbyt/hdk
-
Notifications
You must be signed in to change notification settings - Fork 8
add compile fail if trying to update secrets #53
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
#!/usr/bin/env python3 | ||
""" | ||
Post-upload script for PlatformIO | ||
|
||
This script runs after successful firmware upload and performs the following actions: | ||
1. Renames secrets.json to secrets.json.injected to indicate successful injection | ||
2. Preserves previous injected files with timestamps | ||
3. Provides clear feedback about the secrets injection status | ||
|
||
Usage: | ||
- Automatically runs after: pio run -e <env> --target upload | ||
- Helps track which secrets have been injected into firmware | ||
- Prevents accidental reuse of the same secrets file | ||
|
||
To reuse secrets: cp secrets.json.injected secrets.json | ||
""" | ||
|
||
import os | ||
import json | ||
import shutil | ||
from datetime import datetime | ||
|
||
Import("env") | ||
|
||
|
||
def rename_secrets_after_upload(*args, **kwargs): | ||
""" | ||
Post-upload action to rename secrets.json to secrets.json.injected | ||
This indicates that the secrets have been successfully injected into the firmware. | ||
Adds a syntax error comment to force users to read warnings when reusing. | ||
""" | ||
secrets_file = "secrets.json" | ||
injected_file = "secrets.json.injected" | ||
|
||
if os.path.exists(secrets_file): | ||
try: | ||
# Add timestamp to the injected file for tracking | ||
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") | ||
timestamped_file = f"secrets.json.injected.{timestamp}" | ||
|
||
# If an injected file already exists, rename it with timestamp | ||
if os.path.exists(injected_file): | ||
print(f"Previous {injected_file} found, renaming to {timestamped_file}") | ||
shutil.move(injected_file, timestamped_file) | ||
|
||
# Read the original secrets file | ||
with open(secrets_file, 'r') as f: | ||
original_content = f.read() | ||
|
||
# Create content with intentional JSON syntax error | ||
# Add a trailing comma and comment that breaks JSON parsing | ||
syntax_error_content = f'''{{ | ||
"WARNING_REMOVE_THIS_LINE_TO_USE": "If you modified secrets, ERASE DEVICE FIRST: pio run -e <env> -t erase", | ||
// INTENTIONAL SYNTAX ERROR: Remove this comment line and the line above to use | ||
{original_content[1:-1]}, | ||
"INJECTED_TIMESTAMP": "{timestamp}", | ||
}}''' | ||
|
||
# Write the content with syntax error to injected file | ||
with open(injected_file, 'w') as f: | ||
f.write(syntax_error_content) | ||
|
||
# Remove the original file | ||
os.remove(secrets_file) | ||
|
||
print(f"✓ Successfully created {injected_file} with deployment tracking") | ||
print(f" This indicates secrets have been injected into the firmware.") | ||
print(f" To reuse: copy {injected_file} to {secrets_file} and remove the warning line") | ||
|
||
except Exception as e: | ||
print(f"Warning: Failed to process {secrets_file}: {e}") | ||
# Fallback to simple rename if JSON processing fails | ||
try: | ||
shutil.move(secrets_file, injected_file) | ||
print(f"✓ Fallback: renamed {secrets_file} to {injected_file}") | ||
except Exception as e2: | ||
print(f"Warning: Fallback rename also failed: {e2}") | ||
else: | ||
print(f"No {secrets_file} file found - nothing to rename") | ||
|
||
|
||
def main(): | ||
""" | ||
Main function that sets up the post-upload action | ||
""" | ||
# Add post-upload action to rename secrets file | ||
env.AddPostAction("upload", rename_secrets_after_upload) | ||
|
||
|
||
# Execute main function | ||
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"WIFI_SSID": "myssiD", | ||
"WIFI_PASSWORD": "PASSWORD", | ||
"REMOTE_URL": "http://192.168.1.10:8000/admin/tronbyt_1/next", | ||
"REMOTE_URL": "http://192.168.1.10:8000/ababababab/next", | ||
"REFRESH_INTERVAL_SECONDS": 10, | ||
"DEFAULT_BRIGHTNESS" : 30 | ||
"DEFAULT_BRIGHTNESS" : 30, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.