Skip to content

Commit

Permalink
NEOS-1346: fixes nil pointer error with user uuids (#2516)
Browse files Browse the repository at this point in the history
  • Loading branch information
nickzelei authored Aug 22, 2024
1 parent 29a495c commit 7cf1e8d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
41 changes: 31 additions & 10 deletions backend/services/mgmt/v1alpha1/job-service/runs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"connectrpc.com/connect"
"github.com/jackc/pgx/v5/pgtype"
db_queries "github.com/nucleuscloud/neosync/backend/gen/go/db"
mgmtv1alpha1 "github.com/nucleuscloud/neosync/backend/gen/go/protos/mgmt/v1alpha1"
logger_interceptor "github.com/nucleuscloud/neosync/backend/internal/connect/interceptors/logger"
Expand Down Expand Up @@ -709,18 +710,28 @@ func (s *Service) SetRunContext(
return nil, nucleuserrors.NewUnauthenticated("must provide valid authentication credentials for this endpoint")
}

userUuid, err := s.getUserUuid(ctx)
if err != nil {
return nil, err
var userId *pgtype.UUID
if isWorkerApiKey(ctx) {
uid, err := nucleusdb.ToUuid("00000000-0000-0000-0000-000000000000")
if err != nil {
return nil, err
}
userId = &uid
} else {
userUuid, err := s.getUserUuid(ctx)
if err != nil {
return nil, err
}
userId = userUuid
}

err = s.db.Q.SetRunContext(ctx, s.db.Db, db_queries.SetRunContextParams{
WorkflowID: id.GetJobRunId(),
ExternalID: id.GetExternalId(),
AccountID: *accountUuid,
Value: req.Msg.GetValue(),
CreatedByID: *userUuid,
UpdatedByID: *userUuid,
CreatedByID: *userId,
UpdatedByID: *userId,
})
if err != nil {
return nil, fmt.Errorf("unable to set run context: %w", err)
Expand All @@ -744,18 +755,28 @@ func (s *Service) SetRunContexts(
return nil, nucleuserrors.NewUnauthenticated("must provide valid authentication credentials for this endpoint")
}

userUuid, err := s.getUserUuid(ctx)
if err != nil {
return nil, err
var userId *pgtype.UUID
if isWorkerApiKey(ctx) {
uid, err := nucleusdb.ToUuid("00000000-0000-0000-0000-000000000000")
if err != nil {
return nil, err
}
userId = &uid
} else {
userUuid, err := s.getUserUuid(ctx)
if err != nil {
return nil, err
}
userId = userUuid
}

err = s.db.Q.SetRunContext(ctx, s.db.Db, db_queries.SetRunContextParams{
WorkflowID: id.GetJobRunId(),
ExternalID: id.GetExternalId(),
AccountID: *accountUuid,
Value: req.GetValue(),
CreatedByID: *userUuid,
UpdatedByID: *userUuid,
CreatedByID: *userId,
UpdatedByID: *userId,
})
if err != nil {
return nil, fmt.Errorf("unable to set run context: %w", err)
Expand Down
11 changes: 7 additions & 4 deletions backend/services/mgmt/v1alpha1/user-account-service/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/jackc/pgx/v5/pgtype"
db_queries "github.com/nucleuscloud/neosync/backend/gen/go/db"
mgmtv1alpha1 "github.com/nucleuscloud/neosync/backend/gen/go/protos/mgmt/v1alpha1"
"github.com/nucleuscloud/neosync/backend/internal/apikey"
auth_apikey "github.com/nucleuscloud/neosync/backend/internal/auth/apikey"
authjwt "github.com/nucleuscloud/neosync/backend/internal/auth/jwt"
"github.com/nucleuscloud/neosync/backend/internal/auth/tokenctx"
Expand Down Expand Up @@ -54,10 +55,12 @@ func (s *Service) GetUser(
}

if tokenctxResp.ApiKeyContextData != nil {
// todo: check api key type here
return connect.NewResponse(&mgmtv1alpha1.GetUserResponse{
UserId: nucleusdb.UUIDString(tokenctxResp.ApiKeyContextData.ApiKey.UserID),
}), nil
if tokenctxResp.ApiKeyContextData.ApiKeyType == apikey.AccountApiKey && tokenctxResp.ApiKeyContextData.ApiKey != nil {
return connect.NewResponse(&mgmtv1alpha1.GetUserResponse{
UserId: nucleusdb.UUIDString(tokenctxResp.ApiKeyContextData.ApiKey.UserID),
}), nil
}
return nil, nucleuserrors.NewUnauthenticated(fmt.Sprintf("invalid api key type when calling GetUser: %s", tokenctxResp.ApiKeyContextData.ApiKeyType))
} else if tokenctxResp.JwtContextData != nil {
user, err := s.db.Q.GetUserAssociationByProviderSub(ctx, s.db.Db, tokenctxResp.JwtContextData.AuthUserId)
if err != nil && !nucleusdb.IsNoRows(err) {
Expand Down

0 comments on commit 7cf1e8d

Please sign in to comment.