Skip to content

Commit

Permalink
NoAuthentication option added for "LocalForward" in config (only) #77 (
Browse files Browse the repository at this point in the history
…#97)

* NoAuthentication option added for "LocalForward" in config (only) #77
  • Loading branch information
clemensv authored Aug 30, 2024
1 parent 4370819 commit 81cf5b6
Show file tree
Hide file tree
Showing 10 changed files with 295 additions and 103 deletions.
38 changes: 27 additions & 11 deletions CONFIG.md
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ and multiple entries are permitted.

* **RelayName** - name of the Azure Relay name to bind to
* **ConnectionString** - optional Azure Relay connection string to use just for this forwarder, overriding the global **AzureRelayConnectionString** property.
* **NoAuthentication** - optional, if set to true, the connection is made without authentication with the assumption that the hybrid connection is configured to not require it.

For a single port binding on the Relay name, the following properties can be
used on the same entry. For multiple bindings they can be used to form a list.
Expand All @@ -359,20 +360,32 @@ Examples:

- Single listener binding:
``` YAML
- RelayName: myrelay
BindAddress: 127.0.8.1
BindPort: 8888
LocalForward:
- RelayName: myrelay
BindAddress: 127.0.8.1
BindPort: 8888
```
- Single listener binding (no client authentication):
``` YAML
LocalForward:
- RelayName: myrelay
BindAddress: 127.0.8.1
BindPort: 8888
NoAuthentication: true
```
- Multiple listener binding:
``` YAML
- RelayName: myrelay
Bindings:
- BindAddress: 127.0.8.1
BindPort: 5671
PortName: amqps
- BindAddress: 127.0.8.1
BindPort: 5672
PortName: amqp
LocalForward:
- RelayName: myrelay
Bindings:
- BindAddress: 127.0.8.1
BindPort: 5671
PortName: amqps
- BindAddress: 127.0.8.1
BindPort: 5672
PortName: amqp
```
Expand Down Expand Up @@ -410,12 +423,15 @@ Examples:

- Single listener binding:
``` YAML
RemoteForward:
- RelayName: myrelay
Host: localhost
HostPort: 8888
```

