Skip to content

Commit

Permalink
add whitelist
Browse files Browse the repository at this point in the history
  • Loading branch information
area363 committed Oct 25, 2023
1 parent cade52b commit 10700bf
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Lib9c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public interface IMutableAccessControlService : IAccessControlService
{
void DenyAccess(Address address);
void AllowAccess(Address address);
void DenyWhiteList(Address address);
void AllowWhiteList(Address address);
List<Address> ListBlockedAddresses(int offset, int limit);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,24 @@ public MutableRedisAccessControlService(string storageUri)

public void DenyAccess(Address address)
{
_db.StringSet(address.ToString(), "denied");
_db.StringSet(address.ToString(), "0");
}

public void AllowAccess(Address address)
{
_db.KeyDelete(address.ToString());
}

public void DenyWhiteList(Address address)
{
_db.KeyDelete(address.ToString());
}

public void AllowWhiteList(Address address)
{
_db.StringSet(address.ToString(), "1");
}

public List<Address> ListBlockedAddresses(int offset, int limit)
{
var server = _db.Multiplexer.GetServer(_db.Multiplexer.GetEndPoints().First());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ namespace NineChronicles.Headless.AccessControlCenter.AccessControlService
public class MutableSqliteAccessControlService : SQLiteAccessControlService, IMutableAccessControlService
{
private const string DenyAccessSql =
"INSERT OR IGNORE INTO blocklist (address) VALUES (@Address)";
"INSERT OR IGNORE INTO blocklist (address) VALUES (@Address, 0)";
private const string AllowAccessSql = "DELETE FROM blocklist WHERE address=@Address";

private const string AllowWhiteListSql =
"INSERT OR IGNORE INTO blocklist (address) VALUES (@Address, 0)";
private const string DenyWhiteListSql = "DELETE FROM blocklist WHERE address=@Address";

public MutableSqliteAccessControlService(string connectionString) : base(connectionString)
{
}
Expand All @@ -38,6 +42,28 @@ public void AllowAccess(Address address)
command.ExecuteNonQuery();
}

public void DenyWhiteList(Address address)
{
using var connection = new SqliteConnection(_connectionString);
connection.Open();

using var command = connection.CreateCommand();
command.CommandText = DenyWhiteListSql;
command.Parameters.AddWithValue("@Address", address.ToString());
command.ExecuteNonQuery();
}

public void AllowWhiteList(Address address)
{
using var connection = new SqliteConnection(_connectionString);
connection.Open();

using var command = connection.CreateCommand();
command.CommandText = AllowWhiteListSql;
command.Parameters.AddWithValue("@Address", address.ToString());
command.ExecuteNonQuery();
}

public List<Address> ListBlockedAddresses(int offset, int limit)
{
var blockedAddresses = new List<Address>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ public ActionResult AllowAccess(string address)
return Ok();
}

[HttpPost("entries/{address}/deny-whitelist")]
public ActionResult DenyWhiteList(string address)
{
_accessControlService.DenyWhiteList(new Address(address));
return Ok();
}

[HttpPost("entries/{address}/allow-whitelist")]
public ActionResult AllowWhiteList(string address)
{
_accessControlService.AllowWhiteList(new Address(address));
return Ok();
}

[HttpGet("entries")]
public ActionResult<List<string>> ListBlockedAddresses(int offset, int limit)
{
Expand Down
13 changes: 13 additions & 0 deletions NineChronicles.Headless/Services/RedisAccessControlService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using StackExchange.Redis;
using Libplanet.Crypto;
using Microsoft.AspNetCore.Mvc.Filters;
using Nekoyume.Blockchain;
using Serilog;

Expand All @@ -26,5 +28,16 @@ public bool IsAccessDenied(Address address)

return result;
}

public int GetAccessLevel(Address address)
{
RedisValue result = _db.StringGet(address.ToString());
if (result.IsNull)
{
result = "-1";
}

return Convert.ToInt32(result);
}
}
}
19 changes: 18 additions & 1 deletion NineChronicles.Headless/Services/SQLiteAccessControlService.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using Microsoft.Data.Sqlite;
using Libplanet.Crypto;
using Nekoyume.Blockchain;
Expand All @@ -8,9 +9,11 @@ namespace NineChronicles.Headless.Services
public class SQLiteAccessControlService : IAccessControlService
{
private const string CreateTableSql =
"CREATE TABLE IF NOT EXISTS blocklist (address VARCHAR(42))";
"CREATE TABLE IF NOT EXISTS blocklist (address VARCHAR(42), level INT)";
private const string CheckAccessSql =
"SELECT EXISTS(SELECT 1 FROM blocklist WHERE address=@Address)";
private const string CheckAccessLevelSql =
"SELECT level FROM blocklist WHERE address=@Address";

protected readonly string _connectionString;

Expand Down Expand Up @@ -46,5 +49,19 @@ public bool IsAccessDenied(Address address)

return result;
}

public int GetAccessLevel(Address address)
{
using var connection = new SqliteConnection(_connectionString);
connection.Open();

using var command = connection.CreateCommand();
command.CommandText = CheckAccessLevelSql;
command.Parameters.AddWithValue("@Address", address.ToString());

var queryResult = command.ExecuteScalar() ?? "-1";

return Convert.ToInt32(queryResult);
}
}
}

0 comments on commit 10700bf

Please sign in to comment.