Skip to content

Commit

Permalink
api bypass cli setup
Browse files Browse the repository at this point in the history
  • Loading branch information
aristosMiliaressis committed May 29, 2024
1 parent 752ea06 commit bde47bf
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 15 deletions.
4 changes: 2 additions & 2 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ tasks:
install-cli:
deps: [build-cli]
cmds:
- chmod +x src/pwnctl.cli/bin/Release/net7.0/linux-x64/publish/pwnctl
- mv src/pwnctl.cli/bin/Release/net7.0/linux-x64/publish/pwnctl ~/.local/bin
- chmod +x src/pwnctl.cli/bin/Release/net7.0/linux-x64/publish/pwnctl.cli
- mv src/pwnctl.cli/bin/Release/net7.0/linux-x64/publish/pwnctl.cli ~/.local/bin/pwnctl
- mkdir -p ~/.config/pwnctl/
- chown `whoami` ~/.config/pwnctl/
- mv src/pwnctl.cli/bin/Release/net7.0/linux-x64/publish/config.ini ~/.config/pwnctl/
Expand Down
4 changes: 4 additions & 0 deletions bin/deploy_vps.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/bin/bash
set -e

dotnet publish src/pwnctl.cli/pwnctl.cli.csproj -c Release

cd infra/ansible/

password=$(aws secretsmanager get-secret-value --secret-id /aws/secret/pwnctl/Db/Password | jq -r .SecretString)
Expand All @@ -12,6 +14,8 @@ fi

ansible-playbook -i hosts.ini -k -u root install_postgres.yml --extra-vars "db_pass=$password"

ansible-playbook -i hosts.ini -k -u root install_cli.yml

cd -

vpc_id=$(terraform -chdir=infra/modules/ci output -raw vpc_id)
Expand Down
30 changes: 30 additions & 0 deletions infra/ansible/install_cli.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---

- name: install_cli
hosts: all
become: yes
gather_facts: false

tasks:
- name: Create pwnctl config directory
ansible.builtin.file:
path: /root/.config/pwnctl/
state: directory
mode: '0444'

- name: Copy pwnctl config
ansible.builtin.copy:
src: ../../src/pwnctl.cli/config.local.ini
dest: ~/.config/pwnctl/config.ini
owner: root
group: root
mode: '0644'

- name: Copy pwnctl executable
ansible.builtin.copy:
src: ../../src/pwnctl.cli/bin/Release/net7.0/linux-x64/publish/pwnctl.cli
dest: /usr/local/bin/pwnctl
owner: root
group: root
mode: '0755'

1 change: 1 addition & 0 deletions src/core/pwnctl.app/Configuration/AppConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public sealed class AwsSettings

public sealed class ApiSettings
{
public bool Bypass { get; set; }
public string BaseUrl { get; set; }
public string HMACSecret { get; set; }
public int AccessTimeoutMinutes { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ public static class EnvironmentVariables
public static bool IS_PROD => Environment.GetEnvironmentVariable("PWNCTL_IS_PROD")?.Equals("true") ?? false;

public static bool USE_LOCAL_INTEGRATIONS => Environment.GetEnvironmentVariable("PWNCTL_USE_LOCAL_INTEGRATIONS")?.Equals("true") ?? false;
public static bool BYPASS_API => Environment.GetEnvironmentVariable("PWNCTL_BYPASS_API")?.Equals("true") ?? false;
}
22 changes: 13 additions & 9 deletions src/pwnctl.cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using pwnctl.api.Mediator.Pipelines;
using FluentValidation;
using pwnctl.infra.Configuration;
using pwnctl.app;

internal sealed class Program
{
Expand All @@ -25,13 +26,10 @@ internal sealed class Program
.Select(t => (ModeHandler)Activator.CreateInstance(t))
.ToDictionary(p => p.ModeName, p => p);

public static ISender Sender;
public static ISender Sender = CreateSender();

static async Task Main(string[] args)
{
PwnInfraContextInitializer.Setup();
Setup(EnvironmentVariables.BYPASS_API);

if (args.Length < 1)
{
Console.WriteLine("No mode provided");
Expand All @@ -53,20 +51,26 @@ static async Task Main(string[] args)
await provider.Handle(args);
}

static void Setup(bool direct)
static ISender CreateSender()
{
if (direct)
PwnInfraContextInitializer.Setup();

if (PwnInfraContext.Config.Api.Bypass)
{
var services = new ServiceCollection();
services.AddMediatR(typeof(PwnctlDtoAssemblyMarker), typeof(PwnctlApiAssemblyMarker));
services.AddMediatR(cfg =>
{
cfg.RegisterServicesFromAssemblies(typeof(PwnctlDtoAssemblyMarker).GetTypeInfo().Assembly);
cfg.RegisterServicesFromAssemblies(typeof(PwnctlApiAssemblyMarker).GetTypeInfo().Assembly);
});
services.AddScoped(typeof(IPipelineBehavior<,>), typeof(ValidationPipeline<,>));
services.AddValidatorsFromAssemblyContaining<PwnctlDtoAssemblyMarker>();
services.AddTransient<TaskQueueService, SQSTaskQueueService>();
Sender = new Mediator(services.BuildServiceProvider());
return new Mediator(services.BuildServiceProvider());
}
else
{
Sender = PwnctlApiClient.Default;
return PwnctlApiClient.Default;
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/pwnctl.cli/config.local.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
; config.ini - pwnctl configuration

[Api]
Bypass = "True"

[Db]
Host = "127.0.0.1"

[Logging]
; "File", "Console"
Provider = "File"
; Verbose, Debug, Information, Warning, Error, Fatal
MinLevel = "Debug"
5 changes: 2 additions & 3 deletions src/pwnctl.cli/pwnctl.cli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<Nullable>disable</Nullable>
<EnablePreviewFeatures>True</EnablePreviewFeatures>
<NoWarn>8618</NoWarn>
<AssemblyName>pwnctl</AssemblyName>
<AssemblyName>pwnctl.cli</AssemblyName>
<SelfContained>true</SelfContained>
<RuntimeIdentifier>linux-x64</RuntimeIdentifier>
<PublishReadyToRun>true</PublishReadyToRun>
Expand All @@ -27,8 +27,7 @@
<PackageReference Include="DnsClient" Version="1.5.0" />
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.9.1" />
<PackageReference Include="MediatR" Version="12.2.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.7" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
Expand Down

0 comments on commit bde47bf

Please sign in to comment.