-
Notifications
You must be signed in to change notification settings - Fork 51
improve Onboarding Flow for Program Creation #2778
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
Open
adelowo
wants to merge
16
commits into
main
Choose a base branch
from
feat-ISS-2880
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
f3e2a99
create program if selected
adelowo 72dcf5b
Merge remote-tracking branch 'origin/main' into feat-ISS-2880
adelowo e686f26
fix strings and use constants instead
adelowo 067d0ff
fix review
adelowo 6564222
register listener in test
adelowo 24b9121
add schemautil for tags
adelowo c4c01eb
apply custom util to all other jsonb fields not just tags alone
adelowo cc1e47e
Merge remote-tracking branch 'origin/main' into bug-ISS-2926
adelowo 04dc6ee
rework implementation
adelowo 0d6aea9
revert init
adelowo c695914
Merge remote-tracking branch 'origin/main' into bug-ISS-2926
adelowo b78b93e
Merge remote-tracking branch 'origin/main' into feat-ISS-2880
adelowo 1737418
Merge remote-tracking branch 'origin/bug-ISS-2926' into feat-ISS-2880
adelowo aa78526
fix merge
adelowo 7902434
Merge remote-tracking branch 'origin/main' into feat-ISS-2880
adelowo 5e8f7e2
use jsonx
adelowo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| c89c7e7c9a937583a194a2c8d524ccfb | ||
| 6d449fe0a221fc086960d7c64d3c5254 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| package hooks | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "entgo.io/ent/dialect/sql" | ||
| "github.com/theopenlane/iam/auth" | ||
|
|
||
| "github.com/theopenlane/core/common/enums" | ||
|
|
||
| "github.com/theopenlane/core/v2/internal/controls" | ||
| "github.com/theopenlane/core/v2/internal/ent/entityops" | ||
| "github.com/theopenlane/core/v2/internal/ent/generated" | ||
| "github.com/theopenlane/core/v2/internal/ent/generated/standard" | ||
| "github.com/theopenlane/core/v2/internal/workflows" | ||
| "github.com/theopenlane/core/v2/pkg/gala" | ||
| "github.com/theopenlane/core/v2/pkg/jsonx" | ||
| ) | ||
|
|
||
| func init() { registerListeners(OnboardingProgramListeners) } | ||
|
|
||
| // OnboardingProgramListeners sets up gala to process onboarding requests such as creating programs | ||
| // and cloning them in the background | ||
| func OnboardingProgramListeners() []gala.Registration { | ||
| return []gala.Registration{ | ||
| entityops.MutationListener{ | ||
| Schema: entityops.SchemaOnboarding, | ||
| Operations: []string{entityops.OpCreate}, | ||
| Caller: func(restored *auth.Caller, _ entityops.MutationPayload) *auth.Caller { | ||
| return restored.WithCapabilities(auth.CapInternalOperation) | ||
| }, | ||
| Handle: handleOnboardingProgram, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func handleOnboardingProgram(inv entityops.Invocation, _ entityops.MutationPayload) error { | ||
| // cannot use MutationPayload because the organization_id needed is actually stored by the hook | ||
| // so it will not be available here | ||
| record, ok, err := entityops.LoadEntity(inv.Context, inv.EntityID, inv.Client.Onboarding.Get) | ||
| if err != nil || !ok { | ||
| return err | ||
| } | ||
|
|
||
| if len(record.Compliance) == 0 { | ||
| return nil | ||
| } | ||
|
|
||
| _, err = workflows.WithTx(inv.Context, inv.Client, nil, func(tx *generated.Tx) (struct{}, error) { | ||
| return struct{}{}, createProgram(inv.Context, tx.Client(), record.OrganizationID, record.Compliance) | ||
| }) | ||
|
|
||
| return err | ||
| } | ||
|
|
||
| func generateProgramName(standards []*generated.Standard, year int) string { | ||
| if len(standards) == 1 { | ||
| name := standards[0].ShortName | ||
| if name == "" { | ||
| name = standards[0].Name | ||
| } | ||
|
|
||
| return fmt.Sprintf("%s Program %d", name, year) | ||
| } | ||
|
|
||
| return fmt.Sprintf("Compliance Program %d", year) | ||
| } | ||
|
|
||
| func createProgram(ctx context.Context, client *generated.Client, orgID string, complianceData map[string]interface{}) error { | ||
|
Check failure on line 72 in internal/ent/hooks/listeners_onboarding_program.go
|
||
| standards, labels, err := resolveOnboardingStandards(ctx, client, complianceData) | ||
| if err != nil || len(labels) == 0 { | ||
| return err | ||
| } | ||
|
|
||
| currentYear := time.Now().Year() | ||
|
|
||
| frameworks := strings.Join(labels, ", ") | ||
|
|
||
| description := fmt.Sprintf("Track %s compliance activities, evidence, and audit readiness for %d.", frameworks, currentYear) | ||
|
|
||
| builder := client.Program.Create(). | ||
| SetOwnerID(orgID). | ||
| SetName(generateProgramName(standards, currentYear)). | ||
| SetDescription(description). | ||
| SetFrameworkName(frameworks) | ||
|
|
||
| if auditor, ok := complianceData["auditor_name"].(string); ok && auditor != "" { | ||
| builder.SetAuditor(auditor) | ||
| } | ||
|
|
||
| if email, ok := complianceData["auditor_email"].(string); ok && email != "" { | ||
| builder.SetAuditorEmail(email) | ||
| } | ||
|
|
||
| program, err := builder.Save(ctx) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| for _, std := range standards { | ||
| filters := controls.CloneFilterOptions{StandardID: &std.ID} | ||
| if std.Framework == "soc2" { | ||
| filters.Categories = []string{"Security"} | ||
| } | ||
|
|
||
| where, err := controls.ControlFilterByStandard(ctx, filters, std) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| sources, err := client.Control.Query().Where(where...).WithStandard().WithSubcontrols().All(ctx) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // can't use worker pool here since we are in a tx | ||
| for _, source := range sources { | ||
| input, _ := controls.CreateCloneControlInput(source, &program.ID, orgID) | ||
| cloned, err := client.Control.Create().SetInput(input).Save(ctx) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| for _, subcontrol := range source.Edges.Subcontrols { | ||
| input := controls.CreateCloneSubcontrolInput(subcontrol, orgID, controls.SubcontrolToCreate{RefControl: source}) | ||
| input.ControlID = cloned.ID | ||
| if err := client.Subcontrol.Create().SetInput(*input).Exec(ctx); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func getOnboardingFrameworks(complianceData map[string]interface{}) ([]string, error) { | ||
| var frameworks []string | ||
| if err := jsonx.RoundTrip(complianceData["frameworks"], &frameworks); err != nil { | ||
| return nil, fmt.Errorf("invalid onboarding frameworks: %w", err) | ||
| } | ||
|
|
||
| return frameworks, nil | ||
| } | ||
|
|
||
| func resolveOnboardingStandards(ctx context.Context, client *generated.Client, complianceData map[string]interface{}) ([]*generated.Standard, []string, error) { | ||
| frameworks, err := getOnboardingFrameworks(complianceData) | ||
| if err != nil || len(frameworks) == 0 { | ||
| return nil, nil, err | ||
| } | ||
|
|
||
| standards := make([]*generated.Standard, 0, len(frameworks)) | ||
|
|
||
| labels := make([]string, 0, len(frameworks)) | ||
| seen := make(map[string]bool, len(frameworks)) | ||
|
|
||
| for _, framework := range frameworks { | ||
| if seen[framework] { | ||
| continue | ||
| } | ||
|
|
||
| seen[framework] = true | ||
|
|
||
| if framework == "other" { | ||
| labels = append(labels, "Other") | ||
| continue | ||
| } | ||
|
|
||
| std, err := client.Standard.Query(). | ||
| Where( | ||
| standard.FrameworkEQ(framework), | ||
| standard.StatusEQ(enums.StandardActive), | ||
| standard.SystemOwned(true), | ||
| ). | ||
| Order( | ||
| standard.ByVersion(sql.OrderDesc()), | ||
| standard.ByID(), | ||
| ). | ||
| First(ctx) | ||
| if err != nil { | ||
| return nil, nil, fmt.Errorf("resolve onboarding framework %q: %w", framework, err) | ||
| } | ||
|
|
||
| standards = append(standards, std) | ||
| label := std.ShortName | ||
| if label == "" { | ||
| label = std.Name | ||
| } | ||
|
|
||
| labels = append(labels, label) | ||
| } | ||
|
|
||
| return standards, labels, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.