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: implement additional interfaces for team, permission and oidc #22

Merged
merged 1 commit into from
Dec 10, 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
9 changes: 9 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Client struct {
Finding FindingService
License LicenseService
Metrics MetricsService
OIDC OIDCService
Permission PermissionService
Policy PolicyService
PolicyCondition PolicyConditionService
Expand Down Expand Up @@ -86,6 +87,7 @@ func NewClient(baseURL string, options ...ClientOption) (*Client, error) {
client.Finding = FindingService{client: &client}
client.License = LicenseService{client: &client}
client.Metrics = MetricsService{client: &client}
client.OIDC = OIDCService{client: &client}
client.Permission = PermissionService{client: &client}
client.Policy = PolicyService{client: &client}
client.PolicyCondition = PolicyConditionService{client: &client}
Expand Down Expand Up @@ -251,6 +253,13 @@ func withPageOptions(po PageOptions) requestOption {
}
}

func withAcceptContentType(contentType string) requestOption {
return func(req *http.Request) error {
req.Header.Set("Accept", contentType)
return nil
}
}

func (c Client) doRequest(req *http.Request, v interface{}) (a apiResponse, err error) {
if c.debug {
reqDump, _ := httputil.DumpRequestOut(req, true)
Expand Down
3 changes: 1 addition & 2 deletions finding_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package dtrack_test
import (
"context"

"github.com/google/uuid"

"github.com/DependencyTrack/client-go"
"github.com/google/uuid"
)

// This example demonstrates how to fetch all findings for a given project.
Expand Down
124 changes: 124 additions & 0 deletions oidc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package dtrack

import (
"context"
"fmt"
"net/http"
"strconv"

"github.com/google/uuid"
)

type OIDCService struct {
client *Client
}

type OIDCGroup struct {
Name string `json:"name,omitempty"`
UUID uuid.UUID `json:"uuid,omitempty"`
}

type OIDCMappingRequest struct {
Team uuid.UUID `json:"team"`
Group uuid.UUID `json:"group"`
}

type OIDCMapping struct {
Group OIDCGroup `json:"group"`
UUID uuid.UUID `json:"uuid"`
}

func (s OIDCService) Available(ctx context.Context) (available bool, err error) {
req, err := s.client.newRequest(ctx, http.MethodGet, "/api/v1/oidc/available", withAcceptContentType("text/plain"))
if err != nil {
return
}

var value string

_, err = s.client.doRequest(req, &value)
if err != nil {
return
}
available, err = strconv.ParseBool(value)
return
}

func (s OIDCService) GetAllGroups(ctx context.Context, po PageOptions) (p Page[OIDCGroup], err error) {
req, err := s.client.newRequest(ctx, http.MethodGet, "/api/v1/oidc/group", withPageOptions(po))
if err != nil {
return
}

res, err := s.client.doRequest(req, &p.Items)
if err != nil {
return
}

p.TotalCount = res.TotalCount
return
}

func (s OIDCService) CreateGroup(ctx context.Context, name string) (g OIDCGroup, err error) {
req, err := s.client.newRequest(ctx, http.MethodPut, "/api/v1/oidc/group", withBody(OIDCGroup{Name: name}))
if err != nil {
return
}

_, err = s.client.doRequest(req, &g)
return
}
func (s OIDCService) UpdateGroup(ctx context.Context, group OIDCGroup) (g OIDCGroup, err error) {
req, err := s.client.newRequest(ctx, http.MethodPost, "/api/v1/oidc/group", withBody(group))
if err != nil {
return
}

_, err = s.client.doRequest(req, &g)
return
}

func (s OIDCService) DeleteGroup(ctx context.Context, groupUUID uuid.UUID) (err error) {
req, err := s.client.newRequest(ctx, http.MethodDelete, fmt.Sprintf("/api/v1/oidc/group/%s", groupUUID.String()))
if err != nil {
return
}

_, err = s.client.doRequest(req, nil)
return
}

func (s OIDCService) GetAllTeamsOf(ctx context.Context, group OIDCGroup, po PageOptions) (p Page[Team], err error) {
req, err := s.client.newRequest(ctx, http.MethodGet, fmt.Sprintf("/api/v1/oidc/group/%s/team", group.UUID.String()), withPageOptions(po))
if err != nil {
return
}

res, err := s.client.doRequest(req, &p.Items)
if err != nil {
return
}

p.TotalCount = res.TotalCount
return
}

func (s OIDCService) AddTeamMapping(ctx context.Context, mapping OIDCMappingRequest) (m OIDCMapping, err error) {
req, err := s.client.newRequest(ctx, http.MethodPut, "/api/v1/oidc/mapping", withBody(mapping))
if err != nil {
return
}

_, err = s.client.doRequest(req, &m)
return
}

func (s OIDCService) RemoveTeamMapping(ctx context.Context, mappingID uuid.UUID) (err error) {
req, err := s.client.newRequest(ctx, http.MethodDelete, fmt.Sprintf("/api/v1/oidc/mapping/%s", mappingID.String()))
if err != nil {
return
}

_, err = s.client.doRequest(req, nil)
return
}
12 changes: 11 additions & 1 deletion permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package dtrack
import (
"context"
"fmt"
"github.com/google/uuid"
"net/http"

"github.com/google/uuid"
)

type PermissionService struct {
Expand Down Expand Up @@ -40,3 +41,12 @@ func (ps PermissionService) AddPermissionToTeam(ctx context.Context, permission
_, err = ps.client.doRequest(req, &t)
return
}
func (ps PermissionService) RemovePermissionFromTeam(ctx context.Context, permission Permission, team uuid.UUID) (t Team, err error) {
req, err := ps.client.newRequest(ctx, http.MethodDelete, fmt.Sprintf("/api/v1/permission/%s/team/%s", permission.Name, team.String()))
if err != nil {
return
}

_, err = ps.client.doRequest(req, &t)
return
}
3 changes: 2 additions & 1 deletion project_property.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package dtrack
import (
"context"
"fmt"
"github.com/google/uuid"
"net/http"

"github.com/google/uuid"
)

type ProjectProperty struct {
Expand Down
90 changes: 90 additions & 0 deletions repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,40 @@ package dtrack

import (
"context"
"fmt"
"net/http"

"github.com/google/uuid"
)

const (
RepositoryTypeCargo = "CARGO"
RepositoryTypeComposer = "COMPOSER"
RepositoryTypeCpan = "CPAN"
RepositoryTypeGem = "GEM"
RepositoryTypeGoModules = "GO_MODULES"
RepositoryTypeHex = "HEX"
RepositoryTypeMaven = "MAVEN"
RepositoryTypeNpm = "NPM"
RepositoryTypeNuget = "NUGET"
RepositoryTypePypi = "PYPI"
RepositoryTypeUnsupported = "UNSUPPORTED"
)

type RepositoryType string

type Repository struct {
Type RepositoryType `json:"type"`
Identifier string `json:"identifier"`
Url string `json:"url"`
ResolutionOrder int `json:"resolutionOrder"`
Enabled bool `json:"enabled"`
Internal bool `json:"internal"`
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
UUID uuid.UUID `json:"uuid,omitempty"`
}

type RepositoryMetaComponent struct {
LatestVersion string `json:"latestVersion"`
}
Expand All @@ -26,3 +57,62 @@ func (rs RepositoryService) GetMetaComponent(ctx context.Context, purl string) (
_, err = rs.client.doRequest(req, &r)
return
}

func (rs RepositoryService) GetAll(ctx context.Context, po PageOptions) (p Page[Repository], err error) {
req, err := rs.client.newRequest(ctx, http.MethodGet, "/api/v1/repository", withPageOptions(po))
if err != nil {
return
}

res, err := rs.client.doRequest(req, &p.Items)
if err != nil {
return
}

p.TotalCount = res.TotalCount
return
}

func (rs RepositoryService) GetByType(ctx context.Context, repoType RepositoryType, po PageOptions) (p Page[Repository], err error) {
req, err := rs.client.newRequest(ctx, http.MethodGet, fmt.Sprintf("/api/v1/repository/%s", repoType), withPageOptions(po))
if err != nil {
return
}

res, err := rs.client.doRequest(req, &p.Items)
if err != nil {
return
}

p.TotalCount = res.TotalCount
return
}

func (rs RepositoryService) Create(ctx context.Context, repo Repository) (r Repository, err error) {
req, err := rs.client.newRequest(ctx, http.MethodPut, "/api/v1/repository", withBody(repo))
if err != nil {
return
}

_, err = rs.client.doRequest(req, &r)
return
}
func (rs RepositoryService) Update(ctx context.Context, repo Repository) (r Repository, err error) {
req, err := rs.client.newRequest(ctx, http.MethodPost, "/api/v1/repository", withBody(repo))
if err != nil {
return
}

_, err = rs.client.doRequest(req, &r)
return
}

func (rs RepositoryService) Delete(ctx context.Context, reposUUID uuid.UUID) (err error) {
req, err := rs.client.newRequest(ctx, http.MethodDelete, fmt.Sprintf("/api/v1/repository/%s", reposUUID.String()))
if err != nil {
return
}

_, err = rs.client.doRequest(req, nil)
return
}
19 changes: 15 additions & 4 deletions team.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (
)

type Team struct {
UUID uuid.UUID `json:"uuid,omitempty"`
Name string `json:"name,omitempty"`
APIKeys []APIKey `json:"apiKeys,omitempty"`
Permissions []Permission `json:"permissions"`
UUID uuid.UUID `json:"uuid,omitempty"`
Name string `json:"name,omitempty"`
APIKeys []APIKey `json:"apiKeys,omitempty"`
Permissions []Permission `json:"permissions,omitempty"`
MappedOIDCGroups []OIDCMapping `json:"mappedOidcGroups,omitempty"`
}

type APIKey struct {
Expand Down Expand Up @@ -70,6 +71,16 @@ func (ts TeamService) Create(ctx context.Context, team Team) (t Team, err error)
return
}

func (ts TeamService) Update(ctx context.Context, team Team) (t Team, err error) {
req, err := ts.client.newRequest(ctx, http.MethodPost, "/api/v1/team", withBody(team))
if err != nil {
return
}

_, err = ts.client.doRequest(req, &t)
return
}

func (ts TeamService) Delete(ctx context.Context, team Team) (err error) {
req, err := ts.client.newRequest(ctx, http.MethodDelete, "/api/v1/team", withBody(team))
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion vulnerability.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package dtrack
import (
"context"
"fmt"
"github.com/google/uuid"
"net/http"
"strconv"

"github.com/google/uuid"
)

type Vulnerability struct {
Expand Down