Skip to content

[public-api] Use context logger #16686

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

Merged
merged 19 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions components/public-api-server/pkg/apiv1/ide_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
"fmt"

connect "github.com/bufbuild/connect-go"
"github.com/gitpod-io/gitpod/common-go/log"
v1 "github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1"
"github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1/v1connect"
protocol "github.com/gitpod-io/gitpod/gitpod-protocol"
"github.com/gitpod-io/gitpod/public-api-server/pkg/proxy"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus"
)

func NewIDEClientService(pool proxy.ServerConnectionPool) *IDEClientService {
Expand All @@ -31,21 +31,19 @@ type IDEClientService struct {
}

func (this *IDEClientService) SendHeartbeat(ctx context.Context, req *connect.Request[v1.SendHeartbeatRequest]) (*connect.Response[v1.SendHeartbeatResponse], error) {
logger := ctxlogrus.Extract(ctx)

conn, err := getConnection(ctx, this.connectionPool)
if err != nil {
return nil, err
}

workspace, err := conn.GetWorkspace(ctx, req.Msg.GetWorkspaceId())
if err != nil {
logger.WithError(err).Error("Failed to get workspace.")
log.Extract(ctx).WithError(err).Error("Failed to get workspace.")
return nil, proxy.ConvertError(err)
}

if workspace.LatestInstance == nil {
logger.WithError(err).Error("Failed to get latest instance.")
log.Extract(ctx).WithError(err).Error("Failed to get latest instance.")
return nil, connect.NewError(connect.CodeFailedPrecondition, fmt.Errorf("instance not found"))
}

Expand All @@ -61,21 +59,19 @@ func (this *IDEClientService) SendHeartbeat(ctx context.Context, req *connect.Re
}

func (this *IDEClientService) SendDidClose(ctx context.Context, req *connect.Request[v1.SendDidCloseRequest]) (*connect.Response[v1.SendDidCloseResponse], error) {
logger := ctxlogrus.Extract(ctx)

conn, err := getConnection(ctx, this.connectionPool)
if err != nil {
return nil, err
}

workspace, err := conn.GetWorkspace(ctx, req.Msg.GetWorkspaceId())
if err != nil {
logger.WithError(err).Error("Failed to get workspace.")
log.Extract(ctx).WithError(err).Error("Failed to get workspace.")
return nil, proxy.ConvertError(err)
}

if workspace.LatestInstance == nil {
logger.WithError(err).Error("Failed to get latest instance.")
log.Extract(ctx).WithError(err).Error("Failed to get latest instance.")
return nil, connect.NewError(connect.CodeFailedPrecondition, fmt.Errorf("instance not found"))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var _ v1connect.IdentityProviderServiceHandler = ((*IdentityProviderService)(nil

// GetIDToken implements v1connect.IDPServiceHandler
func (srv *IdentityProviderService) GetIDToken(ctx context.Context, req *connect.Request[v1.GetIDTokenRequest]) (*connect.Response[v1.GetIDTokenResponse], error) {
workspaceID, err := validateWorkspaceID(req.Msg.GetWorkspaceId())
workspaceID, err := validateWorkspaceID(ctx, req.Msg.GetWorkspaceId())
if err != nil {
return nil, err
}
Expand Down
43 changes: 22 additions & 21 deletions components/public-api-server/pkg/apiv1/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/gitpod-io/gitpod/public-api-server/pkg/auth"
"github.com/gitpod-io/gitpod/public-api-server/pkg/proxy"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"gorm.io/gorm"
Expand All @@ -48,7 +49,7 @@ type OIDCService struct {
}

func (s *OIDCService) CreateClientConfig(ctx context.Context, req *connect.Request[v1.CreateClientConfigRequest]) (*connect.Response[v1.CreateClientConfigResponse], error) {
organizationID, err := validateOrganizationID(req.Msg.Config.GetOrganizationId())
organizationID, err := validateOrganizationID(ctx, req.Msg.Config.GetOrganizationId())
if err != nil {
return nil, err
}
Expand All @@ -58,8 +59,6 @@ func (s *OIDCService) CreateClientConfig(ctx context.Context, req *connect.Reque
return nil, connect.NewError(connect.CodeInvalidArgument, err)
}

logger := log.WithField("organization_id", organizationID.String())

conn, err := s.getConnection(ctx)
if err != nil {
return nil, err
Expand All @@ -75,7 +74,7 @@ func (s *OIDCService) CreateClientConfig(ctx context.Context, req *connect.Reque

data, err := db.EncryptJSON(s.cipher, toDbOIDCSpec(oauth2Config, oidcConfig))
if err != nil {
logger.WithError(err).Error("Failed to encrypt oidc client config.")
log.Extract(ctx).WithError(err).Error("Failed to encrypt oidc client config.")
return nil, status.Errorf(codes.Internal, "Failed to store OIDC client config.")
}

Expand All @@ -86,15 +85,17 @@ func (s *OIDCService) CreateClientConfig(ctx context.Context, req *connect.Reque
Data: data,
})
if err != nil {
logger.WithError(err).Error("Failed to store oidc client config in the database.")
log.Extract(ctx).WithError(err).Error("Failed to store oidc client config in the database.")
return nil, status.Errorf(codes.Internal, "Failed to store OIDC client config.")
}

logger = log.WithField("oidc_client_config_id", created.ID.String())
log.AddFields(ctx, logrus.Fields{
"oidcClientConfigId": created.ID.String(),
})

converted, err := dbOIDCClientConfigToAPI(created, s.cipher)
if err != nil {
logger.WithError(err).Error("Failed to convert OIDC Client config to response.")
log.Extract(ctx).WithError(err).Error("Failed to convert OIDC Client config to response.")
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("Failed to convert OIDC Client Config %s for Organization %s to API response", created.ID.String(), organizationID.String()))
}

Expand All @@ -104,18 +105,16 @@ func (s *OIDCService) CreateClientConfig(ctx context.Context, req *connect.Reque
}

func (s *OIDCService) GetClientConfig(ctx context.Context, req *connect.Request[v1.GetClientConfigRequest]) (*connect.Response[v1.GetClientConfigResponse], error) {
organizationID, err := validateOrganizationID(req.Msg.GetOrganizationId())
organizationID, err := validateOrganizationID(ctx, req.Msg.GetOrganizationId())
if err != nil {
return nil, err
}

clientConfigID, err := validateOIDCClientConfigID(req.Msg.GetId())
clientConfigID, err := validateOIDCClientConfigID(ctx, req.Msg.GetId())
if err != nil {
return nil, err
}

logger := log.WithField("oidc_client_config_id", clientConfigID.String()).WithField("organization_id", organizationID.String())

conn, err := s.getConnection(ctx)
if err != nil {
return nil, err
Expand All @@ -132,13 +131,13 @@ func (s *OIDCService) GetClientConfig(ctx context.Context, req *connect.Request[
return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("OIDC Client Config %s for Organization %s does not exist", clientConfigID.String(), organizationID.String()))
}

logger.WithError(err).Error("Failed to delete OIDC Client config.")
log.Extract(ctx).WithError(err).Error("Failed to delete OIDC Client config.")
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("Failed to delete OIDC Client Config %s for Organization %s", clientConfigID.String(), organizationID.String()))
}

converted, err := dbOIDCClientConfigToAPI(record, s.cipher)
if err != nil {
logger.WithError(err).Error("Failed to convert OIDC Client config to response.")
log.Extract(ctx).WithError(err).Error("Failed to convert OIDC Client config to response.")
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("Failed to convert OIDC Client Config %s for Organization %s to API response", clientConfigID.String(), organizationID.String()))
}

Expand All @@ -148,7 +147,7 @@ func (s *OIDCService) GetClientConfig(ctx context.Context, req *connect.Request[
}

func (s *OIDCService) ListClientConfigs(ctx context.Context, req *connect.Request[v1.ListClientConfigsRequest]) (*connect.Response[v1.ListClientConfigsResponse], error) {
organizationID, err := validateOrganizationID(req.Msg.GetOrganizationId())
organizationID, err := validateOrganizationID(ctx, req.Msg.GetOrganizationId())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -194,18 +193,16 @@ func (s *OIDCService) UpdateClientConfig(ctx context.Context, req *connect.Reque
}

func (s *OIDCService) DeleteClientConfig(ctx context.Context, req *connect.Request[v1.DeleteClientConfigRequest]) (*connect.Response[v1.DeleteClientConfigResponse], error) {
organizationID, err := validateOrganizationID(req.Msg.GetOrganizationId())
organizationID, err := validateOrganizationID(ctx, req.Msg.GetOrganizationId())
if err != nil {
return nil, err
}

clientConfigID, err := validateOIDCClientConfigID(req.Msg.GetId())
clientConfigID, err := validateOIDCClientConfigID(ctx, req.Msg.GetId())
if err != nil {
return nil, err
}

logger := log.WithField("oidc_client_config_id", clientConfigID.String()).WithField("organization_id", organizationID.String())

conn, err := s.getConnection(ctx)
if err != nil {
return nil, err
Expand All @@ -222,7 +219,7 @@ func (s *OIDCService) DeleteClientConfig(ctx context.Context, req *connect.Reque
return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("OIDC Client Config %s for Organization %s does not exist", clientConfigID.String(), organizationID.String()))
}

logger.WithError(err).Error("Failed to delete OIDC Client config.")
log.Extract(ctx).WithError(err).Error("Failed to delete OIDC Client config.")
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("Failed to delete OIDC Client Config %s for Organization %s", clientConfigID.String(), organizationID.String()))
}

Expand All @@ -237,7 +234,7 @@ func (s *OIDCService) getConnection(ctx context.Context) (protocol.APIInterface,

conn, err := s.connectionPool.Get(ctx, token)
if err != nil {
log.Log.WithError(err).Error("Failed to get connection to server.")
log.Extract(ctx).WithError(err).Error("Failed to get connection to server.")
return nil, connect.NewError(connect.CodeInternal, errors.New("Failed to establish connection to downstream services. If this issue persists, please contact Gitpod Support."))
}

Expand All @@ -250,6 +247,10 @@ func (s *OIDCService) getUser(ctx context.Context, conn protocol.APIInterface) (
return nil, uuid.Nil, proxy.ConvertError(err)
}

log.AddFields(ctx, logrus.Fields{
"userId": user.ID,
})

if !s.isFeatureEnabled(ctx, conn, user) {
return nil, uuid.Nil, connect.NewError(connect.CodePermissionDenied, errors.New("This feature is currently in beta. If you would like to be part of the beta, please contact us."))
}
Expand All @@ -273,7 +274,7 @@ func (s *OIDCService) isFeatureEnabled(ctx context.Context, conn protocol.APIInt

teams, err := conn.GetTeams(ctx)
if err != nil {
log.WithError(err).Warnf("Failed to retreive Teams for user %s, personal access token feature flag will not evaluate team membership.", user.ID)
log.Extract(ctx).WithError(err).Warnf("Failed to retreive Teams for user %s, personal access token feature flag will not evaluate team membership.", user.ID)
teams = nil
}
for _, team := range teams {
Expand Down
4 changes: 2 additions & 2 deletions components/public-api-server/pkg/apiv1/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (s *ProjectsService) ListProjects(ctx context.Context, req *connect.Request
}

func (s *ProjectsService) DeleteProject(ctx context.Context, req *connect.Request[v1.DeleteProjectRequest]) (*connect.Response[v1.DeleteProjectResponse], error) {
projectID, err := validateProjectID(req.Msg.GetProjectId())
projectID, err := validateProjectID(ctx, req.Msg.GetProjectId())
if err != nil {
return nil, err
}
Expand All @@ -171,7 +171,7 @@ func (s *ProjectsService) getConnection(ctx context.Context) (protocol.APIInterf

conn, err := s.connectionPool.Get(ctx, token)
if err != nil {
log.Log.WithError(err).Error("Failed to get connection to server.")
log.Extract(ctx).WithError(err).Error("Failed to get connection to server.")
return nil, connect.NewError(connect.CodeInternal, errors.New("Failed to establish connection to downstream services. If this issue persists, please contact Gitpod Support."))
}

Expand Down
21 changes: 11 additions & 10 deletions components/public-api-server/pkg/apiv1/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ func (s *TeamService) CreateTeam(ctx context.Context, req *connect.Request[v1.Cr

created, err := conn.CreateTeam(ctx, req.Msg.GetName())
if err != nil {
log.WithError(err).Error("Failed to create team.")
log.Extract(ctx).Error("Failed to create team.")
return nil, proxy.ConvertError(err)
}

team, err := s.toTeamAPIResponse(ctx, conn, created)
if err != nil {
log.WithError(err).Error("Failed to populate team with details.")
log.Extract(ctx).WithError(err).Error("Failed to populate team with details.")
return nil, err
}

Expand All @@ -59,7 +59,7 @@ func (s *TeamService) CreateTeam(ctx context.Context, req *connect.Request[v1.Cr
}

func (s *TeamService) GetTeam(ctx context.Context, req *connect.Request[v1.GetTeamRequest]) (*connect.Response[v1.GetTeamResponse], error) {
teamID, err := validateTeamID(req.Msg.GetTeamId())
teamID, err := validateTeamID(ctx, req.Msg.GetTeamId())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -92,7 +92,7 @@ func (s *TeamService) ListTeams(ctx context.Context, req *connect.Request[v1.Lis

teams, err := conn.GetTeams(ctx)
if err != nil {
log.WithError(err).Error("Failed to list teams from server.")
log.Extract(ctx).WithError(err).Error("Failed to list teams from server.")
return nil, proxy.ConvertError(err)
}

Expand Down Expand Up @@ -124,7 +124,7 @@ func (s *TeamService) ListTeams(ctx context.Context, req *connect.Request[v1.Lis
resultMap := map[string]*v1.Team{}
for res := range resultsChan {
if res.err != nil {
log.WithError(err).Error("Failed to populate team with details.")
log.Extract(ctx).WithError(err).Error("Failed to populate team with details.")
return nil, err
}

Expand All @@ -143,7 +143,7 @@ func (s *TeamService) ListTeams(ctx context.Context, req *connect.Request[v1.Lis
}

func (s *TeamService) DeleteTeam(ctx context.Context, req *connect.Request[v1.DeleteTeamRequest]) (*connect.Response[v1.DeleteTeamResponse], error) {
teamID, err := validateTeamID(req.Msg.GetTeamId())
teamID, err := validateTeamID(ctx, req.Msg.GetTeamId())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -178,7 +178,7 @@ func (s *TeamService) JoinTeam(ctx context.Context, req *connect.Request[v1.Join

response, err := s.toTeamAPIResponse(ctx, conn, team)
if err != nil {
log.WithError(err).Error("Failed to populate team with details.")
log.Extract(ctx).WithError(err).Error("Failed to populate team with details.")
return nil, err
}

Expand All @@ -188,7 +188,7 @@ func (s *TeamService) JoinTeam(ctx context.Context, req *connect.Request[v1.Join
}

func (s *TeamService) ResetTeamInvitation(ctx context.Context, req *connect.Request[v1.ResetTeamInvitationRequest]) (*connect.Response[v1.ResetTeamInvitationResponse], error) {
teamID, err := validateTeamID(req.Msg.GetTeamId())
teamID, err := validateTeamID(ctx, req.Msg.GetTeamId())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -270,15 +270,16 @@ func (s *TeamService) DeleteTeamMember(ctx context.Context, req *connect.Request
}

func (s *TeamService) toTeamAPIResponse(ctx context.Context, conn protocol.APIInterface, team *protocol.Team) (*v1.Team, error) {
logger := log.Extract(ctx).WithField("teamId", team.ID)
members, err := conn.GetTeamMembers(ctx, team.ID)
if err != nil {
log.WithError(err).Error("Failed to get team members.")
logger.WithError(err).Error("Failed to get team members.")
return nil, proxy.ConvertError(err)
}

invite, err := conn.GetGenericInvite(ctx, team.ID)
if err != nil {
log.WithError(err).Error("Failed to get generic invite.")
logger.WithError(err).Error("Failed to get generic invite.")
return nil, proxy.ConvertError(err)
}

Expand Down
Loading