Skip to content
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
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ Check out [Baton](https://github.com/conductorone/baton) to learn more the proje
Linear API key.
The API key can be created in Settings -> Account -> API -> Personal API keys.

Workspace role provisioning uses Linear's `userChangeRole` GraphQL mutation.
Use an API key with **Admin** permission, owned by a workspace admin (or owner).
The caller's role must be at least as high as the requested role; granting `owner`
requires an owner's key.

Roles (`guest`, `user`, `admin`, `owner`) are mutually exclusive: granting a role
replaces the current role. Revoking a matching `guest`, `admin`, or `owner` role
sets the role to `user`; **revoking `guest` therefore increases workspace access**.
The baseline `user` role cannot be revoked. The `app` role and app accounts are
not supported for role provisioning. Already-held grants and already-absent
revocations succeed without a mutation. Role changes do not suspend accounts.

See the [Linear schema](https://github.com/linear/linear/blob/master/packages/sdk/src/schema.graphql)
for `userChangeRole` and its permissions.

## brew

```
Expand Down
3 changes: 2 additions & 1 deletion baton_capabilities.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
]
},
"capabilities": [
"CAPABILITY_SYNC"
"CAPABILITY_SYNC",
"CAPABILITY_PROVISION"
],
"permissions": {}
},
Expand Down
20 changes: 19 additions & 1 deletion docs/connector.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,30 @@ sidebarTitle: "Linear"
| Organizations | <Icon icon="square-check" iconType="solid" color="#c937ae"/> | |
| Teams | <Icon icon="square-check" iconType="solid" color="#c937ae"/> | <Icon icon="square-check" iconType="solid" color="#c937ae"/> |
| Projects | <Icon icon="square-check" iconType="solid" color="#c937ae"/> | |
| Roles | <Icon icon="square-check" iconType="solid" color="#c937ae"/> | |
| Roles | <Icon icon="square-check" iconType="solid" color="#c937ae"/> | <Icon icon="square-check" iconType="solid" color="#c937ae"/> |

{/* AUTO-GENERATED:END - capabilities */}

This connector can also be configured to automatically create and update Linear tickets to track manual provisioning assignments. Go to [Configure Linear as an external ticketing provider](/product/admin/external-ticketing#configure-linear-as-an-external-ticketing-provider) to learn more.

### Workspace role provisioning

The connector can grant `guest`, `user`, `admin`, and `owner` workspace roles using
Linear's `userChangeRole` GraphQL mutation. Roles are mutually exclusive:
granting one replaces the user's current workspace role.

Revoking a matching `guest`, `admin`, or `owner` role changes it to `user`.
**Revoking `guest` increases workspace access** because `user` is Linear's
baseline member role. The baseline `user` role cannot be revoked. The `app` role
and app accounts are not supported. Granting an already-held role or revoking an
absent role succeeds without changing the user. This does not suspend accounts.

Use an API key with **Admin** permission, owned by a workspace admin or owner.
The requesting user's role must be equal to or higher than the requested role;
granting `owner` requires an owner's key. See Linear's
[API schema](https://github.com/linear/linear/blob/master/packages/sdk/src/schema.graphql)
for the mutation's permission requirements.

## Gather Linear credentials

Configuring the connector requires you to pass in credentials generated in Linear. Gather these credentials before you move on.
Expand Down
100 changes: 99 additions & 1 deletion pkg/connector/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ import (
"github.com/conductorone/baton-sdk/pkg/pagination"
ent "github.com/conductorone/baton-sdk/pkg/types/entitlement"
resource "github.com/conductorone/baton-sdk/pkg/types/resource"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

var _ connectorbuilder.ResourceSyncer = (*roleResourceType)(nil)
var (
_ connectorbuilder.ResourceSyncer = (*roleResourceType)(nil)
_ connectorbuilder.ResourceProvisioner = (*roleResourceType)(nil)
)

type roleResourceType struct {
resourceType *v2.ResourceType
Expand All @@ -29,6 +34,7 @@ const (
roleUser = "user"
roleAdmin = "admin"
roleOwner = "owner"
roleApp = "app"
)

var roles = []string{
Expand Down Expand Up @@ -87,6 +93,7 @@ func (o *roleResourceType) Entitlements(_ context.Context, resource *v2.Resource
ent.WithGrantableTo(resourceTypeUser),
ent.WithDescription(fmt.Sprintf("%s Linear role", resource.DisplayName)),
ent.WithDisplayName(fmt.Sprintf("%s Role %s", resource.DisplayName, titleCase(membership))),
ent.WithExclusionGroup("linear-workspace-role:" + resource.GetParentResourceId().GetResource()),
}

assignmentEn := ent.NewAssignmentEntitlement(resource, membership, assignmentOptions...)
Expand All @@ -98,6 +105,97 @@ func (o *roleResourceType) Grants(ctx context.Context, resource *v2.Resource, to
return nil, "", nil, nil
}

// Grant replaces the user's single workspace role with the requested role.
func (o *roleResourceType) Grant(ctx context.Context, principal *v2.Resource, entitlement *v2.Entitlement) (annotations.Annotations, error) {
userID, role, err := roleProvisioningTarget(principal, entitlement)
if err != nil {
return nil, err
}
currentRole, rlData, err := o.client.GetUserRole(ctx, userID)
annos := annotations.New()
if rlData != nil {
annos.Append(rlData)
}
if err != nil {
return annos, fmt.Errorf("baton-linear: reading user role: %w", err)
}
if currentRole == roleApp {
return annos, status.Error(codes.Unimplemented, "baton-linear: app accounts cannot have workspace roles provisioned")
}
if currentRole == role {
annos.Append(&v2.GrantAlreadyExists{})
return annos, nil
}
rlData, err = o.client.ChangeUserRole(ctx, userID, role)
if rlData != nil {
annos.Update(rlData)
}
if err != nil {
return annos, fmt.Errorf("baton-linear: granting workspace role: %w", err)
}
return annos, nil
}

// Revoke resets a matching role to user; it never changes a different role.
func (o *roleResourceType) Revoke(ctx context.Context, grant *v2.Grant) (annotations.Annotations, error) {
userID, role, err := roleProvisioningTarget(grant.GetPrincipal(), grant.GetEntitlement())
if err != nil {
return nil, err
}
currentRole, rlData, err := o.client.GetUserRole(ctx, userID)
annos := annotations.New()
if rlData != nil {
annos.Append(rlData)
}
if err != nil {
return annos, fmt.Errorf("baton-linear: reading user role: %w", err)
}
if currentRole == roleApp {
return annos, status.Error(codes.Unimplemented, "baton-linear: app accounts cannot have workspace roles provisioned")
}
if currentRole != role {
annos.Append(&v2.GrantAlreadyRevoked{})
return annos, nil
}
if role == roleUser {
return annos, status.Error(codes.Unimplemented, "baton-linear: the baseline user role cannot be revoked; Linear requires a workspace role")
}
rlData, err = o.client.ChangeUserRole(ctx, userID, roleUser)
if rlData != nil {
annos.Update(rlData)
}
if err != nil {
return annos, fmt.Errorf("baton-linear: revoking workspace role: %w", err)
}
return annos, nil
}

func roleProvisioningTarget(principal *v2.Resource, entitlement *v2.Entitlement) (string, string, error) {
if principal.GetId().GetResourceType() != resourceTypeUser.Id {
return "", "", status.Error(codes.InvalidArgument, "baton-linear: only users can have workspace roles provisioned")
}
userID := principal.GetExternalId().GetId() //nolint:staticcheck // Preserve explicitly supplied native IDs; ResourceId remains the fallback.
if userID == "" {
// Older syncs store the native Linear UUID only in ResourceId.
userID = principal.GetId().GetResource()
}
if userID == "" {
return "", "", status.Error(codes.InvalidArgument, "baton-linear: missing Linear user ID")
}
if entitlement.GetResource().GetId().GetResourceType() != resourceTypeRole.Id || entitlement.GetSlug() != membership {
return "", "", status.Error(codes.InvalidArgument, "baton-linear: expected a workspace role membership entitlement")
}
role := entitlement.GetResource().GetId().GetResource()
switch role {
case roleGuest, roleUser, roleAdmin, roleOwner:
return userID, role, nil
case roleApp:
return "", "", status.Error(codes.Unimplemented, "baton-linear: the app role cannot be granted or revoked")
default:
return "", "", status.Errorf(codes.InvalidArgument, "baton-linear: unsupported workspace role %q", role)
}
}

func roleBuilder(client *linear.Client) *roleResourceType {
return &roleResourceType{
resourceType: resourceTypeRole,
Expand Down
Loading
Loading