diff --git a/.env.example b/.env.example index d72ff32e..d9268ad6 100644 --- a/.env.example +++ b/.env.example @@ -12,4 +12,5 @@ FCM_API_KEY= # APNS APNS_SANDBOX=false APNS_CERTIFICATE= # base64 encoded .p12 APNS Certificate -APNS_CERTIFICATE_PASSWORD= # Password for provided certificate \ No newline at end of file +APNS_CERTIFICATE_PASSWORD= # Password for provided certificate +APNS_TOPIC= # bundle ID/app ID \ No newline at end of file diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index 7e7007e9..81040fde 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -117,6 +117,7 @@ jobs: env: TF_VAR_onepassword_vault_id: ${{ secrets.ONEPASSWORD_VAULT_ID }} TF_VAR_fcm_api_key: ${{ secrets.FCM_API_KEY }} + TF_VAR_apns_topic: ${{ secrets.APNS_TOPIC }} TF_VAR_apns_certificate: ${{ secrets.APNS_CERTIFICATE }} TF_VAR_apns_certificate_password: ${{ secrets.APNS_CERTIFICATE_PASSWORD }} TF_VAR_image_version: ${{ needs.get-version.outputs.version }} @@ -155,6 +156,7 @@ jobs: env: TF_VAR_onepassword_vault_id: ${{ secrets.ONEPASSWORD_VAULT_ID }} TF_VAR_fcm_api_key: ${{ secrets.FCM_API_KEY }} + TF_VAR_apns_topic: ${{ secrets.APNS_TOPIC }} TF_VAR_apns_certificate: ${{ secrets.APNS_CERTIFICATE }} TF_VAR_apns_certificate_password: ${{ secrets.APNS_CERTIFICATE_PASSWORD }} TF_VAR_image_version: ${{ needs.get-version.outputs.version }} diff --git a/.github/workflows/ci_terraform.yml b/.github/workflows/ci_terraform.yml index 31bcba73..4ac6d264 100644 --- a/.github/workflows/ci_terraform.yml +++ b/.github/workflows/ci_terraform.yml @@ -41,6 +41,7 @@ jobs: env: TF_VAR_onepassword_vault_id: ${{ secrets.ONEPASSWORD_VAULT_ID }} TF_VAR_fcm_api_key: ${{ secrets.FCM_API_KEY }} + TF_VAR_apns_topic: ${{ secrets.APNS_TOPIC }} TF_VAR_apns_certificate: ${{ secrets.APNS_CERTIFICATE }} TF_VAR_apns_certificate_password: ${{ secrets.APNS_CERTIFICATE_PASSWORD }} with: diff --git a/src/env.rs b/src/env.rs index b5e08634..3e80588c 100644 --- a/src/env.rs +++ b/src/env.rs @@ -20,6 +20,7 @@ pub struct Config { pub apns_sandbox: bool, pub apns_certificate: Option, pub apns_certificate_password: Option, + pub apns_topic: Option, // FCM pub fcm_api_key: Option, diff --git a/src/providers/apns.rs b/src/providers/apns.rs index 351170d7..4ed379d6 100644 --- a/src/providers/apns.rs +++ b/src/providers/apns.rs @@ -8,6 +8,7 @@ use tracing::span; #[derive(Debug, Clone)] pub struct ApnsProvider { client: a2::Client, + topic: String, } impl ApnsProvider { @@ -15,12 +16,14 @@ impl ApnsProvider { cert: &mut R, password: String, endpoint: a2::Endpoint, + topic: String, ) -> crate::error::Result where R: Read, { Ok(ApnsProvider { client: a2::Client::certificate(cert, password.as_str(), endpoint)?, + topic }) } } @@ -35,7 +38,8 @@ impl PushProvider for ApnsProvider { let s = span!(tracing::Level::DEBUG, "send_apns_notification"); let _ = s.enter(); - let opt = a2::NotificationOptions::default(); + let mut opt = a2::NotificationOptions::default(); + opt.apns_topic = Some(&self.topic); // TODO set title let notification = diff --git a/src/providers/mod.rs b/src/providers/mod.rs index bb776264..64c40397 100644 --- a/src/providers/mod.rs +++ b/src/providers/mod.rs @@ -83,7 +83,7 @@ impl PushProvider for Provider { &mut self, token: String, payload: MessagePayload, - ) -> crate::error::Result<()> { + ) -> error::Result<()> { let s = span!(tracing::Level::INFO, "send_notification"); let _ = s.enter(); match self { @@ -104,7 +104,7 @@ pub struct Providers { } impl Providers { - pub fn new(config: &Config) -> crate::error::Result { + pub fn new(config: &Config) -> error::Result { let supported = config.supported_providers(); let mut apns = None; if supported.contains(&ProviderKind::Apns) { @@ -113,13 +113,13 @@ impl Providers { false => a2::Endpoint::Production, }; apns = Some( - match (&config.apns_certificate, &config.apns_certificate_password) { - (Some(certificate), Some(password)) => { + match (&config.apns_certificate, &config.apns_certificate_password, &config.apns_topic) { + (Some(certificate), Some(password), Some(topic)) => { let decoded = base64::decode(certificate)?; let mut reader = BufReader::new(&*decoded); let apns_client = - ApnsProvider::new_cert(&mut reader, password.clone(), endpoint)?; + ApnsProvider::new_cert(&mut reader, password.clone(), endpoint, topic)?; Ok(apns_client) } @@ -147,7 +147,7 @@ impl Providers { pub fn get_provider( provider: ProviderKind, state: &AppState, -) -> crate::error::Result { +) -> error::Result { let name = provider.as_str(); let supported = state.config.supported_providers(); diff --git a/terraform/ecs/main.tf b/terraform/ecs/main.tf index 0f9a4039..25706715 100644 --- a/terraform/ecs/main.tf +++ b/terraform/ecs/main.tf @@ -51,6 +51,7 @@ resource "aws_ecs_task_definition" "app_task_definition" { { name = "TELEMETRY_GRPC_URL", value = "http://localhost:4317" }, { name = "FCM_API_KEY", value = var.fcm_api_key }, { name = "APNS_SANDBOX", value = terraform.workspace == "dev" ? "true" : "false" }, + { name = "APNS_TOPIC", value = var.apns_topic }, { name = "APNS_CERTIFICATE", value = var.apns_certificate }, { name = "APNS_CERTIFICATE_PASSWORD", value = var.apns_certificate_password } ], diff --git a/terraform/ecs/variables.tf b/terraform/ecs/variables.tf index d9c8b338..c3c8bcd4 100644 --- a/terraform/ecs/variables.tf +++ b/terraform/ecs/variables.tf @@ -59,6 +59,10 @@ variable "fcm_api_key" { sensitive = true } +variable "apns_topic" { + type = string +} + variable "apns_certificate" { type = string sensitive = true diff --git a/terraform/main.tf b/terraform/main.tf index 18363599..f7f7a221 100644 --- a/terraform/main.tf +++ b/terraform/main.tf @@ -94,6 +94,7 @@ module "ecs" { vpc_cidr = module.vpc.vpc_cidr_block vpc_id = module.vpc.vpc_id fcm_api_key = var.fcm_api_key + apns_topic = var.apns_topic apns_certificate = var.apns_certificate apns_certificate_password = var.apns_certificate_password } diff --git a/terraform/variables.tf b/terraform/variables.tf index 23301a07..fcde9482 100644 --- a/terraform/variables.tf +++ b/terraform/variables.tf @@ -35,6 +35,10 @@ variable "fcm_api_key" { sensitive = true } +variable "apns_topic" { + type = string +} + variable "apns_certificate" { type = string sensitive = true