Skip to content

Commit 2570042

Browse files
authored
feat: implement additional interfaces for team, permission oidc (#22)
Signed-off-by: bakito <[email protected]>
1 parent 4729b5e commit 2570042

File tree

8 files changed

+254
-9
lines changed

8 files changed

+254
-9
lines changed

client.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ type Client struct {
4040
Finding FindingService
4141
License LicenseService
4242
Metrics MetricsService
43+
OIDC OIDCService
4344
Permission PermissionService
4445
Policy PolicyService
4546
PolicyCondition PolicyConditionService
@@ -86,6 +87,7 @@ func NewClient(baseURL string, options ...ClientOption) (*Client, error) {
8687
client.Finding = FindingService{client: &client}
8788
client.License = LicenseService{client: &client}
8889
client.Metrics = MetricsService{client: &client}
90+
client.OIDC = OIDCService{client: &client}
8991
client.Permission = PermissionService{client: &client}
9092
client.Policy = PolicyService{client: &client}
9193
client.PolicyCondition = PolicyConditionService{client: &client}
@@ -251,6 +253,13 @@ func withPageOptions(po PageOptions) requestOption {
251253
}
252254
}
253255

256+
func withAcceptContentType(contentType string) requestOption {
257+
return func(req *http.Request) error {
258+
req.Header.Set("Accept", contentType)
259+
return nil
260+
}
261+
}
262+
254263
func (c Client) doRequest(req *http.Request, v interface{}) (a apiResponse, err error) {
255264
if c.debug {
256265
reqDump, _ := httputil.DumpRequestOut(req, true)

finding_example_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ package dtrack_test
33
import (
44
"context"
55

6-
"github.com/google/uuid"
7-
86
"github.com/DependencyTrack/client-go"
7+
"github.com/google/uuid"
98
)
109

1110
// This example demonstrates how to fetch all findings for a given project.

oidc.go

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package dtrack
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
"strconv"
8+
9+
"github.com/google/uuid"
10+
)
11+
12+
type OIDCService struct {
13+
client *Client
14+
}
15+
16+
type OIDCGroup struct {
17+
Name string `json:"name,omitempty"`
18+
UUID uuid.UUID `json:"uuid,omitempty"`
19+
}
20+
21+
type OIDCMappingRequest struct {
22+
Team uuid.UUID `json:"team"`
23+
Group uuid.UUID `json:"group"`
24+
}
25+
26+
type OIDCMapping struct {
27+
Group OIDCGroup `json:"group"`
28+
UUID uuid.UUID `json:"uuid"`
29+
}
30+
31+
func (s OIDCService) Available(ctx context.Context) (available bool, err error) {
32+
req, err := s.client.newRequest(ctx, http.MethodGet, "/api/v1/oidc/available", withAcceptContentType("text/plain"))
33+
if err != nil {
34+
return
35+
}
36+
37+
var value string
38+
39+
_, err = s.client.doRequest(req, &value)
40+
if err != nil {
41+
return
42+
}
43+
available, err = strconv.ParseBool(value)
44+
return
45+
}
46+
47+
func (s OIDCService) GetAllGroups(ctx context.Context, po PageOptions) (p Page[OIDCGroup], err error) {
48+
req, err := s.client.newRequest(ctx, http.MethodGet, "/api/v1/oidc/group", withPageOptions(po))
49+
if err != nil {
50+
return
51+
}
52+
53+
res, err := s.client.doRequest(req, &p.Items)
54+
if err != nil {
55+
return
56+
}
57+
58+
p.TotalCount = res.TotalCount
59+
return
60+
}
61+
62+
func (s OIDCService) CreateGroup(ctx context.Context, name string) (g OIDCGroup, err error) {
63+
req, err := s.client.newRequest(ctx, http.MethodPut, "/api/v1/oidc/group", withBody(OIDCGroup{Name: name}))
64+
if err != nil {
65+
return
66+
}
67+
68+
_, err = s.client.doRequest(req, &g)
69+
return
70+
}
71+
func (s OIDCService) UpdateGroup(ctx context.Context, group OIDCGroup) (g OIDCGroup, err error) {
72+
req, err := s.client.newRequest(ctx, http.MethodPost, "/api/v1/oidc/group", withBody(group))
73+
if err != nil {
74+
return
75+
}
76+
77+
_, err = s.client.doRequest(req, &g)
78+
return
79+
}
80+
81+
func (s OIDCService) DeleteGroup(ctx context.Context, groupUUID uuid.UUID) (err error) {
82+
req, err := s.client.newRequest(ctx, http.MethodDelete, fmt.Sprintf("/api/v1/oidc/group/%s", groupUUID.String()))
83+
if err != nil {
84+
return
85+
}
86+
87+
_, err = s.client.doRequest(req, nil)
88+
return
89+
}
90+
91+
func (s OIDCService) GetAllTeamsOf(ctx context.Context, group OIDCGroup, po PageOptions) (p Page[Team], err error) {
92+
req, err := s.client.newRequest(ctx, http.MethodGet, fmt.Sprintf("/api/v1/oidc/group/%s/team", group.UUID.String()), withPageOptions(po))
93+
if err != nil {
94+
return
95+
}
96+
97+
res, err := s.client.doRequest(req, &p.Items)
98+
if err != nil {
99+
return
100+
}
101+
102+
p.TotalCount = res.TotalCount
103+
return
104+
}
105+
106+
func (s OIDCService) AddTeamMapping(ctx context.Context, mapping OIDCMappingRequest) (m OIDCMapping, err error) {
107+
req, err := s.client.newRequest(ctx, http.MethodPut, "/api/v1/oidc/mapping", withBody(mapping))
108+
if err != nil {
109+
return
110+
}
111+
112+
_, err = s.client.doRequest(req, &m)
113+
return
114+
}
115+
116+
func (s OIDCService) RemoveTeamMapping(ctx context.Context, mappingID uuid.UUID) (err error) {
117+
req, err := s.client.newRequest(ctx, http.MethodDelete, fmt.Sprintf("/api/v1/oidc/mapping/%s", mappingID.String()))
118+
if err != nil {
119+
return
120+
}
121+
122+
_, err = s.client.doRequest(req, nil)
123+
return
124+
}

permission.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ package dtrack
33
import (
44
"context"
55
"fmt"
6-
"github.com/google/uuid"
76
"net/http"
7+
8+
"github.com/google/uuid"
89
)
910

1011
type PermissionService struct {
@@ -40,3 +41,12 @@ func (ps PermissionService) AddPermissionToTeam(ctx context.Context, permission
4041
_, err = ps.client.doRequest(req, &t)
4142
return
4243
}
44+
func (ps PermissionService) RemovePermissionFromTeam(ctx context.Context, permission Permission, team uuid.UUID) (t Team, err error) {
45+
req, err := ps.client.newRequest(ctx, http.MethodDelete, fmt.Sprintf("/api/v1/permission/%s/team/%s", permission.Name, team.String()))
46+
if err != nil {
47+
return
48+
}
49+
50+
_, err = ps.client.doRequest(req, &t)
51+
return
52+
}

project_property.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ package dtrack
33
import (
44
"context"
55
"fmt"
6-
"github.com/google/uuid"
76
"net/http"
7+
8+
"github.com/google/uuid"
89
)
910

1011
type ProjectProperty struct {

repository.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,40 @@ package dtrack
22

33
import (
44
"context"
5+
"fmt"
56
"net/http"
7+
8+
"github.com/google/uuid"
9+
)
10+
11+
const (
12+
RepositoryTypeCargo = "CARGO"
13+
RepositoryTypeComposer = "COMPOSER"
14+
RepositoryTypeCpan = "CPAN"
15+
RepositoryTypeGem = "GEM"
16+
RepositoryTypeGoModules = "GO_MODULES"
17+
RepositoryTypeHex = "HEX"
18+
RepositoryTypeMaven = "MAVEN"
19+
RepositoryTypeNpm = "NPM"
20+
RepositoryTypeNuget = "NUGET"
21+
RepositoryTypePypi = "PYPI"
22+
RepositoryTypeUnsupported = "UNSUPPORTED"
623
)
724

25+
type RepositoryType string
26+
27+
type Repository struct {
28+
Type RepositoryType `json:"type"`
29+
Identifier string `json:"identifier"`
30+
Url string `json:"url"`
31+
ResolutionOrder int `json:"resolutionOrder"`
32+
Enabled bool `json:"enabled"`
33+
Internal bool `json:"internal"`
34+
Username string `json:"username,omitempty"`
35+
Password string `json:"password,omitempty"`
36+
UUID uuid.UUID `json:"uuid,omitempty"`
37+
}
38+
839
type RepositoryMetaComponent struct {
940
LatestVersion string `json:"latestVersion"`
1041
}
@@ -26,3 +57,62 @@ func (rs RepositoryService) GetMetaComponent(ctx context.Context, purl string) (
2657
_, err = rs.client.doRequest(req, &r)
2758
return
2859
}
60+
61+
func (rs RepositoryService) GetAll(ctx context.Context, po PageOptions) (p Page[Repository], err error) {
62+
req, err := rs.client.newRequest(ctx, http.MethodGet, "/api/v1/repository", withPageOptions(po))
63+
if err != nil {
64+
return
65+
}
66+
67+
res, err := rs.client.doRequest(req, &p.Items)
68+
if err != nil {
69+
return
70+
}
71+
72+
p.TotalCount = res.TotalCount
73+
return
74+
}
75+
76+
func (rs RepositoryService) GetByType(ctx context.Context, repoType RepositoryType, po PageOptions) (p Page[Repository], err error) {
77+
req, err := rs.client.newRequest(ctx, http.MethodGet, fmt.Sprintf("/api/v1/repository/%s", repoType), withPageOptions(po))
78+
if err != nil {
79+
return
80+
}
81+
82+
res, err := rs.client.doRequest(req, &p.Items)
83+
if err != nil {
84+
return
85+
}
86+
87+
p.TotalCount = res.TotalCount
88+
return
89+
}
90+
91+
func (rs RepositoryService) Create(ctx context.Context, repo Repository) (r Repository, err error) {
92+
req, err := rs.client.newRequest(ctx, http.MethodPut, "/api/v1/repository", withBody(repo))
93+
if err != nil {
94+
return
95+
}
96+
97+
_, err = rs.client.doRequest(req, &r)
98+
return
99+
}
100+
func (rs RepositoryService) Update(ctx context.Context, repo Repository) (r Repository, err error) {
101+
req, err := rs.client.newRequest(ctx, http.MethodPost, "/api/v1/repository", withBody(repo))
102+
if err != nil {
103+
return
104+
}
105+
106+
_, err = rs.client.doRequest(req, &r)
107+
return
108+
}
109+
110+
func (rs RepositoryService) Delete(ctx context.Context, reposUUID uuid.UUID) (err error) {
111+
req, err := rs.client.newRequest(ctx, http.MethodDelete, fmt.Sprintf("/api/v1/repository/%s", reposUUID.String()))
112+
if err != nil {
113+
return
114+
}
115+
116+
_, err = rs.client.doRequest(req, nil)
117+
return
118+
}

team.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ import (
99
)
1010

1111
type Team struct {
12-
UUID uuid.UUID `json:"uuid,omitempty"`
13-
Name string `json:"name,omitempty"`
14-
APIKeys []APIKey `json:"apiKeys,omitempty"`
15-
Permissions []Permission `json:"permissions"`
12+
UUID uuid.UUID `json:"uuid,omitempty"`
13+
Name string `json:"name,omitempty"`
14+
APIKeys []APIKey `json:"apiKeys,omitempty"`
15+
Permissions []Permission `json:"permissions,omitempty"`
16+
MappedOIDCGroups []OIDCMapping `json:"mappedOidcGroups,omitempty"`
1617
}
1718

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

74+
func (ts TeamService) Update(ctx context.Context, team Team) (t Team, err error) {
75+
req, err := ts.client.newRequest(ctx, http.MethodPost, "/api/v1/team", withBody(team))
76+
if err != nil {
77+
return
78+
}
79+
80+
_, err = ts.client.doRequest(req, &t)
81+
return
82+
}
83+
7384
func (ts TeamService) Delete(ctx context.Context, team Team) (err error) {
7485
req, err := ts.client.newRequest(ctx, http.MethodDelete, "/api/v1/team", withBody(team))
7586
if err != nil {

vulnerability.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package dtrack
33
import (
44
"context"
55
"fmt"
6-
"github.com/google/uuid"
76
"net/http"
87
"strconv"
8+
9+
"github.com/google/uuid"
910
)
1011

1112
type Vulnerability struct {

0 commit comments

Comments
 (0)