Skip to content

Commit 2303883

Browse files
committed
style: apply dotnet formatting
1 parent 91dde55 commit 2303883

8 files changed

Lines changed: 127 additions & 122 deletions

File tree

src/QueryFarm.VgiRpc.Client.Http/HttpRpcClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
using QueryFarm.VgiRpc.Http;
1010
using QueryFarm.VgiRpc.Logging;
1111
using QueryFarm.VgiRpc.Reflection;
12-
using QueryFarm.VgiRpc.Wire;
1312
using QueryFarm.VgiRpc.Transport;
13+
using QueryFarm.VgiRpc.Wire;
1414

1515
namespace QueryFarm.VgiRpc.Client.Http;
1616

src/QueryFarm.VgiRpc.Http/PeerIdentityAuthentication.cs

Lines changed: 112 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -72,132 +72,132 @@ public static RpcHttpEndpoints.AuthenticateDelegate Compose(
7272
var providerSlots = new SemaphoreSlim(maxProviderConcurrency, maxProviderConcurrency);
7373
return async context =>
7474
{
75-
AuthFailure? missing = null;
76-
if (applicationAuthenticate is not null)
77-
{
78-
try
75+
AuthFailure? missing = null;
76+
if (applicationAuthenticate is not null)
7977
{
80-
await applicationAuthenticate(context).ConfigureAwait(false);
78+
try
79+
{
80+
await applicationAuthenticate(context).ConfigureAwait(false);
81+
}
82+
catch (AuthFailure failure) when (failure.Reason == AuthReason.MissingCredential)
83+
{
84+
missing = failure;
85+
}
8186
}
82-
catch (AuthFailure failure) when (failure.Reason == AuthReason.MissingCredential)
83-
{
84-
missing = failure;
85-
}
86-
}
8787

88-
var identity = AuthIdentity.GetFrom(context);
89-
var existing = identity is null
90-
? AuthContext.Anonymous
91-
: new AuthContext(identity.Domain, identity.Authenticated, identity.Principal);
92-
using var deadline = CancellationTokenSource.CreateLinkedTokenSource(context.RequestAborted);
93-
deadline.CancelAfter(providerTimeout);
94-
var headers = context.Request.Headers.ToDictionary(
95-
pair => pair.Key,
96-
pair => (IReadOnlyList<string>)Array.AsReadOnly(pair.Value.Select(value => value ?? "").ToArray()),
97-
StringComparer.OrdinalIgnoreCase);
98-
var physical = context.Items[PhysicalPeerItem] as PhysicalConnection;
99-
var resolution = physical is null ? null : new PeerResolutionContext(
100-
"http",
101-
physical.RemoteAddress,
102-
destinationAddress: FormatAddress(physical.LocalAddress, physical.LocalPort),
103-
authority: context.Request.Host.Value,
104-
serviceName: serviceName,
105-
headers: headers,
106-
metadata: new Dictionary<string, object?> { ["request_path"] = context.Request.Path.Value ?? "" },
107-
deadline: DateTimeOffset.UtcNow + providerTimeout,
108-
sourceEndpoint: FormatAddress(physical.RemoteAddress, physical.RemotePort));
109-
var results = resolution is null
110-
? providers.Select(provider =>
111-
new PeerIdentityResult(provider.Provider, PeerIdentityStatus.Unavailable)).ToArray()
112-
: await Task.WhenAll(providers.Select(async provider =>
113-
{
114-
if (!await providerSlots.WaitAsync(0, context.RequestAborted).ConfigureAwait(false))
115-
return new PeerIdentityResult(provider.Provider, PeerIdentityStatus.Unavailable);
116-
Task<PeerIdentityResult> providerTask;
117-
try
118-
{
119-
providerTask = provider.ResolveAsync(resolution, deadline.Token).AsTask();
120-
}
121-
catch (PeerIdentityRejectedException)
88+
var identity = AuthIdentity.GetFrom(context);
89+
var existing = identity is null
90+
? AuthContext.Anonymous
91+
: new AuthContext(identity.Domain, identity.Authenticated, identity.Principal);
92+
using var deadline = CancellationTokenSource.CreateLinkedTokenSource(context.RequestAborted);
93+
deadline.CancelAfter(providerTimeout);
94+
var headers = context.Request.Headers.ToDictionary(
95+
pair => pair.Key,
96+
pair => (IReadOnlyList<string>)Array.AsReadOnly(pair.Value.Select(value => value ?? "").ToArray()),
97+
StringComparer.OrdinalIgnoreCase);
98+
var physical = context.Items[PhysicalPeerItem] as PhysicalConnection;
99+
var resolution = physical is null ? null : new PeerResolutionContext(
100+
"http",
101+
physical.RemoteAddress,
102+
destinationAddress: FormatAddress(physical.LocalAddress, physical.LocalPort),
103+
authority: context.Request.Host.Value,
104+
serviceName: serviceName,
105+
headers: headers,
106+
metadata: new Dictionary<string, object?> { ["request_path"] = context.Request.Path.Value ?? "" },
107+
deadline: DateTimeOffset.UtcNow + providerTimeout,
108+
sourceEndpoint: FormatAddress(physical.RemoteAddress, physical.RemotePort));
109+
var results = resolution is null
110+
? providers.Select(provider =>
111+
new PeerIdentityResult(provider.Provider, PeerIdentityStatus.Unavailable)).ToArray()
112+
: await Task.WhenAll(providers.Select(async provider =>
122113
{
123-
providerSlots.Release();
124-
return new PeerIdentityResult(provider.Provider, PeerIdentityStatus.Invalid);
125-
}
126-
catch (PeerIdentityUnavailableException)
127-
{
128-
providerSlots.Release();
129-
return new PeerIdentityResult(provider.Provider, PeerIdentityStatus.Unavailable);
130-
}
131-
catch
132-
{
133-
providerSlots.Release();
134-
throw new InvalidOperationException("peer identity provider failed");
135-
}
114+
if (!await providerSlots.WaitAsync(0, context.RequestAborted).ConfigureAwait(false))
115+
return new PeerIdentityResult(provider.Provider, PeerIdentityStatus.Unavailable);
116+
Task<PeerIdentityResult> providerTask;
117+
try
118+
{
119+
providerTask = provider.ResolveAsync(resolution, deadline.Token).AsTask();
120+
}
121+
catch (PeerIdentityRejectedException)
122+
{
123+
providerSlots.Release();
124+
return new PeerIdentityResult(provider.Provider, PeerIdentityStatus.Invalid);
125+
}
126+
catch (PeerIdentityUnavailableException)
127+
{
128+
providerSlots.Release();
129+
return new PeerIdentityResult(provider.Provider, PeerIdentityStatus.Unavailable);
130+
}
131+
catch
132+
{
133+
providerSlots.Release();
134+
throw new InvalidOperationException("peer identity provider failed");
135+
}
136+
try
137+
{
138+
var result = await providerTask.WaitAsync(providerTimeout, context.RequestAborted).ConfigureAwait(false);
139+
if (result is null || !StringComparer.Ordinal.Equals(provider.Provider, result.Provider))
140+
throw new InvalidOperationException("peer identity provider result mismatch");
141+
providerSlots.Release();
142+
return result;
143+
}
144+
catch (TimeoutException)
145+
{
146+
ReleaseProviderSlotWhenComplete(providerTask, providerSlots);
147+
return new PeerIdentityResult(provider.Provider, PeerIdentityStatus.Unavailable);
148+
}
149+
catch (OperationCanceledException) when (!context.RequestAborted.IsCancellationRequested)
150+
{
151+
if (providerTask.IsCompleted) providerSlots.Release();
152+
else ReleaseProviderSlotWhenComplete(providerTask, providerSlots);
153+
return new PeerIdentityResult(provider.Provider, PeerIdentityStatus.Unavailable);
154+
}
155+
catch (OperationCanceledException)
156+
{
157+
if (providerTask.IsCompleted) providerSlots.Release();
158+
else ReleaseProviderSlotWhenComplete(providerTask, providerSlots);
159+
throw;
160+
}
161+
catch (PeerIdentityRejectedException)
162+
{
163+
providerSlots.Release();
164+
return new PeerIdentityResult(provider.Provider, PeerIdentityStatus.Invalid);
165+
}
166+
catch (PeerIdentityUnavailableException)
167+
{
168+
providerSlots.Release();
169+
return new PeerIdentityResult(provider.Provider, PeerIdentityStatus.Unavailable);
170+
}
171+
catch
172+
{
173+
providerSlots.Release();
174+
throw new InvalidOperationException("peer identity provider failed");
175+
}
176+
})).ConfigureAwait(false);
177+
178+
var evidence = new PeerEvidenceSet(results);
179+
AuthContext auth;
136180
try
137181
{
138-
var result = await providerTask.WaitAsync(providerTimeout, context.RequestAborted).ConfigureAwait(false);
139-
if (result is null || !StringComparer.Ordinal.Equals(provider.Provider, result.Provider))
140-
throw new InvalidOperationException("peer identity provider result mismatch");
141-
providerSlots.Release();
142-
return result;
143-
}
144-
catch (TimeoutException)
145-
{
146-
ReleaseProviderSlotWhenComplete(providerTask, providerSlots);
147-
return new PeerIdentityResult(provider.Provider, PeerIdentityStatus.Unavailable);
148-
}
149-
catch (OperationCanceledException) when (!context.RequestAborted.IsCancellationRequested)
150-
{
151-
if (providerTask.IsCompleted) providerSlots.Release();
152-
else ReleaseProviderSlotWhenComplete(providerTask, providerSlots);
153-
return new PeerIdentityResult(provider.Provider, PeerIdentityStatus.Unavailable);
154-
}
155-
catch (OperationCanceledException)
156-
{
157-
if (providerTask.IsCompleted) providerSlots.Release();
158-
else ReleaseProviderSlotWhenComplete(providerTask, providerSlots);
159-
throw;
182+
auth = await policy(evidence, existing).ConfigureAwait(false);
160183
}
161184
catch (PeerIdentityRejectedException)
162185
{
163-
providerSlots.Release();
164-
return new PeerIdentityResult(provider.Provider, PeerIdentityStatus.Invalid);
186+
throw new AuthFailure(AuthReason.InvalidCredential, "peer identity rejected");
165187
}
166-
catch (PeerIdentityUnavailableException)
188+
catch (PeerIdentityUnavailableException unavailable)
167189
{
168-
providerSlots.Release();
169-
return new PeerIdentityResult(provider.Provider, PeerIdentityStatus.Unavailable);
190+
throw new PeerIdentityUnavailableException(
191+
"peer identity unavailable", unavailable.RetryAfterSeconds);
170192
}
171-
catch
193+
if (missing is not null && !auth.Authenticated) throw missing;
194+
context.Items[AuthItem] = auth;
195+
context.Items[EvidenceItem] = evidence;
196+
var binding = EvidenceBinding(auth);
197+
if (auth.Authenticated || binding is not null)
172198
{
173-
providerSlots.Release();
174-
throw new InvalidOperationException("peer identity provider failed");
199+
AuthIdentity.SetOn(context, auth.Domain, auth.Principal ?? "", binding, auth.Authenticated);
175200
}
176-
})).ConfigureAwait(false);
177-
178-
var evidence = new PeerEvidenceSet(results);
179-
AuthContext auth;
180-
try
181-
{
182-
auth = await policy(evidence, existing).ConfigureAwait(false);
183-
}
184-
catch (PeerIdentityRejectedException)
185-
{
186-
throw new AuthFailure(AuthReason.InvalidCredential, "peer identity rejected");
187-
}
188-
catch (PeerIdentityUnavailableException unavailable)
189-
{
190-
throw new PeerIdentityUnavailableException(
191-
"peer identity unavailable", unavailable.RetryAfterSeconds);
192-
}
193-
if (missing is not null && !auth.Authenticated) throw missing;
194-
context.Items[AuthItem] = auth;
195-
context.Items[EvidenceItem] = evidence;
196-
var binding = EvidenceBinding(auth);
197-
if (auth.Authenticated || binding is not null)
198-
{
199-
AuthIdentity.SetOn(context, auth.Domain, auth.Principal ?? "", binding, auth.Authenticated);
200-
}
201201
};
202202
}
203203

