From 3954a8df9ad7aa5c9979eb0d6fdab996f4d7c45e Mon Sep 17 00:00:00 2001 From: Christina Date: Tue, 14 May 2024 15:50:54 +0200 Subject: [PATCH 01/11] add tags and description to vhost --- spec/vhost_spec.cr | 2 +- src/lavinmq/http/controller/vhosts.cr | 5 ++++- src/lavinmq/vhost.cr | 5 ++++- src/lavinmq/vhost_store.cr | 8 +++++--- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/spec/vhost_spec.cr b/spec/vhost_spec.cr index 71325c44d1..ff07d6be25 100644 --- a/spec/vhost_spec.cr +++ b/spec/vhost_spec.cr @@ -99,7 +99,7 @@ describe LavinMQ::VHost do username = "test-user" user = Server.users.create(username, "password", [LavinMQ::Tag::Administrator]) vhost = "test-vhost" - Server.vhosts.create(vhost, user) + Server.vhosts.create(vhost, "", "", user) p = user.permissions[vhost] p[:config].should eq /.*/ p[:read].should eq /.*/ diff --git a/src/lavinmq/http/controller/vhosts.cr b/src/lavinmq/http/controller/vhosts.cr index 0b0b1e9a2d..61a89680ee 100644 --- a/src/lavinmq/http/controller/vhosts.cr +++ b/src/lavinmq/http/controller/vhosts.cr @@ -33,11 +33,14 @@ module LavinMQ u = user(context) refuse_unless_administrator(context, u) name = URI.decode_www_form(params["name"]) + body = parse_body(context) + tags = body["tags"]?.try(&.to_s) || "" + description = body["description"]?.try(&.to_s) || "" is_update = @amqp_server.vhosts[name]? if name.bytesize > UInt8::MAX bad_request(context, "Vhost name too long, can't exceed 255 characters") end - @amqp_server.vhosts.create(name, u) + @amqp_server.vhosts.create(name, tags, description, u) context.response.status_code = is_update ? 204 : 201 context end diff --git a/src/lavinmq/vhost.cr b/src/lavinmq/vhost.cr index 62f3924b39..000da6c721 100644 --- a/src/lavinmq/vhost.cr +++ b/src/lavinmq/vhost.cr @@ -42,7 +42,8 @@ module LavinMQ @definitions_file_path : String @definitions_deletes = 0 - def initialize(@name : String, @server_data_dir : String, @users : UserStore, @replicator : Replication::Replicator) + def initialize(@name : String, @tags : String, @description : String, + @server_data_dir : String, @users : UserStore, @replicator : Replication::Replicator) @log = Log.for "vhost[name=#{@name}]" @dir = Digest::SHA1.hexdigest(@name) @data_dir = File.join(@server_data_dir, @dir) @@ -212,6 +213,8 @@ module LavinMQ name: @name, dir: @dir, tracing: false, + tags: @tags, + description: @description, cluster_state: NamedTuple.new, } end diff --git a/src/lavinmq/vhost_store.cr b/src/lavinmq/vhost_store.cr index 9e3879398e..3c8183a0f2 100644 --- a/src/lavinmq/vhost_store.cr +++ b/src/lavinmq/vhost_store.cr @@ -20,11 +20,11 @@ module LavinMQ end end - def create(name : String, user : User = @users.default_user, save : Bool = true) + def create(name : String, tags : String = "", description : String = "", user : User = @users.default_user, save : Bool = true) if v = @vhosts[name]? return v end - vhost = VHost.new(name, @data_dir, @users, @replicator) + vhost = VHost.new(name, tags, description, @data_dir, @users, @replicator) Log.info { "Created vhost #{name}" } @users.add_permission(user.name, name, /.*/, /.*/, /.*/) @users.add_permission(UserStore::DIRECT_USER, name, /.*/, /.*/, /.*/) @@ -61,7 +61,9 @@ module LavinMQ File.open(path) do |f| JSON.parse(f).as_a.each do |vhost| name = vhost["name"].as_s - @vhosts[name] = VHost.new(name, @data_dir, @users, @replicator) + tags = vhost["tags"].as_s + description = vhost["description"].as_s + @vhosts[name] = VHost.new(name, tags, description, @data_dir, @users, @replicator) @users.add_permission(UserStore::DIRECT_USER, name, /.*/, /.*/, /.*/) end @replicator.register_file(f) From 2616fc299870f5ff57cd6b9ac0a348f8c56d697a Mon Sep 17 00:00:00 2001 From: Christina Date: Wed, 15 May 2024 09:25:58 +0200 Subject: [PATCH 02/11] let tags and description be nil --- src/lavinmq/vhost_store.cr | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lavinmq/vhost_store.cr b/src/lavinmq/vhost_store.cr index 3c8183a0f2..a4737715f7 100644 --- a/src/lavinmq/vhost_store.cr +++ b/src/lavinmq/vhost_store.cr @@ -61,8 +61,8 @@ module LavinMQ File.open(path) do |f| JSON.parse(f).as_a.each do |vhost| name = vhost["name"].as_s - tags = vhost["tags"].as_s - description = vhost["description"].as_s + tags = vhost["tags"]?.try &.as_s || "" + description = vhost["description"]?.try &.as_s || "" @vhosts[name] = VHost.new(name, tags, description, @data_dir, @users, @replicator) @users.add_permission(UserStore::DIRECT_USER, name, /.*/, /.*/, /.*/) end From dd9c33cbae15a39d1809f80be23d118dd56f57a4 Mon Sep 17 00:00:00 2001 From: Christina Date: Fri, 31 May 2024 13:49:19 +0200 Subject: [PATCH 03/11] format --- src/lavinmq/vhost.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lavinmq/vhost.cr b/src/lavinmq/vhost.cr index 000da6c721..180ab10046 100644 --- a/src/lavinmq/vhost.cr +++ b/src/lavinmq/vhost.cr @@ -43,7 +43,7 @@ module LavinMQ @definitions_deletes = 0 def initialize(@name : String, @tags : String, @description : String, - @server_data_dir : String, @users : UserStore, @replicator : Replication::Replicator) + @server_data_dir : String, @users : UserStore, @replicator : Replication::Replicator) @log = Log.for "vhost[name=#{@name}]" @dir = Digest::SHA1.hexdigest(@name) @data_dir = File.join(@server_data_dir, @dir) From c39bd1a4af28fa81f587cc97abcc91c56276889f Mon Sep 17 00:00:00 2001 From: Christina Date: Wed, 5 Jun 2024 14:13:40 +0200 Subject: [PATCH 04/11] store tags as array of string --- spec/vhost_spec.cr | 2 +- src/lavinmq/http/controller/vhosts.cr | 2 +- src/lavinmq/vhost.cr | 2 +- src/lavinmq/vhost_store.cr | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/spec/vhost_spec.cr b/spec/vhost_spec.cr index ff07d6be25..d5f10c5fa7 100644 --- a/spec/vhost_spec.cr +++ b/spec/vhost_spec.cr @@ -99,7 +99,7 @@ describe LavinMQ::VHost do username = "test-user" user = Server.users.create(username, "password", [LavinMQ::Tag::Administrator]) vhost = "test-vhost" - Server.vhosts.create(vhost, "", "", user) + Server.vhosts.create(vhost, [""], "", user) p = user.permissions[vhost] p[:config].should eq /.*/ p[:read].should eq /.*/ diff --git a/src/lavinmq/http/controller/vhosts.cr b/src/lavinmq/http/controller/vhosts.cr index 61a89680ee..6cfce66568 100644 --- a/src/lavinmq/http/controller/vhosts.cr +++ b/src/lavinmq/http/controller/vhosts.cr @@ -34,7 +34,7 @@ module LavinMQ refuse_unless_administrator(context, u) name = URI.decode_www_form(params["name"]) body = parse_body(context) - tags = body["tags"]?.try(&.to_s) || "" + tags = body["tags"].to_s.split(",") || [] of String description = body["description"]?.try(&.to_s) || "" is_update = @amqp_server.vhosts[name]? if name.bytesize > UInt8::MAX diff --git a/src/lavinmq/vhost.cr b/src/lavinmq/vhost.cr index 180ab10046..b9cfd87741 100644 --- a/src/lavinmq/vhost.cr +++ b/src/lavinmq/vhost.cr @@ -42,7 +42,7 @@ module LavinMQ @definitions_file_path : String @definitions_deletes = 0 - def initialize(@name : String, @tags : String, @description : String, + def initialize(@name : String, @tags : Array(String), @description : String, @server_data_dir : String, @users : UserStore, @replicator : Replication::Replicator) @log = Log.for "vhost[name=#{@name}]" @dir = Digest::SHA1.hexdigest(@name) diff --git a/src/lavinmq/vhost_store.cr b/src/lavinmq/vhost_store.cr index a4737715f7..9e402f722f 100644 --- a/src/lavinmq/vhost_store.cr +++ b/src/lavinmq/vhost_store.cr @@ -20,7 +20,7 @@ module LavinMQ end end - def create(name : String, tags : String = "", description : String = "", user : User = @users.default_user, save : Bool = true) + def create(name : String, tags : Array(String) = [] of String, description : String = "", user : User = @users.default_user, save : Bool = true) if v = @vhosts[name]? return v end @@ -61,7 +61,7 @@ module LavinMQ File.open(path) do |f| JSON.parse(f).as_a.each do |vhost| name = vhost["name"].as_s - tags = vhost["tags"]?.try &.as_s || "" + tags = vhost["tags"]?.try(&.as_a.map(&.to_s)) || [] of String description = vhost["description"]?.try &.as_s || "" @vhosts[name] = VHost.new(name, tags, description, @data_dir, @users, @replicator) @users.add_permission(UserStore::DIRECT_USER, name, /.*/, /.*/, /.*/) From dcff11f98a5c9a5f54e4db162eaf7f74d8f37495 Mon Sep 17 00:00:00 2001 From: Christina Date: Wed, 5 Jun 2024 15:56:27 +0200 Subject: [PATCH 05/11] nilguard no tags --- src/lavinmq/http/controller/vhosts.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lavinmq/http/controller/vhosts.cr b/src/lavinmq/http/controller/vhosts.cr index 6cfce66568..efafa81bbe 100644 --- a/src/lavinmq/http/controller/vhosts.cr +++ b/src/lavinmq/http/controller/vhosts.cr @@ -34,7 +34,7 @@ module LavinMQ refuse_unless_administrator(context, u) name = URI.decode_www_form(params["name"]) body = parse_body(context) - tags = body["tags"].to_s.split(",") || [] of String + tags = body["tags"]?.to_s.split(",") || [] of String description = body["description"]?.try(&.to_s) || "" is_update = @amqp_server.vhosts[name]? if name.bytesize > UInt8::MAX From 9e6f7382a5d71958cc2c700b1370bff71e1deac2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christina=20Dahl=C3=A9n?= <85930202+kickster97@users.noreply.github.com> Date: Tue, 18 Jun 2024 11:25:45 +0200 Subject: [PATCH 06/11] Update src/lavinmq/http/controller/vhosts.cr MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Carl Hörberg --- src/lavinmq/http/controller/vhosts.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lavinmq/http/controller/vhosts.cr b/src/lavinmq/http/controller/vhosts.cr index efafa81bbe..6da1974295 100644 --- a/src/lavinmq/http/controller/vhosts.cr +++ b/src/lavinmq/http/controller/vhosts.cr @@ -34,7 +34,7 @@ module LavinMQ refuse_unless_administrator(context, u) name = URI.decode_www_form(params["name"]) body = parse_body(context) - tags = body["tags"]?.to_s.split(",") || [] of String + tags = body["tags"]?.to_s.split(',').map(&.strip).reject(&.empty?) description = body["description"]?.try(&.to_s) || "" is_update = @amqp_server.vhosts[name]? if name.bytesize > UInt8::MAX From 5f8a8d76d538b225d9a9ada00fc3df5fc70a3945 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christina=20Dahl=C3=A9n?= <85930202+kickster97@users.noreply.github.com> Date: Tue, 18 Jun 2024 11:29:26 +0200 Subject: [PATCH 07/11] Update src/lavinmq/vhost.cr MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Carl Hörberg --- src/lavinmq/vhost.cr | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/lavinmq/vhost.cr b/src/lavinmq/vhost.cr index b9cfd87741..f950ec4e0a 100644 --- a/src/lavinmq/vhost.cr +++ b/src/lavinmq/vhost.cr @@ -42,8 +42,7 @@ module LavinMQ @definitions_file_path : String @definitions_deletes = 0 - def initialize(@name : String, @tags : Array(String), @description : String, - @server_data_dir : String, @users : UserStore, @replicator : Replication::Replicator) + def initialize(@name : String, @server_data_dir : String, @users : UserStore, @replicator : Replication::Replicator, @description = "", @tags = Array(String).new(0)) @log = Log.for "vhost[name=#{@name}]" @dir = Digest::SHA1.hexdigest(@name) @data_dir = File.join(@server_data_dir, @dir) From 66d7f5de90d498149303e654661bbf2eacc92c87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christina=20Dahl=C3=A9n?= <85930202+kickster97@users.noreply.github.com> Date: Tue, 18 Jun 2024 11:31:07 +0200 Subject: [PATCH 08/11] Update spec/vhost_spec.cr MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Carl Hörberg --- spec/vhost_spec.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/vhost_spec.cr b/spec/vhost_spec.cr index d5f10c5fa7..71325c44d1 100644 --- a/spec/vhost_spec.cr +++ b/spec/vhost_spec.cr @@ -99,7 +99,7 @@ describe LavinMQ::VHost do username = "test-user" user = Server.users.create(username, "password", [LavinMQ::Tag::Administrator]) vhost = "test-vhost" - Server.vhosts.create(vhost, [""], "", user) + Server.vhosts.create(vhost, user) p = user.permissions[vhost] p[:config].should eq /.*/ p[:read].should eq /.*/ From d65c0053893121c94e11fccfa4f23b2081cbcf4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christina=20Dahl=C3=A9n?= <85930202+kickster97@users.noreply.github.com> Date: Tue, 18 Jun 2024 11:31:19 +0200 Subject: [PATCH 09/11] Update src/lavinmq/http/controller/vhosts.cr MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Carl Hörberg --- src/lavinmq/http/controller/vhosts.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lavinmq/http/controller/vhosts.cr b/src/lavinmq/http/controller/vhosts.cr index 6da1974295..6093bd3c97 100644 --- a/src/lavinmq/http/controller/vhosts.cr +++ b/src/lavinmq/http/controller/vhosts.cr @@ -35,7 +35,7 @@ module LavinMQ name = URI.decode_www_form(params["name"]) body = parse_body(context) tags = body["tags"]?.to_s.split(',').map(&.strip).reject(&.empty?) - description = body["description"]?.try(&.to_s) || "" + description = body["description"]?.to_s is_update = @amqp_server.vhosts[name]? if name.bytesize > UInt8::MAX bad_request(context, "Vhost name too long, can't exceed 255 characters") From 0b1586e2de21879ae8012a725414c47c333099eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christina=20Dahl=C3=A9n?= <85930202+kickster97@users.noreply.github.com> Date: Tue, 18 Jun 2024 11:31:26 +0200 Subject: [PATCH 10/11] Update src/lavinmq/vhost_store.cr MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Carl Hörberg --- src/lavinmq/vhost_store.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lavinmq/vhost_store.cr b/src/lavinmq/vhost_store.cr index 9e402f722f..3d58cca68c 100644 --- a/src/lavinmq/vhost_store.cr +++ b/src/lavinmq/vhost_store.cr @@ -20,7 +20,7 @@ module LavinMQ end end - def create(name : String, tags : Array(String) = [] of String, description : String = "", user : User = @users.default_user, save : Bool = true) + def create(name : String, user : User = @users.default_user, description = "", tags = Array(String).new(0), save : Bool = true) if v = @vhosts[name]? return v end From 7eb3a2884be7469999dd57e629803e67121275bf Mon Sep 17 00:00:00 2001 From: Christina Date: Tue, 18 Jun 2024 11:36:29 +0200 Subject: [PATCH 11/11] organize tags & description --- src/lavinmq/http/controller/vhosts.cr | 2 +- src/lavinmq/vhost_store.cr | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lavinmq/http/controller/vhosts.cr b/src/lavinmq/http/controller/vhosts.cr index 6093bd3c97..bac28777e8 100644 --- a/src/lavinmq/http/controller/vhosts.cr +++ b/src/lavinmq/http/controller/vhosts.cr @@ -40,7 +40,7 @@ module LavinMQ if name.bytesize > UInt8::MAX bad_request(context, "Vhost name too long, can't exceed 255 characters") end - @amqp_server.vhosts.create(name, tags, description, u) + @amqp_server.vhosts.create(name, u, description, tags) context.response.status_code = is_update ? 204 : 201 context end diff --git a/src/lavinmq/vhost_store.cr b/src/lavinmq/vhost_store.cr index 3d58cca68c..36ae7749d7 100644 --- a/src/lavinmq/vhost_store.cr +++ b/src/lavinmq/vhost_store.cr @@ -24,7 +24,7 @@ module LavinMQ if v = @vhosts[name]? return v end - vhost = VHost.new(name, tags, description, @data_dir, @users, @replicator) + vhost = VHost.new(name, @data_dir, @users, @replicator, description, tags) Log.info { "Created vhost #{name}" } @users.add_permission(user.name, name, /.*/, /.*/, /.*/) @users.add_permission(UserStore::DIRECT_USER, name, /.*/, /.*/, /.*/) @@ -63,7 +63,7 @@ module LavinMQ name = vhost["name"].as_s tags = vhost["tags"]?.try(&.as_a.map(&.to_s)) || [] of String description = vhost["description"]?.try &.as_s || "" - @vhosts[name] = VHost.new(name, tags, description, @data_dir, @users, @replicator) + @vhosts[name] = VHost.new(name, @data_dir, @users, @replicator, description, tags) @users.add_permission(UserStore::DIRECT_USER, name, /.*/, /.*/, /.*/) end @replicator.register_file(f)