Skip to content

Commit c1ee5b2

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "Fix cleaning up console tokens"
2 parents 9ba72ed + 57112a7 commit c1ee5b2

File tree

8 files changed

+72
-8
lines changed

8 files changed

+72
-8
lines changed

nova/compute/manager.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8835,15 +8835,14 @@ def unquiesce_instance(self, context, instance, mapping=None):
88358835

88368836
@periodic_task.periodic_task(spacing=CONF.instance_delete_interval)
88378837
def _cleanup_expired_console_auth_tokens(self, context):
8838-
"""Remove expired console auth tokens for this host.
8838+
"""Remove all expired console auth tokens.
88398839
88408840
Console authorization tokens and their connection data are stored
88418841
in the database when a user asks for a console connection to an
88428842
instance. After a time they expire. We periodically remove any expired
88438843
tokens from the database.
88448844
"""
8845-
objects.ConsoleAuthToken.clean_expired_console_auths_for_host(
8846-
context, self.host)
8845+
objects.ConsoleAuthToken.clean_expired_console_auths(context)
88478846

88488847
def _claim_pci_for_instance_vifs(self, ctxt, instance):
88498848
"""Claim PCI devices for the instance's VIFs on the compute node

nova/db/api.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1864,6 +1864,15 @@ def console_auth_token_destroy_all_by_instance(context, instance_uuid):
18641864
instance_uuid)
18651865

18661866

1867+
def console_auth_token_destroy_expired(context):
1868+
"""Delete expired console authorizations.
1869+
1870+
The console authorizations expire at the time specified by their
1871+
'expires' column. This function is used to garbage collect expired tokens.
1872+
"""
1873+
return IMPL.console_auth_token_destroy_expired(context)
1874+
1875+
18671876
def console_auth_token_destroy_expired_by_host(context, host):
18681877
"""Delete expired console authorizations belonging to the host.
18691878

nova/db/sqlalchemy/api.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5833,6 +5833,13 @@ def console_auth_token_destroy_all_by_instance(context, instance_uuid):
58335833
filter_by(instance_uuid=instance_uuid).delete()
58345834

58355835

5836+
@pick_context_manager_writer
5837+
def console_auth_token_destroy_expired(context):
5838+
context.session.query(models.ConsoleAuthToken).\
5839+
filter(models.ConsoleAuthToken.expires <= timeutils.utcnow_ts()).\
5840+
delete()
5841+
5842+
58365843
@pick_context_manager_writer
58375844
def console_auth_token_destroy_expired_by_host(context, host):
58385845
context.session.query(models.ConsoleAuthToken).\

nova/objects/console_auth_token.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@
3434
@base.NovaObjectRegistry.register
3535
class ConsoleAuthToken(base.NovaTimestampObject, base.NovaObject):
3636
# Version 1.0: Initial version
37-
VERSION = '1.0'
37+
# Version 1.1: Add clean_expired_console_auths method.
38+
# The clean_expired_console_auths_for_host method
39+
# was deprecated.
40+
VERSION = '1.1'
3841

3942
fields = {
4043
'id': fields.IntegerField(),
@@ -176,6 +179,19 @@ def clean_console_auths_for_instance(cls, context, instance_uuid):
176179
"""
177180
db.console_auth_token_destroy_all_by_instance(context, instance_uuid)
178181

