Skip to content

Commit

Permalink
Merge pull request #15 from ConductorOne/ggreer/skip-disabled-users
Browse files Browse the repository at this point in the history
Add command line flag to skip syncing disabled users.
  • Loading branch information
ggreer authored Feb 16, 2024
2 parents 4d16ce8 + 2a27cbf commit cf50930
Show file tree
Hide file tree
Showing 16 changed files with 115 additions and 61 deletions.
7 changes: 4 additions & 3 deletions cmd/baton-retool/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import (
type config struct {
cli.BaseConfig `mapstructure:",squash"` // Puts the base config options in the same place as the connector options

ConnectionString string `mapstructure:"connection-string"`
SkipPages bool `mapstructure:"skip-pages"`
SkipResources bool `mapstructure:"skip-resources"`
ConnectionString string `mapstructure:"connection-string"`
SkipPages bool `mapstructure:"skip-pages"`
SkipResources bool `mapstructure:"skip-resources"`
SkipDisabledUsers bool `mapstructure:"skip-disabled-users"`
}

// validateConfig is run after the configuration is loaded, and should return an error if it isn't valid.
Expand Down
3 changes: 2 additions & 1 deletion cmd/baton-retool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func main() {

cmd.PersistentFlags().Bool("skip-pages", false, "Skip syncing pages")
cmd.PersistentFlags().Bool("skip-resources", false, "Skip syncing resources")
cmd.PersistentFlags().Bool("skip-disabled-users", false, "Skip syncing disabled users")

err = cmd.Execute()
if err != nil {
Expand All @@ -47,7 +48,7 @@ func main() {
func getConnector(ctx context.Context, cfg *config) (types.ConnectorServer, error) {
l := ctxzap.Extract(ctx)

cb, err := connector.New(ctx, cfg.ConnectionString, cfg.SkipPages, cfg.SkipResources)
cb, err := connector.New(ctx, cfg.ConnectionString, cfg.SkipPages, cfg.SkipResources, cfg.SkipDisabledUsers)
if err != nil {
l.Error("error creating connector builder", zap.Error(err))
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/conductorone/baton-retool
go 1.20

require (
github.com/conductorone/baton-sdk v0.1.21
github.com/conductorone/baton-sdk v0.1.22
github.com/georgysavva/scany v1.2.1
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0
github.com/jackc/pgx/v4 v4.18.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I
github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ=
github.com/cockroachdb/cockroach-go/v2 v2.2.0 h1:/5znzg5n373N/3ESjHF5SMLxiW4RKB05Ql//KWfeTFs=
github.com/cockroachdb/cockroach-go/v2 v2.2.0/go.mod h1:u3MiKYGupPPjkn3ozknpMUpxPaNLTFWAya419/zv6eI=
github.com/conductorone/baton-sdk v0.1.21 h1:lFNAtBi/XUzhEfNMA/4LnvaVYbWHpy4PZz4kFMZMKDw=
github.com/conductorone/baton-sdk v0.1.21/go.mod h1:1VMycIep+HU8JXef2wenT3ECzx1w3Jr3KDQG+L6Mv30=
github.com/conductorone/baton-sdk v0.1.22 h1:9IX0q8oPVfuHyvXdLbxCRGgqhjp/SqGv8T1RbiM1Tps=
github.com/conductorone/baton-sdk v0.1.22/go.mod h1:1VMycIep+HU8JXef2wenT3ECzx1w3Jr3KDQG+L6Mv30=
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
Expand Down
10 changes: 8 additions & 2 deletions pkg/client/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type GroupMember struct {
UserID *int64 `db:"userId"`
GroupID *int64 `db:"groupId"`
IsAdmin bool `db:"isAdmin"`
Enabled bool `db:"enabled"`
}

func (g *GroupMember) GetUserID() int64 {
Expand Down Expand Up @@ -152,7 +153,7 @@ func (c *Client) GetGroupResourceFolderDefault(ctx context.Context, groupID int6
return &ret, nil
}

func (c *Client) ListGroupMembers(ctx context.Context, groupID int64, pager *Pager) ([]*GroupMember, string, error) {
func (c *Client) ListGroupMembers(ctx context.Context, groupID int64, pager *Pager, skipDisabledUsers bool) ([]*GroupMember, string, error) {
l := ctxzap.Extract(ctx)
l.Debug("listing group members for group", zap.Int64("group_id", groupID))

Expand All @@ -165,7 +166,12 @@ func (c *Client) ListGroupMembers(ctx context.Context, groupID int64, pager *Pag
args = append(args, groupID)

sb := &strings.Builder{}
_, _ = sb.WriteString(`select "id", "userId", "groupId", "isAdmin" from user_groups WHERE "groupId"=$1 ORDER BY "id" `)
if skipDisabledUsers {
_, _ = sb.WriteString(`select user_groups.id as id, "userId", "groupId", "isAdmin", users.enabled from user_groups
INNER JOIN users ON users.id = "user_groups"."userId" WHERE "groupId"=$1 and users.enabled = true ORDER BY "id" `)
} else {
_, _ = sb.WriteString(`select "id", "userId", "groupId", "isAdmin" from user_groups WHERE "groupId"=$1 ORDER BY "id" `)
}

args = append(args, limit+1)
_, _ = sb.WriteString(fmt.Sprintf("LIMIT $%d ", len(args)))
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

const (
MaxPageSize = 100
MaxPageSize = 250
MinPageSize = 10
)

Expand Down
5 changes: 4 additions & 1 deletion pkg/client/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (u *UserModel) GetLastLoggedIn() time.Time {
return time.Time{}
}

func (c *Client) ListUsersForOrg(ctx context.Context, orgID int64, pager *Pager) ([]*UserModel, string, error) {
func (c *Client) ListUsersForOrg(ctx context.Context, orgID int64, pager *Pager, skipDisabledUsers bool) ([]*UserModel, string, error) {
l := ctxzap.Extract(ctx)
l.Debug("listing users for org", zap.Int64("org_id", orgID))

Expand All @@ -78,6 +78,9 @@ func (c *Client) ListUsersForOrg(ctx context.Context, orgID int64, pager *Pager)
sb := &strings.Builder{}
_, _ = sb.WriteString(`SELECT "id", "email", "firstName", "lastName", "profilePhotoUrl", "enabled", "userName", "organizationId", "lastLoggedIn" from users WHERE "organizationId"=$1 `)
args = append(args, orgID)
if skipDisabledUsers {
_, _ = sb.WriteString("AND enabled = true ")
}
_, _ = sb.WriteString(`ORDER BY "id" `)
_, _ = sb.WriteString("LIMIT $2 ")
args = append(args, limit+1)
Expand Down
26 changes: 14 additions & 12 deletions pkg/connector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ func titleCase(s string) string {
}

type ConnectorImpl struct {
client *client.Client
skipPages bool
skipResources bool
client *client.Client
skipPages bool
skipResources bool
skipDisabledUsers bool
}

func (c *ConnectorImpl) Metadata(ctx context.Context) (*v2.ConnectorMetadata, error) {
Expand All @@ -46,31 +47,32 @@ func (c *ConnectorImpl) Asset(ctx context.Context, asset *v2.AssetRef) (string,

func (c *ConnectorImpl) ResourceSyncers(ctx context.Context) []connectorbuilder.ResourceSyncer {
syncers := []connectorbuilder.ResourceSyncer{
newOrgSyncer(ctx, c.client, c.skipPages, c.skipResources),
newUserSyncer(ctx, c.client),
newGroupSyncer(ctx, c.client),
newOrgSyncer(ctx, c.client, c.skipPages, c.skipResources, c.skipDisabledUsers),
newUserSyncer(ctx, c.client, c.skipDisabledUsers),
newGroupSyncer(ctx, c.client, c.skipDisabledUsers),
}

if !c.skipPages {
syncers = append(syncers, newPageSyncer(ctx, c.client))
syncers = append(syncers, newPageSyncer(ctx, c.client, c.skipDisabledUsers))
}

if !c.skipResources {
syncers = append(syncers, newResourceSyncer(ctx, c.client))
syncers = append(syncers, newResourceSyncer(ctx, c.client, c.skipDisabledUsers))
}

return syncers
}

func New(ctx context.Context, dsn string, skipPages bool, skipResources bool) (*ConnectorImpl, error) {
func New(ctx context.Context, dsn string, skipPages bool, skipResources bool, skipDisabledUsers bool) (*ConnectorImpl, error) {
c, err := client.New(ctx, dsn)
if err != nil {
return nil, err
}

return &ConnectorImpl{
client: c,
skipPages: skipPages,
skipResources: skipResources,
client: c,
skipPages: skipPages,
skipResources: skipResources,
skipDisabledUsers: skipDisabledUsers,
}, nil
}
14 changes: 8 additions & 6 deletions pkg/connector/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ func memberEntitlementId(groupID *v2.ResourceId) string {
}

type groupSyncer struct {
resourceType *v2.ResourceType
client *client.Client
resourceType *v2.ResourceType
client *client.Client
skipDisabledUsers bool
}

func (s *groupSyncer) ResourceType(ctx context.Context) *v2.ResourceType {
Expand Down Expand Up @@ -131,7 +132,7 @@ func (s *groupSyncer) Grants(ctx context.Context, resource *v2.Resource, pToken
return nil, "", nil, err
}

members, nextPageToken, err := s.client.ListGroupMembers(ctx, groupID, &client.Pager{Token: pToken.Token, Size: pToken.Size})
members, nextPageToken, err := s.client.ListGroupMembers(ctx, groupID, &client.Pager{Token: pToken.Token, Size: pToken.Size}, s.skipDisabledUsers)
if err != nil {
return nil, "", nil, err
}
Expand Down Expand Up @@ -246,9 +247,10 @@ func (o *groupSyncer) Revoke(ctx context.Context, grant *v2.Grant) (annotations.
return nil, nil
}

func newGroupSyncer(ctx context.Context, c *client.Client) *groupSyncer {
func newGroupSyncer(ctx context.Context, c *client.Client, skipDisabledUsers bool) *groupSyncer {
return &groupSyncer{
resourceType: resourceTypeGroup,
client: c,
resourceType: resourceTypeGroup,
client: c,
skipDisabledUsers: skipDisabledUsers,
}
}
22 changes: 12 additions & 10 deletions pkg/connector/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ var resourceTypeOrg = &v2.ResourceType{
}

type orgSyncer struct {
resourceType *v2.ResourceType
client *client.Client
skipPages bool
skipResources bool
resourceType *v2.ResourceType
client *client.Client
skipPages bool
skipResources bool
skipDisabledUsers bool
}

func (s *orgSyncer) ResourceType(ctx context.Context) *v2.ResourceType {
Expand Down Expand Up @@ -245,7 +246,7 @@ func (s *orgSyncer) Grants(ctx context.Context, resource *v2.Resource, pToken *p
return nil, "", nil, err
}

members, nextPageToken, err := s.client.ListGroupMembers(ctx, g.ID, &client.Pager{Token: bag.PageToken(), Size: pToken.Size})
members, nextPageToken, err := s.client.ListGroupMembers(ctx, g.ID, &client.Pager{Token: bag.PageToken(), Size: pToken.Size}, s.skipDisabledUsers)
if err != nil {
return nil, "", nil, err
}
Expand Down Expand Up @@ -281,11 +282,12 @@ func (s *orgSyncer) Grants(ctx context.Context, resource *v2.Resource, pToken *p
return ret, nextPageToken, nil, nil
}

func newOrgSyncer(ctx context.Context, c *client.Client, skipPages bool, skipResources bool) *orgSyncer {
func newOrgSyncer(ctx context.Context, c *client.Client, skipPages bool, skipResources bool, skipDisabledUsers bool) *orgSyncer {
return &orgSyncer{
resourceType: resourceTypeOrg,
client: c,
skipPages: skipPages,
skipResources: skipResources,
resourceType: resourceTypeOrg,
client: c,
skipPages: skipPages,
skipResources: skipResources,
skipDisabledUsers: skipDisabledUsers,
}
}
14 changes: 8 additions & 6 deletions pkg/connector/pages.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ var resourceTypePage = &v2.ResourceType{
}

type pageSyncer struct {
resourceType *v2.ResourceType
client *client.Client
resourceType *v2.ResourceType
client *client.Client
skipDisabledUsers bool
}

func (s *pageSyncer) ResourceType(ctx context.Context) *v2.ResourceType {
Expand Down Expand Up @@ -163,7 +164,7 @@ func (s *pageSyncer) Grants(ctx context.Context, resource *v2.Resource, pToken *
return nil, "", nil, err
}

members, nextPageToken, err := s.client.ListGroupMembers(ctx, group.ID, &client.Pager{Token: bag.PageToken(), Size: pToken.Size})
members, nextPageToken, err := s.client.ListGroupMembers(ctx, group.ID, &client.Pager{Token: bag.PageToken(), Size: pToken.Size}, s.skipDisabledUsers)
if err != nil {
return nil, "", nil, err
}
Expand Down Expand Up @@ -216,9 +217,10 @@ func (s *pageSyncer) Grants(ctx context.Context, resource *v2.Resource, pToken *
return ret, nextPageToken, nil, nil
}

func newPageSyncer(ctx context.Context, c *client.Client) *pageSyncer {
func newPageSyncer(ctx context.Context, c *client.Client, skipDisabledUsers bool) *pageSyncer {
return &pageSyncer{
resourceType: resourceTypePage,
client: c,
resourceType: resourceTypePage,
client: c,
skipDisabledUsers: skipDisabledUsers,
}
}
14 changes: 8 additions & 6 deletions pkg/connector/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ var resourceTypeResource = &v2.ResourceType{
}

type resourceSyncer struct {
resourceType *v2.ResourceType
client *client.Client
resourceType *v2.ResourceType
client *client.Client
skipDisabledUsers bool
}

func (s *resourceSyncer) ResourceType(ctx context.Context) *v2.ResourceType {
Expand Down Expand Up @@ -166,7 +167,7 @@ func (s *resourceSyncer) Grants(ctx context.Context, resource *v2.Resource, pTok
return nil, "", nil, err
}

members, nextPageToken, err := s.client.ListGroupMembers(ctx, group.ID, &client.Pager{Token: bag.PageToken(), Size: pToken.Size})
members, nextPageToken, err := s.client.ListGroupMembers(ctx, group.ID, &client.Pager{Token: bag.PageToken(), Size: pToken.Size}, s.skipDisabledUsers)
if err != nil {
return nil, "", nil, err
}
Expand Down Expand Up @@ -219,9 +220,10 @@ func (s *resourceSyncer) Grants(ctx context.Context, resource *v2.Resource, pTok
return ret, nextPageToken, nil, nil
}

func newResourceSyncer(ctx context.Context, c *client.Client) *resourceSyncer {
func newResourceSyncer(ctx context.Context, c *client.Client, skipDisabledUsers bool) *resourceSyncer {
return &resourceSyncer{
resourceType: resourceTypeResource,
client: c,
resourceType: resourceTypeResource,
client: c,
skipDisabledUsers: skipDisabledUsers,
}
}
14 changes: 8 additions & 6 deletions pkg/connector/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ var resourceTypeUser = &v2.ResourceType{
}

type userSyncer struct {
resourceType *v2.ResourceType
client *client.Client
resourceType *v2.ResourceType
client *client.Client
skipDisabledUsers bool
}

func (s *userSyncer) ResourceType(ctx context.Context) *v2.ResourceType {
Expand All @@ -42,7 +43,7 @@ func (s *userSyncer) List(
return nil, "", nil, err
}

users, nextPageToken, err := s.client.ListUsersForOrg(ctx, orgID, &client.Pager{Token: pToken.Token, Size: pToken.Size})
users, nextPageToken, err := s.client.ListUsersForOrg(ctx, orgID, &client.Pager{Token: pToken.Token, Size: pToken.Size}, s.skipDisabledUsers)
if err != nil {
return nil, "", nil, err
}
Expand Down Expand Up @@ -96,9 +97,10 @@ func (s *userSyncer) Grants(ctx context.Context, resource *v2.Resource, pToken *
return nil, "", nil, nil
}

func newUserSyncer(ctx context.Context, c *client.Client) *userSyncer {
func newUserSyncer(ctx context.Context, c *client.Client, skipDisabledUsers bool) *userSyncer {
return &userSyncer{
resourceType: resourceTypeUser,
client: c,
resourceType: resourceTypeUser,
client: c,
skipDisabledUsers: skipDisabledUsers,
}
}
Loading

0 comments on commit cf50930

Please sign in to comment.