Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions library/agent/Agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1558,3 +1558,102 @@ t.test("it sends heartbeat when shutdown is called", async () => {

clock.uninstall();
});

t.test("it sends stats for wildcard routes", async () => {
const clock = FakeTimers.install();

const logger = new LoggerNoop();
const api = new ReportingAPIForTesting();
const agent = createTestAgent({
api,
logger,
token: new Token("123"),
suppressConsoleLog: false,
});
agent.start([]);

agent.getConfig().updateDomains(
[
{ hostname: "*.aikido.dev", mode: "allow" },
{ hostname: "google.com", mode: "block" },
{ hostname: "example.com", mode: "block" },
{ hostname: "aikido.dev", mode: "allow" },
],
false
);

agent.onConnectHostname("aikido.dev", 443);
agent.onConnectHostname("aikido.dev", 80);
agent.onConnectHostname("google.com", 443);
agent.onConnectHostname("example.com", 443);
agent.onConnectHostname("test.aikido.dev", 443);
agent.onConnectHostname("test.aikido.dev", 80);
agent.onConnectHostname("sub.test.aikido.dev", 80);
agent.onConnectHostname("sub.test.aikido.dev", 443);

api.clear();

await agent.flushStats(1000);

t.match(api.getEvents(), [
{
type: "heartbeat",
middlewareInstalled: false,
hostnames: [
{
hostname: "aikido.dev",
port: 443,
hits: 1,
},
{
hostname: "aikido.dev",
port: 80,
hits: 1,
},
{
hostname: "google.com",
port: 443,
hits: 1,
},
{
hostname: "example.com",
port: 443,
hits: 1,
},
{
hostname: "test.aikido.dev",
port: 443,
hits: 1,
},
{
hostname: "test.aikido.dev",
port: 80,
hits: 1,
},
{
hostname: "*.aikido.dev",
port: 443,
hits: 2,
},
{
hostname: "*.aikido.dev",
port: 80,
hits: 2,
},
{
hostname: "sub.test.aikido.dev",
port: 80,
hits: 1,
},
{
hostname: "sub.test.aikido.dev",
port: 443,
hits: 1,
},
],
routes: [],
},
]);

clock.uninstall();
});
12 changes: 10 additions & 2 deletions library/agent/Agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,10 +333,10 @@ export class Agent {
response.domains &&
Array.isArray(response.domains)
) {
this.serviceConfig.setBlockNewOutgoingRequests(
this.serviceConfig.updateDomains(
response.domains,
response.blockNewOutgoingRequests
);
this.serviceConfig.updateDomains(response.domains);
}

if (
Expand Down Expand Up @@ -608,6 +608,14 @@ export class Agent {

onConnectHostname(hostname: string, port: number) {
this.hostnames.add(hostname, port);

// Also report stats for wildcard domains
// e.g. if "sub.example.com" is accessed, we also want to report stats for "*.example.com"
const matchingWildcardDomain =
this.serviceConfig.getMatchingWildcardDomain(hostname);
if (matchingWildcardDomain) {
this.hostnames.add(matchingWildcardDomain.domain, port);
}
}

onRouteExecute(context: Context) {
Expand Down
Loading