Skip to content

Commit

Permalink
feat: #172 use stable agent token for the generated data
Browse files Browse the repository at this point in the history
  • Loading branch information
bohdan-shulha committed Sep 6, 2024
1 parent e59fc5e commit a9f5c2b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
5 changes: 3 additions & 2 deletions app/Console/Commands/SelfHostPtah.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use App\Models\Service;
use App\Models\Team;
use App\Models\User;
use App\Util\AgentToken;
use App\Util\ResourceId;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Casts\Json;
Expand Down Expand Up @@ -40,6 +41,8 @@ class SelfHostPtah extends Command
*/
public function handle()
{
AgentToken::useFakeToken('fake_agent_token');

ResourceId::useIdsPool([
'a1b2c3d4e5f',
'g6h7i8j9k1',
Expand Down Expand Up @@ -84,8 +87,6 @@ public function handle()
'team_id' => $team->id,
]);

$node->update(['agent_token' => 'AGENT_TOKEN']);

InitCluster::run($user, $node, '192.168.1.1');

$network = Network::first();
Expand Down
4 changes: 2 additions & 2 deletions app/Models/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
namespace App\Models;

use App\Traits\HasOwningTeam;
use App\Util\AgentToken;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Support\Str;
use RuntimeException;

class Node extends Model
Expand Down Expand Up @@ -36,7 +36,7 @@ protected static function booted(): void
throw new RuntimeException('Invalid State - The team is at its node limit');
}

$node->agent_token = Str::random(42);
$node->agent_token = AgentToken::make();
});
}

Expand Down
32 changes: 32 additions & 0 deletions app/Util/AgentToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Util;

use Illuminate\Support\Str;
use RuntimeException;

class AgentToken
{
protected static $useFakeToken = false;

protected static $fakeToken = null;

public static function useFakeToken(string $token): void
{
self::$fakeToken = $token;
self::$useFakeToken = true;
}

public static function make(): string
{
if (self::$useFakeToken) {
if (self::$fakeToken === null) {
throw new RuntimeException('Fake token is not set');
}

return self::$fakeToken;
}

return Str::random(42);
}
}
1 change: 1 addition & 0 deletions scripts/self-hosted/install-server.sh
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ sql_dump_file="db.sql"
sql_dump_file_tmp="db.sql.tmp"

sed -e "s|self_hosted_password|$password_hash|g" \
-e "s|fake_agent_token|$random_token|g" \
"$sql_dump_file" > "$sql_dump_file_tmp"

# Replace the original file with the updated one
Expand Down

0 comments on commit a9f5c2b

Please sign in to comment.