SolarManager unter Versionsverwaltung
Erster Stand der Hintergrundprozesse, die auf der Synology unter /volume1/homes/wagner/SolarManager laufen: der Manager selbst, die Sammler je Geraet, die MQTT-Bruecke, der Wecker und - neu hinzugezogen - der AutoAction-Runner, der als Hintergrundprozess hierher gehoert und nicht ins Web-Verzeichnis. Zugangsdaten stehen nicht mehr im Quelltext, sondern in config.ini, die nicht mit eingecheckt wird. Vorlage ist config.ini.example, gelesen wird sie von konfig.py. Betroffen waren solarManager.py (Datenbank und Wattpilot), zeit.py, gatherWaterData.py, wecker.py und skoda_testdaten.py, das sich das Passwort bisher aus dem Quelltext eines anderen Moduls herausgesucht hat. Die Kia-Anbindung ist mit dem Fahrzeug entfallen: kiaTest.py, gatherCarData.py und hyundai_kia_connect_api sind nicht mehr dabei, ebenso gatherInverterData.py, auf das nur noch eine auskommentierte Zeile zeigte. Die mitgelieferten Bibliotheken bleiben im Repository - die NAS hat kein pip, sie muessen neben den Skripten liegen. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
import base64
|
||||
import datetime
|
||||
import functools
|
||||
import hashlib
|
||||
import hmac
|
||||
import os
|
||||
import uuid
|
||||
|
||||
from paho.mqtt.client import Client, CallbackAPIVersion
|
||||
|
||||
|
||||
def get_amazon_auth_headers(access_key, secret_key, region, host, port, headers=None):
|
||||
""" Get the amazon auth headers for working with the amazon websockets
|
||||
protocol
|
||||
|
||||
Requires a lot of extra stuff:
|
||||
|
||||
http://docs.aws.amazon.com/general/latest/gr//sigv4-create-canonical-request.html
|
||||
http://docs.aws.amazon.com/general/latest/gr//signature-v4-examples.html#signature-v4-examples-pythonw
|
||||
http://docs.aws.amazon.com/general/latest/gr//sigv4-signed-request-examples.html#sig-v4-examples-get-auth-header
|
||||
|
||||
Args:
|
||||
access_key (str): Amazon access key (AWS_ACCESS_KEY_ID)
|
||||
secret_key (str): Amazon secret access key (AWS_SECRET_ACCESS_KEY)
|
||||
region (str): aws region
|
||||
host (str): iot endpoint (xxxxxxxxxxxxxx.iot.<region>.amazonaws.com)
|
||||
headers (dict): a dictionary of the original headers- normally websocket headers
|
||||
|
||||
Returns:
|
||||
dict: A string containing the headers that amazon expects in the auth
|
||||
request for the iot websocket service
|
||||
"""
|
||||
|
||||
# pylint: disable=unused-variable,unused-argument
|
||||
|
||||
def sign(key, msg):
|
||||
return hmac.new(key, msg.encode("utf-8"), hashlib.sha256).digest()
|
||||
|
||||
def getSignatureKey(key, dateStamp, regionName, serviceName):
|
||||
kDate = sign(("AWS4" + key).encode("utf-8"), dateStamp)
|
||||
kRegion = sign(kDate, regionName)
|
||||
kService = sign(kRegion, serviceName)
|
||||
kSigning = sign(kService, "aws4_request")
|
||||
return kSigning
|
||||
|
||||
service = "iotdevicegateway"
|
||||
algorithm = "AWS4-HMAC-SHA256"
|
||||
|
||||
t = datetime.datetime.utcnow()
|
||||
amzdate = t.strftime('%Y%m%dT%H%M%SZ')
|
||||
datestamp = t.strftime("%Y%m%d") # Date w/o time, used in credential scope
|
||||
|
||||
if headers is None:
|
||||
headers = {
|
||||
"Host": "{0:s}:443".format(host),
|
||||
"Upgrade": "websocket",
|
||||
"Connection": "Upgrade",
|
||||
"Origin": "https://{0:s}:443".format(host),
|
||||
"Sec-WebSocket-Key": base64.b64encode(uuid.uuid4().bytes),
|
||||
"Sec-Websocket-Version": "13",
|
||||
"Sec-Websocket-Protocol": "mqtt",
|
||||
}
|
||||
|
||||
headers.update({
|
||||
"X-Amz-Date": amzdate,
|
||||
})
|
||||
|
||||
# get into 'canonical' form - lowercase, sorted alphabetically
|
||||
canonical_headers = "\n".join(sorted("{}:{}".format(i.lower(), j).strip() for i, j in headers.items()))
|
||||
# Headers to sign - alphabetical order
|
||||
signed_headers = ";".join(sorted(i.lower().strip() for i in headers.keys()))
|
||||
|
||||
# No payload
|
||||
payload_hash = hashlib.sha256("").hexdigest().lower()
|
||||
|
||||
request_parts = [
|
||||
"GET",
|
||||
"/mqtt",
|
||||
# no query parameters
|
||||
"",
|
||||
canonical_headers + "\n",
|
||||
signed_headers,
|
||||
payload_hash,
|
||||
]
|
||||
|
||||
canonical_request = "\n".join(request_parts)
|
||||
|
||||
# now actually hash request and sign
|
||||
hashed_request = hashlib.sha256(canonical_request).hexdigest()
|
||||
|
||||
credential_scope = "{datestamp:s}/{region:s}/{service:s}/aws4_request".format(**locals())
|
||||
string_to_sign = "{algorithm:s}\n{amzdate:s}\n{credential_scope:s}\n{hashed_request:s}".format(**locals())
|
||||
|
||||
signing_key = getSignatureKey(secret_key, datestamp, region, service)
|
||||
signature = hmac.new(signing_key, (string_to_sign).encode('utf-8'), hashlib.sha256).hexdigest()
|
||||
|
||||
# create auth header
|
||||
authorization_header = "{algorithm:s} Credential={access_key:s}/{credential_scope:s}, SignedHeaders={signed_headers:s}, Signature={signature:s}".format(**locals())
|
||||
|
||||
# get final header string
|
||||
headers["Authorization"] = authorization_header
|
||||
|
||||
return headers
|
||||
|
||||
|
||||
def example_use():
|
||||
access_key = os.environ["AWS_ACCESS_KEY_ID"]
|
||||
secret_key = os.environ["AWS_SECRET_ACCESS_KEY"]
|
||||
port = 8883
|
||||
|
||||
region = "eu-west-1"
|
||||
|
||||
# This is specific to your AWS account
|
||||
host = "abc123def456.iot.{0:s}.amazonaws.com".format(region)
|
||||
|
||||
extra_headers = functools.partial(
|
||||
get_amazon_auth_headers,
|
||||
access_key,
|
||||
secret_key,
|
||||
region,
|
||||
host,
|
||||
port,
|
||||
)
|
||||
|
||||
client = Client(CallbackAPIVersion.VERSION2, transport="websockets")
|
||||
|
||||
client.ws_set_options(headers=extra_headers)
|
||||
|
||||
# Use client as normal from here
|
||||
Reference in New Issue
Block a user