src/QueryFarm.VgiRpc.Http/TailscaleLocalApiProvider.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ public async ValueTask<PeerIdentityResult> ResolveAsync(PeerResolutionContext co
9595
}
9696
catch (TailscaleLocalApiPermissionException) { return Result(PeerIdentityStatus.PermissionDenied); }
9797
catch (Exception exception) when (exception is HttpRequestException or IOException
98-
or OperationCanceledException) { return Result(PeerIdentityStatus.Unavailable); }
98+
or OperationCanceledException)
99+
{ return Result(PeerIdentityStatus.Unavailable); }
99100
catch (Exception exception) when (exception is JsonException or ArgumentException)
100101
{ return Result(PeerIdentityStatus.Invalid); }
101102
}

src/QueryFarm.VgiRpc/Identity/PeerIdentity.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,14 +268,18 @@ public int Compare(string[]? left, string[]? right)
268268

269269
private static string Wire(PeerIdentityStatus value) => value switch
270270
{
271-
PeerIdentityStatus.NotApplicable => "not_applicable", PeerIdentityStatus.PermissionDenied => "permission_denied",
272-
PeerIdentityStatus.NoMatch => "no_match", PeerIdentityStatus.UntrustedProxy => "untrusted_proxy",
271+
PeerIdentityStatus.NotApplicable => "not_applicable",
272+
PeerIdentityStatus.PermissionDenied => "permission_denied",
273+
PeerIdentityStatus.NoMatch => "no_match",
274+
PeerIdentityStatus.UntrustedProxy => "untrusted_proxy",
273275
_ => value.ToString().ToLowerInvariant(),
274276
};
275277
private static string Wire(IdentityAssurance value) => value switch
276278
{
277-
IdentityAssurance.CryptographicPeer => "cryptographic_peer", IdentityAssurance.LocalDaemon => "local_daemon",
278-
IdentityAssurance.ConfiguredProxy => "configured_proxy", _ => throw new ArgumentOutOfRangeException(nameof(value)),
279+
IdentityAssurance.CryptographicPeer => "cryptographic_peer",
280+
IdentityAssurance.LocalDaemon => "local_daemon",
281+
IdentityAssurance.ConfiguredProxy => "configured_proxy",
282+
_ => throw new ArgumentOutOfRangeException(nameof(value)),
279283
};
280284
private static string Wire(PeerSubjectKind value) => value == PeerSubjectKind.TaggedNode ? "tagged_node" : value.ToString().ToLowerInvariant();
281285
private static string Wire(SubjectStability value) => value.ToString().ToLowerInvariant();

