diff --git a/cmd/config.go b/cmd/config.go index 1d64c35..f7c13fb 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -7,7 +7,7 @@ import ( ) // config defines the external configuration required for the connector to run. -// You can add additional fields here and have them automatically mapped to any additional command line flags +// You can add additional fields here and have them automatically mapped to any additional command line flags. type config struct { cli.BaseConfig `mapstructure:",squash"` // Puts the base config options in the same place as the connector options } diff --git a/go.mod b/go.mod index 8b8320e..da36a8c 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/conductorone/baton-demo go 1.19 require ( - github.com/conductorone/baton-sdk v0.0.7 + github.com/conductorone/baton-sdk v0.0.8 github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 go.uber.org/zap v1.23.0 ) @@ -70,5 +70,3 @@ require ( modernc.org/memory v1.4.0 // indirect modernc.org/sqlite v1.19.2 // indirect ) - -replace github.com/conductorone/baton-sdk => /Users/jirwin/projects/baton-sdk diff --git a/go.sum b/go.sum index 153ad6a..caaabdd 100644 --- a/go.sum +++ b/go.sum @@ -89,6 +89,8 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= +github.com/conductorone/baton-sdk v0.0.8 h1:uZmX91T2BT7eOKOB13b4FrEKhJnKCkh4MM49dgjSLcI= +github.com/conductorone/baton-sdk v0.0.8/go.mod h1:jPdcy08LmTIPzgZcSOo7mviSAG0NUbjavg/1LpCTeOI= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= diff --git a/pkg/client/client.go b/pkg/client/client.go index 37d1a46..0797cd9 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -41,17 +41,26 @@ type Project struct { GroupAssignments []string } -// This is a simple example client. While this client would normally be responsible for communicating with an upstream +// Client is a simple example client. While this client would normally be responsible for communicating with an upstream. // API, for this demo the client is only working with in-memory data. -type Client struct{} +type Client struct { + db *database +} + +func NewClient() *Client { + c := &Client{} + c.db = generateDB() + + return c +} -// ListUsers returns all the users from the database +// ListUsers returns all the users from the database. func (c *Client) ListUsers(ctx context.Context) ([]*User, error) { log := ctxzap.Extract(ctx) - log.Info("listing users", zap.Int("user_count", len(db.Users))) + log.Info("listing users", zap.Int("user_count", len(c.db.Users))) - return db.Users, nil + return c.db.Users, nil } // GetUser returns the user requested if it exists, else returns an error. @@ -70,13 +79,13 @@ func (c *Client) GetUser(ctx context.Context, userID string) (*User, error) { return nil, fmt.Errorf("user not found") } -// ListGroups returns all the groups from the database +// ListGroups returns all the groups from the database. func (c *Client) ListGroups(ctx context.Context) ([]*Group, error) { log := ctxzap.Extract(ctx) - log.Info("listing groups", zap.Int("group_count", len(db.Groups))) + log.Info("listing groups", zap.Int("group_count", len(c.db.Groups))) - return db.Groups, nil + return c.db.Groups, nil } // GetGroup returns the group requested if it exists, else returns an error. @@ -95,13 +104,13 @@ func (c *Client) GetGroup(ctx context.Context, groupID string) (*Group, error) { return nil, fmt.Errorf("group not found") } -// ListRoles returns all the roles from the database +// ListRoles returns all the roles from the database. func (c *Client) ListRoles(ctx context.Context) ([]*Role, error) { log := ctxzap.Extract(ctx) - log.Info("listing roles", zap.Int("role_count", len(db.Roles))) + log.Info("listing roles", zap.Int("role_count", len(c.db.Roles))) - return db.Roles, nil + return c.db.Roles, nil } // GetRole returns the role requested if it exists, else returns an error. @@ -120,13 +129,13 @@ func (c *Client) GetRole(ctx context.Context, roleID string) (*Role, error) { return nil, fmt.Errorf("role not found") } -// ListProjects returns all the projects from the database +// ListProjects returns all the projects from the database. func (c *Client) ListProjects(ctx context.Context) ([]*Project, error) { log := ctxzap.Extract(ctx) - log.Info("listing projects", zap.Int("project_count", len(db.Roles))) + log.Info("listing projects", zap.Int("project_count", len(c.db.Roles))) - return db.Projects, nil + return c.db.Projects, nil } // GetProject returns the project requested if it exists, else returns an error. diff --git a/pkg/client/data.go b/pkg/client/data.go index dc281cf..75bb78d 100644 --- a/pkg/client/data.go +++ b/pkg/client/data.go @@ -7,10 +7,8 @@ type database struct { Projects []*Project } -var db *database - -func init() { - db = &database{} +func generateDB() *database { + db := &database{} db.Users = []*User{ { @@ -105,4 +103,6 @@ func init() { }, }, } + + return db } diff --git a/pkg/connector/connector.go b/pkg/connector/connector.go index 97b427c..d39dd86 100644 --- a/pkg/connector/connector.go +++ b/pkg/connector/connector.go @@ -43,10 +43,10 @@ func (d *Demo) Validate(ctx context.Context) (annotations.Annotations, error) { return nil, nil } -// New returns a new instance of the Demo connector +// New returns a new instance of the Demo connector. func New(ctx context.Context) (*Demo, error) { demo := &Demo{ - client: &client.Client{}, + client: client.NewClient(), } return demo, nil diff --git a/pkg/connector/groups.go b/pkg/connector/groups.go index 1a175ca..3c933e0 100644 --- a/pkg/connector/groups.go +++ b/pkg/connector/groups.go @@ -19,8 +19,8 @@ func (o *groupBuilder) ResourceType(ctx context.Context) *v2.ResourceType { return groupResourceType } -// List returns all the groups from the database as resource objects -// Groups include the GroupTrait because they have the 'shape' of the well known Group type +// List returns all the groups from the database as resource objects. +// Groups include the GroupTrait because they have the 'shape' of the well known Group type. func (o *groupBuilder) List(ctx context.Context, parentResourceID *v2.ResourceId, pToken *pagination.Token) ([]*v2.Resource, string, annotations.Annotations, error) { groups, err := o.client.ListGroups(ctx) if err != nil { @@ -56,7 +56,7 @@ func (o *groupBuilder) List(ctx context.Context, parentResourceID *v2.ResourceId return ret, "", nil, nil } -// Entitlements returns a membership and admin entitlement +// Entitlements returns a membership and admin entitlement. func (o *groupBuilder) Entitlements(ctx context.Context, resource *v2.Resource, _ *pagination.Token) ([]*v2.Entitlement, string, annotations.Annotations, error) { var ret []*v2.Entitlement @@ -83,7 +83,7 @@ func (o *groupBuilder) Entitlements(ctx context.Context, resource *v2.Resource, return ret, "", nil, nil } -// Grants returns grant information for group administrators and members +// Grants returns grant information for group administrators and members. func (o *groupBuilder) Grants(ctx context.Context, resource *v2.Resource, pToken *pagination.Token) ([]*v2.Grant, string, annotations.Annotations, error) { grp, err := o.client.GetGroup(ctx, resource.Id.Resource) if err != nil { diff --git a/pkg/connector/projects.go b/pkg/connector/projects.go index 20afcc2..b7133de 100644 --- a/pkg/connector/projects.go +++ b/pkg/connector/projects.go @@ -162,7 +162,6 @@ func (o *projectBuilder) Grants(ctx context.Context, resource *v2.Resource, pTok Principal: memberPrincipal, }) } - } return ret, "", nil, nil diff --git a/pkg/connector/resource_types.go b/pkg/connector/resource_types.go index 7a887d7..2309d89 100644 --- a/pkg/connector/resource_types.go +++ b/pkg/connector/resource_types.go @@ -4,21 +4,21 @@ import ( v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2" ) -// The user resource type is for all user objects from the database +// The user resource type is for all user objects from the database. var userResourceType = &v2.ResourceType{ Id: "user", DisplayName: "User", Traits: []v2.ResourceType_Trait{v2.ResourceType_TRAIT_USER}, } -// The group resource type is for all group objects from the database +// The group resource type is for all group objects from the database. var groupResourceType = &v2.ResourceType{ Id: "group", DisplayName: "Group", Traits: []v2.ResourceType_Trait{v2.ResourceType_TRAIT_GROUP}, } -// The role resource type is for all role objects from the database +// The role resource type is for all role objects from the database. var roleResourceType = &v2.ResourceType{ Id: "role", DisplayName: "Role", diff --git a/pkg/connector/roles.go b/pkg/connector/roles.go index e0851fa..05aa0d5 100644 --- a/pkg/connector/roles.go +++ b/pkg/connector/roles.go @@ -20,7 +20,7 @@ func (o *roleBuilder) ResourceType(ctx context.Context) *v2.ResourceType { } // List returns all the roles from the database as resource objects -// Roles include the role trait because they have the 'shape' of the well known Role type +// Roles include the role trait because they have the 'shape' of the well known Role type. func (o *roleBuilder) List(ctx context.Context, parentResourceID *v2.ResourceId, pToken *pagination.Token) ([]*v2.Resource, string, annotations.Annotations, error) { roles, err := o.client.ListRoles(ctx) if err != nil { @@ -52,7 +52,7 @@ func (o *roleBuilder) List(ctx context.Context, parentResourceID *v2.ResourceId, return ret, "", nil, nil } -// Entitlements returns an assignment entitlement +// Entitlements returns an assignment entitlement. func (o *roleBuilder) Entitlements(_ context.Context, resource *v2.Resource, _ *pagination.Token) ([]*v2.Entitlement, string, annotations.Annotations, error) { var ret []*v2.Entitlement @@ -158,7 +158,6 @@ func (o *roleBuilder) Grants(ctx context.Context, resource *v2.Resource, pToken Principal: memberPrincipal, }) } - } return ret, "", nil, nil diff --git a/pkg/connector/users.go b/pkg/connector/users.go index 6f8f105..f850aa0 100644 --- a/pkg/connector/users.go +++ b/pkg/connector/users.go @@ -19,7 +19,7 @@ func (o *userBuilder) ResourceType(ctx context.Context) *v2.ResourceType { } // List returns all the users from the database as resource objects. -// Users include a UserTrait because they are the 'shape' of a standard user +// Users include a UserTrait because they are the 'shape' of a standard user. func (o *userBuilder) List(ctx context.Context, parentResourceID *v2.ResourceId, pToken *pagination.Token) ([]*v2.Resource, string, annotations.Annotations, error) { users, err := o.client.ListUsers(ctx) if err != nil { @@ -56,7 +56,7 @@ func (o *userBuilder) Entitlements(_ context.Context, resource *v2.Resource, _ * return nil, "", nil, nil } -// Grants always returns an empty slice for users since they don't have any entitlements +// Grants always returns an empty slice for users since they don't have any entitlements. func (o *userBuilder) Grants(ctx context.Context, resource *v2.Resource, pToken *pagination.Token) ([]*v2.Grant, string, annotations.Annotations, error) { return nil, "", nil, nil } diff --git a/vendor/github.com/conductorone/baton-sdk/internal/dotc1z/resources.go b/vendor/github.com/conductorone/baton-sdk/internal/dotc1z/resources.go index 989d181..bce5af0 100644 --- a/vendor/github.com/conductorone/baton-sdk/internal/dotc1z/resources.go +++ b/vendor/github.com/conductorone/baton-sdk/internal/dotc1z/resources.go @@ -102,10 +102,6 @@ func (c *C1File) PutResource(ctx context.Context, resource *v2.Resource) error { zap.String("resource_type_id", resource.Id.ResourceType), ) - if resource.Id == nil || resource.Id.ResourceType == "" || resource.Id.Resource == "" { - return fmt.Errorf("resources must have a valid ID set") - } - updateRecord := goqu.Record{ "resource_type_id": resource.Id.ResourceType, "external_id": resource.Id.Resource, diff --git a/vendor/modules.txt b/vendor/modules.txt index beacfa7..d5242d9 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -117,7 +117,7 @@ github.com/aws/smithy-go/time github.com/aws/smithy-go/transport/http github.com/aws/smithy-go/transport/http/internal/io github.com/aws/smithy-go/waiter -# github.com/conductorone/baton-sdk v0.0.7 => /Users/jirwin/projects/baton-sdk +# github.com/conductorone/baton-sdk v0.0.8 ## explicit; go 1.19 github.com/conductorone/baton-sdk/internal/connector github.com/conductorone/baton-sdk/internal/dotc1z @@ -438,4 +438,3 @@ modernc.org/memory # modernc.org/sqlite v1.19.2 ## explicit; go 1.17 modernc.org/sqlite/lib -# github.com/conductorone/baton-sdk => /Users/jirwin/projects/baton-sdk