Skip to content

Commit

Permalink
try add x509 cert
Browse files Browse the repository at this point in the history
  • Loading branch information
robot committed Sep 4, 2023
1 parent bd224cb commit b96c348
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 24 deletions.
16 changes: 12 additions & 4 deletions slo/playground/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,21 @@ services:
context: ../..
dockerfile: slo/src/Dockerfile
command:
- 'run'
- 'http://ydb:2136'
- 'create'
- 'http://ydb:2135'
- '/local'
- '--prom-pgw'
- 'http://prometheus-pushgateway:9091/metrics'
- '--table-name'
- 'slo-dotnet'
- '--min-partitions-count'
- '6'
- '--max-partitions-count'
- '1000'
- '--partition-size'
- '1'
- '--initial-data-count'
- '1000'
volumes:
- ../data/ydb_certs:/ydb_certs
networks:
- monitor-net
depends_on:
Expand Down
39 changes: 33 additions & 6 deletions slo/src/Cli/Cli.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,30 @@ internal static class Cli
() => 30,
"time to wait before force kill workers");

private static readonly Option<FileInfo?> X590CertPath = new(
"--cert",
result =>
{
const string defaultX590Path = "../ydb_certs/ca.pem";
if (result.Tokens.Count == 0)
{
return new FileInfo(defaultX590Path);
}
var filePath = result.Tokens.Single().Value;
if (File.Exists(filePath))
{
return new FileInfo(filePath);
}
result.ErrorMessage = "File does not exist";
return null;
},
true,
"Path to x509 certificate file"
);

private static readonly Command CreateCommand = new(
"create",
"creates table in database")
Expand All @@ -89,7 +113,8 @@ internal static class Cli
MinPartitionsCountOption,
MaxPartitionsCountOption,
PartitionSizeOption,
InitialDataCountOption
InitialDataCountOption,
X590CertPath
};


Expand All @@ -100,7 +125,8 @@ internal static class Cli
TableOption,
WriteTimeoutOption,
EndpointArgument,
DbArgument
DbArgument,
X590CertPath
};

private static readonly Command RunCommand = new(
Expand All @@ -117,7 +143,8 @@ internal static class Cli
ReadTimeoutOption,
WriteRpsOption,
TimeOption,
ShutdownTimeOption
ShutdownTimeOption,
X590CertPath
};

private static readonly RootCommand RootCommand = new("SLO app")
Expand All @@ -130,18 +157,18 @@ internal static async Task<int> Run(string[] args)
CreateCommand.SetHandler(
async createConfig => { await CliCommands.Create(createConfig); },
new CreateConfigBinder(EndpointArgument, DbArgument, TableOption, MinPartitionsCountOption,
MaxPartitionsCountOption, PartitionSizeOption, InitialDataCountOption, WriteTimeoutOption)
MaxPartitionsCountOption, PartitionSizeOption, InitialDataCountOption, WriteTimeoutOption, X590CertPath)
);

CleanupCommand.SetHandler(
async cleanUpConfig => { await CliCommands.CleanUp(cleanUpConfig); },
new CleanUpConfigBinder(EndpointArgument, DbArgument, TableOption, WriteTimeoutOption)
new CleanUpConfigBinder(EndpointArgument, DbArgument, TableOption, WriteTimeoutOption, X590CertPath)
);

