grpc: remove ptypes references (#3078)

This commit is contained in:
Caleb Doxsey 2022-02-24 08:37:59 -07:00 committed by GitHub
parent 35f697e491
commit 1342523cda
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 83 additions and 177 deletions

View file

@ -4,8 +4,8 @@ import (
"encoding/json"
"time"
"github.com/golang/protobuf/ptypes"
"github.com/google/btree"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/pomerium/pomerium/internal/identity"
"github.com/pomerium/pomerium/pkg/grpc/session"
@ -65,8 +65,8 @@ func (s Session) NextRefresh() time.Time {
var tm time.Time
if s.GetOauthToken().GetExpiresAt() != nil {
expiry, err := ptypes.Timestamp(s.GetOauthToken().GetExpiresAt())
if err == nil && !expiry.IsZero() {
expiry := s.GetOauthToken().GetExpiresAt().AsTime()
if s.GetOauthToken().GetExpiresAt().IsValid() && !expiry.IsZero() {
expiry = expiry.Add(-s.gracePeriod)
if tm.IsZero() || expiry.Before(tm) {
tm = expiry
@ -75,8 +75,8 @@ func (s Session) NextRefresh() time.Time {
}
if s.GetExpiresAt() != nil {
expiry, err := ptypes.Timestamp(s.GetExpiresAt())
if err == nil && !expiry.IsZero() {
expiry := s.GetExpiresAt().AsTime()
if s.GetExpiresAt().IsValid() && !expiry.IsZero() {
if tm.IsZero() || expiry.Before(tm) {
tm = expiry
}
@ -119,14 +119,14 @@ func (s *Session) UnmarshalJSON(data []byte) error {
if exp, ok := raw["exp"]; ok {
var secs int64
if err := json.Unmarshal(exp, &secs); err == nil {
s.Session.IdToken.ExpiresAt, _ = ptypes.TimestampProto(time.Unix(secs, 0))
s.Session.IdToken.ExpiresAt = timestamppb.New(time.Unix(secs, 0))
}
delete(raw, "exp")
}
if iat, ok := raw["iat"]; ok {
var secs int64
if err := json.Unmarshal(iat, &secs); err == nil {
s.Session.IdToken.IssuedAt, _ = ptypes.TimestampProto(time.Unix(secs, 0))
s.Session.IdToken.IssuedAt = timestamppb.New(time.Unix(secs, 0))
}
delete(raw, "iat")
}

View file

@ -6,9 +6,9 @@ import (
"testing"
"time"
"github.com/golang/protobuf/ptypes"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/types/known/structpb"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/pomerium/pomerium/pkg/grpc/session"
"github.com/pomerium/pomerium/pkg/protoutil"
@ -41,21 +41,18 @@ func TestSession_NextRefresh(t *testing.T) {
assert.Equal(t, tm1.Add(time.Minute), s.NextRefresh())
tm2 := time.Date(2020, 6, 5, 13, 0, 0, 0, time.UTC)
pbtm2, _ := ptypes.TimestampProto(tm2)
s.OauthToken = &session.OAuthToken{
ExpiresAt: pbtm2,
ExpiresAt: timestamppb.New(tm2),
}
assert.Equal(t, tm2.Add(-time.Second*10), s.NextRefresh())
tm3 := time.Date(2020, 6, 5, 12, 15, 0, 0, time.UTC)
pbtm3, _ := ptypes.TimestampProto(tm3)
s.ExpiresAt = pbtm3
s.ExpiresAt = timestamppb.New(tm3)
assert.Equal(t, tm3, s.NextRefresh())
}
func TestSession_UnmarshalJSON(t *testing.T) {
tm := time.Date(2020, 6, 5, 12, 0, 0, 0, time.UTC)
pbtm, _ := ptypes.TimestampProto(tm)
var s Session
err := json.Unmarshal([]byte(`{
"iss": "https://some.issuer.com",
@ -69,8 +66,8 @@ func TestSession_UnmarshalJSON(t *testing.T) {
assert.NotNil(t, s.Session.IdToken)
assert.Equal(t, "https://some.issuer.com", s.Session.IdToken.Issuer)
assert.Equal(t, "subject", s.Session.IdToken.Subject)
assert.Equal(t, pbtm, s.Session.IdToken.ExpiresAt)
assert.Equal(t, pbtm, s.Session.IdToken.IssuedAt)
assert.Equal(t, timestamppb.New(tm), s.Session.IdToken.ExpiresAt)
assert.Equal(t, timestamppb.New(tm), s.Session.IdToken.IssuedAt)
assert.Equal(t, map[string]*structpb.ListValue{
"some-other-claim": {Values: []*structpb.Value{protoutil.ToStruct("xyz")}},
}, s.Claims)

View file

@ -490,7 +490,7 @@ func (mgr *Manager) refreshUser(ctx context.Context, userID string) {
continue
}
record, err := user.Put(ctx, mgr.cfg.Load().dataBrokerClient, u.User)
res, err := databroker.Put(ctx, mgr.cfg.Load().dataBrokerClient, u.User)
if err != nil {
log.Error(ctx).Err(err).
Str("user_id", s.GetUserId()).
@ -499,7 +499,7 @@ func (mgr *Manager) refreshUser(ctx context.Context, userID string) {
continue
}
mgr.onUpdateUser(ctx, record, u.User)
mgr.onUpdateUser(ctx, res.GetRecord(), u.User)
}
}