From c2e350367b67d071644d7423e11247a8e3a7b4fe Mon Sep 17 00:00:00 2001 From: Marc PEREZ Date: Fri, 25 Jun 2021 22:43:39 +0200 Subject: [PATCH] Support for AWS SSO + custom attributes to configure SNS platform app This commit adds the possibility to pass an AWS SNS client, which can be initialized with AWS SSO credentials. It also makes the code unit testable by providing a client with stub_responses (I'll make a separate PR for that). We also needed to configure the SNS platform app with various attributes like EventEndpointCreated, so I added a parameter for that. The full list of SNS platform application attributes can be found here: https://docs.aws.amazon.com/sns/latest/api/API_SetPlatformApplicationAttributes.html . --- .../plugin/aws_sns/actions/aws_sns_action.rb | 67 ++++++++++++------- 1 file changed, 44 insertions(+), 23 deletions(-) diff --git a/lib/fastlane/plugin/aws_sns/actions/aws_sns_action.rb b/lib/fastlane/plugin/aws_sns/actions/aws_sns_action.rb index 2595583..a8627be 100644 --- a/lib/fastlane/plugin/aws_sns/actions/aws_sns_action.rb +++ b/lib/fastlane/plugin/aws_sns/actions/aws_sns_action.rb @@ -12,26 +12,30 @@ def self.run(params) access_key = params[:access_key] secret_access_key = params[:secret_access_key] region = params[:region] + client = params[:aws_sns_client] platform = params[:platform] platform_name = params[:platform_name] update_attributes = params[:update_if_exists] + attributes_override = params[:attributes_override] platform_apns_private_key_path = params[:platform_apns_private_key_path] platform_apns_private_key_password = params[:platform_apns_private_key_password] platform_fcm_server_key = params[:platform_fcm_server_key] platform_fcm_server_key ||= params[:platform_gcm_api_key] - UI.user_error!("No S3 access key given, pass using `access_key: 'key'`") unless access_key.to_s.length > 0 - UI.user_error!("No S3 secret access key given, pass using `secret_access_key: 'secret key'`") unless secret_access_key.to_s.length > 0 - UI.user_error!("No S3 region given, pass using `region: 'region'`") unless region.to_s.length > 0 - UI.user_error!("No S3 region given, pass using `platform: 'platform'`") unless platform.to_s.length > 0 - UI.user_error!("No S3 region given, pass using `platform_name: 'platform_name'`") unless platform_name.to_s.length > 0 + if client.nil? + UI.user_error!("No AWS access key given, pass using `access_key: 'key'`") unless access_key.to_s.length > 0 + UI.user_error!("No AWS secret access key given, pass using `secret_access_key: 'secret key'`") unless secret_access_key.to_s.length > 0 + UI.user_error!("No AWS region given, pass using `region: 'region'`") unless region.to_s.length > 0 + end + UI.user_error!("No notification platform given, pass using `platform: 'platform'`") unless platform.to_s.length > 0 + UI.user_error!("No SNS platform application name given, pass using `platform_name: 'platform_name'`") unless platform_name.to_s.length > 0 # # Initialize AWS client # - client = Aws::SNS::Client.new( + client ||= Aws::SNS::Client.new( access_key_id: access_key, secret_access_key: secret_access_key, region: region @@ -58,6 +62,13 @@ def self.run(params) } end + # Set additional AWS platform attributes + if attributes.nil? + attributes = attributes_override + else + attributes = attributes.merge(attributes_override) + end + # # # @@ -104,9 +115,9 @@ def self.run(params) else # else, updating client.set_platform_application_attributes({ - platform_application_arn: arn, - attributes: attributes, - }) + platform_application_arn: arn, + attributes: attributes, + }) UI.important("Updated #{arn}") end @@ -138,27 +149,31 @@ def self.details def self.available_options [ FastlaneCore::ConfigItem.new(key: :access_key, - env_name: "AWS_SNS_ACCESS_KEY", - description: "AWS Access Key ID", - optional: false, - default_value: ENV['AWS_ACCESS_KEY_ID']), + env_name: "AWS_SNS_ACCESS_KEY", + description: "AWS Access Key ID", + optional: false, + default_value: ENV['AWS_ACCESS_KEY_ID']), FastlaneCore::ConfigItem.new(key: :secret_access_key, - env_name: "AWS_SNS_SECRET_ACCESS_KEY", - description: "AWS Secret Access Key", - optional: false, - default_value: ENV['AWS_SECRET_ACCESS_KEY']), + env_name: "AWS_SNS_SECRET_ACCESS_KEY", + description: "AWS Secret Access Key", + optional: false, + default_value: ENV['AWS_SECRET_ACCESS_KEY']), FastlaneCore::ConfigItem.new(key: :region, env_name: "AWS_SNS_REGION", description: "AWS Region", optional: false, default_value: ENV['AWS_REGION']), + FastlaneCore::ConfigItem.new(key: :aws_sns_client, + description: "AWS SNS Client, useful in case of special credentials or custom logging", + is_string: false, + optional: true), FastlaneCore::ConfigItem.new(key: :platform, - env_name: "AWS_SNS_PLATFORM", - description: "AWS Platform", - optional: false, - verify_block: proc do |value| - UI.user_error!("Invalid platform #{value}") unless ['APNS', 'APNS_SANDBOX', 'GCM', 'FCM'].include?(value) - end), + env_name: "AWS_SNS_PLATFORM", + description: "AWS Platform", + optional: false, + verify_block: proc do |value| + UI.user_error!("Invalid platform #{value}") unless ['APNS', 'APNS_SANDBOX', 'GCM', 'FCM'].include?(value) + end), FastlaneCore::ConfigItem.new(key: :platform_name, env_name: "AWS_SNS_PLATFORM_NAME", description: "AWS Platform Name", @@ -186,6 +201,12 @@ def self.available_options description: "updating certificate/key if platform_name already exists", default_value: false, is_string: false, + optional: true), + FastlaneCore::ConfigItem.new(key: :attributes_override, + env_name: "AWS_SNS_PLATFORM_ATTRIBUTES_OVERRIDE", + description: "additional AWS platform attributes such as EventEndpointCreated or SuccessFeedbackRoleArn", + default_value: {}, + is_string: false, optional: true) ] end