@fntio/infra-client
Version:
Self-hosted AWS access portal: credential vending, target-account trust, and grant-driven private networking as CDK stacks and constructs.
134 lines (133 loc) • 6.78 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.Monitoring = void 0;
const cdk = __importStar(require("aws-cdk-lib"));
const constructs_1 = require("constructs");
const cloudwatch = __importStar(require("aws-cdk-lib/aws-cloudwatch"));
const cwactions = __importStar(require("aws-cdk-lib/aws-cloudwatch-actions"));
const events = __importStar(require("aws-cdk-lib/aws-events"));
const targets = __importStar(require("aws-cdk-lib/aws-events-targets"));
const logs = __importStar(require("aws-cdk-lib/aws-logs"));
const sns = __importStar(require("aws-cdk-lib/aws-sns"));
const subs = __importStar(require("aws-cdk-lib/aws-sns-subscriptions"));
const app_function_1 = require("./app-function");
const objects_1 = require("../../lambdacode/issuer/objects");
/** Lambda error/throttle alarms, a DENIED-spike alarm, and an issuer health
* canary — all fanned out through one SNS topic. */
class Monitoring extends constructs_1.Construct {
/** All alarms notify here; add subscriptions to add recipients. */
topic;
constructor(scope, id, props) {
super(scope, id);
this.topic = new sns.Topic(this, "AlertTopic", {
displayName: "Infra ops alerts",
enforceSSL: true,
});
for (const email of props.alertEmails) {
this.topic.addSubscription(new subs.EmailSubscription(email));
}
const notify = new cwactions.SnsAction(this.topic);
const fnAlarm = (fn, name, metric, what) => {
const alarm = new cloudwatch.Alarm(this, name, {
metric,
threshold: 1,
evaluationPeriods: 1,
comparisonOperator: cloudwatch.ComparisonOperator.GREATER_THAN_OR_EQUAL_TO_THRESHOLD,
treatMissingData: cloudwatch.TreatMissingData.NOT_BREACHING,
alarmDescription: `${what} on ${fn.functionName} — check its CloudWatch logs.`,
});
alarm.addAlarmAction(notify);
return alarm;
};
const vendingFn = props.vendingFn;
fnAlarm(vendingFn, "VendApiErrors", vendingFn.metricErrors({ period: cdk.Duration.minutes(5) }), "Invocation errors");
fnAlarm(vendingFn, "VendApiThrottles", vendingFn.metricThrottles({ period: cdk.Duration.minutes(5) }), "Throttled invocations");
// Occasional denials are normal user error; a burst is a token
// systematically probing roles it doesn't own.
const denied = new logs.MetricFilter(this, "DeniedFilter", {
logGroup: props.vendingFn.logGroup,
filterPattern: logs.FilterPattern.stringValue("$.msg", "=", "DENIED*"),
metricNamespace: "InfraClient",
metricName: "VendDenied",
metricValue: "1",
});
const deniedAlarm = new cloudwatch.Alarm(this, "DeniedSpike", {
metric: denied.metric({
period: cdk.Duration.minutes(15),
statistic: "sum",
}),
threshold: 5,
evaluationPeriods: 1,
comparisonOperator: cloudwatch.ComparisonOperator.GREATER_THAN_OR_EQUAL_TO_THRESHOLD,
treatMissingData: cloudwatch.TreatMissingData.NOT_BREACHING,
alarmDescription: "5+ DENIED vend attempts in 15 min — possible probing with a leaked or " +
"over-curious token. Check the Audit table / API logs for the userId.",
});
deniedAlarm.addAlarmAction(notify);
// Outside-in canary: the issuer is static objects behind CloudFront, so
// only an end-to-end probe (like STS does) sees DNS/cert/serving failures
// — and a JWKS missing the active key.
const canary = new app_function_1.AppFunction(this, "IssuerCanary", {
description: "Health probe: fetches the public credential issuer every minute and checks its signing key.",
asset: "issuer-canary",
handler: "handler",
timeout: cdk.Duration.seconds(10),
environment: {
CHECK_URL: `${props.issuerUrl.replace(/\/$/, "")}/${objects_1.ISSUER_OBJECTS.discovery}`,
EXPECT_KID: props.signingKeyId,
},
});
new events.Rule(this, "CanarySchedule", {
description: "Probe the public issuer discovery document every minute.",
schedule: events.Schedule.rate(cdk.Duration.minutes(1)),
targets: [new targets.LambdaFunction(canary)],
});
const issuerHealth = new cloudwatch.Alarm(this, "IssuerHealth", {
metric: canary.metricErrors({ period: cdk.Duration.minutes(5) }),
threshold: 2,
evaluationPeriods: 1,
comparisonOperator: cloudwatch.ComparisonOperator.GREATER_THAN_OR_EQUAL_TO_THRESHOLD,
treatMissingData: cloudwatch.TreatMissingData.NOT_BREACHING,
alarmDescription: "The public issuer discovery URL is failing — vending will fail " +
"everywhere until this recovers. Check DNS, the certificate, the " +
"issuer CloudFront distribution, then the issuer bucket's objects " +
"(runbook: wiki Runbooks, \"Issuer on S3 + CloudFront\").",
});
issuerHealth.addAlarmAction(notify);
issuerHealth.addOkAction(notify); // recovery email, since the outage is silent otherwise
}
}
exports.Monitoring = Monitoring;