diff options
| author | Mark Goddard <mark@stackhpc.com> | 2019-05-16 17:26:45 +0100 |
|---|---|---|
| committer | Mark Goddard <mark@stackhpc.com> | 2019-05-17 14:05:48 +0100 |
| commit | 6c1442c385450004dd253f3f464fe4336194be99 (patch) | |
| tree | c2e1b0bc97f49fdab18051b2c26b3e05001fb239 /tests | |
| parent | 25ac955a4e2645da29f8c7b807f0bac5afb43838 (diff) | |
| download | kolla-ansible-6c1442c385450004dd253f3f464fe4336194be99.tar.gz kolla-ansible-6c1442c385450004dd253f3f464fe4336194be99.zip | |
Fix keystone fernet key rotation scheduling
Right now every controller rotates fernet keys. This is nice because
should any controller die, we know the remaining ones will rotate the
keys. However, we are currently over-rotating the keys.
When we over rotate keys, we get logs like this:
This is not a recognized Fernet token <token> TokenNotFound
Most clients can recover and get a new token, but some clients (like
Nova passing tokens to other services) can't do that because it doesn't
have the password to regenerate a new token.
With three controllers, in crontab in keystone-fernet we see the once a day
correctly staggered across the three controllers:
ssh ctrl1 sudo cat /etc/kolla/keystone-fernet/crontab
0 0 * * * /usr/bin/fernet-rotate.sh
ssh ctrl2 sudo cat /etc/kolla/keystone-fernet/crontab
0 8 * * * /usr/bin/fernet-rotate.sh
ssh ctrl3 sudo cat /etc/kolla/keystone-fernet/crontab
0 16 * * * /usr/bin/fernet-rotate.sh
Currently with three controllers we have this keystone config:
[token]
expiration = 86400 (although, keystone default is one hour)
allow_expired_window = 172800 (this is the keystone default)
[fernet_tokens]
max_active_keys = 4
Currently, kolla-ansible configures key rotation according to the following:
rotation_interval = token_expiration / num_hosts
This means we rotate keys more quickly the more hosts we have, which doesn't
make much sense.
Keystone docs state:
max_active_keys =
((token_expiration + allow_expired_window) / rotation_interval) + 2
For details see:
https://docs.openstack.org/keystone/stein/admin/fernet-token-faq.html
Rotation is based on pushing out a staging key, so should any server
start using that key, other servers will consider that valid. Then each
server in turn starts using the staging key, each in term demoting the
existing primary key to a secondary key. Eventually you prune the
secondary keys when there is no token in the wild that would need to be
decrypted using that key. So this all makes sense.
This change adds new variables for fernet_token_allow_expired_window and
fernet_key_rotation_interval, so that we can correctly calculate the
correct number of active keys. We now set the default rotation interval
so as to minimise the number of active keys to 3 - one primary, one
secondary, one buffer.
This change also fixes the fernet cron job generator, which was broken
in the following cases:
* requesting an interval of more than 1 day resulted in no jobs
* requesting an interval of more than 60 minutes, unless an exact
multiple of 60 minutes, resulted in no jobs
It should now be possible to request any interval up to a week divided
by the number of hosts.
Change-Id: I10c82dc5f83653beb60ddb86d558c5602153341a
Closes-Bug: #1809469
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/test_fernet_rotate_cron_generator.py | 159 |
1 files changed, 100 insertions, 59 deletions
diff --git a/tests/test_fernet_rotate_cron_generator.py b/tests/test_fernet_rotate_cron_generator.py index 7c60bd2f6..6e01f12d8 100644 --- a/tests/test_fernet_rotate_cron_generator.py +++ b/tests/test_fernet_rotate_cron_generator.py @@ -36,47 +36,85 @@ class FernetRotateCronGeneratorTest(base.BaseTestCase): expected = [] self._test(1, 0, 0, expected) - # total_rotation_mins == WEEK_SPAN: + # Invalid: total_rotation_mins > WEEK_SPAN: - def test_1_week_1_host(self): - expected = [{"min": 0, "hour": 0, "day": 0}] - self._test(0, 1, 7 * 24 * 60, expected) + def test_1_week_1_min_1_host(self): + self.assertRaises(generator.RotationIntervalTooLong, + generator.generate, + 0, 1, 7 * 24 * 60 + 1) - def test_1_week_2_hosts(self): - expected = [{"min": 0, "hour": 0, "day": 0}] - self._test(0, 2, 7 * 24 * 60, expected) - - expected = [{"min": 0, "hour": 12, "day": 3}] - self._test(1, 2, 7 * 24 * 60, expected) + def test_1_week_1_min_2_hosts(self): + self.assertRaises(generator.RotationIntervalTooLong, + generator.generate, + 0, 2, 7 * 24 * 60 + 1) - # total_rotation_mins == DAY_SPAN: - - def test_1_day_1_host(self): - expected = [{"min": 0, "hour": 0, "day": "*"}] - self._test(0, 1, 24 * 60, expected) + # host_rotation_mins > DAY_SPAN: def test_1_day_2_hosts(self): - expected = [{"min": 0, "hour": 0, "day": "*"}] + expected = [{"min": 0, "hour": 0, "day": day} + for day in range(0, 7, 2)] self._test(0, 2, 24 * 60, expected) - expected = [{"min": 0, "hour": 12, "day": "*"}] + expected = [{"min": 0, "hour": 0, "day": day} + for day in range(1, 7, 2)] self._test(1, 2, 24 * 60, expected) - # total_rotation_mins % HOUR_SPAN == 0: + def test_1_day_3_hosts(self): + expected = [{"min": 0, "hour": 0, "day": day} + for day in range(0, 7, 3)] + self._test(0, 3, 24 * 60, expected) - def test_1_hour_1_host(self): - # nit: This could be a single hour: '*'. - expected = [{"min": 0, "hour": hour, "day": "*"} - for hour in range(24)] - self._test(0, 1, 60, expected) + expected = [{"min": 0, "hour": 0, "day": day} + for day in range(1, 7, 3)] + self._test(1, 3, 24 * 60, expected) + + expected = [{"min": 0, "hour": 0, "day": day} + for day in range(2, 7, 3)] + self._test(2, 3, 24 * 60, expected) + + def test_2_days_1_host(self): + expected = [{"min": 0, "hour": 0, "day": day} + for day in range(0, 6, 2)] + self._test(0, 1, 2 * 24 * 60, expected) + + def test_2_days_2_hosts(self): + expected = [{"min": 0, "hour": 0, "day": day} + for day in range(0, 7, 4)] + self._test(0, 2, 2 * 24 * 60, expected) + + expected = [{"min": 0, "hour": 0, "day": 2}] + self._test(1, 2, 2 * 24 * 60, expected) + + def test_3_days_1_host(self): + # NOTE: This is the default config in kolla-ansible for 1 host. + expected = [{"min": 0, "hour": 0, "day": day} + for day in range(0, 6, 3)] + self._test(0, 1, 3 * 24 * 60, expected) + + def test_3_days_3_hosts(self): + # NOTE: This is the default config in kolla-ansible for 3 hosts. + expected = [{"min": 0, "hour": 0, "day": 0}] + self._test(0, 3, 3 * 24 * 60, expected) + + expected = [{"min": 0, "hour": 0, "day": 3}] + self._test(1, 3, 3 * 24 * 60, expected) + + expected = [] + self._test(2, 3, 3 * 24 * 60, expected) + + def test_1_week_1_host(self): + expected = [{"min": 0, "hour": 0, "day": 0}] + self._test(0, 1, 7 * 24 * 60, expected) + + # total_rotation_mins > HOUR_SPAN: def test_1_hour_2_hosts(self): expected = [{"min": 0, "hour": hour, "day": "*"} - for hour in range(24)] + for hour in range(0, 24, 2)] self._test(0, 2, 60, expected) - expected = [{"min": 30, "hour": hour, "day": "*"} - for hour in range(24)] + expected = [{"min": 0, "hour": hour, "day": "*"} + for hour in range(1, 24, 2)] self._test(1, 2, 60, expected) def test_2_hours_1_host(self): @@ -86,42 +124,56 @@ class FernetRotateCronGeneratorTest(base.BaseTestCase): def test_2_hours_2_hosts(self): expected = [{"min": 0, "hour": hour, "day": "*"} - for hour in range(0, 24, 2)] + for hour in range(0, 24, 4)] self._test(0, 2, 2 * 60, expected) expected = [{"min": 0, "hour": hour, "day": "*"} - for hour in range(1, 24, 2)] + for hour in range(2, 24, 4)] self._test(1, 2, 2 * 60, expected) - def test_2_days_1_host(self): - # FIXME: Anything greater than 1 day (except 1 week) returns no jobs. - expected = [] - self._test(0, 1, 2 * 24 * 60, expected) + def test_61_minutes_1_host(self): + # FIXME: Anything greater than 1 hour (unless an integer number of + # hours) returns no jobs. + expected = [{"min": hour, "hour": hour, "day": "*"} + for hour in range(0, 23)] + self._test(0, 1, 61, expected) - def test_2_days_2_hosts(self): - # FIXME: Anything greater than 1 day (except 1 week) returns no jobs. - expected = [] - self._test(0, 2, 2 * 24 * 60, expected) + def test_61_minutes_2_hosts(self): + # FIXME: Anything greater than 1 hour (unless an integer number of + # hours) returns no jobs. + expected = [{"min": hour, "hour": hour, "day": "*"} + for hour in range(0, 24, 2)] + self._test(0, 2, 61, expected) - expected = [] - self._test(1, 2, 2 * 24 * 60, expected) + expected = [{"min": hour, "hour": hour, "day": "*"} + for hour in range(1, 23, 2)] + self._test(1, 2, 61, expected) - # total_rotation_mins % MINUTE_SPAN == 0: + def test_1_day_1_host(self): + expected = [{"min": 0, "hour": 0, "day": "*"}] + self._test(0, 1, 24 * 60, expected) + + def test_12_hours_2_hosts(self): + expected = [{"min": 0, "hour": 0, "day": "*"}] + self._test(0, 2, 12 * 60, expected) + + expected = [{"min": 0, "hour": 12, "day": "*"}] + self._test(1, 2, 12 * 60, expected) + + # else: def test_1_minute_1_host(self): - # This could be a single hour: '*'. expected = [{"min": min, "hour": "*", "day": "*"} for min in range(60)] self._test(0, 1, 1, expected) def test_1_minute_2_hosts(self): - # This is kind of broken, but its an edge case so nevermind. expected = [{"min": min, "hour": "*", "day": "*"} - for min in range(60)] + for min in range(0, 60, 2)] self._test(0, 2, 1, expected) expected = [{"min": min, "hour": "*", "day": "*"} - for min in range(60)] + for min in range(1, 60, 2)] self._test(1, 2, 1, expected) def test_2_minutes_1_host(self): @@ -131,24 +183,13 @@ class FernetRotateCronGeneratorTest(base.BaseTestCase): def test_2_minutes_2_hosts(self): expected = [{"min": min, "hour": "*", "day": "*"} - for min in range(0, 60, 2)] + for min in range(0, 60, 4)] self._test(0, 2, 2, expected) expected = [{"min": min, "hour": "*", "day": "*"} - for min in range(1, 60, 2)] + for min in range(2, 60, 4)] self._test(1, 2, 2, expected) - def test_61_minutes_1_host(self): - # FIXME: Anything greater than 1 hour (unless an integer number of - # hours) returns no jobs. - expected = [] - self._test(0, 1, 61, expected) - - def test_61_minutes_2_hosts(self): - # FIXME: Anything greater than 1 hour (unless an integer number of - # hours) returns no jobs. - expected = [] - self._test(0, 1, 61, expected) - - expected = [] - self._test(1, 2, 61, expected) + def test_1_hour_1_host(self): + expected = [{"min": 0, "hour": "*", "day": "*"}] + self._test(0, 1, 60, expected) |
