# The alerts, one EventBridge rule each, matched on System Log event types # from Okta's event type catalog. locals { # Changes made by these actors do not alert. EventBridge matches the # actor's alternateId exactly. not_expected = length(var.expected_actors) == 0 ? {} : { actor = { alternateId = [{ "anything-but" = var.expected_actors }] } } detections = { admin-granted = { events = [ "user.account.privilege.grant", "group.privilege.grant", "app.oauth2.client.privilege.grant", "iam.resourceset.bindings.add", "iam.role.create", "iam.role.permissions.add", "system.api_token.create", "system.api_token.enable", ] skip_expected = true doc = "An admin role was granted to a user, group, or app, a custom role was made or widened, or an API token was made." } sign-in-changed = { events = [ "system.idp.lifecycle.create", "system.idp.lifecycle.update", "system.idp.lifecycle.activate", "system.idp.key.create", "policy.lifecycle.update", "policy.lifecycle.delete", "policy.lifecycle.deactivate", "policy.rule.update", "policy.rule.delete", "policy.rule.deactivate", "application.policy.sign_on.update", "application.policy.sign_on.rule.delete", "security.authenticator.lifecycle.update", "security.authenticator.lifecycle.deactivate", "system.mfa.factor.deactivate", ] skip_expected = true doc = "An identity provider, sign-on policy, policy rule, or authenticator was added, changed, or switched off." } mfa-removed = { events = ["user.mfa.factor.reset_all", "user.mfa.factor.deactivate", "user.mfa.attempt_bypass"] skip_expected = false doc = "A user's MFA factors were reset or removed, or someone tried to bypass a factor." } impersonation = { events = ["user.session.impersonation.grant", "user.session.impersonation.initiate"] skip_expected = false doc = "Okta support was granted access to the org, or started a session as a user." } logging-weakened = { events = [ "system.log_stream.lifecycle.update", "system.log_stream.lifecycle.deactivate", "system.log_stream.lifecycle.delete", "event_hook.deactivated", "event_hook.deleted", ] skip_expected = true doc = "A log stream or event hook was changed, deactivated, or deleted." } # A session that changes context or roams is how a stolen session cookie # shows up. The API token event needs a network zone set on the token. session-moved = { events = ["user.session.context.change", "security.session.detect_client_roaming", "system.api_token.request_outside_allowed_range"] skip_expected = false doc = "A user's session changed context or moved to another network, or an API token was used from outside its allowed zone." } } } resource "aws_cloudwatch_event_rule" "detection" { for_each = local.detections name = "okta-${each.key}" description = each.value.doc event_bus_name = aws_cloudwatch_event_bus.okta.name state = "ENABLED" event_pattern = jsonencode({ source = [local.source] detail = merge({ eventType = each.value.events }, each.value.skip_expected ? local.not_expected : {}) }) lifecycle { precondition { condition = length(jsonencode({ source = [local.source], detail = merge({ eventType = each.value.events }, local.not_expected) })) <= 2048 error_message = "The okta-${each.key} event pattern is over EventBridge's 2,048 character limit. Shorten expected_actors." } } } # The email names the event, who did it, from where, and to whom. resource "aws_cloudwatch_event_target" "detection" { for_each = local.detections rule = aws_cloudwatch_event_rule.detection[each.key].name event_bus_name = aws_cloudwatch_event_bus.okta.name arn = aws_sns_topic.alerts.arn input_transformer { input_paths = { type = "$.detail.eventType" message = "$.detail.displayMessage" actor = "$.detail.actor.alternateId" ip = "$.detail.client.ipAddress" target = "$.detail.target[0].alternateId" result = "$.detail.outcome.result" time = "$.detail.published" } input_template = "\"Okta ${each.key}: (, ) by from , on , at