Skip to content
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

Adding business logic for reachability enricher - leveraging atom. #340

Closed
wants to merge 4 commits into from

Conversation

andream16
Copy link
Contributor

@andream16 andream16 commented Sep 6, 2024

Code changes to create a new enricher which enriches existing vulnerability reports with atom reachability reports.

This is a refactor from #271.

I tried to decompose the code by responsibility and added godoc. Let me know if some bits are not clear :).

I opted for passing around concrete types instead of abstractions as we are not leveraging mocks and we're not expecting to swap implementations. This can be done later, if needed. Right now this choice helps with making things clearer and easier to find.


Still working on the Task. Will add it later!

@andream16 andream16 changed the title [DRAFT] Adding business logic for reachability enricher - leveraging atom. Adding business logic for reachability enricher - leveraging atom. Sep 10, 2024
@andream16
Copy link
Contributor Author

Need to add task.

@andream16
Copy link
Contributor Author

Need to add README.md

return nil
})

if err := g.Wait(); err != nil && !isCtxErr(err) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gracefully exit when done and handle correctly context cancellations.

}
)

func TestEnricher(t *testing.T) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the main test. Executes everything end to end.

@@ -0,0 +1,8 @@
package conf
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_test files are ignored by go build but they can still be used to access unexported bits in tests. Useful for not polluting the available API but still be able to keep tests consistent.


// Enrich looks for untagged inputs and processes them outputting if any of them is reachable.
// The reachability checks leverage atom - https://github.com/AppThreat/atom.
func (r *enricher) Enrich(ctx context.Context) error {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the main business logic layer.

@@ -0,0 +1,92 @@
package main
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are not familiar with the concept, code in internal cannot be imported by other go modules. The build tool would report an error if this happens - leading to a failing build.

This makes sure to leak to potential user only the API that we actually want to expose. This is a nice Go built in feature

@andream16 andream16 self-assigned this Sep 10, 2024
}

func NewParser() (*Parser, error) {
purlPkg, err := regexp.Compile(`(?P<p1>[^/:]+/(?P<p2>[^/]+))(?:(?:.|/)v\d+)?@`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we using regexp to parse this and not https://github.com/package-url/packageurl-go?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will look into it!

@@ -0,0 +1,59 @@
package conf
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps this should be part of the base enricher?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by that?

@@ -0,0 +1,97 @@
package fs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps this should also be part of the base enricher. not for this PR but maybe good to be in a ticket for the OSS refactoring project

return nil, errors.New("invalid empty reachable purls")
}

matcherFileLine, err := regexp.Compile(`(?P<file>[^/]+):(?P<line>[\d-]+)`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does atom only have lines?
i thought it had ranges, happy to be wrong if your tests show otherwise :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my tests and my understanding it looks ok but please feel free to test too!


lineStart, lineEnd, err := s.searchLineRange(fileContent)
if err != nil {
return false, fmt.Errorf("failed to parse line range: %w", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we use go-errors Errorf by convention

@andream16 andream16 closed this Sep 17, 2024
@andream16
Copy link
Contributor Author

Closed in favour of #360 (review)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants