New command added to list user limits
This commit is contained in:
		
							parent
							
								
									dbbe62e8a4
								
							
						
					
					
						commit
						128201baa6
					
				|  | @ -0,0 +1,89 @@ | |||
| ## The contents of this file are subject to the Mozilla Public License | ||||
| ## Version 1.1 (the "License"); you may not use this file except in | ||||
| ## compliance with the License. You may obtain a copy of the License | ||||
| ## at https://www.mozilla.org/MPL/ | ||||
| ## | ||||
| ## Software distributed under the License is distributed on an "AS IS" | ||||
| ## basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See | ||||
| ## the License for the specific language governing rights and | ||||
| ## limitations under the License. | ||||
| ## | ||||
| ## The Original Code is RabbitMQ. | ||||
| ## | ||||
| ## The Initial Developer of the Original Code is GoPivotal, Inc. | ||||
| ## Copyright (c) 2007-2020 VMware, Inc. or its affiliates.  All rights reserved. | ||||
| 
 | ||||
| defmodule RabbitMQ.CLI.Ctl.Commands.ListUserLimitsCommand do | ||||
| 
 | ||||
|   @behaviour RabbitMQ.CLI.CommandBehaviour | ||||
| 
 | ||||
|   def scopes(), do: [:ctl, :diagnostics] | ||||
|   def switches(), do: [global: :boolean, user: :string] | ||||
| 
 | ||||
|   def merge_defaults(args, opts) do | ||||
|     {args, Map.merge(%{user: "guest"}, opts)} | ||||
|   end | ||||
| 
 | ||||
|   use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments | ||||
|   use RabbitMQ.CLI.Core.RequiresRabbitAppRunning | ||||
| 
 | ||||
|   def run([], %{node: node_name, global: true}) do | ||||
|     case :rabbit_misc.rpc_call(node_name, :rabbit_auth_backend_internal, :get_user_limits, []) do | ||||
|       [] -> | ||||
|         [] | ||||
| 
 | ||||
|       {:error, err} -> | ||||
|         {:error, err} | ||||
| 
 | ||||
|       {:badrpc, node} -> | ||||
|         {:badrpc, node} | ||||
| 
 | ||||
|       val -> | ||||
|         Enum.map(val, fn {user, val} -> | ||||
|           {:ok, val_encoded} = JSON.encode(Map.new(val)) | ||||
|           [user: user, limits: val_encoded] | ||||
|         end) | ||||
|     end | ||||
|   end | ||||
| 
 | ||||
|   def run([], %{node: node_name, user: username}) do | ||||
|     case :rabbit_misc.rpc_call(node_name, :rabbit_auth_backend_internal, :get_user_limits, [username]) do | ||||
|       :undefined -> | ||||
|         {:error, {:no_such_user, username}} | ||||
| 
 | ||||
|       {:error, err} -> | ||||
|         {:error, err} | ||||
| 
 | ||||
|       {:badrpc, node} -> | ||||
|         {:badrpc, node} | ||||
| 
 | ||||
|       val when is_list(val) or is_map(val) -> | ||||
|         {:ok, val_encoded} = JSON.encode(Map.new(val)) | ||||
|         val_encoded | ||||
|     end | ||||
|   end | ||||
| 
 | ||||
|   use RabbitMQ.CLI.DefaultOutput | ||||
| 
 | ||||
|   def formatter(), do: RabbitMQ.CLI.Formatters.Table | ||||
| 
 | ||||
|   def usage, do: "list_user_limits [--user <username>] [--global]" | ||||
| 
 | ||||
|   def usage_additional() do | ||||
|     [ | ||||
|       ["--global", "list limits for all the users"] | ||||
|     ] | ||||
|   end | ||||
| 
 | ||||
|   def help_section(), do: :user_management | ||||
| 
 | ||||
|   def description(), do: "Displays configured user limits" | ||||
| 
 | ||||
|   def banner([], %{global: true}) do | ||||
|     "Listing limits for all users ..." | ||||
|   end | ||||
| 
 | ||||
|   def banner([], %{user: username}) do | ||||
|     "Listing limits for user \"#{username}\" ..." | ||||
|   end | ||||
| end | ||||
|  | @ -69,7 +69,7 @@ defmodule ClearUserLimitsCommandTest do | |||
|   end | ||||
| 
 | ||||
|   test "run: if limit exists, returns ok and clears it", context do | ||||
|     :ok = set_user_limits(@user, @definition) | ||||
|     :ok = set_user_limits(@user, @channel_definition) | ||||
| 
 | ||||
|     assert get_user_limits(@user) != [] | ||||
| 
 | ||||
