Skip to content

Commit

Permalink
feat: support projectTags and isLatest flags for bom uploads
Browse files Browse the repository at this point in the history
Signed-off-by: nscuro <[email protected]>
  • Loading branch information
nscuro committed Oct 27, 2024
1 parent 85997f7 commit 0b72e6c
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 11 deletions.
23 changes: 12 additions & 11 deletions about_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func TestAboutService_Get(t *testing.T) {
_, client := setUpContainer(t)
client := setUpContainer(t, testContainerOptions{})

about, err := client.About.Get(context.TODO())
require.NoError(t, err)
Expand All @@ -30,7 +30,11 @@ func TestAboutService_Get(t *testing.T) {
require.Equal(t, "Alpine", about.Framework.Name)
}

func setUpContainer(t *testing.T) (testcontainers.Container, *Client) {
type testContainerOptions struct {
APIPermissions []string
}

func setUpContainer(t *testing.T, options testContainerOptions) *Client {
ctx := context.Background()

container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
Expand Down Expand Up @@ -69,17 +73,14 @@ func setUpContainer(t *testing.T) (testcontainers.Container, *Client) {
client, err = NewClient(apiURL, WithBearerToken(bearerToken))
require.NoError(t, err)

// TODO: Pass desired permissions as parameter to setUpContainer
team, err := client.Team.Create(ctx, Team{
Name: "test",
Permissions: []Permission{
{
Name: "VIEW_PORTFOLIO",
},
},
})
team, err := client.Team.Create(ctx, Team{Name: "test"})
require.NoError(t, err)

for _, permissionName := range options.APIPermissions {
_, err = client.Permission.AddPermissionToTeam(ctx, Permission{Name: permissionName}, team.UUID)
require.NoError(t, err)
}

apiKey, err := client.Team.GenerateAPIKey(ctx, team.UUID)
require.NoError(t, err)

Expand Down
78 changes: 78 additions & 0 deletions bom_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package dtrack

import (
"context"
"encoding/base64"
"github.com/stretchr/testify/require"
"testing"
)

func TestBOMService_Upload(t *testing.T) {
client := setUpContainer(t, testContainerOptions{
APIPermissions: []string{
PermissionBOMUpload,
PermissionProjectCreationUpload,
PermissionViewPortfolio,
},
})

_, err := client.BOM.Upload(context.Background(), BOMUploadRequest{
ProjectName: "acme-app",
ProjectVersion: "1.2.3",
ProjectTags: []Tag{
{Name: "foo"},
{Name: "bar"},
},
IsLatest: true,
AutoCreate: true,
BOM: base64.StdEncoding.EncodeToString([]byte(`
{
"bomFormat": "CycloneDX",
"specVersion": "1.4",
"version": 1,
"components": []
}`)),
})
require.NoError(t, err)

project, err := client.Project.Lookup(context.Background(), "acme-app", "1.2.3")
require.NoError(t, err)
require.Contains(t, project.Tags, Tag{Name: "foo"})
require.Contains(t, project.Tags, Tag{Name: "bar"})
require.True(t, project.IsLatest)
}

func TestBOMService_PostBom(t *testing.T) {
client := setUpContainer(t, testContainerOptions{
APIPermissions: []string{
PermissionBOMUpload,
PermissionProjectCreationUpload,
PermissionViewPortfolio,
},
})

_, err := client.BOM.PostBom(context.Background(), BOMUploadRequest{
ProjectName: "acme-app",
ProjectVersion: "1.2.3",
ProjectTags: []Tag{
{Name: "foo"},
{Name: "bar"},
},
IsLatest: true,
AutoCreate: true,
BOM: `
{
"bomFormat": "CycloneDX",
"specVersion": "1.4",
"version": 1,
"components": []
}`,
})
require.NoError(t, err)

project, err := client.Project.Lookup(context.Background(), "acme-app", "1.2.3")
require.NoError(t, err)
require.Contains(t, project.Tags, Tag{Name: "foo"})
require.Contains(t, project.Tags, Tag{Name: "bar"})
require.True(t, project.IsLatest)
}
17 changes: 17 additions & 0 deletions permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@ import (
"github.com/google/uuid"
)

const (
PermissionAccessManagement = "ACCESS_MANAGEMENT"
PermissionBOMUpload = "BOM_UPLOAD"
PermissionPolicyManagement = "POLICY_MANAGEMENT"
PermissionPolicyViolationAnalysis = "POLICY_VIOLATION_ANALYSIS"
PermissionPortfolioManagement = "PORTFOLIO_MANAGEMENT"
PermissionProjectCreationUpload = "PROJECT_CREATION_UPLOAD"
PermissionSystemConfiguration = "SYSTEM_CONFIGURATION"
PermissionTagManagement = "TAG_MANAGEMENT"
PermissionViewBadges = "VIEW_BADGES"
PermissionViewPolicyViolation = "VIEW_POLICY_VIOLATION"
PermissionViewPortfolio = "VIEW_PORTFOLIO"
PermissionViewVulnerability = "VIEW_VULNERABILITY"
PermissionVulnerabilityAnalysis = "VULNERABILITY_ANALYSIS"
PermissionVulnerabilityManagement = "VULNERABILITY_MANAGEMENT"
)

type PermissionService struct {
client *Client
}
Expand Down
1 change: 1 addition & 0 deletions project.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Project struct {
Properties []ProjectProperty `json:"properties,omitempty"`
Tags []Tag `json:"tags,omitempty"`
Active bool `json:"active"`
IsLatest bool `json:"isLatest"` // Since v4.12.0
Metrics ProjectMetrics `json:"metrics"`
ParentRef *ParentRef `json:"parent,omitempty"`
LastBOMImport int `json:"lastBomImport"`
Expand Down

0 comments on commit 0b72e6c

Please sign in to comment.