Skip to content

Commit

Permalink
postgres: return an empty list of addresses on dns errors (#3638)
Browse files Browse the repository at this point in the history
postgres: return an empty list of addresses on dns errors (#3637)

Co-authored-by: Caleb Doxsey <cdoxsey@pomerium.com>
  • Loading branch information
backport-actions-token[bot] and calebdoxsey committed Sep 30, 2022
1 parent c0a8870 commit 88abdf8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
23 changes: 22 additions & 1 deletion pkg/storage/postgres/backend.go
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"net"
"sync"
"time"

Expand Down Expand Up @@ -320,7 +321,7 @@ func (backend *Backend) init(ctx context.Context) (serverVersion uint64, pool *p
return serverVersion, pool, nil
}

config, err := pgxpool.ParseConfig(backend.dsn)
config, err := ParseConfig(backend.dsn)
if err != nil {
return serverVersion, nil, err
}
Expand Down Expand Up @@ -374,3 +375,23 @@ func (backend *Backend) doPeriodically(f func(ctx context.Context) error, dur ti
}
}
}

// ParseConfig parses a DSN into a pgxpool.Config.
func ParseConfig(dsn string) (*pgxpool.Config, error) {
config, err := pgxpool.ParseConfig(dsn)
if err != nil {
return nil, err
}
config.ConnConfig.LookupFunc = lookup
return config, nil
}

func lookup(ctx context.Context, host string) (addrs []string, err error) {
addrs, err = net.DefaultResolver.LookupHost(ctx, host)
// ignore no such host errors
if e := new(net.DNSError); errors.As(err, &e) && e.IsNotFound {
addrs = nil
err = nil
}
return addrs, err
}
14 changes: 14 additions & 0 deletions pkg/storage/postgres/backend_test.go
Expand Up @@ -166,3 +166,17 @@ func TestBackend(t *testing.T) {
return nil
}))
}

func TestLookup(t *testing.T) {
t.Parallel()

ctx, clearTimeout := context.WithTimeout(context.Background(), time.Second*10)
t.Cleanup(clearTimeout)

cfg, err := ParseConfig("host=localhost")
assert.NoError(t, err)

addrs, err := cfg.ConnConfig.LookupFunc(ctx, "test.unknown")
assert.NoError(t, err)
assert.Empty(t, addrs)
}

0 comments on commit 88abdf8

Please sign in to comment.