src/QueryFarm.VgiRpc/Server/ICallContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using QueryFarm.VgiRpc.Errors;
2-
using QueryFarm.VgiRpc.Logging;
32
using QueryFarm.VgiRpc.Identity;
3+
using QueryFarm.VgiRpc.Logging;
44

55
namespace QueryFarm.VgiRpc.Server;
66

test/QueryFarm.VgiRpc.Http.Tests/PeerIdentityAuthenticationTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using Microsoft.AspNetCore.Http;
21
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.AspNetCore.Http;
33
using Microsoft.Extensions.DependencyInjection;
44
using QueryFarm.VgiRpc.Http;
55
using QueryFarm.VgiRpc.Identity;

test/QueryFarm.VgiRpc.Http.Tests/TailscaleLocalApiProviderTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
using System.IO.Pipes;
12
using System.Net;
23
using System.Net.Sockets;
3-
using System.IO.Pipes;
44
using System.Text;
55
using QueryFarm.VgiRpc.Http;
66
using QueryFarm.VgiRpc.Identity;

test/QueryFarm.VgiRpc.Tests/Identity/PeerIdentityTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
using System.Text.Json;
12
using QueryFarm.VgiRpc.Identity;
23
using QueryFarm.VgiRpc.Logging;
34
using QueryFarm.VgiRpc.Server;
4-
using System.Text.Json;
55
using Xunit;
66

77
namespace QueryFarm.VgiRpc.Tests.Identity;

0 commit comments

Comments
 (0)