Class: Fastlane::Actions::AwsSnsAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/aws_sns/actions/aws_sns_action.rb

Class Method Summary collapse

Class Method Details

.authorsObject



126
127
128
# File 'lib/fastlane/plugin/aws_sns/actions/aws_sns_action.rb', line 126

def self.authors
  ["Josh Holtz"]
end

.available_optionsObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/fastlane/plugin/aws_sns/actions/aws_sns_action.rb', line 138

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']),
    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']),
    FastlaneCore::ConfigItem.new(key: :region,
                                env_name: "AWS_SNS_REGION",
                                description: "AWS Region",
                                optional: false,
                                default_value: ENV['AWS_REGION']),
    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),
    FastlaneCore::ConfigItem.new(key: :platform_name,
                                env_name: "AWS_SNS_PLATFORM_NAME",
                                description: "AWS Platform Name",
                                optional: false),
    FastlaneCore::ConfigItem.new(key: :platform_apns_private_key_path,
                                env_name: "AWS_SNS_PLATFORM_APNS_PRIVATE_KEY_PATH",
                                description: "AWS Platform APNS Private Key Path",
                                optional: true),
    FastlaneCore::ConfigItem.new(key: :platform_apns_private_key_password,
                                env_name: "AWS_SNS_PLATFORM_APNS_PRIVATE_KEY_PASSWORD",
                                description: "AWS Platform APNS Private Key Password",
                                optional: true,
                                default_value: ""),
    FastlaneCore::ConfigItem.new(key: :platform_fcm_server_key,
                                env_name: "AWS_SNS_PLATFORM_FCM_SERVER_KEY",
                                description: "AWS Platform FCM SERVER KEY",
                                optional: true),
    FastlaneCore::ConfigItem.new(key: :platform_gcm_api_key,
                                env_name: "AWS_SNS_PLATFORM_GCM_API_KEY",
                                description: "AWS Platform GCM API KEY",
                                deprecated: "Use :platform_fcm_server_key instead",
                                optional: true),
    FastlaneCore::ConfigItem.new(key: :update_if_exists,
                                env_name: "AWS_SNS_UDPATE_IF_EXISTS",
                                description: "updating certificate/key if platform_name already exists",
                                default_value: false,
                                is_string: false,
                                optional: true)
  ]
end

.descriptionObject



122
123
124
# File 'lib/fastlane/plugin/aws_sns/actions/aws_sns_action.rb', line 122

def self.description
  "Creates AWS SNS platform applications"
end

.detailsObject



134
135
136
# File 'lib/fastlane/plugin/aws_sns/actions/aws_sns_action.rb', line 134

def self.details
  "Creates AWS SNS platform applications for iOS and Android"
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


193
194
195
# File 'lib/fastlane/plugin/aws_sns/actions/aws_sns_action.rb', line 193

def self.is_supported?(platform)
  [:ios, :android].include?(platform)
end

.return_valueObject



130
131
132
# File 'lib/fastlane/plugin/aws_sns/actions/aws_sns_action.rb', line 130

def self.return_value
  "Platform Application ARN"
end

.run(params) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/fastlane/plugin/aws_sns/actions/aws_sns_action.rb', line 10

def self.run(params)

  access_key = params[:access_key]
  secret_access_key = params[:secret_access_key]
  region = params[:region]

  platform = params[:platform]
  platform_name = params[:platform_name]
  update_attributes = params[:update_if_exists]
  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

  #
  # Initialize AWS client
  #
  client = Aws::SNS::Client.new(
    access_key_id: access_key,
    secret_access_key: secret_access_key,
    region: region
  )

  #
  # Create APNS and GCM attributes
  #
  if ['APNS', 'APNS_SANDBOX'].include?(platform)
    UI.user_error!("Platform private key does not exist at path: #{platform_apns_private_key_path}") unless File.exist?(platform_apns_private_key_path)

    file = File.read(platform_apns_private_key_path)
    p12 = OpenSSL::PKCS12.new(file, platform_apns_private_key_password)
    cert = p12.certificate

    attributes = {
      'PlatformCredential': p12.key.to_s,
      'PlatformPrincipal': cert.to_s
    }
  elsif ['GCM', 'FCM'].include?(platform) && !platform_fcm_server_key.nil?
    platform = 'GCM'
    attributes = {
      'PlatformCredential': platform_fcm_server_key
    }
  end

  #
  #
  #
  UI.crash!("Unable to create any attributes to create platform application") unless attributes
  begin

    arn = nil
    
    #
    # Try to find the arn for platform_name
    #
    if update_attributes

      # Loop as long as list platform applications returns next_page or return the desired name
      next_token = nil
      loop do

        resp = client.list_platform_applications({
          next_token: next_token,
        })

        next_token = resp.next_token
        # TODO: Must find a best search method !
        platform_application = resp.platform_applications.find { |platform_application| platform_application.platform_application_arn.end_with? platform_name }
        
        unless platform_application.nil? 
          arn = platform_application.platform_application_arn
          break
        end
        break if next_token.nil?
      end

    end
    
    # Not arn? OK, we create it !
    if arn.nil?  
      resp = client.create_platform_application({
        name: platform_name,
        platform: platform,
        attributes: attributes,
      })
      arn = resp.platform_application_arn
      UI.important("Created #{arn}")
    else
      # else, updating
      client.set_platform_application_attributes({
       platform_application_arn: arn,
       attributes: attributes,
     })
      UI.important("Updated #{arn}")
    end

    Actions.lane_context[SharedValues::AWS_SNS_PLATFORM_APPLICATION_ARN] = arn
    ENV[SharedValues::AWS_SNS_PLATFORM_APPLICATION_ARN.to_s] = arn
  rescue => error
    UI.crash!("Create Platform Error: #{error.inspect}")
  end

  Actions.lane_context[SharedValues::AWS_SNS_PLATFORM_APPLICATION_ARN]
end