-
Notifications
You must be signed in to change notification settings - Fork 1
/
lambda_function.py
executable file
·206 lines (176 loc) · 6.5 KB
/
lambda_function.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env python3
# Copyright (c) Andrew Jorgensen. All rights reserved.
# SPDX-License-Identifier: MIT
"""Sends notifications of Chrome OS updates
Uses the same update service API that a Chromebook uses to check what the
latest version is for a particular update channel / track. Reads configuration
from the environment because it's intended to be used in AWS Lambda, at least
in this form here.
TABLE_NAME should contain the name of a DynamoDB table to store known version
information for your Chromebooks.
TOPIC_ARN should contain the ARN of an SNS topic to send notifications to (you
can subscribe to SMS notifications).
CHROMEBOOKS_JSON should contain a JSON document describing the Chromebooks you
want queried.
The format of CHROMEBOOKS_JSON is like this, so that you can query many:
[
{
"appid": "{92A7272A-834A-47A3-9112-E8FD55831660}",
"track": "stable-channel",
"board": "kevin-signed-mpkeys",
"hardware_class": "KEVIN D25-A3E-B2A-O8Y"
}
]
"""
from os import environ
# requests is neat and trendy, but it's not in the standard library, so...
from urllib.request import urlopen
from xml.etree import ElementTree
from datetime import date
import json
import re
import boto3
# If the protocol changes, keeping these as constants may make updates easier
AUSERVER = environ.get("AUSERVER") or "https://tools.google.com/service/update2"
REQUEST = """<?xml version="1.0" encoding="UTF-8"?>
<request protocol="3.0" ismachine="1">
<app appid="{appid}" track="{track}" board="{board}" hardware_class="{hardware_class}" delta_okay="false">
<updatecheck/>
</app>
</request>"""
VERSION_ATTRIB = "ChromeVersion"
VERSION_XPATH = f".//action[@{VERSION_ATTRIB}]"
EOL_ATTRIB = "_eol_date"
EOL_XPATH = f".//updatecheck[@{EOL_ATTRIB}]"
SECONDS_IN_DAY = 24 * 60 * 60
# Default this to something valid for testing
CHROMEBOOKS_JSON = (
environ.get("CHROMEBOOKS_JSON")
or """[
{
"appid": "{92A7272A-834A-47A3-9112-E8FD55831660}",
"track": "stable-channel",
"board": "kevin-signed-mpkeys",
"hardware_class": "KEVIN D25-A3E-B2A-O8Y"
},
{
"appid": "{C924E0C4-AF80-4B6B-A6F0-DD75EDBCC37C}",
"track": "stable-channel",
"board": "reven-signed-mp-v2keys",
"hardware_class": "REVEN-ANAE A6A-A7I"
}
]"""
)
CHROMEBOOKS = json.loads(CHROMEBOOKS_JSON)
if __name__ != "__main__":
# Only connect to AWS when used as a module
TABLE_NAME = environ.get("TABLE_NAME") or "cros-updates"
TABLE = boto3.resource("dynamodb").Table(TABLE_NAME)
TOPIC_ARN = (
# Obviously this default value is in a specific account
# There's a way to get the ARN from the name, but that's tedious
environ.get("TOPIC_ARN")
or "arn:aws:sns:us-west-2:246745595609:cros-updates"
)
TOPIC = boto3.resource("sns").Topic(TOPIC_ARN)
# Get chromebook names from the recovery table
RECOVERY = []
for url in (
"https://dl.google.com/dl/edgedl/chromeos/recovery/recovery2.json",
"https://dl.google.com/dl/edgedl/chromeos/recovery/cloudready_recovery2.json",
):
with urlopen(url) as RECOVERY2_JSON:
RECOVERY.extend(json.load(RECOVERY2_JSON))
for CHROMEBOOK in CHROMEBOOKS:
for RECORD in RECOVERY:
if re.fullmatch(RECORD["hwidmatch"], CHROMEBOOK["hardware_class"]):
CHROMEBOOK["name"] = RECORD["name"]
break
def request_update(appid, track, board, hardware_class):
request = REQUEST.format(
appid=appid, track=track, board=board, hardware_class=hardware_class
)
print(json.dumps({"Request": request}))
with urlopen(AUSERVER, data=request.encode()) as response:
data = response.read()
print(json.dumps({"Response": data.decode(), "Status": response.status}))
return UpdateResponse(data)
class UpdateResponse:
"""Representation of a chromeOS update response"""
def __init__(self, data):
self._root = ElementTree.fromstring(data)
@property
def version(self):
"""Get the Chrome version for a chromeOS device"""
for element in self._root.findall(VERSION_XPATH):
return element.attrib[VERSION_ATTRIB]
@property
def eol(self):
"""Get the EOL date for a chromeOS device"""
try:
element = self._root.find(EOL_XPATH)
_eol_date = element.attrib[EOL_ATTRIB]
except AttributeError:
return None
return date.fromtimestamp(int(_eol_date) * SECONDS_IN_DAY)
def lambda_handler(event, context):
"""AWS Lambda Handler"""
for chromebook in CHROMEBOOKS:
item = TABLE.get_item(
Key={
k: v for k, v in chromebook.items() if k in ("appid", "hardware_class")
}
)
print(json.dumps(item.get("Item"), sort_keys=True))
item = item.get("Item", chromebook)
response = request_update(
**(
{
k: chromebook[k]
for k in ("appid", "track", "board", "hardware_class")
}
)
)
name = chromebook.get(
"name", chromebook["hardware_class"].split()[0].lower().capitalize()
)
print(
json.dumps(
{"name": name, "version": response.version, "eol": str(response.eol)}
)
)
old_version = item.get("version")
if response.version != old_version:
message = f"{name} updated from {old_version} to {response.version}"
if response.eol:
eol = response.eol.strftime("%B %Y")
message += f" and supported until {eol}"
TOPIC.publish(Message=message)
print(json.dumps({"Message": message}))
item["version"] = response.version
item["name"] = name
print(json.dumps(item, sort_keys=True))
TABLE.put_item(Item=item)
if __name__ == "__main__":
for chromebook in CHROMEBOOKS:
response = request_update(
**(
{
k: chromebook[k]
for k in ("appid", "track", "board", "hardware_class")
}
)
)
name = chromebook.get(
"name", chromebook["hardware_class"].split()[0].lower().capitalize()
)
print(
json.dumps(
{"name": name, "version": response.version, "eol": str(response.eol)}
)
)
message = f"{name} updated to {response.version}"
if response.eol:
eol = response.eol.strftime("%B %Y")
message += f" and supported until {eol}"
print(message)