Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix panic in CertAuthority.Clone because of non-UTC times. #12057

Merged
merged 10 commits into from
Apr 20, 2022
12 changes: 12 additions & 0 deletions lib/services/authority.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,18 @@ func UnmarshalCertAuthority(bytes []byte, opts ...MarshalOption) (types.CertAuth
if cfg.ID != 0 {
ca.SetResourceID(cfg.ID)
}
// Correct problems with existing CAs that contain non-UTC times, which
// causes panics when doing a gogoproto Clone; should only ever be
// possible with LastRotated, but we enforce it on all the times anyway.
// See https://github.com/gogo/protobuf/issues/519 .
if ca.Spec.Rotation != nil {
apiutils.UTC(&ca.Spec.Rotation.Started)
apiutils.UTC(&ca.Spec.Rotation.LastRotated)
apiutils.UTC(&ca.Spec.Rotation.Schedule.UpdateClients)
apiutils.UTC(&ca.Spec.Rotation.Schedule.UpdateServers)
apiutils.UTC(&ca.Spec.Rotation.Schedule.Standby)
}

return &ca, nil
}

Expand Down
49 changes: 48 additions & 1 deletion lib/services/authority_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package services
package services_test

import (
"crypto/x509/pkix"
Expand All @@ -24,7 +24,10 @@ import (
"github.com/stretchr/testify/require"

"github.com/gravitational/teleport/api/types"
"github.com/gravitational/teleport/lib/auth/testauthority"
. "github.com/gravitational/teleport/lib/services"
"github.com/gravitational/teleport/lib/tlsca"
"github.com/gravitational/teleport/lib/utils"
)

func TestCertPoolFromCertAuthorities(t *testing.T) {
Expand Down Expand Up @@ -161,3 +164,47 @@ func TestCertAuthorityEquivalence(t *testing.T) {
ca1modID.SetResourceID(ca1.GetResourceID() + 1)
require.True(t, CertAuthoritiesEquivalent(ca1, ca1modID))
}

func TestCertAuthorityUTCUnmarshal(t *testing.T) {
t.Parallel()
ta := testauthority.New()
t.Cleanup(ta.Close)

_, pub, err := ta.GenerateKeyPair("")
require.NoError(t, err)
_, cert, err := tlsca.GenerateSelfSignedCA(pkix.Name{CommonName: "clustername"}, nil, time.Hour)
require.NoError(t, err)

caLocal, err := types.NewCertAuthority(types.CertAuthoritySpecV2{
Type: types.HostCA,
ClusterName: "clustername",
ActiveKeys: types.CAKeySet{
SSH: []*types.SSHKeyPair{{PublicKey: pub}},
TLS: []*types.TLSKeyPair{{Cert: cert}},
},
Rotation: &types.Rotation{
LastRotated: time.Now().In(time.FixedZone("not UTC", 2*60*60)),
},
})
require.NoError(t, err)
// needed for CertAuthoritiesEquivalent, as this will get called by
// UnmarshalCertAuthority
require.NoError(t, SyncCertAuthorityKeys(caLocal))

_, offset := caLocal.GetRotation().LastRotated.Zone()
require.NotZero(t, offset)

item, err := utils.FastMarshal(caLocal)
require.NoError(t, err)
require.Contains(t, string(item), "+02:00\"")
caUTC, err := UnmarshalCertAuthority(item)
require.NoError(t, err)

_, offset = caUTC.GetRotation().LastRotated.Zone()
require.Zero(t, offset)

// see https://github.com/gogo/protobuf/issues/519
require.NotPanics(t, func() { caUTC.Clone() })

require.True(t, CertAuthoritiesEquivalent(caLocal, caUTC))
}