|  |  | |||
|  | @ -0,0 +1,113 @@ | |||
| ## The contents of this file are subject to the Mozilla Public License | ||||
| ## Version 1.1 (the "License"); you may not use this file except in | ||||
| ## compliance with the License. You may obtain a copy of the License | ||||
| ## at https://www.mozilla.org/MPL/ | ||||
| ## | ||||
| ## Software distributed under the License is distributed on an "AS IS" | ||||
| ## basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See | ||||
| ## the License for the specific language governing rights and | ||||
| ## limitations under the License. | ||||
| ## | ||||
| ## The Original Code is RabbitMQ. | ||||
| ## | ||||
| ## The Initial Developer of the Original Code is GoPivotal, Inc. | ||||
| ## Copyright (c) 2007-2020 VMware, Inc. or its affiliates.  All rights reserved. | ||||
| 
 | ||||
| 
 | ||||
| defmodule ListUserLimitsCommandTest do | ||||
|   use ExUnit.Case, async: false | ||||
|   import TestHelper | ||||
| 
 | ||||
|   @command RabbitMQ.CLI.Ctl.Commands.ListUserLimitsCommand | ||||
| 
 | ||||
|   @user "guest" | ||||
|   @user1 "test_user1" | ||||
|   @password1 "password1" | ||||
|   @connection_limit_defn "{\"max-connections\":100}" | ||||
|   @channel_limit_defn "{\"max-channels\":1000}" | ||||
| 
 | ||||
|   setup_all do | ||||
|     RabbitMQ.CLI.Core.Distribution.start() | ||||
| 
 | ||||
|     :ok | ||||
|   end | ||||
| 
 | ||||
|   setup context do | ||||
|     user = context[:user] || @user | ||||
| 
 | ||||
|     clear_user_limits(user) | ||||
| 
 | ||||
|     on_exit(context, fn -> | ||||
|       clear_user_limits(user) | ||||
|     end) | ||||
| 
 | ||||
|     { | ||||
|       :ok, | ||||
|       opts: %{ | ||||
|         node: get_rabbit_hostname(), | ||||
|         global: true | ||||
|       }, | ||||
|       user: user | ||||
|     } | ||||
|   end | ||||
| 
 | ||||
|   test "merge_defaults: does not change defined user" do | ||||
|     assert match?({[], %{user: "test_user"}}, @command.merge_defaults([], %{user: "test_user"})) | ||||
|   end | ||||
| 
 | ||||
|   test "validate: providing arguments fails validation" do | ||||
|     assert @command.validate(["many"], %{}) == {:validation_failure, :too_many_args} | ||||
|     assert @command.validate(["too", "many"], %{}) == {:validation_failure, :too_many_args} | ||||
|   end | ||||
| 
 | ||||
|   test "run: a well-formed command returns an empty list if there are no limits", context do | ||||
|     assert @command.run([], context[:opts]) == [] | ||||
|   end | ||||
| 
 | ||||
|   test "run: a well-formed user specific command returns an empty json object if there are no limits", context do | ||||
|     assert @command.run([], %{node: get_rabbit_hostname(), | ||||
|                               user: @user}) == "{}" | ||||
|   end | ||||
| 
 | ||||
|   test "run: list limits for all users", context do | ||||
|     add_user(@user1, @password1) | ||||
|     on_exit(fn() -> | ||||
|       delete_user(@user1) | ||||
|     end) | ||||
|     set_user_limits(@user, @connection_limit_defn) | ||||
|     set_user_limits(@user1, @channel_limit_defn) | ||||
| 
 | ||||
|     assert Enum.sort(@command.run([], context[:opts])) == | ||||
|            Enum.sort([[user: @user,  limits: @connection_limit_defn], | ||||
|                       [user: @user1, limits: @channel_limit_defn]]) | ||||
|   end | ||||
| 
 | ||||
|   test "run: list limits for a single user", context do | ||||
|     user_opts = Map.put(context[:opts], :user, @user) | ||||
|     set_user_limits(@user, @connection_limit_defn) | ||||
| 
 | ||||
|     assert @command.run([], user_opts) == | ||||
|            [[user: @user, limits: @connection_limit_defn]] | ||||
|   end | ||||
| 
 | ||||
|   test "run: an unreachable node throws a badrpc" do | ||||
|     opts = %{node: :jake@thedog, user: "guest", timeout: 200} | ||||
| 
 | ||||
|     assert match?({:badrpc, _}, @command.run([], opts)) | ||||
|   end | ||||
| 
 | ||||
|   @tag user: "user" | ||||
|   test "run: providing a non-existent user reports an error", _context do | ||||
|     s = "non-existent-user" | ||||
| 
 | ||||
|     assert @command.run([], %{node: get_rabbit_hostname(), | ||||
|                               user: s}) == {:error, {:no_such_user, s}} | ||||
|   end | ||||
| 
 | ||||
|   test "banner", context do | ||||
|     assert @command.banner([], %{user: context[:user]}) | ||||
|       == "Listing limits for user \"#{context[:user]}\" ..." | ||||
|     assert @command.banner([], %{global: true}) | ||||
|       == "Listing limits for all users ..." | ||||
|   end | ||||
| end | ||||
		Loading…
	
		Reference in New Issue