Skip to content

Commit 2f6fbc1

Browse files
authored
Merge pull request #88 from ConductorOne/lauren/remove-unnecessary-requests
remove unecessary requests
2 parents c6b1736 + 70172ef commit 2f6fbc1

File tree

5 files changed

+177
-72
lines changed

5 files changed

+177
-72
lines changed

pkg/client/projects.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ func (c *Client) GetProject(ctx context.Context, projectID string) (*jira.Projec
2525
return project, nil
2626
}
2727

28-
func (c *Client) ListProjects(ctx context.Context) ([]*jira.Project, error) {
28+
func (c *Client) ListProjectsWithDetails(ctx context.Context) ([]*jira.Project, error) {
2929
l := ctxzap.Extract(ctx)
3030

3131
projects, _, err := c.client.Project.GetAll(ctx, nil)
3232
if err != nil {
33-
l.Error("Error getting projects", zap.Error(err))
33+
l.Error("Error getting projects with details", zap.Error(err))
3434
return nil, err
3535
}
3636

@@ -46,6 +46,29 @@ func (c *Client) ListProjects(ctx context.Context) ([]*jira.Project, error) {
4646
return ret, nil
4747
}
4848

49+
func (c *Client) ListProjects(ctx context.Context) ([]*jira.Project, error) {
50+
l := ctxzap.Extract(ctx)
51+
52+
projects, _, err := c.client.Project.GetAll(ctx, nil)
53+
if err != nil {
54+
l.Error("Error getting projects", zap.Error(err))
55+
return nil, err
56+
}
57+
58+
var ret []*jira.Project
59+
for _, i := range *projects {
60+
p := &jira.Project{
61+
Self: i.Self,
62+
ID: i.ID,
63+
Key: i.Key,
64+
Name: i.Name,
65+
}
66+
ret = append(ret, p)
67+
}
68+
69+
return ret, nil
70+
}
71+
4972
func (c *Client) ListStatuses(ctx context.Context) ([]*jira.Status, error) {
5073
l := ctxzap.Extract(ctx)
5174

pkg/connector/group.go

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
sdkResource "github.com/conductorone/baton-sdk/pkg/types/resource"
1414
"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
1515
"go.uber.org/zap"
16+
"google.golang.org/protobuf/types/known/structpb"
1617

1718
"github.com/conductorone/baton-jira-datacenter/pkg/client"
1819
)
@@ -27,12 +28,47 @@ func (g *groupBuilder) ResourceType(ctx context.Context) *v2.ResourceType {
2728
return groupResourceType
2829
}
2930

31+
func extractGroupLabelRolesFromGroupProfile(profile *structpb.Struct) []client.Labels {
32+
if profile == nil {
33+
return nil
34+
}
35+
field, ok := profile.Fields["group_label_roles"]
36+
if !ok || field.GetListValue() == nil {
37+
return nil
38+
}
39+
var actors []client.Labels
40+
for _, val := range field.GetListValue().Values {
41+
m := val.GetStructValue()
42+
if m == nil {
43+
continue
44+
}
45+
gl := client.Labels{
46+
Text: m.Fields["text"].GetStringValue(),
47+
Type: m.Fields["type"].GetStringValue(),
48+
Title: m.Fields["title"].GetStringValue(),
49+
}
50+
actors = append(actors, gl)
51+
}
52+
return actors
53+
}
54+
3055
func groupResource(ctx context.Context, group client.Group, parentResourceID *v2.ResourceId) (*v2.Resource, error) {
3156
// jira groups does not include ids only names
3257
profile := map[string]interface{}{
3358
"group_name": group.Name,
3459
"group_id": group.Name,
3560
}
61+
62+
var groupLabelRoles []any
63+
for _, gl := range group.Labels {
64+
groupLabelRoles = append(groupLabelRoles, map[string]any{
65+
"text": gl.Text,
66+
"title": gl.Title,
67+
"type": gl.Type,
68+
})
69+
}
70+
profile["group_label_roles"] = groupLabelRoles
71+
3672
groupTraitOptions := []sdkResource.GroupTraitOption{sdkResource.WithGroupProfile(profile)}
3773
resource, err := sdkResource.NewGroupResource(
3874
group.Name,
@@ -71,9 +107,19 @@ func (g *groupBuilder) List(ctx context.Context, parentResourceID *v2.ResourceId
71107

72108
func (g *groupBuilder) Entitlements(ctx context.Context, resource *v2.Resource, _ *pagination.Token) ([]*v2.Entitlement, string, annotations.Annotations, error) {
73109
groupId := resource.Id.Resource
74-
groupRoles, err := g.client.GetGroupLabelRoles(ctx, groupId)
110+
111+
groupTrait, err := sdkResource.GetGroupTrait(resource)
75112
if err != nil {
76-
return nil, "", nil, err
113+
return nil, "", nil, fmt.Errorf("list-entitlements: Failed to get group trait from group: %w", err)
114+
}
115+
groupProfile := groupTrait.GetProfile()
116+
117+
groupRoles := extractGroupLabelRolesFromGroupProfile(groupProfile)
118+
if groupRoles == nil {
119+
groupRoles, err = g.client.GetGroupLabelRoles(ctx, groupId)
120+
if err != nil {
121+
return nil, "", nil, err
122+
}
77123
}
78124

79125
rv := make([]*v2.Entitlement, 0, len(groupRoles)+1)
@@ -113,9 +159,18 @@ func (g *groupBuilder) Grants(ctx context.Context, resource *v2.Resource, pToken
113159

114160
l := ctxzap.Extract(ctx)
115161

116-
roles, err := g.client.GetGroupLabelRoles(ctx, groupId)
162+
groupTrait, err := sdkResource.GetGroupTrait(resource)
117163
if err != nil {
118-
return nil, "", nil, err
164+
return nil, "", nil, fmt.Errorf("list-grants: failed to get group trait from group: %w", err)
165+
}
166+
groupProfile := groupTrait.GetProfile()
167+
168+
roles := extractGroupLabelRolesFromGroupProfile(groupProfile)
169+
if roles == nil {
170+
roles, err = g.client.GetGroupLabelRoles(ctx, groupId)
171+
if err != nil {
172+
return nil, "", nil, err
173+
}
119174
}
120175

121176
for _, member := range groupMembers {

pkg/connector/permission.go

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -80,30 +80,19 @@ func (r *permissionBuilder) List(ctx context.Context, parentResourceID *v2.Resou
8080
func (r *permissionBuilder) Entitlements(ctx context.Context, resource *v2.Resource, _ *pagination.Token) ([]*v2.Entitlement, string, annotations.Annotations, error) {
8181
var rv []*v2.Entitlement
8282
permissionId := resource.DisplayName
83-
permissions, err := r.client.ListAllPermissionScheme(ctx)
84-
if err != nil {
85-
return nil, "", nil, err
86-
}
8783

88-
for _, permission := range permissions.Permissions {
89-
if permissionId != permission.Permission {
90-
continue
91-
}
92-
93-
// create entitlements for each project role
94-
permissionOptions := []ent.EntitlementOption{
95-
ent.WithGrantableTo(userResourceType, groupResourceType),
96-
ent.WithDisplayName(fmt.Sprintf("%s Permission %s", resource.DisplayName, permission.Permission)),
97-
ent.WithDescription(fmt.Sprintf("%s access to %s permission in Jira DC", titleCase(permission.Permission), resource.DisplayName)),
98-
}
99-
100-
rv = append(rv, ent.NewPermissionEntitlement(
101-
resource,
102-
permission.Permission,
103-
permissionOptions...,
104-
))
84+
permissionOptions := []ent.EntitlementOption{
85+
ent.WithGrantableTo(userResourceType, groupResourceType),
86+
ent.WithDisplayName(fmt.Sprintf("%s Permission %s", resource.DisplayName, permissionId)),
87+
ent.WithDescription(fmt.Sprintf("%s access to %s permission in Jira DC", titleCase(permissionId), permissionId)),
10588
}
10689

90+
rv = append(rv, ent.NewPermissionEntitlement(
91+
resource,
92+
permissionId,
93+
permissionOptions...,
94+
))
95+
10796
return rv, "", nil, nil
10897
}
10998

pkg/connector/role.go

Lines changed: 82 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"errors"
66
"fmt"
77
"net/http"
8-
"strconv"
98

109
v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2"
1110
"github.com/conductorone/baton-sdk/pkg/annotations"
@@ -16,6 +15,7 @@ import (
1615
"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
1716
"go.uber.org/zap"
1817
"golang.org/x/exp/slices"
18+
"google.golang.org/protobuf/types/known/structpb"
1919

2020
"github.com/conductorone/baton-jira-datacenter/pkg/client"
2121
)
@@ -26,23 +26,60 @@ type roleBuilder struct {
2626

2727
const NF = -1
2828

29+
func extractActorsFromRoleProfile(profile *structpb.Struct) []client.Actors {
30+
if profile == nil {
31+
return nil
32+
}
33+
field, ok := profile.Fields["actors"]
34+
if !ok || field.GetListValue() == nil {
35+
return nil
36+
}
37+
var actors []client.Actors
38+
for _, val := range field.GetListValue().Values {
39+
m := val.GetStructValue()
40+
if m == nil {
41+
continue
42+
}
43+
44+
a := client.Actors{
45+
ID: int(m.Fields["id"].GetNumberValue()),
46+
DisplayName: m.Fields["displayName"].GetStringValue(),
47+
Type: m.Fields["type"].GetStringValue(),
48+
Name: m.Fields["name"].GetStringValue(),
49+
}
50+
actors = append(actors, a)
51+
}
52+
return actors
53+
}
54+
2955
// Create a new connector resource for a jira role.
3056
func roleResource(ctx context.Context, role client.RolesAPIData, parentResourceID *v2.ResourceId) (*v2.Resource, error) {
31-
profile := map[string]interface{}{
57+
profile := map[string]any{
3258
"role_id": role.ID,
3359
"role_name": role.Name,
3460
"role_Description": role.Description,
3561
}
3662

37-
groupTraitOptions := []sdkResource.GroupTraitOption{
63+
var actorsList []any
64+
for _, a := range role.Actors {
65+
actorsList = append(actorsList, map[string]any{
66+
"id": a.ID,
67+
"displayName": a.DisplayName,
68+
"type": a.Type,
69+
"name": a.Name,
70+
})
71+
}
72+
profile["actors"] = actorsList
73+
74+
roleTraitOptions := []sdkResource.GroupTraitOption{
3875
sdkResource.WithGroupProfile(profile),
3976
}
4077

4178
ret, err := sdkResource.NewGroupResource(
4279
role.Name,
4380
roleResourceType,
4481
role.ID,
45-
groupTraitOptions,
82+
roleTraitOptions,
4683
sdkResource.WithParentResourceID(parentResourceID),
4784
)
4885
if err != nil {
@@ -61,6 +98,7 @@ func (r *roleBuilder) ResourceType(ctx context.Context) *v2.ResourceType {
6198
func (r *roleBuilder) List(ctx context.Context, parentResourceID *v2.ResourceId, pToken *pagination.Token) ([]*v2.Resource, string, annotations.Annotations, error) {
6299
var ret []*v2.Resource
63100
roles, err := r.client.ListAllRoles(ctx)
101+
64102
if err != nil {
65103
return nil, "", nil, err
66104
}
@@ -105,57 +143,57 @@ func (r *roleBuilder) Entitlements(ctx context.Context, resource *v2.Resource, _
105143

106144
func (r *roleBuilder) Grants(ctx context.Context, resource *v2.Resource, pToken *pagination.Token) ([]*v2.Grant, string, annotations.Annotations, error) {
107145
var rv []*v2.Grant
108-
// List roles in general
109-
roles, err := r.client.ListAllRoles(ctx)
110-
if err != nil {
111-
return nil, "", nil, err
112-
}
113146

114-
roleId, err := strconv.Atoi(resource.Id.Resource)
147+
l := ctxzap.Extract(ctx)
148+
149+
roleTrait, err := sdkResource.GetGroupTrait(resource)
115150
if err != nil {
116-
return nil, "", nil, err
151+
return nil, "", nil, fmt.Errorf("list-grants: failed to get group trait from role: %w", err)
117152
}
153+
roleProfile := roleTrait.GetProfile()
118154

119-
l := ctxzap.Extract(ctx)
155+
roleActors := extractActorsFromRoleProfile(roleProfile)
120156

121-
for _, role := range roles {
122-
if roleId != role.ID {
123-
continue
157+
if roleActors == nil {
158+
role, err := r.client.GetRole(ctx, resource.Id.Resource)
159+
if err != nil {
160+
return nil, "", nil, err
124161
}
125-
// An actor can be (users or groups)
126-
for _, actor := range role.Actors {
127-
switch actor.Type {
128-
case userRole:
129-
user, err := r.client.GetUser(ctx, actor.Name)
130-
if err != nil {
131-
if errors.Is(err, client.ErrUserNotFound) {
132-
l.Warn("User not found", zap.String("userId", actor.Name))
133-
continue
134-
}
135-
return nil, "", nil, err
136-
}
162+
roleActors = role.Actors
163+
}
137164

138-
ur, err := userResource(user)
139-
if err != nil {
140-
return nil, "", nil, err
165+
for _, actor := range roleActors {
166+
switch actor.Type {
167+
case userRole:
168+
user, err := r.client.GetUser(ctx, actor.Name)
169+
if err != nil {
170+
if errors.Is(err, client.ErrUserNotFound) {
171+
l.Warn("User not found", zap.String("userId", actor.Name))
172+
continue
141173
}
174+
return nil, "", nil, err
175+
}
142176

143-
membershipGrant := grant.NewGrant(resource, role.Name, ur.Id)
144-
rv = append(rv, membershipGrant)
145-
case groupRole:
146-
group := client.Group{
147-
Name: actor.Name,
148-
}
149-
gr, err := groupResource(ctx, group, nil)
150-
if err != nil {
151-
return nil, "", nil, err
152-
}
177+
ur, err := userResource(user)
178+
if err != nil {
179+
return nil, "", nil, err
180+
}
153181

154-
membershipGrant := grant.NewGrant(resource, role.Name, gr.Id)
155-
rv = append(rv, membershipGrant)
156-
default:
157-
return nil, "", nil, fmt.Errorf("jira(dc)-connector: invalid member resource type: %s", actor.Type)
182+
membershipGrant := grant.NewGrant(resource, resource.DisplayName, ur.Id)
183+
rv = append(rv, membershipGrant)
184+
case groupRole:
185+
group := client.Group{
186+
Name: actor.Name,
158187
}
188+
gr, err := groupResource(ctx, group, nil)
189+
if err != nil {
190+
return nil, "", nil, err
191+
}
192+
193+
membershipGrant := grant.NewGrant(resource, resource.DisplayName, gr.Id)
194+
rv = append(rv, membershipGrant)
195+
default:
196+
return nil, "", nil, fmt.Errorf("jira(dc)-connector: invalid member resource type: %s", actor.Type)
159197
}
160198
}
161199

pkg/connector/tickets.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func (d *Connector) ListTicketSchemas(ctx context.Context, pToken *pagination.To
169169
projectKeyMap[str] = true
170170
}
171171

172-
projects, err := d.jiraClient.ListProjects(ctx)
172+
projects, err := d.jiraClient.ListProjectsWithDetails(ctx)
173173
if err != nil {
174174
return nil, "", nil, err
175175
}

0 commit comments

Comments
 (0)