# hkb-site-traffic

Service role of the hkb-site-traffic job. Reads the site's access logs and metrics, writes the traffic page's data file.

- **Lives in:** [Prod](/architecture/prod/). **Trusted by:** `codebuild.amazonaws.com`.
- **Boundary:** `hkb-workload-boundary`.
- **Reach:** 9 named actions across 5 services (cloudwatch, kms, logs, s3, ssm); 1 statement on all resources.
- **Source:** `baseline/hkb-baseline-site-traffic.yaml`, resource `TrafficRole`.

## Trust policy

```yaml
AssumeRolePolicyDocument:
  Version: "2012-10-17"
  Statement:
    - Effect: Allow
      Principal:
        Service: codebuild.amazonaws.com
      Action: sts:AssumeRole
      Condition:
        StringEquals:
          aws:SourceAccount: !Ref AWS::AccountId
```

## Permissions

```yaml
Policies:
  - PolicyName: hkb-site-traffic
    PolicyDocument:
      Version: "2012-10-17"
      Statement:
        - Sid: ReadAccessLogs
          Effect: Allow
          Action:
            - s3:ListBucket
            - s3:GetObject
          Resource:
            - !Sub arn:${AWS::Partition}:s3:::hkb-site-logs-${LogArchiveAccountId}
            - !Sub arn:${AWS::Partition}:s3:::hkb-site-logs-${LogArchiveAccountId}/*
        - Sid: DecryptAccessLogs
          Effect: Allow
          Action: kms:Decrypt
          Resource: !Sub arn:${AWS::Partition}:kms:${AWS::Region}:${LogArchiveAccountId}:key/*
          Condition:
            StringEquals:
              kms:ViaService: !Sub s3.${AWS::Region}.amazonaws.com
        - Sid: ReadRequestMetrics
          Effect: Allow
          Action: cloudwatch:GetMetricData
          Resource: "*"
        - Sid: ReadSettings
          Effect: Allow
          Action: ssm:GetParameters
          Resource: !Sub arn:${AWS::Partition}:ssm:${AWS::Region}:${AWS::AccountId}:parameter/hkb/site/traffic_*
        # Only the data file; the site sync leaves it alone (--exclude in hkb-pipeline-site-sync).
        - Sid: WriteTrafficData
          Effect: Allow
          Action: s3:PutObject
          Resource: !Sub arn:${AWS::Partition}:s3:::hkb-site-${AWS::AccountId}/ops/*/data.js
        - Sid: ReadTourStops
          Effect: Allow
          Action: s3:GetObject
          Resource: !Sub arn:${AWS::Partition}:s3:::hkb-site-${AWS::AccountId}/tour/stops.txt
        # The site key (S3) and the default SSM key (the SecureString of addresses)
        - Sid: UseLocalKeys
          Effect: Allow
          Action:
            - kms:Decrypt
            - kms:GenerateDataKey
          Resource: !Sub arn:${AWS::Partition}:kms:${AWS::Region}:${AWS::AccountId}:key/*
          Condition:
            StringEquals:
              kms:ViaService:
                - !Sub s3.${AWS::Region}.amazonaws.com
                - !Sub ssm.${AWS::Region}.amazonaws.com
        - Sid: OwnLogs
          Effect: Allow
          Action:
            - logs:CreateLogStream
            - logs:PutLogEvents
          Resource: !Sub ${TrafficLogGroup.Arn}:*
```