RunCommand.SetHandler(async runConfig => { await CliCommands.Run(runConfig); },
new RunConfigBinder(EndpointArgument, DbArgument, TableOption, InitialDataCountOption, PromPgwOption,
ReportPeriodOption, ReadRpsOption, ReadTimeoutOption, WriteRpsOption, WriteTimeoutOption, TimeOption,
ShutdownTimeOption));
ShutdownTimeOption, X590CertPath));
return await RootCommand.InvokeAsync(args);
}
}
21 changes: 19 additions & 2 deletions slo/src/Cli/CliCommands.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Security.Cryptography.X509Certificates;
using Prometheus;
using slo.Jobs;
using Ydb.Sdk;
Expand All @@ -9,10 +10,26 @@ public static class CliCommands
{
internal static async Task Create(CreateConfig createConfig)
{
X509Certificate? cert = null;
var x590CertPath = createConfig.X590CertPath;
if (x590CertPath != null)
{
Console.WriteLine($"full name = {x590CertPath.FullName}");
cert = new X509Certificate(x590CertPath.FullName);

var sr = new StreamReader(x590CertPath.FullName);

while (sr.EndOfStream != true)
{
Console.WriteLine(await sr.ReadLineAsync());
}
}

Console.WriteLine(createConfig);
var config = new DriverConfig(
createConfig.Endpoint,
createConfig.Db
createConfig.Db,
customServerCertificate: cert
);

await using var driver = await Driver.CreateInitialized(config);
Expand Down Expand Up @@ -61,7 +78,7 @@ internal static async Task Run(RunConfig runConfig)
await table.Init(runConfig.InitialDataCount, 1, 6, 1000);

Console.WriteLine(runConfig.PromPgw);

using var prometheus = new MetricPusher(endpoint: runConfig.PromPgw, job: "slo");

prometheus.Start();
Expand Down
22 changes: 16 additions & 6 deletions slo/src/Cli/ConfigBinders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ internal class CreateConfigBinder : BinderBase<CreateConfig>
private readonly Option<int> _partitionSizeOption;
private readonly Option<string> _tableOption;
private readonly Option<int> _writeTimeoutOption;
private readonly Option<FileInfo?> _x590CertPathOption;

public CreateConfigBinder(Argument<string> endpointArgument, Argument<string> dbArgument,
Option<string> tableOption, Option<int> minPartitionsCountOption, Option<int> maxPartitionsCountOption,
Option<int> partitionSizeOption, Option<int> initialDataCountOption, Option<int> writeTimeoutOption)
Option<int> partitionSizeOption, Option<int> initialDataCountOption, Option<int> writeTimeoutOption,
Option<FileInfo?> x590CertPathOption)
{
_endpointArgument = endpointArgument;
_dbArgument = dbArgument;
Expand All @@ -26,6 +28,7 @@ public CreateConfigBinder(Argument<string> endpointArgument, Argument<string> db
_partitionSizeOption = partitionSizeOption;
_initialDataCountOption = initialDataCountOption;
_writeTimeoutOption = writeTimeoutOption;
_x590CertPathOption = x590CertPathOption;
}

protected override CreateConfig GetBoundValue(BindingContext bindingContext)
Expand All @@ -38,7 +41,8 @@ protected override CreateConfig GetBoundValue(BindingContext bindingContext)
bindingContext.ParseResult.GetValueForOption(_maxPartitionsCountOption),
bindingContext.ParseResult.GetValueForOption(_partitionSizeOption),
bindingContext.ParseResult.GetValueForOption(_initialDataCountOption),
bindingContext.ParseResult.GetValueForOption(_writeTimeoutOption)
bindingContext.ParseResult.GetValueForOption(_writeTimeoutOption),
bindingContext.ParseResult.GetValueForOption(_x590CertPathOption)
);
}
}
Expand All @@ -49,14 +53,16 @@ internal class CleanUpConfigBinder : BinderBase<CleanUpConfig>
private readonly Argument<string> _endpointArgument;
private readonly Option<string> _tableOption;
private readonly Option<int> _writeTimeoutOption;
private readonly Option<FileInfo?> _x590CertPathOption;

public CleanUpConfigBinder(Argument<string> endpointArgument, Argument<string> dbArgument,
Option<string> tableOption, Option<int> writeTimeoutOption)
Option<string> tableOption, Option<int> writeTimeoutOption, Option<FileInfo?> x590CertPathOption)
{
_endpointArgument = endpointArgument;
_dbArgument = dbArgument;
_tableOption = tableOption;
_writeTimeoutOption = writeTimeoutOption;
_x590CertPathOption = x590CertPathOption;
}

