dex icon indicating copy to clipboard operation
dex copied to clipboard

Key rotation regression

Open Calpicow opened this issue 9 years ago • 7 comments

Running commit 15242a86b83058d0b3409a304320878006e1c3c6 with Postgres. There appears to be an issue rotating keys into the verification_keys column. It has worked once, but it became null this morning.

Not too much in logs, but here they are: 0001-01-01 00:00:00 +0000 UTC looks suspicious

[dex-463404543-42rwx] time="2017-06-21T16:29:35Z" level=info msg="keys rotated, next rotation: 2017-06-21 22:29:35.834860805 +0000 UTC" 
[dex-463404543-p3sl7] time="2017-06-21T16:29:22Z" level=info msg="keys expired, rotating" 
[dex-463404543-p3sl7] time="2017-06-21T16:29:25Z" level=info msg="keys rotated, next rotation: 0001-01-01 00:00:00 +0000 UTC" 
[dex-463404543-m9zwj] time="2017-06-21T04:29:14Z" level=info msg="keys expired, rotating" 
[dex-463404543-m9zwj] time="2017-06-21T04:29:17Z" level=info msg="keys rotated, next rotation: 2017-06-21 10:29:17.110805512 +0000 UTC" 
[dex-463404543-m9zwj] time="2017-06-21T10:29:17Z" level=info msg="keys expired, rotating" 
[dex-463404543-m9zwj] time="2017-06-21T10:29:21Z" level=info msg="keys rotated, next rotation: 2017-06-21 16:29:21.706952356 +0000 UTC" 
[dex-463404543-m9zwj] time="2017-06-21T16:29:21Z" level=info msg="keys expired, rotating" 
[dex-463404543-m9zwj] time="2017-06-21T16:29:25Z" level=info msg="keys rotated, next rotation: 2017-06-21 22:29:25.01521144 +0000 UTC"```

Calpicow avatar Jun 21 '17 18:06 Calpicow

Looks like a regression caused by https://github.com/coreos/dex/commit/dd1e901dd99b73dfc98553595ce63567f2777870. Not returning error causes the rest of the code path in https://github.com/coreos/dex/blob/master/storage/sql/crud.go#L394 (and other storage drivers) to execute, essentially wiping all the key data since it returns an empty storage.Keys{}.

Calpicow avatar Jun 21 '17 21:06 Calpicow

Encountered this on a newly booted Tectonic cluster. Restarting both pods fixed the issue. It's almost like the initial generation timed out or failed.

robszumski avatar Jul 06 '17 20:07 robszumski

Logs observed by @robszumski :

in the dex pods:

time="2017-07-06T01:58:19Z" level=error msg="failed to create ID token: no key to sign payload with"

Also observed wrong dates for next rotation:

ime="2017-07-06T01:55:56Z" level=info msg="keys rotated, next rotation: 0001-01-01 00:00:00 +0000 UTC"
time="2017-07-06T01:56:26Z" level=info msg="keys expired, rotating"
time="2017-07-06T01:56:29Z" level=info msg="keys rotated, next rotation: 0001-01-01 00:00:00 +0000 UTC"
time="2017-07-06T01:56:59Z" level=info msg="keys expired, rotating"
time="2017-07-06T01:57:02Z" level=info msg="keys rotated, next rotation: 0001-01-01 00:00:00 +0000 UTC"
time="2017-07-06T01:57:32Z" level=info msg="keys expired, rotating"

rithujohn191 avatar Jul 06 '17 20:07 rithujohn191

Could this be the reason for https://github.com/coreos/tectonic-forum/issues/161?

miracle2k avatar Jul 07 '17 08:07 miracle2k

@robszumski: It's a matter of time until it happens again. The more dex replicas you run, the more likely it is (but should never occur with 1 replica).

@miracle2k: Yes, when the kid of your token no longer exists in dex, kubectl is not able to refresh the token, even when the token is expired and the refresh token is still valid.

We could either revert commit dd1e901dd99b73dfc98553595ce63567f2777870 for a quick fix, or we could do something like the following:

storage.Keys -> *storage.Keys, return nil instead of empty storage.Keys if key has already been rotated. https://github.com/coreos/dex/blob/master/server/rotation.go#L125-L132

err = k.Storage.UpdateKeys(func(keys storage.Keys) (*storage.Keys, error) {
    tNow := k.now()

    // if you are running multiple instances of dex, another instance
    // could have already rotated the keys.
    if tNow.Before(keys.NextRotation) {
        return nil, nil
    }

All storage drivers will need to be changed to accommodate for this as well. Ex: https://github.com/coreos/dex/blob/master/storage/sql/crud.go#L392-L395

nk, err := updater(old)
if nk == nil {
    return nil
}
if err != nil {
    return err
}

Calpicow avatar Jul 07 '17 19:07 Calpicow

I still get this with a fresh install of tectonic v1.6.7 on AWS

fbsb avatar Jul 29 '17 16:07 fbsb

@fabsb the issue has been fixed on Dex v2.5.0 which will be available by default on tectonic v1.7.1. For all other versions you will have to manually upgrade to the latest Dex version until we come up with a migration story.

rithujohn191 avatar Jul 31 '17 18:07 rithujohn191