Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
68f9d4b832 |
@@ -13,14 +13,17 @@ import (
|
|||||||
"github.com/jackc/pgx/v5"
|
"github.com/jackc/pgx/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const firewallClientSelectCols = `
|
||||||
|
id, name, COALESCE(hostname, ''), token_prefix, status,
|
||||||
|
last_seen_at, COALESCE(last_seen_at_source, ''), COALESCE(last_seen_ip, ''),
|
||||||
|
last_apply_at, COALESCE(last_apply_status, ''), COALESCE(last_apply_error, ''),
|
||||||
|
COALESCE(last_apply_prefix_count, 0), COALESCE(last_apply_ip_count, 0), COALESCE(last_apply_source, ''),
|
||||||
|
COALESCE(client_version, ''), created_at, approved_at, approved_by_api_key_id, revoked_at`
|
||||||
|
|
||||||
func (p *Postgres) ListFirewallClients(tenantID string) ([]*store.FirewallClient, error) {
|
func (p *Postgres) ListFirewallClients(tenantID string) ([]*store.FirewallClient, error) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
rows, err := p.pool.Query(ctx, `
|
rows, err := p.pool.Query(ctx, `
|
||||||
SELECT id, name, hostname, token_prefix, status,
|
SELECT `+firewallClientSelectCols+`
|
||||||
last_seen_at, last_seen_at_source, last_seen_ip,
|
|
||||||
last_apply_at, last_apply_status, last_apply_error,
|
|
||||||
last_apply_prefix_count, last_apply_ip_count, last_apply_source,
|
|
||||||
client_version, created_at, approved_at, approved_by_api_key_id, revoked_at
|
|
||||||
FROM firewall_client WHERE tenant_id=$1 ORDER BY created_at DESC`, tenantID)
|
FROM firewall_client WHERE tenant_id=$1 ORDER BY created_at DESC`, tenantID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -40,11 +43,7 @@ func (p *Postgres) ListFirewallClients(tenantID string) ([]*store.FirewallClient
|
|||||||
func (p *Postgres) GetFirewallClient(tenantID, id string) (*store.FirewallClient, error) {
|
func (p *Postgres) GetFirewallClient(tenantID, id string) (*store.FirewallClient, error) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
row := p.pool.QueryRow(ctx, `
|
row := p.pool.QueryRow(ctx, `
|
||||||
SELECT id, name, hostname, token_prefix, status,
|
SELECT `+firewallClientSelectCols+`
|
||||||
last_seen_at, last_seen_at_source, last_seen_ip,
|
|
||||||
last_apply_at, last_apply_status, last_apply_error,
|
|
||||||
last_apply_prefix_count, last_apply_ip_count, last_apply_source,
|
|
||||||
client_version, created_at, approved_at, approved_by_api_key_id, revoked_at
|
|
||||||
FROM firewall_client WHERE id=$1 AND tenant_id=$2`, id, tenantID)
|
FROM firewall_client WHERE id=$1 AND tenant_id=$2`, id, tenantID)
|
||||||
c, err := scanFirewallClientRow(row.Scan, tenantID)
|
c, err := scanFirewallClientRow(row.Scan, tenantID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -147,11 +146,7 @@ func (p *Postgres) LookupFirewallClientByTokenHash(hash []byte) (*store.Firewall
|
|||||||
}
|
}
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
row := p.pool.QueryRow(ctx, `
|
row := p.pool.QueryRow(ctx, `
|
||||||
SELECT tenant_id, id, name, hostname, token_prefix, status,
|
SELECT tenant_id, `+firewallClientSelectCols+`
|
||||||
last_seen_at, last_seen_at_source, last_seen_ip,
|
|
||||||
last_apply_at, last_apply_status, last_apply_error,
|
|
||||||
last_apply_prefix_count, last_apply_ip_count, last_apply_source,
|
|
||||||
client_version, created_at, approved_at, approved_by_api_key_id, revoked_at
|
|
||||||
FROM firewall_client WHERE token_hash=$1`, hash)
|
FROM firewall_client WHERE token_hash=$1`, hash)
|
||||||
c, err := scanFirewallClientLookupRow(row.Scan)
|
c, err := scanFirewallClientLookupRow(row.Scan)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -0,0 +1,52 @@
|
|||||||
|
package repository
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"evobgp/internal/authkey"
|
||||||
|
"evobgp/internal/db"
|
||||||
|
"evobgp/internal/store"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestPostgresFirewallClientCreateAndGetIntegration(t *testing.T) {
|
||||||
|
dsn := os.Getenv("EVOBGP_TEST_DATABASE_URL")
|
||||||
|
if dsn == "" {
|
||||||
|
t.Skip("EVOBGP_TEST_DATABASE_URL not set")
|
||||||
|
}
|
||||||
|
ctx := context.Background()
|
||||||
|
pool, err := db.OpenPostgresPool(ctx, dsn)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer pool.Close()
|
||||||
|
pg, err := NewPostgres(ctx, pool, true)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
tenant, _, _, _, _ := pg.DemoIDs()
|
||||||
|
if tenant == "" {
|
||||||
|
t.Fatal("demo tenant required")
|
||||||
|
}
|
||||||
|
tok := "evobgp_fw_pgtest_" + t.Name()
|
||||||
|
hash := authkey.HashToken(tok)
|
||||||
|
client, err := pg.CreateFirewallClient(tenant, &store.FirewallClientCreate{
|
||||||
|
Name: "pg-firewall-test",
|
||||||
|
Hostname: "test.local",
|
||||||
|
TokenPrefix: tok[:12],
|
||||||
|
TokenHash: hash,
|
||||||
|
ClientVersion: "test/1",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("create: %v", err)
|
||||||
|
}
|
||||||
|
got, err := pg.GetFirewallClient(tenant, client.ID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("get: %v", err)
|
||||||
|
}
|
||||||
|
if got.Name != "pg-firewall-test" || got.Status != "pending" {
|
||||||
|
t.Fatalf("got %+v", got)
|
||||||
|
}
|
||||||
|
_ = pg.DeleteFirewallClient(tenant, client.ID)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user