182+
@base.remotable_classmethod
183+
def clean_expired_console_auths(cls, context):
184+
"""Remove all expired console authorizations.
185+
186+
:param context: the context
187+
188+
All expired authorizations will be removed.
189+
Tokens that have not expired will remain.
190+
"""
191+
db.console_auth_token_destroy_expired(context)
192+
193+
# TODO(takashin): This method was deprecated and will be removed
194+
# in a next major version bump.
179195
@base.remotable_classmethod
180196
def clean_expired_console_auths_for_host(cls, context, host):
181197
"""Remove all expired console authorizations for the host.

nova/tests/unit/compute/test_compute_mgr.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4728,11 +4728,10 @@ def test_clean_instance_console_tokens_no_consoles_enabled(self,
47284728
self.compute._clean_instance_console_tokens(self.context, instance)
47294729
mock_clean.assert_not_called()
47304730

4731-
@mock.patch('nova.objects.ConsoleAuthToken.'
4732-
'clean_expired_console_auths_for_host')
4731+
@mock.patch('nova.objects.ConsoleAuthToken.clean_expired_console_auths')
47334732
def test_cleanup_expired_console_auth_tokens(self, mock_clean):
47344733
self.compute._cleanup_expired_console_auth_tokens(self.context)
4735-
mock_clean.assert_called_once_with(self.context, self.compute.host)
4734+
mock_clean.assert_called_once_with(self.context)
47364735

47374736
@mock.patch.object(nova.context.RequestContext, 'elevated')
47384737
@mock.patch.object(nova.objects.InstanceList, 'get_by_host')

nova/tests/unit/db/test_db_api.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9818,6 +9818,35 @@ def test_console_auth_token_get_valid_by_uuid(self):
98189818
self.assertEqual(hash1, db_obj1['token_hash'])
98199819
self.assertIsNone(db_obj2, "the token uuid should not match")
98209820

9821+
def test_console_auth_token_destroy_expired(self):
9822+
uuid1 = uuidsentinel.uuid1
9823+
uuid2 = uuidsentinel.uuid2
9824+
uuid3 = uuidsentinel.uuid3
9825+
hash1 = utils.get_sha256_str(uuidsentinel.token1)
9826+
hash2 = utils.get_sha256_str(uuidsentinel.token2)
9827+
hash3 = utils.get_sha256_str(uuidsentinel.token3)
9828+
self.addCleanup(timeutils.clear_time_override)
9829+
timeutils.set_time_override(timeutils.utcnow())
9830+
self._create_instances([uuid1, uuid2, uuid3])
9831+
9832+
self._create(hash1, uuid1, 10)
9833+
self._create(hash2, uuid2, 10, host='other-host')
9834+
timeutils.advance_time_seconds(100)
9835+
self._create(hash3, uuid3, 10)
9836+
9837+
db.console_auth_token_destroy_expired(self.context)
9838+
9839+
# the api only supports getting unexpired tokens
9840+
# but by rolling back time we can see if a token that
9841+
# should be deleted is still there
9842+
timeutils.advance_time_seconds(-100)
9843+
db_obj1 = db.console_auth_token_get_valid(self.context, hash1, uuid1)
9844+
db_obj2 = db.console_auth_token_get_valid(self.context, hash2, uuid2)
9845+
db_obj3 = db.console_auth_token_get_valid(self.context, hash3, uuid3)
9846+
self.assertIsNone(db_obj1, "the token should have been deleted")
9847+
self.assertIsNone(db_obj2, "the token should have been deleted")
9848+
self.assertIsNotNone(db_obj3, "a valid token should be found here")
9849+
98219850
def test_console_auth_token_destroy_expired_by_host(self):
98229851
uuid1 = uuidsentinel.uuid1
98239852
uuid2 = uuidsentinel.uuid2

nova/tests/unit/objects/test_console_auth_token.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@ def test_clean_console_auths_for_instance(self, mock_destroy):
148148
mock_destroy.assert_called_once_with(
149149
self.context, uuidsentinel.instance)
150150

151+
@mock.patch('nova.db.api.console_auth_token_destroy_expired')
152+
def test_clean_expired_console_auths(self, mock_destroy):
153+
token_obj.ConsoleAuthToken.clean_expired_console_auths(self.context)
154+
mock_destroy.assert_called_once_with(self.context)
155+
151156
@mock.patch('nova.db.api.console_auth_token_destroy_expired_by_host')
152157
def test_clean_expired_console_auths_for_host(self, mock_destroy):
153158
token_obj.ConsoleAuthToken.clean_expired_console_auths_for_host(

nova/tests/unit/objects/test_objects.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1045,7 +1045,7 @@ def obj_name(cls):
10451045
'CellMappingList': '1.1-496ef79bb2ab41041fff8bcb57996352',
10461046
'ComputeNode': '1.19-af6bd29a6c3b225da436a0d8487096f2',
10471047
'ComputeNodeList': '1.17-52f3b0962b1c86b98590144463ebb192',
1048-
'ConsoleAuthToken': '1.0-a61bf7b54517c4013a12289c5a5268ea',
1048+
'ConsoleAuthToken': '1.1-8da320fb065080eb4d3c2e5c59f8bf52',
10491049
'CpuDiagnostics': '1.0-d256f2e442d1b837735fd17dfe8e3d47',
10501050
'DNSDomain': '1.0-7b0b2dab778454b6a7b6c66afe163a1a',
10511051
'DNSDomainList': '1.0-4ee0d9efdfd681fed822da88376e04d2',

0 commit comments

Comments
 (0)