-
Notifications
You must be signed in to change notification settings - Fork 1
/
revoke.go
53 lines (42 loc) · 1.64 KB
/
revoke.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package c1api
import (
"context"
"errors"
"github.com/conductorone/baton-sdk/pkg/annotations"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
"go.uber.org/zap"
v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2"
v1 "github.com/conductorone/baton-sdk/pb/c1/connectorapi/baton/v1"
"github.com/conductorone/baton-sdk/pkg/tasks"
"github.com/conductorone/baton-sdk/pkg/types"
)
type revokeHelpers interface {
ConnectorClient() types.ConnectorClient
FinishTask(ctx context.Context, annos annotations.Annotations, err error) error
}
type revokeTaskHandler struct {
task *v1.Task
helpers revokeHelpers
}
func (r *revokeTaskHandler) HandleTask(ctx context.Context) error {
l := ctxzap.Extract(ctx).With(zap.String("task_id", r.task.Id), zap.Stringer("task_type", tasks.GetType(r.task)))
if r.task.GetRevoke() == nil || r.task.GetRevoke().GetGrant() == nil {
l.Error("revoke task was nil or missing grant", zap.Any("revoke", r.task.GetRevoke()), zap.Any("grant", r.task.GetRevoke().GetGrant()))
return r.helpers.FinishTask(ctx, nil, errors.Join(errors.New("invalid task type"), ErrTaskNonRetryable))
}
cc := r.helpers.ConnectorClient()
resp, err := cc.Revoke(ctx, &v2.GrantManagerServiceRevokeRequest{
Grant: r.task.GetRevoke().GetGrant(),
})
if err != nil {
l.Error("failed while granting entitlement", zap.Error(err))
return r.helpers.FinishTask(ctx, nil, errors.Join(err, ErrTaskNonRetryable))
}
return r.helpers.FinishTask(ctx, resp.GetAnnotations(), nil)
}
func newRevokeTaskHandler(task *v1.Task, helpers revokeHelpers) tasks.TaskHandler {
return &revokeTaskHandler{
task: task,
helpers: helpers,
}
}