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

feat: Wire in new datasources & finding structures #81

Merged
merged 7 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 0 additions & 146 deletions api/github.go

This file was deleted.

10 changes: 7 additions & 3 deletions config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ label = "Go"
slack_emoji = ":golang:"

[[ecosystem]]
label = "Npm"
Copy link
Contributor

Choose a reason for hiding this comment

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

lol 😅

label = "Js"
slack_emoji = ":javascript:"

[[ecosystem]]
label = "Pip"
label = "Python"
slack_emoji = ":python:"

[[ecosystem]]
label = "Rubygems"
label = "Ruby"
slack_emoji = ":ruby:"

# Configure the severities we're reporting on
Expand All @@ -34,6 +34,10 @@ slack_emoji = ":severity_medium:"
label = "Low"
slack_emoji = ":severity_low:"

[[severity]]
label = "Undefined"
slack_emoji = ":white_check_mark:"

# Configure individual teams to report directly on their work
[[team]]
name = "Some Team"
Expand Down
10 changes: 5 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,18 @@ func LoadEnv(params ViperParams) error {
return nil
}

func GetIconForSeverity(severity string, severities []SeverityConfig) (string, error) {
func GetIconForSeverity(severity FindingSeverityType, severities []SeverityConfig) (string, error) {
for _, config := range severities {
if config.Label == severity {
if config.Label == SeverityNames[severity] {
return config.Slack_emoji, nil
}
}
return "", fmt.Errorf("No Slack icon available for severity %s", severity)
return "", fmt.Errorf("No Slack icon available for severity %s", SeverityNames[severity])
}

func GetIconForEcosystem(ecosystem string, ecosystems []EcosystemConfig) (string, error) {
func GetIconForEcosystem(ecosystem FindingEcosystemType, ecosystems []EcosystemConfig) (string, error) {
for _, config := range ecosystems {
if config.Label == ecosystem {
if strings.ToLower(config.Label) == string(ecosystem) {
return config.Slack_emoji, nil
}
}
Expand Down
45 changes: 23 additions & 22 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package config
package config_test

import (
"fmt"
Expand All @@ -7,53 +7,54 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/underdog-tech/vulnbot/config"
)

func TestGetIconForConfiguredSeverity(t *testing.T) {
severities := []SeverityConfig{
severities := []config.SeverityConfig{
{Label: "High", Slack_emoji: ":high:"},
{Label: "Low", Slack_emoji: ":low:"},
}
icon, err := GetIconForSeverity("High", severities)
icon, err := config.GetIconForSeverity(config.FindingSeverityHigh, severities)
assert.Equal(t, icon, ":high:")
assert.Nil(t, err)
}

func TestGetIconForUnconfiguredSeverity(t *testing.T) {
severities := []SeverityConfig{
severities := []config.SeverityConfig{
{Label: "High", Slack_emoji: ":high:"},
{Label: "Low", Slack_emoji: ":low:"},
}
icon, err := GetIconForSeverity("Medium", severities)
icon, err := config.GetIconForSeverity(config.FindingSeverityModerate, severities)
assert.Empty(t, icon)
assert.Error(t, err)
}

func TestGetIconForConfiguredEcosystem(t *testing.T) {
ecosystems := []EcosystemConfig{
{Label: "Pip", Slack_emoji: ":python:"},
ecosystems := []config.EcosystemConfig{
{Label: "Python", Slack_emoji: ":python:"},
{Label: "Go", Slack_emoji: ":golang:"},
}
icon, err := GetIconForEcosystem("Pip", ecosystems)
icon, err := config.GetIconForEcosystem(config.FindingEcosystemPython, ecosystems)
assert.Equal(t, icon, ":python:")
assert.Nil(t, err)
}

func TestGetConfiguredTeamConfigBySlug(t *testing.T) {
testersTeam := TeamConfig{Name: "Testers", Github_slug: "testers-team"}
failersTeam := TeamConfig{Name: "Failers", Github_slug: "failers-team"}
TeamConfigs := []TeamConfig{
testersTeam := config.TeamConfig{Name: "Testers", Github_slug: "testers-team"}
failersTeam := config.TeamConfig{Name: "Failers", Github_slug: "failers-team"}
TeamConfigs := []config.TeamConfig{
testersTeam,
failersTeam,
}
team, err := GetTeamConfigBySlug("testers-team", TeamConfigs)
team, err := config.GetTeamConfigBySlug("testers-team", TeamConfigs)
assert.Equal(t, team, testersTeam)
assert.Nil(t, err)
}

func TestGetUnconfiguredTeamConfigBySlug(t *testing.T) {
TeamConfigs := []TeamConfig{} // Empty is easiest for this purpose
team, err := GetTeamConfigBySlug("unknown-team", TeamConfigs)
TeamConfigs := []config.TeamConfig{} // Empty is easiest for this purpose
team, err := config.GetTeamConfigBySlug("unknown-team", TeamConfigs)
assert.Empty(t, team)
assert.Error(t, err)
}
Expand All @@ -76,14 +77,14 @@ func TestLoadConfig(t *testing.T) {
}
testDataPath := filepath.Join(currentDir, "/testdata/test_config.toml")

var config Config
_ = LoadConfig(ViperParams{
Output: &config,
var cfg config.Config
_ = config.LoadConfig(config.ViperParams{
Output: &cfg,
ConfigPath: &testDataPath,
})

assert.IsType(t, Config{}, config)
assert.Equal(t, expectedSlackChannel, config.Default_slack_channel)
assert.IsType(t, config.Config{}, cfg)
assert.Equal(t, expectedSlackChannel, cfg.Default_slack_channel)
}

func TestLoadEnv(t *testing.T) {
Expand All @@ -94,12 +95,12 @@ func TestLoadEnv(t *testing.T) {
}
testDataPath := filepath.Join(currentDir, "/testdata/config.env")

var env Env
_ = LoadEnv(ViperParams{
var env config.Env
_ = config.LoadEnv(config.ViperParams{
EnvFileName: &testDataPath,
Output: &env,
})

assert.IsType(t, Env{}, env)
assert.IsType(t, config.Env{}, env)
assert.Equal(t, expectedSlackAuthToken, env.SlackAuthToken)
}
39 changes: 39 additions & 0 deletions internal/datasources.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package internal

import (
"sync"

"github.com/underdog-tech/vulnbot/config"
"github.com/underdog-tech/vulnbot/logger"
"github.com/underdog-tech/vulnbot/querying"
)

func GetDataSources(env config.Env, cfg config.Config) []querying.DataSource {
dataSources := []querying.DataSource{}

if env.GithubToken != "" {
ghds := querying.NewGithubDataSource(cfg, env)
dataSources = append(dataSources, &ghds)
}

Check warning on line 17 in internal/datasources.go

View check run for this annotation

Codecov / codecov/patch

internal/datasources.go#L11-L17

Added lines #L11 - L17 were not covered by tests

return dataSources

Check warning on line 19 in internal/datasources.go

View check run for this annotation

Codecov / codecov/patch

internal/datasources.go#L19

Added line #L19 was not covered by tests
}

func QueryAllDataSources(dataSources *[]querying.DataSource) *querying.ProjectCollection {
log := logger.Get()
projects := querying.NewProjectCollection()
wg := new(sync.WaitGroup)

for _, ds := range *dataSources {
wg.Add(1)
go func(currentDS querying.DataSource) {
err := currentDS.CollectFindings(projects, wg)
if err != nil {
log.Error().Err(err).Type("datasource", currentDS).Msg("Failed to query datasource")
}

Check warning on line 33 in internal/datasources.go

View check run for this annotation

Codecov / codecov/patch

internal/datasources.go#L22-L33

Added lines #L22 - L33 were not covered by tests
}(ds)
}
wg.Wait()

return projects

Check warning on line 38 in internal/datasources.go

View check run for this annotation

Codecov / codecov/patch

internal/datasources.go#L36-L38

Added lines #L36 - L38 were not covered by tests
}
Loading