|
| 1 | +package ostest |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + |
| 9 | + "github.com/rs/zerolog" |
| 10 | + "github.com/snyk/go-application-framework/pkg/apiclients/testapi" |
| 11 | + "github.com/snyk/go-application-framework/pkg/workflow" |
| 12 | + |
| 13 | + service "github.com/snyk/cli-extension-os-flows/internal/common" |
| 14 | + "github.com/snyk/cli-extension-os-flows/internal/errors" |
| 15 | + "github.com/snyk/cli-extension-os-flows/internal/flags" |
| 16 | + "github.com/snyk/cli-extension-os-flows/internal/legacy/definitions" |
| 17 | +) |
| 18 | + |
| 19 | +// RunUnifiedTestFlow handles the unified test API flow. |
| 20 | +func RunUnifiedTestFlow( |
| 21 | + ctx context.Context, |
| 22 | + ictx workflow.InvocationContext, |
| 23 | + testClient testapi.TestClient, |
| 24 | + riskScoreThreshold *uint16, |
| 25 | + severityThreshold *testapi.Severity, |
| 26 | + orgID string, |
| 27 | + errFactory *errors.ErrorFactory, |
| 28 | + logger *zerolog.Logger, |
| 29 | +) ([]workflow.Data, error) { |
| 30 | + logger.Info().Msg("Starting open source test") |
| 31 | + |
| 32 | + // Create depgraphs and get their associated target files |
| 33 | + depGraphs, displayTargetFiles, err := createDepGraphs(ictx) |
| 34 | + if err != nil { |
| 35 | + return nil, err |
| 36 | + } |
| 37 | + var allFindings []definitions.LegacyVulnerabilityResponse |
| 38 | + var allSummaries []workflow.Data |
| 39 | + |
| 40 | + localPolicy := createLocalPolicy(riskScoreThreshold, severityThreshold) |
| 41 | + |
| 42 | + for i, depGraph := range depGraphs { |
| 43 | + displayTargetFile := "" |
| 44 | + if i < len(displayTargetFiles) { |
| 45 | + displayTargetFile = displayTargetFiles[i] |
| 46 | + } |
| 47 | + |
| 48 | + // Create depgraph subject |
| 49 | + depGraphSubject := testapi.DepGraphSubjectCreate{ |
| 50 | + Type: testapi.DepGraphSubjectCreateTypeDepGraph, |
| 51 | + DepGraph: depGraph, |
| 52 | + Locator: testapi.LocalPathLocator{ |
| 53 | + Paths: []string{displayTargetFile}, |
| 54 | + Type: testapi.LocalPath, |
| 55 | + }, |
| 56 | + } |
| 57 | + |
| 58 | + // Create test subject with depgraph |
| 59 | + var subject testapi.TestSubjectCreate |
| 60 | + err = subject.FromDepGraphSubjectCreate(depGraphSubject) |
| 61 | + if err != nil { |
| 62 | + return nil, fmt.Errorf("failed to create test subject: %w", err) |
| 63 | + } |
| 64 | + |
| 65 | + // Project name assigned as follows: --project-name || config project name || scannedProject?.depTree?.name |
| 66 | + // TODO: use project name from Config file |
| 67 | + config := ictx.GetConfiguration() |
| 68 | + projectName := config.GetString(flags.FlagProjectName) |
| 69 | + if projectName == "" && len(depGraph.Pkgs) > 0 { |
| 70 | + projectName = depGraph.Pkgs[0].Info.Name |
| 71 | + } |
| 72 | + |
| 73 | + packageManager := depGraph.PkgManager.Name |
| 74 | + depCount := max(0, len(depGraph.Pkgs)-1) |
| 75 | + |
| 76 | + // Run the test with the depgraph subject |
| 77 | + findings, summary, err := RunTest(ctx, testClient, subject, projectName, packageManager, depCount, displayTargetFile, orgID, errFactory, logger, localPolicy) |
| 78 | + if err != nil { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + |
| 82 | + if findings != nil { |
| 83 | + allFindings = append(allFindings, *findings) |
| 84 | + } |
| 85 | + if summary != nil { |
| 86 | + allSummaries = append(allSummaries, summary) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + var finalOutput []workflow.Data |
| 91 | + if len(allFindings) > 0 { |
| 92 | + var findingsData any |
| 93 | + if len(allFindings) == 1 { |
| 94 | + findingsData = allFindings[0] |
| 95 | + } else { |
| 96 | + findingsData = allFindings |
| 97 | + } |
| 98 | + |
| 99 | + var buffer bytes.Buffer |
| 100 | + encoder := json.NewEncoder(&buffer) |
| 101 | + encoder.SetEscapeHTML(false) |
| 102 | + encoder.SetIndent("", " ") |
| 103 | + err := encoder.Encode(findingsData) |
| 104 | + if err != nil { |
| 105 | + return nil, errFactory.NewLegacyJSONTransformerError(fmt.Errorf("marshaling to json: %w", err)) |
| 106 | + } |
| 107 | + // encoder.Encode adds a newline, which we trim to match Marshal's behavior. |
| 108 | + findingsBytes := bytes.TrimRight(buffer.Bytes(), "\n") |
| 109 | + |
| 110 | + finalOutput = append(finalOutput, NewWorkflowData(ApplicationJSONContentType, findingsBytes)) |
| 111 | + } |
| 112 | + |
| 113 | + finalOutput = append(finalOutput, allSummaries...) |
| 114 | + |
| 115 | + return finalOutput, nil |
| 116 | +} |
| 117 | + |
| 118 | +// Create local policy only if risk score or severity threshold are specified. |
| 119 | +func createLocalPolicy(riskScoreThreshold *uint16, severityThreshold *testapi.Severity) *testapi.LocalPolicy { |
| 120 | + if riskScoreThreshold == nil && severityThreshold == nil { |
| 121 | + return nil |
| 122 | + } |
| 123 | + |
| 124 | + localPolicy := &testapi.LocalPolicy{} |
| 125 | + if riskScoreThreshold != nil { |
| 126 | + localPolicy.RiskScoreThreshold = riskScoreThreshold |
| 127 | + } |
| 128 | + if severityThreshold != nil { |
| 129 | + localPolicy.SeverityThreshold = severityThreshold |
| 130 | + } |
| 131 | + return localPolicy |
| 132 | +} |
| 133 | + |
| 134 | +// createDepGraphs creates depgraphs from the file parameter in the context. |
| 135 | +func createDepGraphs(ictx workflow.InvocationContext) ([]testapi.IoSnykApiV1testdepgraphRequestDepGraph, []string, error) { |
| 136 | + depGraphResult, err := service.GetDepGraph(ictx) |
| 137 | + if err != nil { |
| 138 | + return nil, nil, fmt.Errorf("failed to get dependency graph: %w", err) |
| 139 | + } |
| 140 | + |
| 141 | + if len(depGraphResult.DepGraphBytes) == 0 { |
| 142 | + return nil, nil, fmt.Errorf("no dependency graphs found") |
| 143 | + } |
| 144 | + |
| 145 | + depGraphs := make([]testapi.IoSnykApiV1testdepgraphRequestDepGraph, len(depGraphResult.DepGraphBytes)) |
| 146 | + for i, depGraphBytes := range depGraphResult.DepGraphBytes { |
| 147 | + var depGraphStruct testapi.IoSnykApiV1testdepgraphRequestDepGraph |
| 148 | + err = json.Unmarshal(depGraphBytes, &depGraphStruct) |
| 149 | + if err != nil { |
| 150 | + return nil, nil, |
| 151 | + fmt.Errorf("unmarshaling depGraph from args failed: %w", err) |
| 152 | + } |
| 153 | + depGraphs[i] = depGraphStruct |
| 154 | + } |
| 155 | + |
| 156 | + return depGraphs, depGraphResult.DisplayTargetFiles, nil |
| 157 | +} |
0 commit comments