Skip to content

Commit baa5fda

Browse files
authored
Merge pull request #5 from ConductorOne/jirwin/nofail-redundant-provisioning
If a user is already linked to a group, or no longer linked to a group, don't fail provisioning
2 parents e340d22 + baf0abc commit baa5fda

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

.github/workflows/ci.yaml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@ jobs:
5454
# BATON_ACCESS_TOKEN: 'secret_token'
5555
# The following parameters are passed to grant/revoke commands
5656
# Change these to the correct IDs for your test data
57-
CONNECTOR_GRANT: 'grant:entitlement:group:1234:member:user:9876'
58-
CONNECTOR_ENTITLEMENT: 'entitlement:group:1234:member'
59-
CONNECTOR_PRINCIPAL: 'user:9876'
57+
BATON_FORMAL_API_KEY: ${{ secrets.BATON_FORMAL_API_KEY }}
58+
CONNECTOR_GRANT: 'group:group_01hwv4ketae9vsa36rafvkn8mr:member:user:user_01hw3ydazpetds0xzgayvpc2vw'
59+
CONNECTOR_ENTITLEMENT: 'group:group_01hwv4ketae9vsa36rafvkn8mr:member'
60+
CONNECTOR_PRINCIPAL: 'user_01hw3ydazpetds0xzgayvpc2vw'
6061
CONNECTOR_PRINCIPAL_TYPE: 'user'
6162
steps:
6263
- name: Install Go
@@ -97,8 +98,11 @@ jobs:
9798
# Change the grant arguments to the correct IDs for your test data
9899
run: ./baton-formal --grant-entitlement="${{ env.CONNECTOR_ENTITLEMENT }}" --grant-principal="${{ env.CONNECTOR_PRINCIPAL }}" --grant-principal-type="${{ env.CONNECTOR_PRINCIPAL_TYPE }}"
99100

100-
- name: Check grant was re-granted
101+
- name: resync
102+
run: ./baton-formal
101103

104+
- name: Check grant was re-granted
102105
run:
103-
baton grants --entitlement="${{ env.CONNECTOR_ENTITLEMENT }}" --output-format=json | jq --exit-status ".grants[].principal.id.resource == \"${{ env.CONNECTOR_PRINCIPAL }}\""
106+
baton grants --entitlement="${{ env.CONNECTOR_ENTITLEMENT }}" --output-format=json | jq ".grants[].principal.id.resource == \"user_01hw3ydazpetds0xzgayvpc2vw\""
107+
104108

pkg/connector/groups.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package connector
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67

78
corev1 "buf.build/gen/go/formal/core/protocolbuffers/go/core/v1"
@@ -12,6 +13,8 @@ import (
1213
ent "github.com/conductorone/baton-sdk/pkg/types/entitlement"
1314
"github.com/conductorone/baton-sdk/pkg/types/grant"
1415
"github.com/formalco/go-sdk/sdk/v2"
16+
"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
17+
"go.uber.org/zap"
1518
)
1619

1720
type groupBuilder struct {
@@ -118,6 +121,7 @@ func (o *groupBuilder) Grants(ctx context.Context, resource *v2.Resource, pToken
118121
}
119122

120123
func (o *groupBuilder) Grant(ctx context.Context, principal *v2.Resource, entitlement *v2.Entitlement) (annotations.Annotations, error) {
124+
l := ctxzap.Extract(ctx)
121125
if principal.Id.ResourceType != userResourceType.Id {
122126
return nil, fmt.Errorf("only users can have group link granted")
123127
}
@@ -129,6 +133,18 @@ func (o *groupBuilder) Grant(ctx context.Context, principal *v2.Resource, entitl
129133

130134
response, err := o.client.GroupServiceClient.CreateUserGroupLink(ctx, request)
131135
if err != nil {
136+
var connectErr *connect.Error
137+
if errors.As(err, &connectErr) {
138+
if connectErr.Code() == connect.CodeAlreadyExists {
139+
l.Debug(
140+
"group link already exists, returning successfully",
141+
zap.String("principal", principal.Id.Resource),
142+
zap.String("entitlement", entitlement.Resource.Id.Resource),
143+
)
144+
return nil, nil
145+
}
146+
}
147+
132148
return nil, fmt.Errorf("GroupServiceClient.CreateUserGroupLink error: %w", err)
133149
}
134150

@@ -140,6 +156,7 @@ func (o *groupBuilder) Grant(ctx context.Context, principal *v2.Resource, entitl
140156
}
141157

142158
func (o *groupBuilder) Revoke(ctx context.Context, grant *v2.Grant) (annotations.Annotations, error) {
159+
l := ctxzap.Extract(ctx)
143160
if grant.Principal.Id.ResourceType != userResourceType.Id {
144161
return nil, fmt.Errorf("only users can have group link revoked")
145162
}
@@ -189,7 +206,13 @@ func (o *groupBuilder) Revoke(ctx context.Context, grant *v2.Grant) (annotations
189206
if err != nil {
190207
return nil, fmt.Errorf("rateLimitAnnotations error: %w", err)
191208
}
192-
return rateLimit, fmt.Errorf("user is not linked to group")
209+
210+
l.Debug(
211+
"group link not found, returning successfully",
212+
zap.String("principal", grant.Principal.Id.Resource),
213+
zap.String("entitlement", grant.Entitlement.Resource.Id.Resource),
214+
)
215+
return rateLimit, nil
193216
}
194217

195218
func newGroupBuilder(client *sdk.FormalSDK) *groupBuilder {

0 commit comments

Comments
 (0)