Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
164 lines (144 loc) · 7.53 KB
/
Copy pathProgram.cs
File metadata and controls
164 lines (144 loc) · 7.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// A Scry server that keeps a record of everything it sends: the disclosure audit, kept in SQL Server
// in the application's own database, with the explorer that reads it back.
//
// dotnet run --project samples/Sample.DisclosureServer
//
// Open it, be alice, and ask a few queries in the query explorer. Then be the auditor and open
// /scry-disclosures: who was sent which row, what a caller received, and whether a caller ever saw
// a member — see /docs/disclosure-audit.md.
//
// A host of its own, apart from Sample.WebServer, so that the record holds nothing but what was
// asked of it here, and so that the larger sample runs with nothing recorded.
class Program
{
// A LocalDB instance of this sample's own. The other samples and the test projects share one,
// and a second process building the same template while another holds it deadlocks inside SQL Server.
static SqlInstance<SampleContext> sqlInstance = new(
constructInstance: _ => new(_.Options),
buildTemplate: _ =>
{
SampleContext.Initialize(_);
return Task.CompletedTask;
},
storage: Storage.FromSuffix<SampleContext>("Disclosure"));
static async Task Main(string[] args)
{
// Named after this method, unless the launcher names it: the browser suite runs several of
// these at once, as it does of Sample.WebServer.
var name = Environment.GetEnvironmentVariable("SAMPLE_DATABASE");
SqlDatabase<SampleContext> database;
if (name is null)
{
database = await sqlInstance.Build();
}
else
{
database = await sqlInstance.Build(name);
}
var builder = WebApplication.CreateBuilder(args);
var services = builder.Services;
services.AddDbContext<SampleContext>(_ => _.UseSqlServer(database.ConnectionString));
// The clock the record is timed by: the system's, unless the launcher pins one. The browser
// suite does, so that a screenshot of the record shows the same times on every run.
var clock = Clock();
// What says nothing has changed since an answer was given: when this server started, since
// nothing writes to its rows after that.
var started = DateTimeOffset.UtcNow.Ticks.ToString(CultureInfo.InvariantCulture);
// begin-snippet: sampleDisclosureAudit
services
.AddScry<SampleContext>(_ =>
{
// What the model needs before any server will start: see Sample.WebServer.
_.AddPocoSource(_ => Holiday.Seed());
_.AddAttachmentPolicy<Department, SignedIn<Department>>();
_.AddAttachmentPolicy<Employee, SignedIn<Employee>>();
// Every answer is a committed row in this database before it is sent, and an answer
// the database does not take is not given. The application's own database, so that
// recording adds nothing that can be down to the path of an answer.
_.UseSqlServerDisclosureAudit(
database.ConnectionString,
store =>
{
// The record is kept as a log is: a new segment of it each month, and
// two years of them. The oldest is then dropped, and what it held can no
// longer be asked. Set Retire to write it somewhere else first.
store.RollAfter = TimeSpan.FromDays(30);
store.Keep = 24;
store.Clock = clock;
},
audit =>
{
audit.Node = "sample";
audit.Clock = clock;
// A row is recorded by its key, and a source with none has to be given one or
// owned up to: a view by what its rows are grouped on.
audit.Key<EmployeeSummary>(_ => _.Department);
// Left out of the record: a calendar is nobody's to ask after, and neither is
// the logo or the handbook a department publishes. An answer of nothing but
// these is sent as it would be with the audit off. One that reads anything
// else is recorded, with a marker where a holiday, a logo or a handbook was.
audit.Exclude<Holiday>();
audit.Exclude<Department>(_ => _.Logo, _ => _.Handbook);
});
// A query asked by URL may be kept by the caller's own cache, which has to ask before
// using its copy again. Being told the copy still stands is a 304, and is recorded
// before it is said. Nothing writes to this sample's rows once it has started, so an
// answer stands for as long as the server does. A real host reads a change marker
// here, as Sample.WebServer does with Delta — and then keeps the record in a database
// of its own: Delta reads the database's log position, which every answer recorded
// beside the rows would move, so no recorded answer would ever be found unchanged.
_.QueryFreshness = (_, _) => new(started);
// Whose cache an answer belongs in: the caller's, since what is sent depends on who asks.
_.CacheScope = _ => _.User.Identity?.Name;
});
// end-snippet
var app = builder.Build();
app.UseDemoSignIn();
app.MapDemoSignIn();
app.MapScry("/api/query");
// Something to ask with. Open to anybody with a name, since what it asks goes through the
// same endpoint, policies and record as any other client's.
app.MapScryExplorer(_ =>
{
_.Route = "/scry";
_.EnableGuard = _ => _.User.Identity?.IsAuthenticated == true;
});
// begin-snippet: mapDisclosureExplorer
// The record, read back. Shut to everybody but the one person whose job it is — a real host
// puts its own authorization here, and RequireAuthorization on what this returns.
app.MapScryDisclosureExplorer(_ =>
{
_.EnableGuard = DemoSignIn.IsReviewer;
_.EnableExport = DemoSignIn.IsReviewer;
// Off unless a host turns it on, and it cannot be taken back: on here so there is
// something to try it against.
_.EnableErase = DemoSignIn.IsReviewer;
});
// end-snippet
await app.RunAsync();
}
static TimeProvider Clock()
{
if (DateTimeOffset.TryParse(Environment.GetEnvironmentVariable("SAMPLE_CLOCK"), out var start))
{
return new Pinned(start);
}
return TimeProvider.System;
}
// Starts where it is told and says a second later each time it is asked.
sealed class Pinned(DateTimeOffset start) :
TimeProvider
{
long asked;
public override DateTimeOffset GetUtcNow() =>
start.AddSeconds(Interlocked.Increment(ref asked));
}
// The check that authorizes an attachment's fetch: anybody with a name. A source exposing an
// attachment with no check refuses to start — see Sample.WebServer's HandbookPolicy.
sealed class SignedIn<TSource> :
IAttachmentPolicy<TSource>
{
public bool Authorize(ScryAttachmentContext context) =>
context.Services.GetService<IHttpContextAccessor>()?.HttpContext?.User.Identity?.IsAuthenticated == true;
}
}