Skip to content

Commit e2d5791

Browse files
committed
Validate sidecar paths
1 parent 2067f3e commit e2d5791

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

src/CommunityToolkit.Aspire.Hosting.Dapr/Core/DaprDistributedApplicationLifecycleHook.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,31 @@ public async Task BeforeStartAsync(DistributedApplicationModel appModel, Cancell
5555

5656
var sidecarOptions = sidecarOptionsAnnotation?.Options;
5757

58+
void ValidatePaths(DaprSidecarOptions? options)
59+
{
60+
if (options?.RunFile is string runFile)
61+
{
62+
string runFilePath = Path.GetFullPath(Path.Combine(appHostDirectory, runFile));
63+
64+
if (!File.Exists(runFilePath))
65+
{
66+
throw new FileNotFoundException($"The run file '{runFile}' could not be found.");
67+
}
68+
}
69+
70+
if (options?.RuntimePath is string runtimePath)
71+
{
72+
string runtimeDirectory = Path.GetFullPath(Path.Combine(appHostDirectory, runtimePath));
73+
74+
if (!Directory.Exists(runtimeDirectory))
75+
{
76+
throw new DirectoryNotFoundException($"The runtime path '{runtimePath}' could not be found.");
77+
}
78+
}
79+
}
80+
81+
ValidatePaths(sidecarOptions);
82+
5883
[return: NotNullIfNotNull(nameof(path))]
5984
string? NormalizePath(string? path)
6085
{

tests/dapr-shared/DaprTests.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,58 @@ public async Task WithDaprSideCarAddsAnnotationBasedOnTheSidecarAppOptions(strin
166166
Assert.NotNull(container.Annotations.OfType<DaprSidecarAnnotation>());
167167
}
168168

169+
[Fact]
170+
public async Task WithDaprSidecar_InvalidRunFile_Throws()
171+
{
172+
using var builder = TestDistributedApplicationBuilder.Create();
173+
174+
builder.AddDapr(o =>
175+
{
176+
o.DaprPath = "dapr";
177+
});
178+
179+
builder.AddContainer("name", "image")
180+
.WithEndpoint("http", e =>
181+
{
182+
e.Port = 8000;
183+
e.AllocatedEndpoint = new(e, "localhost", 80);
184+
})
185+
.WithDaprSidecar(new DaprSidecarOptions
186+
{
187+
RunFile = Guid.NewGuid().ToString("N")
188+
});
189+
190+
using var app = builder.Build();
191+
192+
await Assert.ThrowsAsync<FileNotFoundException>(() => ExecuteBeforeStartHooksAsync(app, default));
193+
}
194+
195+
[Fact]
196+
public async Task WithDaprSidecar_InvalidRuntimePath_Throws()
197+
{
198+
using var builder = TestDistributedApplicationBuilder.Create();
199+
200+
builder.AddDapr(o =>
201+
{
202+
o.DaprPath = "dapr";
203+
});
204+
205+
builder.AddContainer("name", "image")
206+
.WithEndpoint("http", e =>
207+
{
208+
e.Port = 8000;
209+
e.AllocatedEndpoint = new(e, "localhost", 80);
210+
})
211+
.WithDaprSidecar(new DaprSidecarOptions
212+
{
213+
RuntimePath = Guid.NewGuid().ToString("N")
214+
});
215+
216+
using var app = builder.Build();
217+
218+
await Assert.ThrowsAsync<DirectoryNotFoundException>(() => ExecuteBeforeStartHooksAsync(app, default));
219+
}
220+
169221
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "ExecuteBeforeStartHooksAsync")]
170222
private static extern Task ExecuteBeforeStartHooksAsync(DistributedApplication app, CancellationToken cancellationToken);
171223
}

0 commit comments

Comments
 (0)