feat: add gateway bootstrap endpoint with token-based authentication
Add Bootstrap and AgentSyncBundle handlers to gateway package with X-Gateway-Bootstrap-Token header authentication. Implement UpsertByName repository method for idempotent gateway registration. Update gateway entrypoint script to auto-generate keys and bootstrap gateway on first run, persisting gateway ID to disk. Add GATEWAY_BOOTSTRAP_TOKEN config and update environment variables for gateway name, bootstrap URL, and sync URL.
This commit is contained in:
@@ -15,6 +15,7 @@ type Repository interface {
|
||||
FirstActive(ctx context.Context) (Gateway, error)
|
||||
BuildSyncBundle(ctx context.Context, gatewayID uuid.UUID) (wireguard.GatewayBundle, error)
|
||||
Update(ctx context.Context, gatewayID uuid.UUID, input UpdateRequest) (Gateway, error)
|
||||
UpsertByName(ctx context.Context, input BootstrapRequest) (Gateway, error)
|
||||
}
|
||||
|
||||
type PGRepository struct {
|
||||
@@ -136,6 +137,27 @@ func (r *PGRepository) Update(ctx context.Context, gatewayID uuid.UUID, input Up
|
||||
return item, err
|
||||
}
|
||||
|
||||
func (r *PGRepository) UpsertByName(ctx context.Context, input BootstrapRequest) (Gateway, error) {
|
||||
row := r.db.QueryRow(ctx, `
|
||||
insert into gateways (id, name, endpoint, public_key, listen_port, vpn_cidr, dns_servers, is_active)
|
||||
values ($1, $2, $3, $4, $5, $6::cidr, $7::text[], true)
|
||||
on conflict (name)
|
||||
do update set
|
||||
endpoint = excluded.endpoint,
|
||||
public_key = excluded.public_key,
|
||||
listen_port = excluded.listen_port,
|
||||
vpn_cidr = excluded.vpn_cidr,
|
||||
dns_servers = excluded.dns_servers,
|
||||
is_active = true,
|
||||
updated_at = now()
|
||||
returning id, name, endpoint, public_key, listen_port, vpn_cidr::text, dns_servers, is_active
|
||||
`, uuid.New(), input.Name, input.Endpoint, input.PublicKey, input.ListenPort, input.VPNCIDR, input.DNSServers)
|
||||
|
||||
var item Gateway
|
||||
err := row.Scan(&item.ID, &item.Name, &item.Endpoint, &item.PublicKey, &item.ListenPort, &item.VPNCIDR, &item.DNSServers, &item.IsActive)
|
||||
return item, err
|
||||
}
|
||||
|
||||
func gatewayInterfaceAddress(cidr string) (string, error) {
|
||||
prefix, err := netip.ParsePrefix(cidr)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user