From 4bc859433b0d37251b183202f5c83f1507c0a21d Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Sun, 14 Jun 2020 10:31:20 +0200 Subject: [PATCH] [MM-24604] Migrate get channel and get channel by name endpoints to local mode (#14547) * add unix socket listener for mmctl local mode * First working PoC * Adds the channel list endpoint * Add team list endpoint * Add a LocalClient to the api test helper and start local mode * Add helper to test with both SystemAdmin and Local clients * Add some docs * Adds TestForAllClients test helper * Incorporating @ashishbhate's proposal for adding test names to the helpers * [MM-24146] Add unix socket listener for mmctl local mode (#14296) * add unix socket listener for mmctl local mode * add a constant for local-mode socket path * reflect review comments * Fix init errors after merge * Adds create channel tests * Always init local mode to allow for enabling-disabling it via config * Check the RemoteAddr of the request before marking session as local * [MM-24604] Migrate get channel and get channel by name endpoints to local mode * Remove local test conditional Co-authored-by: Ibrahim Serdar Acikgoz --- api4/api.go | 11 +++++------ api4/channel_local.go | 2 ++ api4/channel_test.go | 28 ++++++++++++++-------------- app/authorization.go | 1 + 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/api4/api.go b/api4/api.go index 5acd00f3da..5ffbef2fe2 100644 --- a/api4/api.go +++ b/api4/api.go @@ -274,6 +274,9 @@ func InitLocal(configservice configservice.ConfigService, globalOptionsFunc app. api.BaseRoutes.UserByUsername = api.BaseRoutes.Users.PathPrefix("/username/{username:[A-Za-z0-9\\_\\-\\.]+}").Subrouter() api.BaseRoutes.UserByEmail = api.BaseRoutes.Users.PathPrefix("/email/{email:.+}").Subrouter() + api.BaseRoutes.Bots = api.BaseRoutes.ApiRoot.PathPrefix("/bots").Subrouter() + api.BaseRoutes.Bot = api.BaseRoutes.ApiRoot.PathPrefix("/bots/{bot_user_id:[A-Za-z0-9]+}").Subrouter() + api.BaseRoutes.Teams = api.BaseRoutes.ApiRoot.PathPrefix("/teams").Subrouter() api.BaseRoutes.Team = api.BaseRoutes.Teams.PathPrefix("/{team_id:[A-Za-z0-9]+}").Subrouter() api.BaseRoutes.TeamByName = api.BaseRoutes.Teams.PathPrefix("/name/{team_name:[A-Za-z0-9_-]+}").Subrouter() @@ -283,14 +286,13 @@ func InitLocal(configservice configservice.ConfigService, globalOptionsFunc app. api.BaseRoutes.Channels = api.BaseRoutes.ApiRoot.PathPrefix("/channels").Subrouter() api.BaseRoutes.Channel = api.BaseRoutes.Channels.PathPrefix("/{channel_id:[A-Za-z0-9]+}").Subrouter() api.BaseRoutes.ChannelByName = api.BaseRoutes.Team.PathPrefix("/channels/name/{channel_name:[A-Za-z0-9_-]+}").Subrouter() - api.BaseRoutes.ChannelsForTeam = api.BaseRoutes.Team.PathPrefix("/channels").Subrouter() + api.BaseRoutes.ChannelByNameForTeamName = api.BaseRoutes.TeamByName.PathPrefix("/channels/name/{channel_name:[A-Za-z0-9_-]+}").Subrouter() + api.BaseRoutes.ChannelsForTeam = api.BaseRoutes.Team.PathPrefix("/channels").Subrouter() api.BaseRoutes.ChannelMembers = api.BaseRoutes.Channel.PathPrefix("/members").Subrouter() api.BaseRoutes.ChannelMember = api.BaseRoutes.ChannelMembers.PathPrefix("/{user_id:[A-Za-z0-9]+}").Subrouter() api.BaseRoutes.ChannelMembersForUser = api.BaseRoutes.User.PathPrefix("/teams/{team_id:[A-Za-z0-9]+}/channels/members").Subrouter() - api.BaseRoutes.License = api.BaseRoutes.ApiRoot.PathPrefix("/license").Subrouter() - api.BaseRoutes.Plugins = api.BaseRoutes.ApiRoot.PathPrefix("/plugins").Subrouter() api.BaseRoutes.Plugin = api.BaseRoutes.Plugins.PathPrefix("/{plugin_id:[A-Za-z0-9\\_\\-\\.]+}").Subrouter() @@ -299,9 +301,6 @@ func InitLocal(configservice configservice.ConfigService, globalOptionsFunc app. api.BaseRoutes.License = api.BaseRoutes.ApiRoot.PathPrefix("/license").Subrouter() - api.BaseRoutes.Bots = api.BaseRoutes.ApiRoot.PathPrefix("/bots").Subrouter() - api.BaseRoutes.Bot = api.BaseRoutes.ApiRoot.PathPrefix("/bots/{bot_user_id:[A-Za-z0-9]+}").Subrouter() - api.BaseRoutes.Groups = api.BaseRoutes.ApiRoot.PathPrefix("/groups").Subrouter() api.BaseRoutes.Posts = api.BaseRoutes.ApiRoot.PathPrefix("/posts").Subrouter() diff --git a/api4/channel_local.go b/api4/channel_local.go index 2b577586dd..13a66b4826 100644 --- a/api4/channel_local.go +++ b/api4/channel_local.go @@ -13,6 +13,8 @@ import ( func (api *API) InitChannelLocal() { api.BaseRoutes.Channels.Handle("", api.ApiLocal(getAllChannels)).Methods("GET") api.BaseRoutes.Channels.Handle("", api.ApiLocal(localCreateChannel)).Methods("POST") + api.BaseRoutes.Channel.Handle("", api.ApiLocal(getChannel)).Methods("GET") + api.BaseRoutes.ChannelByName.Handle("", api.ApiLocal(getChannelByName)).Methods("GET") api.BaseRoutes.Channel.Handle("", api.ApiLocal(deleteChannel)).Methods("DELETE") api.BaseRoutes.ChannelMember.Handle("", api.ApiLocal(localRemoveChannelMember)).Methods("DELETE") diff --git a/api4/channel_test.go b/api4/channel_test.go index 196b69e8f0..9e060c54c8 100644 --- a/api4/channel_test.go +++ b/api4/channel_test.go @@ -655,14 +655,16 @@ func TestGetChannel(t *testing.T) { _, resp = Client.GetChannel(th.BasicChannel.Id, "") CheckForbiddenStatus(t, resp) - _, resp = th.SystemAdminClient.GetChannel(th.BasicChannel.Id, "") - CheckNoError(t, resp) + th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) { + _, resp = client.GetChannel(th.BasicChannel.Id, "") + CheckNoError(t, resp) - _, resp = th.SystemAdminClient.GetChannel(th.BasicPrivateChannel.Id, "") - CheckNoError(t, resp) + _, resp = client.GetChannel(th.BasicPrivateChannel.Id, "") + CheckNoError(t, resp) - _, resp = th.SystemAdminClient.GetChannel(th.BasicUser.Id, "") - CheckNotFoundStatus(t, resp) + _, resp = client.GetChannel(th.BasicUser.Id, "") + CheckNotFoundStatus(t, resp) + }) } func TestGetDeletedChannelsForTeam(t *testing.T) { @@ -1612,8 +1614,10 @@ func TestGetChannelByName(t *testing.T) { _, resp = Client.GetChannelByName(th.BasicChannel.Name, th.BasicTeam.Id, "") CheckForbiddenStatus(t, resp) - _, resp = th.SystemAdminClient.GetChannelByName(th.BasicChannel.Name, th.BasicTeam.Id, "") - CheckNoError(t, resp) + th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) { + _, resp = client.GetChannelByName(th.BasicChannel.Name, th.BasicTeam.Id, "") + CheckNoError(t, resp) + }) } func TestGetChannelByNameForTeamName(t *testing.T) { @@ -1676,12 +1680,8 @@ func TestGetChannelMembers(t *testing.T) { CheckBadRequestStatus(t, resp) _, resp = client.GetChannelMembers("", 0, 60, "") - // NOTE: for some reason, while using the LocalClient, the route /channels//members is not handled - if client == th.LocalClient { - CheckNotFoundStatus(t, resp) - } else { - CheckBadRequestStatus(t, resp) - } + CheckBadRequestStatus(t, resp) + _, resp = client.GetChannelMembers(th.BasicChannel.Id, 0, 60, "") CheckNoError(t, resp) }) diff --git a/app/authorization.go b/app/authorization.go index dc1abef620..56cd02cb8e 100644 --- a/app/authorization.go +++ b/app/authorization.go @@ -47,6 +47,7 @@ func (a *App) SessionHasPermissionToChannel(session model.Session, channelId str if session.IsUnrestricted() { return true } + ids, err := a.Srv().Store.Channel().GetAllChannelMembersForUser(session.UserId, true, true) var channelRoles []string