Skip to content

Commit

Permalink
[v7] backport #12057 (panic in CertAuthority.Clone) (#12108)
Browse files Browse the repository at this point in the history
* improve ca cmp (#10351)

* Fix panic in `CertAuthority.Clone` because of non-UTC times. (#12057)

Co-authored-by: Forrest <30576607+fspmarshall@users.noreply.github.com>
  • Loading branch information
espadolini and fspmarshall authored Apr 20, 2022
1 parent a5078bf commit 0342a0c
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 14 deletions.
12 changes: 12 additions & 0 deletions lib/services/authority.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,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 @@ -157,3 +160,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))
}
32 changes: 19 additions & 13 deletions lib/services/local/trust.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,33 +89,39 @@ func (s *CA) UpsertCertAuthority(ca types.CertAuthority) error {
}

// CompareAndSwapCertAuthority updates the cert authority value
// if the existing value matches existing parameter, returns nil if succeeds,
// if the existing value matches expected parameter, returns nil if succeeds,
// trace.CompareFailed otherwise.
func (s *CA) CompareAndSwapCertAuthority(new, existing types.CertAuthority) error {
func (s *CA) CompareAndSwapCertAuthority(new, expected types.CertAuthority) error {
if err := services.ValidateCertAuthority(new); err != nil {
return trace.Wrap(err)
}
newValue, err := services.MarshalCertAuthority(new)

key := backend.Key(authoritiesPrefix, string(new.GetType()), new.GetName())

actualItem, err := s.Get(context.TODO(), key)
if err != nil {
return trace.Wrap(err)
}
newItem := backend.Item{
Key: backend.Key(authoritiesPrefix, string(new.GetType()), new.GetName()),
Value: newValue,
Expires: new.Expiry(),
actual, err := services.UnmarshalCertAuthority(actualItem.Value)
if err != nil {
return trace.Wrap(err)
}

if !services.CertAuthoritiesEquivalent(actual, expected) {
return trace.CompareFailed("cluster %v settings have been updated, try again", new.GetName())
}

existingValue, err := services.MarshalCertAuthority(existing)
newValue, err := services.MarshalCertAuthority(new)
if err != nil {
return trace.Wrap(err)
}
existingItem := backend.Item{
Key: backend.Key(authoritiesPrefix, string(existing.GetType()), existing.GetName()),
Value: existingValue,
Expires: existing.Expiry(),
newItem := backend.Item{
Key: key,
Value: newValue,
Expires: new.Expiry(),
}

_, err = s.CompareAndSwap(context.TODO(), existingItem, newItem)
_, err = s.CompareAndSwap(context.TODO(), *actualItem, newItem)
if err != nil {
if trace.IsCompareFailed(err) {
return trace.CompareFailed("cluster %v settings have been updated, try again", new.GetName())
Expand Down

0 comments on commit 0342a0c

Please sign in to comment.