From 5229ddb66a4d55f87b05cd51992882d0610da60a Mon Sep 17 00:00:00 2001 From: michalkrzyz Date: Thu, 28 Nov 2024 11:48:02 +0100 Subject: [PATCH] feat(authN) - Add updated_by to inserts #360 (#410) Add assignments in app handlers Add inserts in database querries Adjust tests --- go.mod | 2 +- internal/app/activity/activity_handler.go | 1 + internal/app/component/component_handler.go | 1 + .../app/component_instance/component_instance_handler.go | 1 + internal/app/component_version/component_version_handler.go | 1 + internal/app/evidence/evidence_handler.go | 1 + internal/app/issue/issue_handler.go | 1 + internal/app/issue_match/issue_match_handler.go | 1 + .../app/issue_match_change/issue_match_change_handler.go | 1 + internal/app/issue_repository/issue_repository_handler.go | 1 + internal/app/issue_variant/issue_variant_handler.go | 1 + internal/app/service/service_handler.go | 1 + internal/app/support_group/support_group_handler.go | 1 + internal/app/user/user_handler.go | 1 + internal/database/mariadb/activity.go | 6 ++++-- internal/database/mariadb/component.go | 6 ++++-- internal/database/mariadb/component_instance.go | 6 ++++-- internal/database/mariadb/component_version.go | 6 ++++-- internal/database/mariadb/evidence.go | 6 ++++-- internal/database/mariadb/issue.go | 6 ++++-- internal/database/mariadb/issue_match.go | 6 ++++-- internal/database/mariadb/issue_match_change.go | 6 ++++-- internal/database/mariadb/issue_repository.go | 6 ++++-- internal/database/mariadb/issue_variant.go | 6 ++++-- internal/database/mariadb/service.go | 6 ++++-- internal/database/mariadb/support_group.go | 6 ++++-- internal/database/mariadb/user.go | 6 ++++-- internal/e2e/metadata_test.go | 4 ++-- 28 files changed, 68 insertions(+), 29 deletions(-) diff --git a/go.mod b/go.mod index f99c44a2..65386cf6 100644 --- a/go.mod +++ b/go.mod @@ -22,6 +22,7 @@ require ( github.com/samber/lo v1.47.0 github.com/sirupsen/logrus v1.9.3 github.com/stretchr/testify v1.10.0 + github.com/vektah/gqlparser/v2 v2.5.19 golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 k8s.io/utils v0.0.0-20240502163921-fe8a2dddb1d0 ) @@ -85,7 +86,6 @@ require ( github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.12 // indirect github.com/urfave/cli/v2 v2.27.5 // indirect - github.com/vektah/gqlparser/v2 v2.5.19 // indirect github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect go.opentelemetry.io/otel v1.24.0 // indirect diff --git a/internal/app/activity/activity_handler.go b/internal/app/activity/activity_handler.go index 00b3d0bb..2b10dd52 100644 --- a/internal/app/activity/activity_handler.go +++ b/internal/app/activity/activity_handler.go @@ -143,6 +143,7 @@ func (a *activityHandler) CreateActivity(activity *entity.Activity) (*entity.Act l.Error(err) return nil, NewActivityHandlerError("Internal error while creating activity (GetUserId).") } + activity.UpdatedBy = activity.CreatedBy newActivity, err := a.database.CreateActivity(activity) diff --git a/internal/app/component/component_handler.go b/internal/app/component/component_handler.go index 975635c6..4e917f98 100644 --- a/internal/app/component/component_handler.go +++ b/internal/app/component/component_handler.go @@ -121,6 +121,7 @@ func (cs *componentHandler) CreateComponent(component *entity.Component) (*entit l.Error(err) return nil, NewUserHandlerError("Internal error while creating component (GetUserId).") } + component.UpdatedBy = component.CreatedBy components, err := cs.ListComponents(f, &entity.ListOptions{}) diff --git a/internal/app/component_instance/component_instance_handler.go b/internal/app/component_instance/component_instance_handler.go index 129c361d..1a9a624b 100644 --- a/internal/app/component_instance/component_instance_handler.go +++ b/internal/app/component_instance/component_instance_handler.go @@ -122,6 +122,7 @@ func (ci *componentInstanceHandler) CreateComponentInstance(componentInstance *e l.Error(err) return nil, NewComponentInstanceHandlerError("Internal error while creating componentInstance (GetUserId).") } + componentInstance.UpdatedBy = componentInstance.CreatedBy newComponentInstance, err := ci.database.CreateComponentInstance(componentInstance) diff --git a/internal/app/component_version/component_version_handler.go b/internal/app/component_version/component_version_handler.go index 1ae6b932..109918ed 100644 --- a/internal/app/component_version/component_version_handler.go +++ b/internal/app/component_version/component_version_handler.go @@ -120,6 +120,7 @@ func (cv *componentVersionHandler) CreateComponentVersion(componentVersion *enti l.Error(err) return nil, NewComponentVersionHandlerError("Internal error while creating componentVersion (GetUserId).") } + componentVersion.UpdatedBy = componentVersion.CreatedBy newComponent, err := cv.database.CreateComponentVersion(componentVersion) diff --git a/internal/app/evidence/evidence_handler.go b/internal/app/evidence/evidence_handler.go index 6363d608..95297de4 100644 --- a/internal/app/evidence/evidence_handler.go +++ b/internal/app/evidence/evidence_handler.go @@ -115,6 +115,7 @@ func (e *evidenceHandler) CreateEvidence(evidence *entity.Evidence) (*entity.Evi l.Error(err) return nil, NewEvidenceHandlerError("Internal error while creating evidence (GetUserId).") } + evidence.UpdatedBy = evidence.CreatedBy newEvidence, err := e.database.CreateEvidence(evidence) diff --git a/internal/app/issue/issue_handler.go b/internal/app/issue/issue_handler.go index be19aff1..fac11df9 100644 --- a/internal/app/issue/issue_handler.go +++ b/internal/app/issue/issue_handler.go @@ -176,6 +176,7 @@ func (is *issueHandler) CreateIssue(issue *entity.Issue) (*entity.Issue, error) l.Error(err) return nil, NewIssueHandlerError("Internal error while creating issue (GetUserId).") } + issue.UpdatedBy = issue.CreatedBy issues, err := is.ListIssues(f, &entity.IssueListOptions{}) diff --git a/internal/app/issue_match/issue_match_handler.go b/internal/app/issue_match/issue_match_handler.go index 834a0fb8..6191b451 100644 --- a/internal/app/issue_match/issue_match_handler.go +++ b/internal/app/issue_match/issue_match_handler.go @@ -147,6 +147,7 @@ func (im *issueMatchHandler) CreateIssueMatch(issueMatch *entity.IssueMatch) (*e l.Error(err) return nil, NewIssueMatchHandlerError("Internal error while retrieving effective severity (GetUserId).") } + issueMatch.UpdatedBy = issueMatch.CreatedBy severityFilter := &entity.SeverityFilter{ IssueId: []*int64{&issueMatch.IssueId}, diff --git a/internal/app/issue_match_change/issue_match_change_handler.go b/internal/app/issue_match_change/issue_match_change_handler.go index e2b32ef0..66c27cb9 100644 --- a/internal/app/issue_match_change/issue_match_change_handler.go +++ b/internal/app/issue_match_change/issue_match_change_handler.go @@ -119,6 +119,7 @@ func (imc *issueMatchChangeHandler) CreateIssueMatchChange(issueMatchChange *ent l.Error(err) return nil, NewIssueMatchChangeHandlerError("Internal error while creating issueMatchChange (GetUserId).") } + issueMatchChange.UpdatedBy = issueMatchChange.CreatedBy newIssueMatchChange, err := imc.database.CreateIssueMatchChange(issueMatchChange) diff --git a/internal/app/issue_repository/issue_repository_handler.go b/internal/app/issue_repository/issue_repository_handler.go index f6e4fded..fb1d1d51 100644 --- a/internal/app/issue_repository/issue_repository_handler.go +++ b/internal/app/issue_repository/issue_repository_handler.go @@ -118,6 +118,7 @@ func (ir *issueRepositoryHandler) CreateIssueRepository(issueRepository *entity. l.Error(err) return nil, NewIssueRepositoryHandlerError("Internal error while creating issueRepository (GetUserId).") } + issueRepository.BaseIssueRepository.UpdatedBy = issueRepository.BaseIssueRepository.CreatedBy issueRepositories, err := ir.ListIssueRepositories(f, &entity.ListOptions{}) diff --git a/internal/app/issue_variant/issue_variant_handler.go b/internal/app/issue_variant/issue_variant_handler.go index 7f8bfd1f..d21752f4 100644 --- a/internal/app/issue_variant/issue_variant_handler.go +++ b/internal/app/issue_variant/issue_variant_handler.go @@ -184,6 +184,7 @@ func (iv *issueVariantHandler) CreateIssueVariant(issueVariant *entity.IssueVari l.Error(err) return nil, NewIssueVariantHandlerError("Internal error while creating issueVariant (GetUserId).") } + issueVariant.UpdatedBy = issueVariant.CreatedBy issueVariants, err := iv.ListIssueVariants(f, &entity.ListOptions{}) diff --git a/internal/app/service/service_handler.go b/internal/app/service/service_handler.go index 72a31f72..10c21373 100644 --- a/internal/app/service/service_handler.go +++ b/internal/app/service/service_handler.go @@ -170,6 +170,7 @@ func (s *serviceHandler) CreateService(service *entity.Service) (*entity.Service l.Error(err) return nil, NewServiceHandlerError("Internal error while creating service (GetUserId).") } + service.BaseService.UpdatedBy = service.BaseService.CreatedBy services, err := s.ListServices(f, &entity.ListOptions{}) diff --git a/internal/app/support_group/support_group_handler.go b/internal/app/support_group/support_group_handler.go index 7c3e68c6..f26ca922 100644 --- a/internal/app/support_group/support_group_handler.go +++ b/internal/app/support_group/support_group_handler.go @@ -148,6 +148,7 @@ func (sg *supportGroupHandler) CreateSupportGroup(supportGroup *entity.SupportGr l.Error(err) return nil, NewSupportGroupHandlerError("Internal error while creating supportGroup (GetUserId).") } + supportGroup.UpdatedBy = supportGroup.CreatedBy supportGroups, err := sg.ListSupportGroups(f, &entity.ListOptions{}) diff --git a/internal/app/user/user_handler.go b/internal/app/user/user_handler.go index e8ad6461..51cb82b6 100644 --- a/internal/app/user/user_handler.go +++ b/internal/app/user/user_handler.go @@ -118,6 +118,7 @@ func (u *userHandler) CreateUser(user *entity.User) (*entity.User, error) { l.Error(err) return nil, NewUserHandlerError("Internal error while creating user (GetUserId).") } + user.UpdatedBy = user.CreatedBy users, err := u.ListUsers(f, &entity.ListOptions{}) diff --git a/internal/database/mariadb/activity.go b/internal/database/mariadb/activity.go index 2dd69c64..bec47c8c 100644 --- a/internal/database/mariadb/activity.go +++ b/internal/database/mariadb/activity.go @@ -212,10 +212,12 @@ func (s *SqlDatabase) CreateActivity(activity *entity.Activity) (*entity.Activit query := ` INSERT INTO Activity ( activity_status, - activity_created_by + activity_created_by, + activity_updated_by ) VALUES ( :activity_status, - :activity_created_by + :activity_created_by, + :activity_updated_by ) ` diff --git a/internal/database/mariadb/component.go b/internal/database/mariadb/component.go index ddfc1601..5a52f00d 100644 --- a/internal/database/mariadb/component.go +++ b/internal/database/mariadb/component.go @@ -206,11 +206,13 @@ func (s *SqlDatabase) CreateComponent(component *entity.Component) (*entity.Comp INSERT INTO Component ( component_ccrn, component_type, - component_created_by + component_created_by, + component_updated_by ) VALUES ( :component_ccrn, :component_type, - :component_created_by + :component_created_by, + :component_updated_by ) ` diff --git a/internal/database/mariadb/component_instance.go b/internal/database/mariadb/component_instance.go index a2e66202..c80c3af2 100644 --- a/internal/database/mariadb/component_instance.go +++ b/internal/database/mariadb/component_instance.go @@ -228,13 +228,15 @@ func (s *SqlDatabase) CreateComponentInstance(componentInstance *entity.Componen componentinstance_count, componentinstance_component_version_id, componentinstance_service_id, - componentinstance_created_by + componentinstance_created_by, + componentinstance_updated_by ) VALUES ( :componentinstance_ccrn, :componentinstance_count, :componentinstance_component_version_id, :componentinstance_service_id, - :componentinstance_created_by + :componentinstance_created_by, + :componentinstance_updated_by ) ` diff --git a/internal/database/mariadb/component_version.go b/internal/database/mariadb/component_version.go index 4fc5e661..41456747 100644 --- a/internal/database/mariadb/component_version.go +++ b/internal/database/mariadb/component_version.go @@ -213,11 +213,13 @@ func (s *SqlDatabase) CreateComponentVersion(componentVersion *entity.ComponentV INSERT INTO ComponentVersion ( componentversion_component_id, componentversion_version, - componentversion_created_by + componentversion_created_by, + componentversion_updated_by ) VALUES ( :componentversion_component_id, :componentversion_version, - :componentversion_created_by + :componentversion_created_by, + :componentversion_updated_by ) ` diff --git a/internal/database/mariadb/evidence.go b/internal/database/mariadb/evidence.go index 8838826c..89845f96 100644 --- a/internal/database/mariadb/evidence.go +++ b/internal/database/mariadb/evidence.go @@ -224,7 +224,8 @@ func (s *SqlDatabase) CreateEvidence(evidence *entity.Evidence) (*entity.Evidenc evidence_vector, evidence_rating, evidence_raa_end, - evidence_created_by + evidence_created_by, + evidence_updated_by ) VALUES ( :evidence_author_id, :evidence_activity_id, @@ -233,7 +234,8 @@ func (s *SqlDatabase) CreateEvidence(evidence *entity.Evidence) (*entity.Evidenc :evidence_vector, :evidence_rating, :evidence_raa_end, - :evidence_created_by + :evidence_created_by, + :evidence_updated_by ) ` diff --git a/internal/database/mariadb/issue.go b/internal/database/mariadb/issue.go index 88e995ac..a56b9ebc 100644 --- a/internal/database/mariadb/issue.go +++ b/internal/database/mariadb/issue.go @@ -408,12 +408,14 @@ func (s *SqlDatabase) CreateIssue(issue *entity.Issue) (*entity.Issue, error) { issue_primary_name, issue_type, issue_description, - issue_created_by + issue_created_by, + issue_updated_by ) VALUES ( :issue_primary_name, :issue_type, :issue_description, - :issue_created_by + :issue_created_by, + :issue_updated_by ) ` diff --git a/internal/database/mariadb/issue_match.go b/internal/database/mariadb/issue_match.go index 9e5d1c6b..6ba5466a 100644 --- a/internal/database/mariadb/issue_match.go +++ b/internal/database/mariadb/issue_match.go @@ -274,7 +274,8 @@ func (s *SqlDatabase) CreateIssueMatch(issueMatch *entity.IssueMatch) (*entity.I issuematch_user_id, issuematch_component_instance_id, issuematch_issue_id, - issuematch_created_by + issuematch_created_by, + issuematch_updated_by ) VALUES ( :issuematch_status, :issuematch_remediation_date, @@ -284,7 +285,8 @@ func (s *SqlDatabase) CreateIssueMatch(issueMatch *entity.IssueMatch) (*entity.I :issuematch_user_id, :issuematch_component_instance_id, :issuematch_issue_id, - :issuematch_created_by + :issuematch_created_by, + :issuematch_updated_by ) ` diff --git a/internal/database/mariadb/issue_match_change.go b/internal/database/mariadb/issue_match_change.go index f2273254..edd67daf 100644 --- a/internal/database/mariadb/issue_match_change.go +++ b/internal/database/mariadb/issue_match_change.go @@ -185,12 +185,14 @@ func (s *SqlDatabase) CreateIssueMatchChange(imc *entity.IssueMatchChange) (*ent issuematchchange_action, issuematchchange_activity_id, issuematchchange_issue_match_id, - issuematchchange_created_by + issuematchchange_created_by, + issuematchchange_updated_by ) VALUES ( :issuematchchange_action, :issuematchchange_activity_id, :issuematchchange_issue_match_id, - :issuematchchange_created_by + :issuematchchange_created_by, + :issuematchchange_updated_by ) ` diff --git a/internal/database/mariadb/issue_repository.go b/internal/database/mariadb/issue_repository.go index 74117308..a86840e6 100644 --- a/internal/database/mariadb/issue_repository.go +++ b/internal/database/mariadb/issue_repository.go @@ -222,11 +222,13 @@ func (s *SqlDatabase) CreateIssueRepository(issueRepository *entity.IssueReposit INSERT INTO IssueRepository ( issuerepository_name, issuerepository_url, - issuerepository_created_by + issuerepository_created_by, + issuerepository_updated_by ) VALUES ( :issuerepository_name, :issuerepository_url, - :issuerepository_created_by + :issuerepository_created_by, + :issuerepository_updated_by ) ` diff --git a/internal/database/mariadb/issue_variant.go b/internal/database/mariadb/issue_variant.go index 8fa4841b..400fc193 100644 --- a/internal/database/mariadb/issue_variant.go +++ b/internal/database/mariadb/issue_variant.go @@ -247,7 +247,8 @@ func (s *SqlDatabase) CreateIssueVariant(issueVariant *entity.IssueVariant) (*en issuevariant_rating, issuevariant_secondary_name, issuevariant_description, - issuevariant_created_by + issuevariant_created_by, + issuevariant_updated_by ) VALUES ( :issuevariant_issue_id, :issuevariant_repository_id, @@ -255,7 +256,8 @@ func (s *SqlDatabase) CreateIssueVariant(issueVariant *entity.IssueVariant) (*en :issuevariant_rating, :issuevariant_secondary_name, :issuevariant_description, - :issuevariant_created_by + :issuevariant_created_by, + :issuevariant_updated_by ) ` diff --git a/internal/database/mariadb/service.go b/internal/database/mariadb/service.go index 9064115b..5421679a 100644 --- a/internal/database/mariadb/service.go +++ b/internal/database/mariadb/service.go @@ -345,10 +345,12 @@ func (s *SqlDatabase) CreateService(service *entity.Service) (*entity.Service, e query := ` INSERT INTO Service ( service_ccrn, - service_created_by + service_created_by, + service_updated_by ) VALUES ( :service_ccrn, - :service_created_by + :service_created_by, + :service_updated_by ) ` diff --git a/internal/database/mariadb/support_group.go b/internal/database/mariadb/support_group.go index 024fc031..e2c43dbb 100644 --- a/internal/database/mariadb/support_group.go +++ b/internal/database/mariadb/support_group.go @@ -210,10 +210,12 @@ func (s *SqlDatabase) CreateSupportGroup(supportGroup *entity.SupportGroup) (*en query := ` INSERT INTO SupportGroup ( supportgroup_ccrn, - supportgroup_created_by + supportgroup_created_by, + supportgroup_updated_by ) VALUES ( :supportgroup_ccrn, - :supportgroup_created_by + :supportgroup_created_by, + :supportgroup_updated_by ) ` diff --git a/internal/database/mariadb/user.go b/internal/database/mariadb/user.go index 59078262..f4b916a8 100644 --- a/internal/database/mariadb/user.go +++ b/internal/database/mariadb/user.go @@ -223,12 +223,14 @@ func (s *SqlDatabase) CreateUser(user *entity.User) (*entity.User, error) { user_name, user_unique_user_id, user_type, - user_created_by + user_created_by, + user_updated_by ) VALUES ( :user_name, :user_unique_user_id, :user_type, - :user_created_by + :user_created_by, + :user_updated_by ) ` diff --git a/internal/e2e/metadata_test.go b/internal/e2e/metadata_test.go index bb0d82a9..fed32e26 100644 --- a/internal/e2e/metadata_test.go +++ b/internal/e2e/metadata_test.go @@ -74,7 +74,7 @@ var _ = Describe("Creating and updating entity via API", Label("e2e", "Entities" createTestIssue(cfg.Port) issue = getTestIssue(cfg.Port) }) - It("shall assign CreatedBy and CreatedAt metadata fields and shall keep nil in UpdatedBy, UpdatedAt and DeltedAt metadata fields", func() { + It("shall assign CreatedBy, CreatedAt, UpdatedBy and UpdatedAt metadata fields and shall keep nil in DeltedAt metadata fields", func() { Expect(*issue.Description).To(Equal(testCreatedIssueDescription)) Expect(issue.Type.String()).To(Equal(testCreatedIssueType)) @@ -85,7 +85,7 @@ var _ = Describe("Creating and updating entity via API", Label("e2e", "Entities" Expect(err).Should(BeNil()) Expect(createdAt).Should(BeTemporally("~", time.Now().UTC(), 3*time.Second)) - Expect(*issue.Metadata.UpdatedBy).To(Equal(fmt.Sprintf("%d", e2e_common.EmptyUserId))) + Expect(*issue.Metadata.UpdatedBy).To(Equal(fmt.Sprintf("%d", e2e_common.SystemUserId))) updatedAt, err := time.Parse(dbDateLayout, *issue.Metadata.UpdatedAt) Expect(err).Should(BeNil())