Skip to content

Commit

Permalink
ensure publisher accepts non-name address
Browse files Browse the repository at this point in the history
  • Loading branch information
rossjones committed Mar 19, 2024
1 parent 00e01e1 commit db89868
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
22 changes: 8 additions & 14 deletions pkg/publisher/local/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type Publisher struct {
func NewLocalPublisher(ctx context.Context, directory string, host string, port int) *Publisher {
p := &Publisher{
baseDirectory: directory,
host: resolveAddress(ctx, host),
host: ResolveAddress(ctx, host),
port: port,
urlPrefix: fmt.Sprintf("http://%s:%d", host, port),
}
Expand Down Expand Up @@ -93,20 +93,14 @@ func (p *Publisher) PublishResult(

var _ publisher.Publisher = (*Publisher)(nil)

func resolveAddress(ctx context.Context, address string) string {
func ResolveAddress(ctx context.Context, address string) string {
addressType, ok := network.AddressTypeFromString(address)
if !ok {
log.Ctx(ctx).Debug().Stringer("AddressType", addressType).Msgf("unable to find address type: %s, binding to 0.0.0.0", address)
return "0.0.0.0"
}

// If we were provided with an address type and not an address, so we should look up
// an address from the type.
addrs, err := network.GetNetworkAddress(addressType, network.AllAddresses)
if err == nil && len(addrs) > 0 {
return addrs[0]
if ok {
addrs, err := network.GetNetworkAddress(addressType, network.AllAddresses)
if err == nil && len(addrs) > 0 {
return addrs[0]
}
}

log.Ctx(ctx).Error().Err(err).Stringer("AddressType", addressType).Msgf("unable to find address for type, using 127.0.0.1")
return "127.0.0.1"
return address
}
8 changes: 8 additions & 0 deletions pkg/publisher/local/publisher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ func (s *PublisherTestSuite) SetupTest() {
s.pub = local.NewLocalPublisher(s.ctx, s.baseDir, defaultHost, defaultPort)
}

func (s *PublisherTestSuite) TestAddressResolving() {
ctx := context.Background()

s.Require().Equal("127.0.0.1", local.ResolveAddress(ctx, "127.0.0.1"), "address did not resolve to itself")
s.Require().Equal("192.168.1.100", local.ResolveAddress(ctx, "192.168.1.100"), "address did not resolve to itself")
s.Require().Equal("127.0.0.1", local.ResolveAddress(ctx, "local"))
}

func (s *PublisherTestSuite) TestPublishFolder() {
source := s.T().TempDir()

Expand Down

0 comments on commit db89868

Please sign in to comment.