- Multiple listener binding:
``` YAML
RemoteForward:
- RelayName: myrelay
Bindings:
- Host: broker.corp.example.com
Expand Down
26 changes: 26 additions & 0 deletions src/Microsoft.Azure.Relay.Bridge/Configuration/LocalForward.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,5 +219,31 @@ public List<LocalForwardBinding> Bindings
}
}
}

public bool NoAuthentication
{
get
{
if (bindings.Count == 1)
{
return bindings[0].NoAuthentication;
}
else
{
return false;
}
}
set
{
if (bindings.Count == 0)
{
bindings.Add(new LocalForwardBinding { NoAuthentication = value });
}
else
{
bindings[0].NoAuthentication = value;
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class LocalForwardBinding
private string bindAddress;
private string hostName;
private int bindPort;
private bool noAuthentication = false;
private string bindLocalSocket = null;

string portName;
Expand Down Expand Up @@ -190,5 +191,11 @@ public string BindLocalSocket
bindLocalSocket = val;
}
}

public bool NoAuthentication
{
get => noAuthentication;
set => noAuthentication = value;
}
}
}
6 changes: 3 additions & 3 deletions src/Microsoft.Azure.Relay.Bridge/LocalForwardHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void StartEndpoint(LocalForward localForward, LocalForwardBinding binding)
try
{
{
socketListenerBridge = SocketLocalForwardBridge.FromConnectionString(this.config, rcbs, binding.PortName);
socketListenerBridge = SocketLocalForwardBridge.FromConnectionString(this.config, rcbs, binding.PortName, binding.NoAuthentication);
socketListenerBridge.Run(binding.BindLocalSocket);

this.socketListenerBridges.Add(socketListenerBridge);
Expand Down Expand Up @@ -115,7 +115,7 @@ void StartEndpoint(LocalForward localForward, LocalForwardBinding binding)
if (bindToAddress != null)
{
tcpListenerBridge =
TcpLocalForwardBridge.FromConnectionString(this.config, rcbs, binding.PortName);
TcpLocalForwardBridge.FromConnectionString(this.config, rcbs, binding.PortName, binding.NoAuthentication);
tcpListenerBridge.Run(new IPEndPoint(bindToAddress, binding.BindPort));

this.listenerBridges.Add(tcpListenerBridge);
Expand Down Expand Up @@ -156,7 +156,7 @@ void StartEndpoint(LocalForward localForward, LocalForwardBinding binding)
if (bindToAddress != null)
{
udpListenerBridge =
UdpLocalForwardBridge.FromConnectionString(this.config, rcbs, binding.PortName);
UdpLocalForwardBridge.FromConnectionString(this.config, rcbs, binding.PortName, binding.NoAuthentication);
udpListenerBridge.Run(new IPEndPoint(bindToAddress, -binding.BindPort));

this.udpBridges.Add(udpListenerBridge);
Expand Down
12 changes: 8 additions & 4 deletions src/Microsoft.Azure.Relay.Bridge/SocketLocalForwardBridge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@ sealed class SocketLocalForwardBridge : IDisposable
Socket socketListener;
string localEndpoint;

public SocketLocalForwardBridge(Config config, RelayConnectionStringBuilder connectionString, string portName)
public SocketLocalForwardBridge(Config config, RelayConnectionStringBuilder connectionString, string portName, bool noAuth)
{
PortName = portName;
this.config = config;
if (connectionString.SharedAccessKeyName == null && connectionString.SharedAccessSignature == null)
if (noAuth)
{
this.hybridConnectionClient = new HybridConnectionClient(new Uri(connectionString.Endpoint, connectionString.EntityPath));
}
else if (string.IsNullOrEmpty(connectionString.SharedAccessKeyName) && string.IsNullOrEmpty(connectionString.SharedAccessSignature))
{
this.hybridConnectionClient = new HybridConnectionClient(new Uri(connectionString.Endpoint, connectionString.EntityPath), Host.DefaultAzureCredentialTokenProvider);
}
Expand All @@ -52,9 +56,9 @@ public SocketLocalForwardBridge(Config config, RelayConnectionStringBuilder conn
public HybridConnectionClient HybridConnectionClient => hybridConnectionClient;

public static SocketLocalForwardBridge FromConnectionString(Config config,
RelayConnectionStringBuilder connectionString, string bindingPortName)
RelayConnectionStringBuilder connectionString, string bindingPortName, bool noAuth)
{
return new SocketLocalForwardBridge(config, connectionString, bindingPortName);
return new SocketLocalForwardBridge(config, connectionString, bindingPortName, noAuth);
}

public void Close()
Expand Down
14 changes: 9 additions & 5 deletions src/Microsoft.Azure.Relay.Bridge/TcpLocalForwardBridge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Microsoft.Azure.Relay.Bridge
sealed class TcpLocalForwardBridge : IDisposable
{
public string PortName { get; }

private readonly Config config;
readonly CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();

Expand All @@ -26,11 +26,15 @@ sealed class TcpLocalForwardBridge : IDisposable
TcpListener tcpListener;
string localEndpoint;

public TcpLocalForwardBridge(Config config, RelayConnectionStringBuilder connectionString, string portName)
public TcpLocalForwardBridge(Config config, RelayConnectionStringBuilder connectionString, string portName, bool noAuth)
{
PortName = portName;
this.config = config;
if (connectionString.SharedAccessKeyName == null && connectionString.SharedAccessSignature == null)
if (noAuth)
{
this.hybridConnectionClient = new HybridConnectionClient(new Uri(connectionString.Endpoint, connectionString.EntityPath));
}
else if (string.IsNullOrEmpty(connectionString.SharedAccessKeyName) && string.IsNullOrEmpty(connectionString.SharedAccessSignature))
{
this.hybridConnectionClient = new HybridConnectionClient(new Uri(connectionString.Endpoint, connectionString.EntityPath), Host.DefaultAzureCredentialTokenProvider);
}
Expand All @@ -51,9 +55,9 @@ public TcpLocalForwardBridge(Config config, RelayConnectionStringBuilder connect
public HybridConnectionClient HybridConnectionClient => hybridConnectionClient;

public static TcpLocalForwardBridge FromConnectionString(Config config,
RelayConnectionStringBuilder connectionString, string portName)
RelayConnectionStringBuilder connectionString, string portName, bool noAuth)
{
return new TcpLocalForwardBridge(config, connectionString, portName);
return new TcpLocalForwardBridge(config, connectionString, portName, noAuth);
}

public void Close()
Expand Down
12 changes: 8 additions & 4 deletions src/Microsoft.Azure.Relay.Bridge/UdpLocalForwardBridge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ sealed class UdpLocalForwardBridge : IDisposable
UdpClient udpClient;
string localEndpoint;

public UdpLocalForwardBridge(Config config, RelayConnectionStringBuilder connectionString, string portName)
public UdpLocalForwardBridge(Config config, RelayConnectionStringBuilder connectionString, string portName, bool noAuth)
{
PortName = portName;
this.config = config;
if (connectionString.SharedAccessKeyName == null && connectionString.SharedAccessSignature == null)
if (noAuth)
{
this.hybridConnectionClient = new HybridConnectionClient(new Uri(connectionString.Endpoint, connectionString.EntityPath));
}
else if (string.IsNullOrEmpty(connectionString.SharedAccessKeyName) && string.IsNullOrEmpty(connectionString.SharedAccessSignature))
{
this.hybridConnectionClient = new HybridConnectionClient(new Uri(connectionString.Endpoint, connectionString.EntityPath), Host.DefaultAzureCredentialTokenProvider);
}
Expand All @@ -55,9 +59,9 @@ public UdpLocalForwardBridge(Config config, RelayConnectionStringBuilder connect
public HybridConnectionClient HybridConnectionClient => hybridConnectionClient;

public static UdpLocalForwardBridge FromConnectionString(Config config,
RelayConnectionStringBuilder connectionString, string portName)
RelayConnectionStringBuilder connectionString, string portName, bool noAuth)
{
return new UdpLocalForwardBridge(config, connectionString, portName);
return new UdpLocalForwardBridge(config, connectionString, portName, noAuth);
}

public void Close()
Expand Down
Loading

0 comments on commit 81cf5b6

Please sign in to comment.