From 2338515444883931f2e51bd189bf69609c28a66e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Sw=C3=A4rd?= Date: Fri, 10 Feb 2023 15:12:19 +0100 Subject: [PATCH] Make ldap functions package local and remove a superfluous comment. --- services/graph/pkg/identity/backend.go | 5 -- services/graph/pkg/identity/cs3.go | 11 ----- services/graph/pkg/identity/ldap.go | 10 ++-- .../pkg/identity/ldap_education_class.go | 6 +-- services/graph/pkg/identity/ldap_group.go | 8 ++-- services/graph/pkg/identity/mocks/backend.go | 48 ------------------- .../graph/pkg/service/v0/educationclasses.go | 1 - 7 files changed, 12 insertions(+), 77 deletions(-) diff --git a/services/graph/pkg/identity/backend.go b/services/graph/pkg/identity/backend.go index d8a295d214..4d2679773f 100644 --- a/services/graph/pkg/identity/backend.go +++ b/services/graph/pkg/identity/backend.go @@ -6,7 +6,6 @@ import ( "github.com/CiscoM31/godata" cs3 "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" - "github.com/go-ldap/ldap/v3" libregraph "github.com/owncloud/libre-graph-api-go" "github.com/owncloud/ocis/v2/services/graph/pkg/service/v0/errorcode" ) @@ -41,10 +40,6 @@ type Backend interface { AddMembersToGroup(ctx context.Context, groupID string, memberID []string) error // RemoveMemberFromGroup removes a single member (by ID) from a group RemoveMemberFromGroup(ctx context.Context, groupID string, memberID string) error - // RemoveEntryByDNAndAttributeFromEntry creates a request to remove a single member entry by attribute and DN from an ldap entry - RemoveEntryByDNAndAttributeFromEntry(entry *ldap.Entry, dn string, attribute string) (*ldap.ModifyRequest, error) - // ExpandLDAPAttributeEntries reads an attribute from an ldap entry and expands to users - ExpandLDAPAttributeEntries(ctx context.Context, e *ldap.Entry, attribute string) ([]*ldap.Entry, error) } // EducationBackend defines the Interface for an EducationBackend implementation diff --git a/services/graph/pkg/identity/cs3.go b/services/graph/pkg/identity/cs3.go index b6d9f3b9b9..d6eab4029d 100644 --- a/services/graph/pkg/identity/cs3.go +++ b/services/graph/pkg/identity/cs3.go @@ -9,7 +9,6 @@ import ( cs3user "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" cs3rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1" "github.com/cs3org/reva/v2/pkg/rgrpc/todo/pool" - "github.com/go-ldap/ldap/v3" libregraph "github.com/owncloud/libre-graph-api-go" "github.com/owncloud/ocis/v2/ocis-pkg/log" @@ -208,16 +207,6 @@ func (i *CS3) RemoveMemberFromGroup(ctx context.Context, groupID string, memberI return errorcode.New(errorcode.NotSupported, "not implemented") } -// RemoveEntryByDNAndAttributeFromEntry implements the Backend Interface. It's currently not supported for the CS3 backend -func (i *CS3) RemoveEntryByDNAndAttributeFromEntry(entry *ldap.Entry, dn string, attribute string) (*ldap.ModifyRequest, error) { - return nil, errorcode.New(errorcode.NotSupported, "not implemented") -} - -// ExpandLDAPAttributeEntries implements the Backend Interface. It's currently not supported for the CS3 backend -func (i *CS3) ExpandLDAPAttributeEntries(ctx context.Context, e *ldap.Entry, attribute string) ([]*ldap.Entry, error) { - return nil, errorcode.New(errorcode.NotSupported, "not implemented") -} - func createGroupModelFromCS3(g *cs3group.Group) *libregraph.Group { if g.Id == nil { g.Id = &cs3group.GroupId{} diff --git a/services/graph/pkg/identity/ldap.go b/services/graph/pkg/identity/ldap.go index a18d7a55f1..867750e5a7 100644 --- a/services/graph/pkg/identity/ldap.go +++ b/services/graph/pkg/identity/ldap.go @@ -180,7 +180,7 @@ func (i *LDAP) DeleteUser(ctx context.Context, nameOrID string) error { for _, group := range groupEntries { logger.Debug().Str("group", group.DN).Str("user", e.DN).Msg("Cleaning up group membership") - if mr, err := i.RemoveEntryByDNAndAttributeFromEntry(group, e.DN, i.groupAttributeMap.member); err == nil { + if mr, err := i.removeEntryByDNAndAttributeFromEntry(group, e.DN, i.groupAttributeMap.member); err == nil { if err = i.conn.Modify(mr); err != nil { // Errors when deleting the memberships are only logged as warnings but not returned // to the user as we already successfully deleted the users itself @@ -610,8 +610,8 @@ func stringToScope(scope string) (int, error) { return s, nil } -// RemoveEntryByDNAndAttributeFromEntry creates a request to remove a single member entry by attribute and DN from an ldap entry -func (i *LDAP) RemoveEntryByDNAndAttributeFromEntry(entry *ldap.Entry, dn string, attribute string) (*ldap.ModifyRequest, error) { +// removeEntryByDNAndAttributeFromEntry creates a request to remove a single member entry by attribute and DN from an ldap entry +func (i *LDAP) removeEntryByDNAndAttributeFromEntry(entry *ldap.Entry, dn string, attribute string) (*ldap.ModifyRequest, error) { nOldDN, err := ldapdn.ParseNormalize(dn) if err != nil { return nil, err @@ -647,8 +647,8 @@ func (i *LDAP) RemoveEntryByDNAndAttributeFromEntry(entry *ldap.Entry, dn string return &mr, nil } -// ExpandLDAPAttributeEntries reads an attribute from an ldap entry and expands to users -func (i *LDAP) ExpandLDAPAttributeEntries(ctx context.Context, e *ldap.Entry, attribute string) ([]*ldap.Entry, error) { +// expandLDAPAttributeEntries reads an attribute from an ldap entry and expands to users +func (i *LDAP) expandLDAPAttributeEntries(ctx context.Context, e *ldap.Entry, attribute string) ([]*ldap.Entry, error) { logger := i.logger.SubloggerWithRequestID(ctx) logger.Debug().Str("backend", "ldap").Msg("ExpandLDAPAttributeEntries") result := []*ldap.Entry{} diff --git a/services/graph/pkg/identity/ldap_education_class.go b/services/graph/pkg/identity/ldap_education_class.go index 5288de4d18..b2a34a5018 100644 --- a/services/graph/pkg/identity/ldap_education_class.go +++ b/services/graph/pkg/identity/ldap_education_class.go @@ -228,7 +228,7 @@ func (i *LDAP) GetEducationClassMembers(ctx context.Context, id string) ([]*libr return nil, err } - memberEntries, err := i.ExpandLDAPAttributeEntries(ctx, e, i.groupAttributeMap.member) + memberEntries, err := i.expandLDAPAttributeEntries(ctx, e, i.groupAttributeMap.member) result := make([]*libregraph.EducationUser, 0, len(memberEntries)) if err != nil { return nil, err @@ -351,7 +351,7 @@ func (i *LDAP) GetEducationClassTeachers(ctx context.Context, classID string) ([ return nil, err } - teacherEntries, err := i.ExpandLDAPAttributeEntries(ctx, class, i.educationConfig.classAttributeMap.teachers) + teacherEntries, err := i.expandLDAPAttributeEntries(ctx, class, i.educationConfig.classAttributeMap.teachers) result := make([]*libregraph.EducationUser, 0, len(teacherEntries)) if err != nil { return nil, err @@ -445,7 +445,7 @@ func (i *LDAP) RemoveTeacherFromEducationClass(ctx context.Context, classID stri return err } - if mr, err := i.RemoveEntryByDNAndAttributeFromEntry(class, teacher.DN, i.educationConfig.classAttributeMap.teachers); err == nil { + if mr, err := i.removeEntryByDNAndAttributeFromEntry(class, teacher.DN, i.educationConfig.classAttributeMap.teachers); err == nil { return i.conn.Modify(mr) } diff --git a/services/graph/pkg/identity/ldap_group.go b/services/graph/pkg/identity/ldap_group.go index 65e2986617..e82e8d5682 100644 --- a/services/graph/pkg/identity/ldap_group.go +++ b/services/graph/pkg/identity/ldap_group.go @@ -37,7 +37,7 @@ func (i *LDAP) GetGroup(ctx context.Context, nameOrID string, queryParam url.Val return nil, errorcode.New(errorcode.ItemNotFound, "not found") } if slices.Contains(sel, "members") || slices.Contains(exp, "members") { - members, err := i.ExpandLDAPAttributeEntries(ctx, e, i.groupAttributeMap.member) + members, err := i.expandLDAPAttributeEntries(ctx, e, i.groupAttributeMap.member) if err != nil { return nil, err } @@ -115,7 +115,7 @@ func (i *LDAP) GetGroups(ctx context.Context, queryParam url.Values) ([]*libregr continue } if expandMembers { - members, err := i.ExpandLDAPAttributeEntries(ctx, e, i.groupAttributeMap.member) + members, err := i.expandLDAPAttributeEntries(ctx, e, i.groupAttributeMap.member) if err != nil { return nil, err } @@ -142,7 +142,7 @@ func (i *LDAP) GetGroupMembers(ctx context.Context, groupID string) ([]*libregra return nil, err } - memberEntries, err := i.ExpandLDAPAttributeEntries(ctx, e, i.groupAttributeMap.member) + memberEntries, err := i.expandLDAPAttributeEntries(ctx, e, i.groupAttributeMap.member) result := make([]*libregraph.User, 0, len(memberEntries)) if err != nil { return nil, err @@ -279,7 +279,7 @@ func (i *LDAP) RemoveMemberFromGroup(ctx context.Context, groupID string, member } logger.Debug().Str("backend", "ldap").Str("groupdn", ge.DN).Str("member", me.DN).Msg("remove member") - if mr, err := i.RemoveEntryByDNAndAttributeFromEntry(ge, me.DN, i.groupAttributeMap.member); err == nil { + if mr, err := i.removeEntryByDNAndAttributeFromEntry(ge, me.DN, i.groupAttributeMap.member); err == nil { return i.conn.Modify(mr) } return nil diff --git a/services/graph/pkg/identity/mocks/backend.go b/services/graph/pkg/identity/mocks/backend.go index 595a3db85b..9fcdcd8c19 100644 --- a/services/graph/pkg/identity/mocks/backend.go +++ b/services/graph/pkg/identity/mocks/backend.go @@ -7,8 +7,6 @@ import ( godata "github.com/CiscoM31/godata" - ldap "github.com/go-ldap/ldap/v3" - libregraph "github.com/owncloud/libre-graph-api-go" mock "github.com/stretchr/testify/mock" @@ -109,29 +107,6 @@ func (_m *Backend) DeleteUser(ctx context.Context, nameOrID string) error { return r0 } -// ExpandLDAPAttributeEntries provides a mock function with given fields: ctx, e, attribute -func (_m *Backend) ExpandLDAPAttributeEntries(ctx context.Context, e *ldap.Entry, attribute string) ([]*ldap.Entry, error) { - ret := _m.Called(ctx, e, attribute) - - var r0 []*ldap.Entry - if rf, ok := ret.Get(0).(func(context.Context, *ldap.Entry, string) []*ldap.Entry); ok { - r0 = rf(ctx, e, attribute) - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).([]*ldap.Entry) - } - } - - var r1 error - if rf, ok := ret.Get(1).(func(context.Context, *ldap.Entry, string) error); ok { - r1 = rf(ctx, e, attribute) - } else { - r1 = ret.Error(1) - } - - return r0, r1 -} - // GetGroup provides a mock function with given fields: ctx, nameOrID, queryParam func (_m *Backend) GetGroup(ctx context.Context, nameOrID string, queryParam url.Values) (*libregraph.Group, error) { ret := _m.Called(ctx, nameOrID, queryParam) @@ -247,29 +222,6 @@ func (_m *Backend) GetUsers(ctx context.Context, oreq *godata.GoDataRequest) ([] return r0, r1 } -// RemoveEntryByDNAndAttributeFromEntry provides a mock function with given fields: entry, dn, attribute -func (_m *Backend) RemoveEntryByDNAndAttributeFromEntry(entry *ldap.Entry, dn string, attribute string) (*ldap.ModifyRequest, error) { - ret := _m.Called(entry, dn, attribute) - - var r0 *ldap.ModifyRequest - if rf, ok := ret.Get(0).(func(*ldap.Entry, string, string) *ldap.ModifyRequest); ok { - r0 = rf(entry, dn, attribute) - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(*ldap.ModifyRequest) - } - } - - var r1 error - if rf, ok := ret.Get(1).(func(*ldap.Entry, string, string) error); ok { - r1 = rf(entry, dn, attribute) - } else { - r1 = ret.Error(1) - } - - return r0, r1 -} - // RemoveMemberFromGroup provides a mock function with given fields: ctx, groupID, memberID func (_m *Backend) RemoveMemberFromGroup(ctx context.Context, groupID string, memberID string) error { ret := _m.Called(ctx, groupID, memberID) diff --git a/services/graph/pkg/service/v0/educationclasses.go b/services/graph/pkg/service/v0/educationclasses.go index 0591190f37..33abbbaf05 100644 --- a/services/graph/pkg/service/v0/educationclasses.go +++ b/services/graph/pkg/service/v0/educationclasses.go @@ -463,7 +463,6 @@ func (g Graph) GetEducationClassTeachers(w http.ResponseWriter, r *http.Request) logger.Debug().Str("id", classID).Msg("calling get class teachers on backend") teachers, err := g.identityEducationBackend.GetEducationClassTeachers(r.Context(), classID) - // teachers, err := g.identityEducationBackend.GetEducationClassMembers(r.Context(), classID) if err != nil { logger.Debug().Err(err).Msg("could not get class teachers: backend error") var errcode errorcode.Error