protected override CleanUpConfig GetBoundValue(BindingContext bindingContext)
Expand All @@ -65,7 +71,8 @@ protected override CleanUpConfig GetBoundValue(BindingContext bindingContext)
bindingContext.ParseResult.GetValueForArgument(_endpointArgument),
bindingContext.ParseResult.GetValueForArgument(_dbArgument),
bindingContext.ParseResult.GetValueForOption(_tableOption)!,
bindingContext.ParseResult.GetValueForOption(_writeTimeoutOption)
bindingContext.ParseResult.GetValueForOption(_writeTimeoutOption),
bindingContext.ParseResult.GetValueForOption(_x590CertPathOption)
);
}
}
Expand All @@ -84,12 +91,13 @@ internal class RunConfigBinder : BinderBase<RunConfig>
private readonly Option<int> _timeOption;
private readonly Option<int> _writeRpsOption;
private readonly Option<int> _writeTimeoutOption;
private readonly Option<FileInfo?> _x590CertPathOption;

public RunConfigBinder(Argument<string> endpointArgument, Argument<string> dbArgument,
Option<string> tableOption, Option<int> initialDataCountOption, Option<string> promPgwOption,
Option<int> reportPeriodOption, Option<int> readRpsOption, Option<int> readTimeoutOption,
Option<int> writeRpsOption, Option<int> writeTimeoutOption, Option<int> timeOption,
Option<int> shutdownTimeOption)
Option<int> shutdownTimeOption, Option<FileInfo?> x590CertPathOption)
{
_endpointArgument = endpointArgument;
_dbArgument = dbArgument;
Expand All @@ -103,6 +111,7 @@ public RunConfigBinder(Argument<string> endpointArgument, Argument<string> dbArg
_writeTimeoutOption = writeTimeoutOption;
_timeOption = timeOption;
_shutdownTimeOption = shutdownTimeOption;
_x590CertPathOption = x590CertPathOption;
}

protected override RunConfig GetBoundValue(BindingContext bindingContext)
Expand All @@ -119,7 +128,8 @@ protected override RunConfig GetBoundValue(BindingContext bindingContext)
bindingContext.ParseResult.GetValueForOption(_writeRpsOption),
bindingContext.ParseResult.GetValueForOption(_writeTimeoutOption),
bindingContext.ParseResult.GetValueForOption(_timeOption),
bindingContext.ParseResult.GetValueForOption(_shutdownTimeOption)
bindingContext.ParseResult.GetValueForOption(_shutdownTimeOption),
bindingContext.ParseResult.GetValueForOption(_x590CertPathOption)
);
}
}
11 changes: 5 additions & 6 deletions slo/src/Cli/Configs.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
namespace slo.Cli;

internal record CreateConfig(string Endpoint, string Db, string TableName, int MinPartitionsCount,
int MaxPartitionsCount, int PartitionSize, int InitialDataCount, int WriteTimeout);
int MaxPartitionsCount, int PartitionSize, int InitialDataCount, int WriteTimeout, FileInfo? X590CertPath);

internal record CleanUpConfig(string Endpoint, string Db, string TableName, int WriteTimeout);
internal record CleanUpConfig(string Endpoint, string Db, string TableName, int WriteTimeout, FileInfo? X590CertPath);

internal record RunConfig(string Endpoint, string Db, string TableName, int InitialDataCount,
string PromPgw,
int ReportPeriod, int ReadRps, int ReadTimeout, int WriteRps, int WriteTimeout, int Time,
int ShutdownTime);
internal record RunConfig(string Endpoint, string Db, string TableName, int InitialDataCount, string PromPgw,
int ReportPeriod, int ReadRps, int ReadTimeout, int WriteRps, int WriteTimeout, int Time, int ShutdownTime,
FileInfo? X590CertPath);

0 comments on commit b96c348

Please sign in to comment.