Skip to content

Commit

Permalink
Download certs on FT GS only when missing from disk
Browse files Browse the repository at this point in the history
  • Loading branch information
maddieford committed Aug 31, 2023
1 parent 5a4fae8 commit 564d15f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
30 changes: 27 additions & 3 deletions azurelinuxagent/common/protocol/goal_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,9 @@ def _update(self, force_update):
#
if self._extensions_goal_state.source == GoalStateSource.FastTrack and self._goal_state_properties & GoalStateProperties.Certificates:
self._check_certificates()
self._check_and_download_missing_certs_on_disk()

def _check_certificates(self):
# Re-download certificates in case they have been removed from disk since last download
if self._certs_uri is not None:
self._download_certificates(self._certs_uri)
# Check that certificates needed by extensions are in goal state certs.summary
for extension in self.extensions_goal_state.extensions:
for settings in extension.settings:
Expand All @@ -321,6 +319,32 @@ def _download_certificates(self, certs_uri):
self._history.save_certificates(json.dumps(certs.summary))
return certs

def _check_and_download_missing_certs_on_disk(self):
# Re-download certificates if any have been removed from disk since last download
if self._certs_uri is not None:
certificates = self.certs.summary
certs_missing_from_disk = False

for c in certificates:
cert_path = os.path.join(conf.get_lib_dir(), c['thumbprint'] + '.crt')
if not os.path.isfile(cert_path):
certs_missing_from_disk = True
message = "Certificate required by goal state is not on disk: {0}".format(cert_path)
self.logger.info(message)
add_event(op=WALAEventOperation.GoalState, message=message)
if certs_missing_from_disk:
# Try to re-download certs. Sometimes download may fail if certs_uri is outdated/contains wrong
# container id (for example, when the VM is moved to a new container after resuming from
# hibernation). If download fails we should report and continue with goal state processing, as some
# extensions in the goal state may succeed.
try:
self._download_certificates(self._certs_uri)
except Exception as e:
message = "Unable to download certificates. Goal state processing will continue, some " \
"extensions requiring certificates may fail. Error: {0}".format(ustr(e))
self.logger.warn(message)
add_event(op=WALAEventOperation.GoalState, is_success=False, message=message)

def _restore_wire_server_goal_state(self, incarnation, xml_text, xml_doc, vm_settings_support_stopped_error):
msg = 'The HGAP stopped supporting vmSettings; will fetched the goal state from the WireServer.'
self.logger.info(msg)
Expand Down
4 changes: 2 additions & 2 deletions tests/common/protocol/test_goal_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def http_get_handler(url, *_, **__):
protocol.set_http_handlers(http_get_handler=None)
goal_state.update()
self._assert_directory_contents(
self._find_history_subdirectory("234-987"), ["VmSettings.json", "Certificates.json"])
self._find_history_subdirectory("234-987"), ["VmSettings.json"])

def test_it_should_redact_the_protected_settings_when_saving_to_the_history_directory(self):
with mock_wire_protocol(wire_protocol_data.DATA_FILE_VM_SETTINGS) as protocol:
Expand Down Expand Up @@ -464,7 +464,7 @@ def http_get_handler(url, *_, **__):
goal_state = GoalState(protocol.client)

self.assertEqual(2, protocol.mock_wire_data.call_counts['goalstate'], "There should have been exactly 2 requests for the goal state (original + refresh)")
self.assertEqual(4, http_get_handler.certificate_requests, "There should have been exactly 4 requests for the goal state certificates (2x original + 2x refresh)")
self.assertEqual(2, http_get_handler.certificate_requests, "There should have been exactly 2 requests for the goal state certificates (original + refresh)")

thumbprints = [c.thumbprint for c in goal_state.certs.cert_list.certificates]

Expand Down

0 comments on commit 564d15f

Please sign in to comment.