Class: ZuoraConnect::AppInstanceBase

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/zuora_connect/app_instance_base.rb

Direct Known Subclasses

AppInstance

Constant Summary collapse

REFRESH_TIMEOUT =

Used to determine how long to wait on current refresh call before executing another

2.minute
INSTANCE_REFRESH_WINDOW =

Used to set how how long till app starts attempting to refresh cached task connect data

1.hours
INSTANCE_REDIS_CACHE_PERIOD =

Used to determine how long to cached task data will live for

24.hours
API_LIMIT_TIMEOUT =

Used to set the default for expiring timeout when api rate limiting is in effect

2.minutes
BLANK_OBJECT_ID_LOOKUP =
'BlankValueSupplied'
HOLDING_PATTERN_SLEEP =
5.seconds
CONNECT_APPLICATION_ID =
0
CONNECT_COMMUNICATION_SLEEP =
Rails.env.test? ? 0.seconds : 5.seconds
CATALOG_LOOKUP_PAGE_SIZE =
10_000
CATALOG_LOOKUP_CACHE_TIME_KEY =
'CatalogCachedAt'
CATALOG_LOOKUP_TTL =
60.seconds
CATALOG_LOOKUP_CACHE_RESULT_KEY =
'CatalogCache'
TIMEZONE_LOG_RATE_LIMIT_KEY =
'TimezoneLoggedAt'
TIMEZONE_LOG_PERIOD =
4.hours
IGNORED_LOCALS =
['fr', 'ja', 'es', 'zh', 'de', 'it', 'sv']
INTERNAL_HOSTS =
[]
LOGIN_TENANT_DESTINATION =
'target_login'
AWS_AUTH_ERRORS =
[
  Aws::Sigv4::Errors::MissingCredentialsError,
  Aws::Errors::MissingCredentialsError,
  Aws::S3::Errors::AccessDenied,
  Aws::SES::Errors::AccessDenied,
  Aws::KMS::Errors::AccessDeniedException
].freeze
AWS_AUTH_ERRORS_MSG =
"AWS Auth Errors".freeze
@@telegraf_host =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *arguments, &block) ⇒ Object



1389
1390
1391
1392
1393
1394
# File 'app/models/zuora_connect/app_instance_base.rb', line 1389

def method_missing(method_sym, *arguments, &block)
  if method_sym.to_s.include?("login")
    ZuoraConnect.logger.fatal("Method Missing #{method_sym}. Instance Data: #{self.task_data} Instance Logins: #{self.logins}")
  end
  super
end

Instance Attribute Details

#api_versionObject

Returns the value of attribute api_version.



14
15
16
# File 'app/models/zuora_connect/app_instance_base.rb', line 14

def api_version
  @api_version
end

#connect_userObject

Returns the value of attribute connect_user.



14
15
16
# File 'app/models/zuora_connect/app_instance_base.rb', line 14

def connect_user
  @connect_user
end

#drop_messageObject

Returns the value of attribute drop_message.



14
15
16
# File 'app/models/zuora_connect/app_instance_base.rb', line 14

def drop_message
  @drop_message
end

#last_refreshObject

Returns the value of attribute last_refresh.



14
15
16
# File 'app/models/zuora_connect/app_instance_base.rb', line 14

def last_refresh
  @last_refresh
end

#loginsObject

Returns the value of attribute logins.



14
15
16
# File 'app/models/zuora_connect/app_instance_base.rb', line 14

def logins
  @logins
end

#logitemsObject

Returns the value of attribute logitems.



14
15
16
# File 'app/models/zuora_connect/app_instance_base.rb', line 14

def logitems
  @logitems
end

#modeObject

Returns the value of attribute mode.



14
15
16
# File 'app/models/zuora_connect/app_instance_base.rb', line 14

def mode
  @mode
end

#new_session_messageObject

Returns the value of attribute new_session_message.



14
15
16
# File 'app/models/zuora_connect/app_instance_base.rb', line 14

def new_session_message
  @new_session_message
end

#optionsObject

Returns the value of attribute options.



14
15
16
# File 'app/models/zuora_connect/app_instance_base.rb', line 14

def options
  @options
end

#passwordObject

Returns the value of attribute password.



14
15
16
# File 'app/models/zuora_connect/app_instance_base.rb', line 14

def password
  @password
end

#s3_clientObject

START S3 Helping Methods #####



1283
1284
1285
# File 'app/models/zuora_connect/app_instance_base.rb', line 1283

def s3_client
  @s3_client
end

#task_dataObject

Returns the value of attribute task_data.



14
15
16
# File 'app/models/zuora_connect/app_instance_base.rb', line 14

def task_data
  @task_data
end

#user_timezoneObject

Returns the value of attribute user_timezone.



14
15
16
# File 'app/models/zuora_connect/app_instance_base.rb', line 14

def user_timezone
  @user_timezone
end

#usernameObject

Returns the value of attribute username.



14
15
16
# File 'app/models/zuora_connect/app_instance_base.rb', line 14

def username
  @username
end

Class Method Details

.decrypt_response(resp) ⇒ Object



1380
1381
1382
# File 'app/models/zuora_connect/app_instance_base.rb', line 1380

def self.decrypt_response(resp)
  OpenSSL::PKey::RSA.new(ZuoraConnect.configuration.private_key).private_decrypt(resp)
end

.read_master_dbObject



1396
1397
1398
1399
1400
1401
1402
1403
1404
# File 'app/models/zuora_connect/app_instance_base.rb', line 1396

def self.read_master_db
  if self.connection.respond_to?(:stick_to_primary!)
    self.connection.stick_to_primary!(false)
    yield
    Makara::Context.release_all
  else
    yield
  end
end

.refresh_aggregate_table(aggregate_name: 'all_tasks_processing', table_name: 'tasks', where_clause: "where status in ('Processing', 'Queued')", index_table: true, ignore_indexes: []) ⇒ Object

Traverse entire database and run a query on table(table_name) with where clause(where_clause) Data from each schema will be loaded into table(aggregate_name) into the public schema



1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
# File 'app/models/zuora_connect/app_instance_base.rb', line 1308

def self.refresh_aggregate_table(aggregate_name: 'all_tasks_processing', table_name: 'tasks', where_clause: "where status in ('Processing', 'Queued')", index_table: true, ignore_indexes: [])
  self.update_functions

  sql_result = ActiveRecord::Base.connection.execute <<-eos
    SELECT pid, relname, mode
    FROM pg_locks l
    JOIN pg_class t ON l.relation = t.oid AND t.relkind = 'r'
    WHERE t.relname = '#{aggregate_name}' AND l.mode ='AccessExclusiveLock';
  eos
  raise ZuoraConnect::Exceptions::Error.new("An existing lock detected while dropping table '#{aggregate_name}'") if sql_result.count > 0

  if index_table
    ActiveRecord::Base.connection.execute('SELECT "shared_extensions".refresh_aggregate_table(\'%s\', \'%s\', %s, \'Index\', \'{%s}\');' % [aggregate_name, table_name, ActiveRecord::Base.connection.quote(where_clause), ignore_indexes.map { |index| "\"#{index}\"" }.join(',')])
  else
    ActiveRecord::Base.connection.execute('SELECT "shared_extensions".refresh_aggregate_table(\'%s\', \'%s\', %s, \'NO\',\'{}\');' % [aggregate_name, table_name, ActiveRecord::Base.connection.quote(where_clause)])
  end
end

.update_functionsObject

Load a psql script as a function in a transaction lock



1327
1328
1329
1330
1331
1332
# File 'app/models/zuora_connect/app_instance_base.rb', line 1327

def self.update_functions
  ActiveRecord::Base.transaction do
    ActiveRecord::Base.connection.execute("SELECT pg_advisory_xact_lock(#{Zlib.crc32('refresh_aggregate_table')})")
    ActiveRecord::Base.connection.execute(File.read("#{Gem.loaded_specs["zuora_connect"].gem_dir}/app/views/sql/refresh_aggregate_table.txt"))
  end
end

.without_stickingObject



1406
1407
1408
1409
1410
1411
1412
1413
1414
# File 'app/models/zuora_connect/app_instance_base.rb', line 1406

def self.without_sticking
   if self.connection.respond_to?(:without_sticking)
    self.connection.without_sticking do
      yield
    end
  else
    yield
  end
end

.write_to_telegraf(*args) ⇒ Object



577
578
579
580
581
582
583
584
# File 'app/models/zuora_connect/app_instance_base.rb', line 577

def self.write_to_telegraf(*args)
  if ZuoraConnect.configuration.enable_metrics && !defined?(Prometheus)
    @@telegraf_host = ZuoraConnect::Telegraf.new() if @@telegraf_host == nil
    unicorn_stats = ZuoraObservability::Metrics.unicorn_listener if defined?(Unicorn) && Unicorn.respond_to?(:listener_names)
    @@telegraf_host.write(direction: 'Raindrops', tags: {}, values: unicorn_stats)  unless unicorn_stats.blank?
    return @@telegraf_host.write(*args)
  end
end

Instance Method Details

#apartment_switch(method = nil, migrate = false) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/models/zuora_connect/app_instance_base.rb', line 114

def apartment_switch(method = nil, migrate = false)
  switch_count ||= 0
  if self.persisted?
    begin
      Apartment::Tenant.switch!(self.id)
    rescue Apartment::TenantNotFound => ex
      sleep(2)
      begin
        Apartment::Tenant.create(self.id.to_s)
      rescue Apartment::TenantExists => ex
      end
      if (switch_count += 1) < 2
        retry
      else
        raise
      end
    end
    if migrate
      Apartment::Migrator.migrate(self.id)
    end
  end
  Thread.current[:appinstance] = self
end

#api_limit(start: true, time: API_LIMIT_TIMEOUT.to_i) ⇒ Object

START Resque Helping Methods ####



962
963
964
965
966
967
968
# File 'app/models/zuora_connect/app_instance_base.rb', line 962

def api_limit(start: true, time: API_LIMIT_TIMEOUT.to_i)
  if start
    Redis.current.zadd("APILimits", Time.now.to_i + time, self.id)
  else
    Redis.current.zrem("APILimits", self.id)
  end
end

#api_limit?Boolean

Returns:

  • (Boolean)


970
971
972
973
# File 'app/models/zuora_connect/app_instance_base.rb', line 970

def api_limit?
  Redis.current.zremrangebyscore("APILimits", "0", "(#{Time.now.to_i}")
  return Redis.current.zscore("APILimits", self.id).present?
end

#attr_builder(field, val) ⇒ Object



1384
1385
1386
1387
# File 'app/models/zuora_connect/app_instance_base.rb', line 1384

def attr_builder(field,val)
  singleton_class.class_eval { attr_accessor "#{field}" }
  send("#{field}=", val)
end

#auto_deployed?Boolean

Returns:

  • (Boolean)


351
352
353
# File 'app/models/zuora_connect/app_instance_base.rb', line 351

def auto_deployed?
  self.id >= 25000000
end

#aws_auth_clientObject



475
476
477
478
479
480
481
# File 'app/models/zuora_connect/app_instance_base.rb', line 475

def aws_auth_client
  if Rails.env.to_s == 'development'
    return Aws::Credentials.new(aws_secrets['AWS_ACCESS_KEY_ID'], aws_secrets['AWS_SECRET_ACCESS_KEY'])
  else
    return nil
  end
end

#aws_secretsObject



447
448
449
# File 'app/models/zuora_connect/app_instance_base.rb', line 447

def aws_secrets
  (Rails.application.secrets.aws || {}).transform_keys { |key| key.to_s }
end

#build_task(task_data: {}, session: {}) ⇒ Object

START Task Methods ####



588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
# File 'app/models/zuora_connect/app_instance_base.rb', line 588

def build_task(task_data: {}, session: {})
  session = {} if session.blank?
  self.task_data = task_data
  if self.task_data.blank? &&  ZuoraConnect.configuration.local_task_data
    self.task_data = self.zuora_logins
  end
 
  self.mode = self.task_data["mode"]

  if self.task_data['id'].to_s != self.id.to_s
    raise ZuoraConnect::Exceptions::MissMatch.new("Wrong Instance Identifier/Lookup")
  end

  self.task_data.each do |k,v|
    if k.match(/^(.*)_login$/)
      tmp = ZuoraConnect::Login.new(v)
      if v["tenant_type"] == "Zuora"
        if tmp.entities.size > 0
          tmp.entities.each do |value|
            entity_id = value["id"]
            tmp.client(entity_id).current_session          = session["#{self.id}::#{k}::#{entity_id}:current_session"]               if session["#{self.id}::#{k}::#{entity_id}:current_session"]
            tmp.client(entity_id).bearer_token             = session["#{self.id}::#{k}::#{entity_id}:bearer_token"]                  if session["#{self.id}::#{k}::#{entity_id}:bearer_token"]
            tmp.client(entity_id).oauth_session_expires_at = session["#{self.id}::#{k}::#{entity_id}:oauth_session_expires_at"]      if session["#{self.id}::#{k}::#{entity_id}:oauth_session_expires_at"]
          end
        else
          tmp.client.current_session                       = session["#{self.id}::#{k}:current_session"]                             if session["#{self.id}::#{k}:current_session"]
          tmp.client.bearer_token                          = session["#{self.id}::#{k}:bearer_token"]                                if session["#{self.id}::#{k}:bearer_token"] && tmp.client.respond_to?(:bearer_token) ## need incase session id goes from basic to aouth in same redis store
          tmp.client.oauth_session_expires_at              = session["#{self.id}::#{k}:oauth_session_expires_at"]                    if session["#{self.id}::#{k}:oauth_session_expires_at"]  && tmp.client.respond_to?(:oauth_session_expires_at)
        end
      end
      self.logins[k] = tmp
      self.attr_builder(k, @logins[k])
    elsif k == "options"
      v.each do |opt|
        self.options[opt["config_name"]] = opt
      end
    elsif k == "user_settings"
      self.timezone =  v["timezone"]
      self.locale = v["local"]
    end
  end
rescue ZuoraConnect::Exceptions::MissMatch => ex
  raise
rescue ZuoraConnect::Exceptions::InvalidCredentialSet => ex
  raise
rescue => ex
  ZuoraConnect.logger.error("Build Task Error", ex)
  ZuoraConnect.logger.error("Task Data: #{self.task_data}") if self.task_data.present?
  if session.present?
    ZuoraConnect.logger.error("Task Session: #{session.to_h}")  if session.methods.include?(:to_h)
    ZuoraConnect.logger.error("Task Session: #{session.to_hash}") if session.methods.include?(:to_hash)
  end
  raise
end

#cache_app_instance(force_cache: false) ⇒ Object



871
872
873
874
875
876
877
878
879
# File 'app/models/zuora_connect/app_instance_base.rb', line 871

def cache_app_instance(force_cache: false)
  if defined?(Redis.current)
    #Task data must be present and the last refresh cannot be old. We dont want to overwite new cache data with old
    if self.task_data.present? &&  ((self.last_refresh.to_i > INSTANCE_REFRESH_WINDOW.ago.to_i ) || force_cache)
      ZuoraConnect.logger.debug("Caching AppInstance", self.default_ougai_items)
      Redis.current.setex("AppInstance:#{self.id}", INSTANCE_REDIS_CACHE_PERIOD.to_i, self.encrypt_data(data: self.save_data))
    end
  end
end

#catalog_loaded?Boolean

Returns:

  • (Boolean)


1064
1065
1066
# File 'app/models/zuora_connect/app_instance_base.rb', line 1064

def catalog_loaded?
  return ActiveRecord::Base.connection.execute('SELECT id FROM "public"."zuora_connect_app_instances" WHERE "id" = %s AND catalog = \'{}\' LIMIT 1' % [self.id]).first.nil?
end

#catalog_lookup(entity_id: nil, object: :product, object_id: nil, child_objects: false, cache: false, source: 'DB') ⇒ Object

Catalog lookup provides method to lookup zuora catalog efficiently. entity_id: If the using catalog json be field to store multiple entity product catalogs. object: The Object class desired to be returned. Available [:product, :rateplan, :charge] object_id: The id or id’s of the object/objects to be returned. child_objects: Whether to include child objects of the object in question. cache: Store individual “1” object lookup in redis for caching.



1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
# File 'app/models/zuora_connect/app_instance_base.rb', line 1074

def catalog_lookup(entity_id: nil, object: :product, object_id: nil, child_objects: false, cache: false, source: 'DB')
  entity_reference = entity_id.blank? ? 'Default' : entity_id

  if object_id.present? && ![Array, String].include?(object_id.class)
    raise "Object Id can only be a string or an array of strings"
  end

  if source == 'API'
    catalog_container = {}

    if (Redis.current.hget(CATALOG_LOOKUP_CACHE_TIME_KEY, self.id).to_i + CATALOG_LOOKUP_TTL.to_i) > Time.now.to_i
      begin
        catalog_container = JSON.parse(Redis.current.hget(CATALOG_LOOKUP_CACHE_RESULT_KEY, self.id))
      rescue JSON::ParserError => ex
        Rails.logger.warn('Failed to parse catalog cache', ex)
      end
    else
       = self.(type: 'Zuora').first
       = .client(entity_reference)

      response = {
        'nextPage' => .rest_endpoint("catalog/products?pageSize=#{CATALOG_LOOKUP_PAGE_SIZE}")
      }

      while response['nextPage'].present?
        url = .rest_endpoint(response['nextPage'].split('/v1/').last)
        output_json, response = .rest_call(debug: false, url: url, timeout_retry: true)

        case object
        when :product
          output_json.fetch('products', []).each do |product|
            rate_plans = {}
            product['productRatePlans'].each do |rate_plan|
              charges = {}
              rate_plan['productRatePlanCharges'].each do |charge|
                charges[charge['id']] = charge.merge(
                  {
                    'productId' => product['id'],
                    'productName' => product['name'],
                    'productRatePlanId' => rate_plan['id'],
                    'productRatePlanName' => rate_plan['name'],
                  }
                )
              end

              rate_plan['productRatePlanCharges'] = charges
              rate_plans[rate_plan['id']] = rate_plan.merge(
                {
                  'productId' => product['id'],
                  'productName' => product['name']
                }
              )
            end

            product['productRatePlans'] = rate_plans
            catalog_container[product['id']] = product
          end
        else
          raise "Available objects include [:product]"
        end
      end

      Redis.current.hset(CATALOG_LOOKUP_CACHE_RESULT_KEY, self.id, catalog_container.to_json)
      Redis.current.hset(CATALOG_LOOKUP_CACHE_TIME_KEY, self.id, Time.now.to_i)
    end

    if object_id.nil?
      catalog_container.transform_values! { |v| v.except('productRatePlans') }
    elsif object_id.is_a?(String)
      catalog_container = catalog_container[object_id]
    end

    return catalog_container || {}
  end

  if defined?(Redis.current) && object_id.present? && object_id.class == String && object_id.present?
    stub_catalog = cache ? decrypt_data(data: Redis.current.get("Catalog:#{self.id}:#{object_id}:Children:#{child_objects}")) : nil
    object_hierarchy = decrypt_data(data: Redis.current.get("Catalog:#{self.id}:#{object_id}:Hierarchy"))
  end

  if defined?(object_hierarchy)
    object_hierarchy ||= (JSON.parse(ActiveRecord::Base.connection.execute('SELECT catalog_mapping #> \'{%s}\' AS item FROM "public"."zuora_connect_app_instances" WHERE "id" = %s LIMIT 1' % [entity_reference, self.id]).first["item"] || "{}") [object_id] || {"productId" => "SAFTEY", "productRatePlanId" => "SAFTEY", "productRatePlanChargeId" => "SAFTEY"})
  end

  case object
  when :product
    if object_id.nil?
      string =
        "SELECT "\
          "json_object_agg(product_id, product #{child_objects ? '' : '- \'productRatePlans\''}) AS item "\
        "FROM "\
          "\"public\".\"zuora_connect_app_instances\", "\
          "jsonb_each((\"public\".\"zuora_connect_app_instances\".\"catalog\" #> '{%s}' )) AS e(product_id, product) "\
        "WHERE "\
          "\"id\" = %s" % [entity_reference, self.id]
    else
      if object_id.class == String
        string =
          "SELECT "\
            "(catalog #> '{%s, %s}') #{child_objects ? '' : '- \'productRatePlans\''} AS item "\
          "FROM "\
            "\"public\".\"zuora_connect_app_instances\" "\
          "WHERE "\
            "\"id\" = %s" % [entity_reference, object_id.blank? ? BLANK_OBJECT_ID_LOOKUP : object_id, self.id]
      elsif object_id.class == Array
        string =
          "SELECT "\
            "json_object_agg(product_id, product #{child_objects ? '' : '- \'productRatePlans\''}) AS item "\
          "FROM "\
            "\"public\".\"zuora_connect_app_instances\", "\
            "jsonb_each((\"public\".\"zuora_connect_app_instances\".\"catalog\" #> '{%s}' )) AS e(product_id, product) "\
          "WHERE "\
            "\"product_id\" IN (\'%s\') AND "\
            "\"id\" = %s" % [entity_reference, object_id.join("\',\'"), self.id]
      end
    end

  when :rateplan
    if object_id.nil?
      string =
        "SELECT "\
          "json_object_agg(rateplan_id, rateplan #{child_objects ? '' : '- \'productRatePlanCharges\''}) AS item "\
        "FROM "\
          "\"public\".\"zuora_connect_app_instances\", "\
          "jsonb_each((\"public\".\"zuora_connect_app_instances\".\"catalog\" #> '{%s}' )) AS e(product_id, product), "\
          "jsonb_each(product #> '{productRatePlans}') AS ee(rateplan_id, rateplan) "\
        "WHERE "\
          "\"id\" = %s" % [entity_reference, self.id]
    else
      if object_id.class == String
        string =
          "SELECT "\
            "(catalog #> '{%s, %s, productRatePlans, %s}') #{child_objects ? '' : '- \'productRatePlanCharges\''} AS item "\
          "FROM "\
            "\"public\".\"zuora_connect_app_instances\" "\
          "WHERE "\
            "\"id\" = %s" % [entity_reference, object_hierarchy['productId'], object_id.blank? ? BLANK_OBJECT_ID_LOOKUP : object_id,  self.id]
      elsif object_id.class == Array
        string =
          "SELECT "\
            "json_object_agg(rateplan_id, rateplan #{child_objects ? '' : '- \'productRatePlanCharges\''}) AS item "\
          "FROM "\
            "\"public\".\"zuora_connect_app_instances\", "\
            "jsonb_each((\"public\".\"zuora_connect_app_instances\".\"catalog\" #> '{%s}' )) AS e(product_id, product), "\
            "jsonb_each(product #> '{productRatePlans}') AS ee(rateplan_id, rateplan) "\
          "WHERE "\
            "\"rateplan_id\" IN (\'%s\') AND "\
            "\"id\" = %s" % [entity_reference, object_id.join("\',\'"), self.id]
      end
    end

  when :charge
    if object_id.nil?
      string =
        "SELECT "\
          "json_object_agg(charge_id, charge) as item "\
        "FROM "\
          "\"public\".\"zuora_connect_app_instances\", "\
          "jsonb_each((\"public\".\"zuora_connect_app_instances\".\"catalog\" #> '{%s}' )) AS e(product_id, product), "\
          "jsonb_each(product #> '{productRatePlans}') AS ee(rateplan_id, rateplan), "\
          "jsonb_each(rateplan #> '{productRatePlanCharges}') AS eee(charge_id, charge) "\
        "WHERE "\
          "\"id\" = %s" % [entity_reference, self.id]
    else
      if object_id.class == String
        string =
          "SELECT "\
            "catalog #> '{%s, %s, productRatePlans, %s, productRatePlanCharges, %s}' AS item "\
          "FROM "\
            "\"public\".\"zuora_connect_app_instances\" "\
          "WHERE "\
            "\"id\" = %s" % [entity_reference, object_hierarchy['productId'], object_hierarchy['productRatePlanId'], object_id.blank? ? BLANK_OBJECT_ID_LOOKUP : object_id , self.id]

      elsif object_id.class == Array
        string =
          "SELECT "\
            "json_object_agg(charge_id, charge) AS item "\
          "FROM "\
            "\"public\".\"zuora_connect_app_instances\", "\
            "jsonb_each((\"public\".\"zuora_connect_app_instances\".\"catalog\" #> '{%s}' )) AS e(product_id, product), "\
            "jsonb_each(product #> '{productRatePlans}') AS ee(rateplan_id, rateplan), "\
            "jsonb_each(rateplan #> '{productRatePlanCharges}') AS eee(charge_id, charge) "\
          "WHERE "\
            "\"charge_id\" IN (\'%s\') AND "\
            "\"id\" = %s" % [entity_reference, object_id.join("\',\'"), self.id]
      end
    end
  else
    raise "Available objects include [:product, :rateplan, :charge]"
  end

  stub_catalog ||= JSON.parse(ActiveRecord::Base.connection.execute(string).first["item"] || "{}")

  if defined?(Redis.current) && object_id.present? && object_id.class == String && object_id.present?
    if cache
      Redis.current.sadd("Catalog:#{self.id}:Keys", ["Catalog:#{self.id}:#{object_id}:Hierarchy", "Catalog:#{self.id}:#{object_id}:Children:#{child_objects}"])
      Redis.current.set("Catalog:#{self.id}:#{object_id}:Hierarchy", encrypt_data(data: object_hierarchy))
      Redis.current.set("Catalog:#{self.id}:#{object_id}:Children:#{child_objects}", encrypt_data(data: stub_catalog))
    else
      Redis.current.sadd("Catalog:#{self.id}:Keys", ["Catalog:#{self.id}:#{object_id}:Hierarchy"])
      Redis.current.set("Catalog:#{self.id}:#{object_id}:Hierarchy", encrypt_data(data: object_hierarchy))
    end
  end

  return stub_catalog
end

#catalog_outdated?(time: Time.now - 12.hours) ⇒ Boolean

Returns:

  • (Boolean)


1060
1061
1062
# File 'app/models/zuora_connect/app_instance_base.rb', line 1060

def catalog_outdated?(time: Time.now - 12.hours)
  return self.catalog_updated_at.blank? || (self.catalog_updated_at < time)
end

#check_oauth_state(method = nil) ⇒ Object

START Connect OAUTH Methods ####



756
757
758
759
760
761
762
# File 'app/models/zuora_connect/app_instance_base.rb', line 756

def check_oauth_state(method=nil)
  #Refresh token if already expired
  if self.oauth_expired?
    ZuoraConnect.logger.debug("Before '#{method}' method, Oauth expired")
    self.refresh_oauth
  end
end

#connect_secretsObject



451
452
453
# File 'app/models/zuora_connect/app_instance_base.rb', line 451

def connect_secrets
  (Rails.application.secrets.connect || {}).transform_keys { |key| key.to_s }
end

#data_lookup(session: {}) ⇒ Object



843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
# File 'app/models/zuora_connect/app_instance_base.rb', line 843

def data_lookup(session: {})
  if defined?(Redis.current)
    begin
      redis_get_command ||= 0
      cached_instance = Redis.current.get("AppInstance:#{self.id}")
    rescue *(ZuoraAPI::Login::CONNECTION_EXCEPTIONS + ZuoraAPI::Login::CONNECTION_READ_EXCEPTIONS) => ex
      if (redis_get_command += 1) < 3
        retry
      else
        raise
      end
    end
    if cached_instance.blank?
      ZuoraConnect.logger.debug("Cached AppInstance Missing", self.default_ougai_items)
      return session
    else
      ZuoraConnect.logger.debug("Cached AppInstance Found", self.default_ougai_items)
      return decrypt_data(data: cached_instance, rescue_return: session).merge(session)
    end
  else
    return session
  end
end

#decrypt_data(data: nil, rescue_return: nil, log_fatal: true) ⇒ Object



927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
# File 'app/models/zuora_connect/app_instance_base.rb', line 927

def decrypt_data(data: nil, rescue_return: nil, log_fatal: true)
  return data if data.blank?
  if Rails.env == 'development'
    begin
      return JSON.parse(data)
    rescue JSON::ParserError => ex
      return data
    end
  else
    begin
      return JSON.parse(encryptor.decrypt_and_verify(CGI::unescape(data)))
    rescue ActiveSupport::MessageEncryptor::InvalidMessage => ex
      Rails.logger.error('Error Decrypting', ex, self.default_ougai_items) if log_fatal && !Rails.env.test?
      return JSON.parse(encryptor.decrypt_and_verify(data))
    rescue ActiveSupport::MessageVerifier::InvalidSignature => ex
      ZuoraConnect.logger.error("Error Decrypting", ex, self.default_ougai_items) if log_fatal
      return rescue_return
    rescue JSON::ParserError => ex
      ZuoraConnect.logger.error("JSON Parse Error", ex, self.default_ougai_items) if log_fatal
      return encryptor.decrypt_and_verify(CGI::unescape(data))
    end
  end
end

#decrypted_data_keyObject



442
443
444
445
# File 'app/models/zuora_connect/app_instance_base.rb', line 442

def decrypted_data_key
  $cleartextkey ||= kms_client.decrypt(ciphertext_blob: Base64.strict_decode64(encrypted_data_key)).plaintext
  return $cleartextkey
end

#default_ougai_itemsObject



138
139
140
# File 'app/models/zuora_connect/app_instance_base.rb', line 138

def default_ougai_items
  return {app_instance_id: self.id, tenant_ids: self.zuora_tenant_ids, organization: self.organizations, environment: self.environment}
end

#delete_app_instanceObject



867
868
869
# File 'app/models/zuora_connect/app_instance_base.rb', line 867

def delete_app_instance
  Redis.current.del("AppInstance:#{self.id}")
end

#drop_instanceObject

Method for overiding droping of an app instance



1348
1349
1350
1351
# File 'app/models/zuora_connect/app_instance_base.rb', line 1348

def drop_instance
  self.drop_message = 'Ok to drop'
  return true
end

#encrypt_data(data: nil) ⇒ Object



951
952
953
954
955
956
957
958
# File 'app/models/zuora_connect/app_instance_base.rb', line 951

def encrypt_data(data: nil)
  return data if data.blank?
  if Rails.env == 'development'
    return data.to_json
  else
    return encryptor.encrypt_and_sign(data.to_json)
  end
end

#encrypted_data_keyObject



468
469
470
471
472
473
# File 'app/models/zuora_connect/app_instance_base.rb', line 468

def encrypted_data_key
  #Base64.strict_encode64(kms_client.generate_data_key(key_id: kms_key, key_spec: 'AES_256').ciphertext_blob)
  encrypted_data_key_value = ENV['ENCRYPTED_DATA_KEY'] || connect_secrets['ENCRYPTED_DATA_KEY']
  raise ZuoraConnect::Exceptions::Error.new("Missing encrypted data key 'ENCRYPTED_DATA_KEY'.") if encrypted_data_key_value.blank?
  return encrypted_data_key_value
end

#encryptorObject



918
919
920
921
922
923
924
925
# File 'app/models/zuora_connect/app_instance_base.rb', line 918

def encryptor
  # Default values for Rails 4 apps
  key_iter_num, key_size, salt, signed_salt = [1000, 64, "encrypted cookie", "signed encrypted cookie"]
  raise ZuoraConnect::Exceptions::Error.new("'secret_key_base' is not set for rails environment '#{Rails.env}'. Please set in secrets file.") if Rails.application.secrets.secret_key_base.blank?
  key_generator = ActiveSupport::KeyGenerator.new(Rails.application.secrets.secret_key_base, iterations: key_iter_num)
  secret, sign_secret = [key_generator.generate_key(salt, 32), key_generator.generate_key(signed_salt)]
  return ActiveSupport::MessageEncryptor.new(secret, sign_secret)
end

#fetch_cipher(type) ⇒ Object



483
484
485
486
487
488
489
490
# File 'app/models/zuora_connect/app_instance_base.rb', line 483

def fetch_cipher(type)
  raise "Type must be set to 'encrypt' or 'decrypt'" if !['decrypt','encrypt'].include?(type)
  cipher = OpenSSL::Cipher.new('AES-256-CBC')
  cipher.send(type)
  cipher.key = self.decrypted_data_key
  cipher.iv = Base64.strict_decode64(self.iv_key)
  return cipher
end

#fetch_connect_data(session: {}) ⇒ Object



355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'app/models/zuora_connect/app_instance_base.rb', line 355

def fetch_connect_data(session: {})
  refresh_count ||= 0
  self.check_oauth_state
  request_url = ZuoraConnect.configuration.url + "/api/#{self.api_version}/tools/tasks/#{self.id}.json"
  response = HTTParty.get(request_url,:body => {:access_token => self.access_token})

  if response.code == 200
    begin
      parsed_json = JSON.parse(response.body)
    rescue JSON::ParserError => ex
      raise ZuoraConnect::Exceptions::ConnectCommunicationError.new("JSON parse error", response.body, response.code)
    end

    self.build_task(task_data: parsed_json, session: session)
    self.set_backup_creds
    self.save(validate: false) if self.changed?
  else
    raise ZuoraConnect::Exceptions::ConnectCommunicationError.new("Error communicating with Connect for '#{request_url}' with #{response.code}", response.body, response.code)
  end
rescue *(ZuoraAPI::Login::CONNECTION_EXCEPTIONS + ZuoraAPI::Login::CONNECTION_READ_EXCEPTIONS) => ex
  refresh_count += 1
  if refresh_count < 3
    sleep(10)
    ZuoraConnect.logger.debug("REFRESH TASK - Connection Failure Retrying(#{refresh_count})", ex, self.default_ougai_items)
    retry
  else
    ZuoraConnect.logger.fatal("REFRESH TASK - Connection Failed", ex)
    raise
  end
rescue ZuoraConnect::Exceptions::ConnectCommunicationError => ex
  refresh_count += 1
  if refresh_count < 3
    ZuoraConnect.logger.debug("REFRESH TASK - Communication Failure Retrying(#{refresh_count})", ex, self.default_ougai_items)
    self.refresh_oauth if ex.code == 401
    retry
  else
    ZuoraConnect.logger.fatal("REFRESH TASK - Communication Failed #{ex.code}", ex, self.default_ougai_items)
    raise
  end
end

#fetch_org_details(debug: false) ⇒ Object



682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
# File 'app/models/zuora_connect/app_instance_base.rb', line 682

def fetch_org_details(debug: false)
  details_count ||= 0
  self.refresh if !defined?(self.)

  response = HTTParty.get("#{ZuoraConnect.configuration.url}/api/#{self.api_version}/tenants/search?hostname=#{self..client.hostname}&node_id=#{self.zuora_entity_ids.first}")

  if response.success?
    parsed_json =  JSON.parse(response.body)

    #Set Org
    if self.auto_deployed? && parsed_json['organization'].present?
       = self.zuora_logins
      .delete('organization')
      self.zuora_logins = .merge({'organizations' => [parsed_json['organization']]})
    end
    if defined?(ZuoraConnect::AppInstance::CONNECT_APPLICATION_ID)
      downloads = parsed_json.fetch('downloads',[]).select{|a| a['applicationId'] == ZuoraConnect::AppInstance::CONNECT_APPLICATION_ID }.map { |h| h.slice('Name', 'provisionState') }
      Rails.logger.info("Instance Downloads: #{downloads.to_s}") if debug
      if downloads.size > 1
        self.provision_status = 'MultipleProvisioningRecords'
        self.provisioned_app = downloads.map {|d| d['Name']}.to_s
      elsif downloads.size == 1
        self.provision_status = downloads.first['provisionState']
        self.provisioned_app = downloads.first['Name']
      else
        self.provision_status = 'NoProvisioningRecords'
        self.provisioned_app = nil
      end
    end
    if self.changed? 
      self.save(:validate => false) 
      self.refresh
    end

    return parsed_json
  end
rescue *(ZuoraAPI::Login::CONNECTION_EXCEPTIONS + ZuoraAPI::Login::CONNECTION_READ_EXCEPTIONS) => ex
  if (details_count += 1) < 3
    retry
  else
    raise
  end
end

#get_catalog(page_size: 5, zuora_login: self.login_lookup(type: "Zuora").first, entity_id: nil) ⇒ Object

START Catalog Helping Methods #####



1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
# File 'app/models/zuora_connect/app_instance_base.rb', line 1001

def get_catalog(page_size: 5, zuora_login: self.(type: "Zuora").first, entity_id: nil)
  self.update_column(:catalog_update_attempt_at, Time.now.utc)

  entity_reference = entity_id.blank? ? 'Default' : entity_id
  ZuoraConnect.logger.debug("Fetch Catalog")
  ZuoraConnect.logger.debug("Zuora Entity: #{entity_id.blank? ? 'default' : entity_id}")

   = .client(entity_id)

  old_logger = ActiveRecord::Base.logger
  ActiveRecord::Base.logger = nil
  ActiveRecord::Base.connection.execute('UPDATE "public"."zuora_connect_app_instances" SET "catalog" = jsonb_set("catalog", \'{tmp}\', \'{}\'), "catalog_mapping" = jsonb_set("catalog_mapping", \'{tmp}\', \'{}\') where "id" = %{id}' % {:id => self.id})

  response = {'nextPage' => .rest_endpoint("catalog/products?pageSize=#{page_size}")}
  while !response["nextPage"].blank?
    url = .rest_endpoint(response["nextPage"].split('/v1/').last)
    ZuoraConnect.logger.debug("Fetch Catalog URL #{url}")
    output_json, response = .rest_call(:debug => false, :url => url, :timeout_retry => true)

    output_json["products"].each do |product|
      ActiveRecord::Base.connection.execute('UPDATE "public"."zuora_connect_app_instances" SET "catalog_mapping" = jsonb_set("catalog_mapping", \'{tmp, %s}\', \'%s\') where "id" = %s' % [product["id"], {"productId" => product["id"]}.to_json.gsub("'", "''"), self.id])
      rateplans = {}

      product["productRatePlans"].each do |rateplan|
        ActiveRecord::Base.connection.execute('UPDATE "public"."zuora_connect_app_instances" SET "catalog_mapping" = jsonb_set("catalog_mapping", \'{tmp, %s}\', \'%s\') where "id" = %s' % [rateplan["id"],  {"productId" => product["id"], "productRatePlanId" => rateplan["id"]}.to_json.gsub("'", "''"), self.id])
        charges = {}

        rateplan["productRatePlanCharges"].each do |charge|
          ActiveRecord::Base.connection.execute('UPDATE "public"."zuora_connect_app_instances" SET "catalog_mapping" = jsonb_set("catalog_mapping", \'{tmp, %s}\', \'%s\') where "id" = %s' % [charge["id"],  {"productId" => product["id"], "productRatePlanId" => rateplan["id"], "productRatePlanChargeId" => charge["id"]}.to_json.gsub("'", "''"), self.id])

          charges[charge["id"]] = charge.merge({"productId" => product["id"], "productName" => product["name"], "productRatePlanId" => rateplan["id"], "productRatePlanName" => rateplan["name"] })
        end

        rateplan["productRatePlanCharges"] = charges
        rateplans[rateplan["id"]] = rateplan.merge({"productId" => product["id"], "productName" => product["name"]})
      end
      product["productRatePlans"] = rateplans

      ActiveRecord::Base.connection.execute('UPDATE "public"."zuora_connect_app_instances" SET "catalog" = jsonb_set("catalog", \'{tmp, %s}\', \'%s\') where "id" = %s' % [product["id"], product.to_json.gsub("'", "''"), self.id])
    end
  end

  # Move from tmp to actual
  ActiveRecord::Base.connection.execute('UPDATE "public"."zuora_connect_app_instances" SET "catalog" = jsonb_set("catalog", \'{%{entity}}\', "catalog" #> \'{tmp}\'), "catalog_mapping" = jsonb_set("catalog_mapping", \'{%{entity}}\',  "catalog_mapping" #> \'{tmp}\') where "id" = %{id}' % {:entity => entity_reference, :id => self.id})
  if defined?(Redis.current)
    catalog_keys = Redis.current.smembers("Catalog:#{self.id}:Keys")
    Redis.current.del(catalog_keys.push("Catalog:#{self.id}:Keys"))
  end
  # Clear tmp holder
  ActiveRecord::Base.connection.execute('UPDATE "public"."zuora_connect_app_instances" SET "catalog" = jsonb_set("catalog", \'{tmp}\', \'{}\'), "catalog_mapping" = jsonb_set("catalog_mapping", \'{tmp}\', \'{}\') where "id" = %{id}' % {:id => self.id})

  ActiveRecord::Base.logger = old_logger
  self.update_column(:catalog_updated_at, Time.now.utc)
  self.touch

  # DO NOT RETURN CATALOG. THIS IS NOT SCALABLE WITH LARGE CATALOGS. USE THE  CATALOG_LOOKUP method provided
  return true
end

#get_s3_file_url(key) ⇒ Object



1298
1299
1300
1301
1302
# File 'app/models/zuora_connect/app_instance_base.rb', line 1298

def get_s3_file_url(key)
  require 'aws-sdk-s3'
  signer = Aws::S3::Presigner.new(client: self.s3_client)
  url = signer.presigned_url(:get_object, bucket: ZuoraConnect.configuration.s3_bucket_name, key: "#{ZuoraConnect.configuration.s3_folder_name}/#{self.id.to_s}/#{key}")
end

#initObject



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
# File 'app/models/zuora_connect/app_instance_base.rb', line 42

def init
  self.connect_user = 'Nobody'
  self.logitems = {}
  self.task_data = {}
  self.options = Hash.new
  self.logins = Hash.new
  self.api_version = "v2"
  self.attr_builder("timezone", ZuoraConnect.configuration.default_time_zone)
  self.attr_builder("locale", ZuoraConnect.configuration.default_locale)
  PaperTrail.whodunnit = "Backend" if defined?(PaperTrail)
  if defined?(ElasticAPM) && ElasticAPM.running?
    ElasticAPM.set_user("Backend")
    if ElasticAPM.respond_to?(:set_label)
      ElasticAPM.set_label(:app_instance, self.id)
    else
      ElasticAPM.set_label(:app_instance, self.id)
    end
  end

  if INSTANCE_REFRESH_WINDOW > INSTANCE_REDIS_CACHE_PERIOD
    raise "The instance refresh window cannot be greater than the instance cache period"
  end
  self.apartment_switch(nil, false)

  if ZuoraConnect.logger.is_a?(Ougai::Logger)
    ZuoraConnect.logger.with_fields.merge!(default_ougai_items)
  end
  if Rails.logger.is_a?(Ougai::Logger)
    Rails.logger.with_fields.merge!(default_ougai_items)
  end
end

#initialize_redis_placeholdersObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/models/zuora_connect/app_instance_base.rb', line 74

def initialize_redis_placeholders
  if defined?(Redis.current)
    unless Redis.current.zscore("AppInstance:Deleted", "placeholder").present? # O(1)
      Redis.current.zadd("AppInstance:Deleted", 9_999_999_999, "placeholder") # O(log(N))
    end
    if self.id.present?
      if Redis.current.zscore("AppInstance:Deleted", self.id).present? # O(1)
        Redis.current.zrem("AppInstance:Deleted", self.id) # O(log(N))
      end
    end
    unless Redis.current.zscore("APILimits", "placeholder").present? # O(1)
      Redis.current.zadd("APILimits", 9_999_999_999, "placeholder") # O(log(N))
    end
    unless Redis.current.zscore("InstanceRefreshing", "placeholder").present? # O(1)
      Redis.current.zadd("InstanceRefreshing", 9_999_999_999, "placeholder") # O(log(N))
    end
  end
  if defined?(Resque.redis)
    unless Resque.redis.zscore("PauseQueue", "placeholder").present? # O(1)
      Resque.redis.zadd("PauseQueue", 9_999_999_999, "placeholder") # O(log(N))
    end
  end
  true
end

#instance_failure(failure) ⇒ Object



1365
1366
1367
# File 'app/models/zuora_connect/app_instance_base.rb', line 1365

def instance_failure(failure)
  raise failure
end

#iv_keyObject



461
462
463
464
465
466
# File 'app/models/zuora_connect/app_instance_base.rb', line 461

def iv_key
  iv_key_value = ENV['IV_KEY'] || connect_secrets['IV_KEY']
  #Create new one 'Base64.strict_encode64(OpenSSL::Cipher.new('AES-256-CBC').random_iv)'
  raise ZuoraConnect::Exceptions::Error.new("Missing IV cipher key") if iv_key_value.blank?
  return iv_key_value
end

#kms_clientObject



437
438
439
440
# File 'app/models/zuora_connect/app_instance_base.rb', line 437

def kms_client
  @kms_client ||= Aws::KMS::Client.new({region: aws_secrets['AWS_REGION'], credentials: self.aws_auth_client}.delete_if { |k, v| v.blank? })
  return @kms_client
end

#kms_decrypt(value, field_name: nil, encryption_type: ZuoraConnect.configuration.encryption_type) ⇒ Object



492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
# File 'app/models/zuora_connect/app_instance_base.rb', line 492

def kms_decrypt(value, field_name: nil, encryption_type: ZuoraConnect.configuration.encryption_type)
  kms_tries ||= 0
  original_encryption_type ||= encryption_type.dup

  case encryption_type
  when :direct
    result = kms_client.decrypt(ciphertext_blob: [value].pack("H*") ).plaintext 
    #Update original encryption
    if original_encryption_type != encryption_type && field_name.present?
      ZuoraConnect.logger.debug("Updating encryption to '#{original_encryption_type}', from '#{encryption_type}' for field '#{field_name}'", self.default_ougai_items)
      self.update_column(field_name, self.kms_encrypt(result, encryption_type: original_encryption_type))
    end

    return result
  when :envelope
    cipher = fetch_cipher('decrypt') 
    result = cipher.update(Base64.strict_decode64(value)) + cipher.final

    #Update original encryption
    if original_encryption_type != encryption_type && field_name.present?
      ZuoraConnect.logger.debug("Updating encryption to '#{original_encryption_type}', from '#{encryption_type}' for field '#{field_name}'", self.default_ougai_items)
      self.update_column(field_name, self.kms_encrypt(result, encryption_type: original_encryption_type))
    end
    return result
  else
    ZuoraConnect::Exceptions::Error.new("Invalid encryption method '#{encryption_type}'.")
  end
rescue ArgumentError => ex
  if ex.message == 'invalid base64' && encryption_type == :envelope && (kms_tries += 1) < 3
    ZuoraConnect.logger.warn("Fallback to encryption 'direct', from '#{encryption_type}'", ex, self.default_ougai_items)
    encryption_type = :direct 
    retry
  end
  raise#Add protection when decrypting
rescue Aws::KMS::Errors::InvalidCiphertextException => ex
  if encryption_type == :direct && (kms_tries += 1) < 3
    ZuoraConnect.logger.warn("Fallback to encryption 'envelope', from '#{encryption_type}'", ex, self.default_ougai_items)
    encryption_type = :envelope 
    retry
  end
  raise
rescue *AWS_AUTH_ERRORS => ex
  if (kms_tries += 1) < 3
    Rails.logger.warn(AWS_AUTH_ERRORS_MSG, ex)
    retry
  else
    Rails.logger.error(AWS_AUTH_ERRORS_MSG, ex)
    raise
  end
end

#kms_encrypt(value, encryption_type: ZuoraConnect.configuration.encryption_type) ⇒ Object



543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
# File 'app/models/zuora_connect/app_instance_base.rb', line 543

def kms_encrypt(value, encryption_type: ZuoraConnect.configuration.encryption_type)
  kms_tries ||= 0
  case encryption_type
  when :direct
    resp = kms_client.encrypt({key_id: kms_key(raise_on_blank: true), plaintext: value})
    return resp.ciphertext_blob.unpack('H*').first
  when :envelope
    cipher = fetch_cipher('encrypt')
    value = cipher.update(value.to_s)
    value << cipher.final
    return Base64.strict_encode64(value)
  else
    ZuoraConnect::Exceptions::Error.new("Invalid encryption method '#{encryption_type}'.")
  end
rescue *AWS_AUTH_ERRORS => ex
  if (kms_tries += 1) < 3
    Rails.logger.warn(AWS_AUTH_ERRORS_MSG, ex)
    retry
  else
    Rails.logger.error(AWS_AUTH_ERRORS_MSG, ex)
    raise
  end
end

#kms_key(raise_on_blank: false) ⇒ Object



455
456
457
458
459
# File 'app/models/zuora_connect/app_instance_base.rb', line 455

def kms_key(raise_on_blank: false)
  kms_value = ENV['AWS_KMS_ARN'] || aws_secrets['AWS_KMS_ARN']
  raise ZuoraConnect::Exceptions::Error.new("Missing KMS key") if raise_on_blank && kms_value.blank?
  return kms_value
end

#login_lookup(type: "Zuora") ⇒ Object



1372
1373
1374
1375
1376
1377
1378
# File 'app/models/zuora_connect/app_instance_base.rb', line 1372

def (type: "Zuora")
  results = []
  self.logins.each do |name, |
    results <<  if .tenant_type == type
  end
  return results
end

#logitem(item: {}, reset: false) ⇒ Object

START Metrics Methods ####



569
570
571
572
573
574
575
# File 'app/models/zuora_connect/app_instance_base.rb', line 569

def logitem(item: {}, reset: false)
  self.logitems = {} if self.logitems.class != Hash
  if item.class == Hash
    self.logitems = reset ? item : self.logitems.merge(item)
  end
  Thread.current[:appinstance] = self
end

#mark_for_refreshObject



839
840
841
# File 'app/models/zuora_connect/app_instance_base.rb', line 839

def mark_for_refresh
  return defined?(Redis.current) ? Redis.current.zadd("InstanceRefreshing", Time.now.to_i + REFRESH_TIMEOUT.to_i, self.id, {:nx => true}) : true
end

#marked_for_refresh?Boolean

START AppInstance Temporary Persistance Methods ####

Returns:

  • (Boolean)


813
814
815
816
817
818
819
820
# File 'app/models/zuora_connect/app_instance_base.rb', line 813

def marked_for_refresh?
  if defined?(Redis.current)
    Redis.current.zremrangebyscore("InstanceRefreshing", "0", "(#{Time.now.to_i}")
    return Redis.current.zscore("InstanceRefreshing", self.id).present?
  else
    return false
  end
end

#new_session(session: self.data_lookup, username: self.access_token, password: self.refresh_token, holding_pattern: false, **args) ⇒ Object



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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'app/models/zuora_connect/app_instance_base.rb', line 142

def new_session(session: self.data_lookup, username: self.access_token, password: self.refresh_token, holding_pattern: false, **args)
  self.api_version = "v2"
  self.username = username
  self.password = password
  self.last_refresh = session["#{self.id}::last_refresh"]
  self.connect_user = session["#{self.id}::user::email"] if session["#{self.id}::user::email"].present?
  PaperTrail.whodunnit = self.connect_user if defined?(PaperTrail)
  ElasticAPM.set_user(self.connect_user)   if defined?(ElasticAPM) && ElasticAPM.running?
  recoverable_session = false

  ## DEV MODE TASK DATA MOCKUP
  if ZuoraConnect.configuration.mode != "Production"
    mock_task_data = {
      "id" => ZuoraConnect.configuration.dev_mode_appinstance,
      "mode" => ZuoraConnect.configuration.dev_mode_mode,
      "name" => "Developer Instance"
    }

    case ZuoraConnect.configuration.dev_mode_options.class
    when Hash
      self.options = ZuoraConnect.configuration.dev_mode_options
    when Array
      mock_task_data["options"] = ZuoraConnect.configuration.dev_mode_options
    end

    ZuoraConnect.configuration.dev_mode_logins.each do |k,v|
      v = v.merge({"entities": [] }) if !v.keys.include?("entities")
      mock_task_data[k] = v
    end

    self.build_task(task_data: mock_task_data, session: session)
    self.set_backup_creds if !self['zuora_logins'].present?
    self.last_refresh = Time.now.to_i
  else
    time_expire = (session["#{self.id}::last_refresh"] || Time.now).to_i - INSTANCE_REFRESH_WINDOW.ago.to_i

    if session.empty?
      self.new_session_message = "REFRESHING - Session Empty"
      ZuoraConnect.logger.debug(self.new_session_message)
      raise ZuoraConnect::Exceptions::HoldingPattern if holding_pattern && !self.mark_for_refresh
      self.refresh(session: session)

    elsif (self.id != session["appInstance"].to_i)
      self.new_session_message = "REFRESHING - AppInstance ID(#{self.id}) does not match session id(#{session["appInstance"].to_i})"
      ZuoraConnect.logger.debug(self.new_session_message)
      raise ZuoraConnect::Exceptions::HoldingPattern if holding_pattern && !self.mark_for_refresh
      self.refresh(session: session)

    elsif session["#{self.id}::task_data"].blank? && !ZuoraConnect.configuration.local_task_data
      self.new_session_message = "REFRESHING - Task Data Blank"
      ZuoraConnect.logger.debug(self.new_session_message)
      raise ZuoraConnect::Exceptions::HoldingPattern if holding_pattern && !self.mark_for_refresh
      self.refresh(session: session)

    elsif session["#{self.id}::last_refresh"].blank?
      self.new_session_message = "REFRESHING - No Time on Cookie"
      recoverable_session = true
      ZuoraConnect.logger.debug(self.new_session_message)
      raise ZuoraConnect::Exceptions::HoldingPattern if holding_pattern && !self.mark_for_refresh
      self.refresh(session: session)

    # If the cache is expired and we can aquire a refresh lock
    elsif (session["#{self.id}::last_refresh"].to_i < INSTANCE_REFRESH_WINDOW.ago.to_i) && self.mark_for_refresh
      self.new_session_message = "REFRESHING - Session Old by #{time_expire.abs} second"
      recoverable_session = true
      ZuoraConnect.logger.debug(self.new_session_message)
      self.refresh(session: session)

    else
      if time_expire < 0
        self.new_session_message = ["REBUILDING - Expired by #{time_expire} seconds", self.marked_for_refresh? ? " cache updating as of #{self.reset_mark_refreshed_at} seconds ago" : nil].compact.join(',')
      else
        self.new_session_message = "REBUILDING - Expires in #{time_expire} seconds"
      end
      ZuoraConnect.logger.debug(self.new_session_message, self.default_ougai_items)
      self.build_task(task_data: session["#{self.id}::task_data"], session: session)
    end
  end
  return self
rescue ZuoraConnect::Exceptions::HoldingPattern => ex
  while self.marked_for_refresh?
    ZuoraConnect.logger.info("Holding - Expires in #{self.reset_mark_expires_at}. '#{self.new_session_message}'", self.default_ougai_items)
    sleep(HOLDING_PATTERN_SLEEP)
  end
  self.reload_attributes([:refresh_token, :oauth_expires_at, :access_token])
  session = self.data_lookup(session: session)
  retry
rescue ZuoraConnect::Exceptions::MissMatch => ex
  self.delete_app_instance
  session = {}
  ZuoraConnect.logger.error(ex, self.default_ougai_items.merge({app_instance_id_new: self.task_data['id']}))
  retry
rescue ZuoraConnect::Exceptions::InvalidCredentialSet => ex
  raise
rescue => ex
  if recoverable_session
    ZuoraConnect.logger.warn("REBUILDING - Using backup expired cache", ex, self.default_ougai_items)
    self.build_task(task_data: session["#{self.id}::task_data"], session: session)
    return self
  else
    ZuoraConnect.logger.error("Failed new session", ex, self.default_ougai_items)
    raise
  end
ensure
  begin
    I18n.locale = self.locale
  rescue I18n::InvalidLocale => ex
    ZuoraConnect.logger.error(ex) if !IGNORED_LOCALS.include?(ex.locale.to_s.downcase)
  end

  self.set_timezone

  if self.task_data.present?
    tenants = self.task_data.fetch('tenant_ids', [])
    organizations = self.task_data.fetch('organizations', [])
    if defined?(ElasticAPM) && ElasticAPM.running?
      if ElasticAPM.respond_to?(:set_label)
        ElasticAPM.set_label(:tenant_id, tenants.first)
        ElasticAPM.set_label(:organization, organizations.first)
      else
        ElasticAPM.set_label(:tenant_id, tenants.first)
        ElasticAPM.set_label(:organization, organizations.first)
      end
    end

    tenants = (self.task_data.dig(LOGIN_TENANT_DESTINATION,'entities') || []).select {|entity| !entity['skip'].to_bool}.map{|e| e['entityId']}.uniq if tenants.blank?
    params = {
      name: self.task_data.dig('name'),
      zuora_entity_ids: (self.task_data.dig(LOGIN_TENANT_DESTINATION,'entities') || []).select {|entity| !entity['skip'].to_bool}.map{|e| e['id']}.uniq,
      zuora_global_tenant_id: task_data.dig(LOGIN_TENANT_DESTINATION, 'entities', 0, 'globalEntityId').to_i, # tenant id of the global/parent entity, 0 if nil
      zuora_tenant_ids: tenants.map(&:to_s).uniq,
      organizations: organizations
    }
    if self.methods.include?(LOGIN_TENANT_DESTINATION.to_sym)
      client = self.send(LOGIN_TENANT_DESTINATION).client
      if defined?(client.rest_domain)
        ZuoraConnect::RequestIdMiddleware.zuora_rest_domain = client.rest_domain
        params.merge!({zuora_domain: client.rest_domain, environment: client.environment })
      end
    end

    # makes it safe to add elements to params which don't correspond to existing columns in an app's schema
    # rejects those elements which do not correspond to model attributes for your app
    params = params.reject{|k,v| !self.attributes.keys.member?(k.to_s) || self[k] == v}
    self.update_columns(params) if params.present?
  end
end

#new_session_for_api_requests(params: {}) ⇒ Object

Overide this method to avoid the new session call for api requests that use the before filter authenticate_app_api_request. This can be usefull for apps that dont need connect metadata call, or credentials, to operate for api requests



1337
1338
1339
# File 'app/models/zuora_connect/app_instance_base.rb', line 1337

def new_session_for_api_requests(params: {})
  return true
end

#new_session_for_ui_requests(params: {}) ⇒ Object

Overide this method to avoid the new session call for ui requests that use the before filter authenticate_connect_app_request. This can be usefull for apps that dont need connect metadata call, or credentials, to operate for ui requests



1343
1344
1345
# File 'app/models/zuora_connect/app_instance_base.rb', line 1343

def new_session_for_ui_requests(params: {})
  return true
end

#oauth_expired?Boolean

Returns:

  • (Boolean)


764
765
766
# File 'app/models/zuora_connect/app_instance_base.rb', line 764

def oauth_expired?
  return self.oauth_expires_at.present? ? (self.oauth_expires_at < Time.now.utc) : true
end

#prune_dataObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/models/zuora_connect/app_instance_base.rb', line 99

def prune_data
  if defined?(Redis.current)
    Redis.current.zadd("AppInstance:Deleted", Time.now.to_i, self.id)
    Redis.current.del("AppInstance:#{self.id}")
    Redis.current.zrem("APILimits", self.id)
    Redis.current.zrem("InstanceRefreshing", self.id)
  end
  if defined?(Resque.redis)
    Resque.redis.zrange("PauseQueue", 0, -1).each do |key|
      Resque.redis.zrem("PauseQueue", key) if key.split("__").first.to_i == self.id
    end
  end
  return true
end

#queue_pause(time: nil, current_user: 'Default') ⇒ Object



980
981
982
983
984
985
986
987
988
# File 'app/models/zuora_connect/app_instance_base.rb', line 980

def queue_pause(time: nil, current_user: 'Default')
  key = "#{self.id}__#{current_user}"
  if time.present?
    raise "Time must be integer of seconds instead of #{time.class}." if !['Integer', 'Fixnum'].include?(time.class.to_s)
    Resque.redis.zadd("PauseQueue", Time.now.to_i + time, key)
  else
    Resque.redis.zadd("PauseQueue", 9999999999, key)
  end
end

#queue_paused?Boolean

Returns:

  • (Boolean)


975
976
977
978
# File 'app/models/zuora_connect/app_instance_base.rb', line 975

def queue_paused?
  Resque.redis.zremrangebyscore("PauseQueue", "0", "(#{Time.now.to_i}")
  return Resque.redis.zrange("PauseQueue", 0, -1).map {|key| key.split("__")[0]}.include?(self.id.to_s)
end

#queue_start(current_user: 'Default') ⇒ Object



990
991
992
993
994
995
996
997
# File 'app/models/zuora_connect/app_instance_base.rb', line 990

def queue_start(current_user: 'Default')
  paused_user = Resque.redis.zrange("PauseQueue", 0, -1).map {|key| key.split("__")[0] == "#{self.id}" ? key.split("__")[1] : nil}.compact.first
  if paused_user == current_user || paused_user.blank?
    Resque.redis.zrem("PauseQueue", "#{self.id}__#{paused_user}")
  else
    raise "Can only unpause for user #{paused_user}."
  end
end

#refresh(session: {}) ⇒ Object



396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# File 'app/models/zuora_connect/app_instance_base.rb', line 396

def refresh(session: {})
  refresh_count ||= 0
  skip_connect ||= ZuoraConnect.configuration.skip_connect
  #Check how app was deployed
  if !self.auto_deployed? && (!skip_connect || self['zuora_logins'].blank?)
    self.fetch_connect_data(session: session)
  else
    self.build_task(task_data: self.zuora_logins, session: session)
  end
  self.last_refresh = Time.now.to_i
  self.cache_app_instance
  self.reset_mark_for_refresh
 
rescue => ex
  refresh_count += 1
  if self['zuora_logins'].present? && refresh_count < 3
    ZuoraConnect.logger.warn("REFRESH TASK - Fallback to local encrypted store", ex, self.default_ougai_items)
    skip_connect = true
    retry
  end
  raise
end

#refresh_oauthObject



768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
# File 'app/models/zuora_connect/app_instance_base.rb', line 768

def refresh_oauth
  refresh_oauth_count ||= 0
  response = HTTParty.post("#{ZuoraConnect.configuration.url}/oauth/token", body: {
    :grant_type => "refresh_token",
    :redirect_uri => ZuoraConnect.configuration.oauth_client_redirect_uri,
    :refresh_token => self.refresh_token
  })

  if response.code == 200
    response_body = JSON.parse(response.body)

    self.refresh_token = response_body["refresh_token"]
    self.access_token = response_body["access_token"]
    self.oauth_expires_at = Time.at(response_body["created_at"].to_i) + response_body["expires_in"].seconds
    self.save(:validate => false)
  else
    raise ZuoraConnect::Exceptions::ConnectCommunicationError.new("Error Refreshing Access Token", response.body, response.code)
  end
rescue *(ZuoraAPI::Login::CONNECTION_EXCEPTIONS + ZuoraAPI::Login::CONNECTION_READ_EXCEPTIONS) => ex
  if (refresh_oauth_count += 1) < 3
    sleep(CONNECT_COMMUNICATION_SLEEP)
    ZuoraConnect.logger.debug("REFRESH OAUTH - Connection Failure Retrying(#{refresh_oauth_count})", ex, self.default_ougai_items)
    retry
  else
    Rails.logger.fatal("REFRESH OAUTH - Connection Failed", ex, self.default_ougai_items)
    raise
  end
rescue ZuoraConnect::Exceptions::ConnectCommunicationError => ex
  sleep(CONNECT_COMMUNICATION_SLEEP)
  self.reload_attributes([:refresh_token, :oauth_expires_at, :access_token]) #Reload only the refresh token for retry

  #After reload, if nolonger expired return
  return if !self.oauth_expired?

  if (refresh_oauth_count += 1) < 3
    ZuoraConnect.logger.debug("REFRESH OAUTH - Communication Failure Retrying(#{refresh_oauth_count})", ex, self.default_ougai_items)
    retry
  else
    ZuoraConnect.logger.fatal("REFRESH OAUTH - Communication Failed #{ex.code}", ex, self.default_ougai_items)
    raise
  end
end

#reload_attributes(selected_attributes) ⇒ Object



1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
# File 'app/models/zuora_connect/app_instance_base.rb', line 1353

def reload_attributes(selected_attributes)
  raise "Attibutes must be array" unless selected_attributes.is_a?(Array)
  selected_attributes.push(:organizations, :environment, :zuora_tenant_ids)
  selected_attributes.uniq!
  value_attributes = self.class.unscoped.where(id: id).select(selected_attributes).first.attributes
  value_attributes.each do |key, value|
    next if key == "id" && value.blank?
    self.send(:write_attribute, key, value)
  end
  self
end

#reset_mark_expires_atObject



830
831
832
833
834
835
836
837
# File 'app/models/zuora_connect/app_instance_base.rb', line 830

def reset_mark_expires_at
  if defined?(Redis.current)
    refresh_time = Redis.current.zscore("InstanceRefreshing", self.id)
    return refresh_time.present? ? (refresh_time - Time.now.to_i).round(0) : 0
  else
    return 0
  end
end

#reset_mark_for_refreshObject



822
823
824
# File 'app/models/zuora_connect/app_instance_base.rb', line 822

def reset_mark_for_refresh
  Redis.current.zrem("InstanceRefreshing", self.id) if defined?(Redis.current)
end

#reset_mark_refreshed_atObject



826
827
828
# File 'app/models/zuora_connect/app_instance_base.rb', line 826

def reset_mark_refreshed_at
  return defined?(Redis.current) ? REFRESH_TIMEOUT.to_i - reset_mark_expires_at : 0
end

#save_data(session = Hash.new) ⇒ Object



881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
# File 'app/models/zuora_connect/app_instance_base.rb', line 881

def save_data(session = Hash.new)
  self.logins.each do |key, |
    if .tenant_type == "Zuora"
      if .available_entities.size > 1 && Rails.application.config.session_store != ActionDispatch::Session::CookieStore
        .available_entities.each do |entity_key|
          session["#{self.id}::#{key}::#{entity_key}:current_session"]            = .client(entity_key).current_session            if .client.respond_to?(:current_session) && .client(entity_key).current_session.present?
          session["#{self.id}::#{key}::#{entity_key}:bearer_token"]               = .client(entity_key).bearer_token               if .client.respond_to?(:bearer_token) && .client(entity_key).bearer_token.present?
          session["#{self.id}::#{key}::#{entity_key}:oauth_session_expires_at"]   = .client(entity_key).oauth_session_expires_at   if .client.respond_to?(:oauth_session_expires_at) && .client(entity_key).oauth_session_expires_at.present?
        end
      else
        session["#{self.id}::#{key}:current_session"]             = .client.current_session            if .client.respond_to?(:current_session) && .client.current_session.present?
        session["#{self.id}::#{key}:bearer_token"]                = .client.bearer_token               if .client.respond_to?(:bearer_token) && .client.bearer_token.present?
        session["#{self.id}::#{key}:oauth_session_expires_at"]    = .client.oauth_session_expires_at   if .client.respond_to?(:oauth_session_expires_at) && .client.oauth_session_expires_at.present?
      end
    end
  end

  session["#{self.id}::task_data"] = self.task_data if !ZuoraConnect.configuration.local_task_data

  #Redis is not defined strip out old data
  if !defined?(Redis.current)
    strip_cache_data(object: session["#{self.id}::task_data"])
  end

  session["#{self.id}::last_refresh"] = self.last_refresh
  session["appInstance"] = self.id
  return session
end

#send_emailObject



1369
1370
# File 'app/models/zuora_connect/app_instance_base.rb', line 1369

def send_email
end

#set_backup_credsObject

START KMS ENCRYPTION Methods ####



420
421
422
423
424
# File 'app/models/zuora_connect/app_instance_base.rb', line 420

def set_backup_creds
  if self.kms_key.present? && self.kms_key.match(/^arn:aws:.*/) && self.task_data.present?
    self.zuora_logins = self.strip_cache_data(object: self.task_data.dup, keys: ['applications', 'tokens', 'user_settings'])
  end
end

#set_timezone(timezone: self.timezone, type: :default) ⇒ Object



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'app/models/zuora_connect/app_instance_base.rb', line 290

def set_timezone(timezone: self.timezone, type: :default)
  if timezone.blank?
    timezone = self.timezone
  end

  if type == :default
    Time.zone = timezone
  elsif type == :user
    begin
      sql = <<-eos
        SELECT zuora_users.zuora_identity_response FROM "#{self.id}".zuora_users ORDER BY zuora_users.updated_at DESC LIMIT 1;
      eos
      user = ActiveRecord::Base.connection.execute(sql).to_a.first

      if user.present?
        zuora_identity_response = JSON.parse(user.fetch('zuora_identity_response', '{}'))
        self.user_timezone = zuora_identity_response.values.first&.dig('timeZone')
      else
        if (Redis.current.hget(TIMEZONE_LOG_RATE_LIMIT_KEY, self.id).to_i + TIMEZONE_LOG_PERIOD.to_i) <= Time.now.to_i
          Rails.logger.error('Cannot find any user to set the timezone', app_instance_id: self.id)
          Redis.current.hset(TIMEZONE_LOG_RATE_LIMIT_KEY, self.id, Time.now.to_i)
        end
      end
    rescue => ex
      Rails.logger.error('There is an error while getting timezone users', ex)
    end

    if self.user_timezone.present?
      # connect instance which has a custom timezone
      if !self.auto_deployed? && (
        ActiveSupport::TimeZone[self.task_data.dig('user_settings', 'timezone') || '']&.utc_offset !=
          ActiveSupport::TimeZone[self.user_timezone]&.utc_offset
      )
        if self.environment == 'Production' &&
          (Redis.current.hget(TIMEZONE_LOG_RATE_LIMIT_KEY, self.id).to_i + TIMEZONE_LOG_PERIOD.to_i) <= Time.now.to_i
          ZuoraConnect.logger.error(
            "Instance and user timezones are different. User has '#{self.user_timezone}' and " \
            "instance has '#{self.task_data.dig('user_settings', 'timezone')}'",
            app_instance_id: self.id
          )
          Redis.current.hset(TIMEZONE_LOG_RATE_LIMIT_KEY, self.id, Time.now.to_i)
        end
        self.user_timezone = nil
        Time.zone = timezone
      else
        begin
          Time.zone = self.user_timezone
        rescue ArgumentError
          Rails.logger.error('Malformed user timezone', app_instance_id: self.id)
          Time.zone = timezone
        end
      end
    else
      Time.zone = timezone
    end
  end
rescue => e
  Rails.logger.error('Malformed timezone used', e, app_instance_id: self.id)
  Time.zone = self.timezone
end

#strip_cache_data(object: {}, keys: ['applications', 'tokens','tenant_ids', 'organizations','user_settings']) ⇒ Object



910
911
912
913
914
915
916
# File 'app/models/zuora_connect/app_instance_base.rb', line 910

def strip_cache_data(object: {}, keys: ['applications', 'tokens','tenant_ids', 'organizations','user_settings'] )
  keys.each {|key| object.delete(key) }
  object.select {|k,v| k.include?('login') && v['tenant_type'] == 'Zuora'}.each do |, |
    object[]['entities'] = .fetch('entities',[]).map {|entity| entity.slice('id', 'tenantId', 'entityId', 'displayName', 'identifier')}
  end
  return object
end

#update_logins(options) ⇒ Object

This can update an existing login, add a new login, change to another existing login EXAMPLE: “ftp_login_14”,“username”: “ftplogin7”,“tenant_type”: “Custom”,“password”: “test2”,“url”: “www.ftp.com”,“custom_data”: { “path”: “/var/usr/test”}



649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
# File 'app/models/zuora_connect/app_instance_base.rb', line 649

def update_logins(options)
   ||= 0
  response = HTTParty.post(ZuoraConnect.configuration.url + "/api/#{self.api_version}/tools/tasks/#{self.id}/logins",:body => {:access_token => self.username}.merge(options))
  parsed_json =  JSON.parse(response.body)
  if response.code == 200
    if defined?(Redis.current)
      self.build_task(task_data: parsed_json, session: self.data_lookup)
      self.last_refresh = Time.now.to_i
      self.cache_app_instance
    end
    return parsed_json
  elsif response.code == 400
    raise ZuoraConnect::Exceptions::APIError.new(message: parsed_json['errors'].join(' '), response: response)
  else
    raise ZuoraConnect::Exceptions::ConnectCommunicationError.new("Error Communicating with Connect", response.body, response.code)
  end
rescue *(ZuoraAPI::Login::CONNECTION_EXCEPTIONS + ZuoraAPI::Login::CONNECTION_READ_EXCEPTIONS) => ex
  if ( += 1) < 3
    retry
  else
    raise
  end
rescue ZuoraConnect::Exceptions::ConnectCommunicationError => ex
  if ( += 1) < 3
    if ex.code == 401
      self.refresh_oauth
    end
    retry
  else
    raise
  end
end

#update_task(options) ⇒ Object



726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
# File 'app/models/zuora_connect/app_instance_base.rb', line 726

def update_task(options)
  update_task_count ||= 0
  response = HTTParty.post(ZuoraConnect.configuration.url + "/api/#{self.api_version}/tools/tasks/#{self.id}/update_task",:body => {:access_token => self.username}.merge(options))
  parsed_json =  JSON.parse(response.body)
  if response.code == 200
    return parsed_json
  elsif response.code == 400
    raise ZuoraConnect::Exceptions::APIError.new(message: parsed_json['errors'].join(' '), response: response)
  else
    raise ZuoraConnect::Exceptions::ConnectCommunicationError.new("Error Communicating with Connect", response.body, response.code)
  end
rescue *(ZuoraAPI::Login::CONNECTION_EXCEPTIONS + ZuoraAPI::Login::CONNECTION_READ_EXCEPTIONS) => ex
  if (update_task_count += 1) < 3
    retry
  else
    raise
  end
rescue ZuoraConnect::Exceptions::ConnectCommunicationError => ex
  if (update_task_count += 1) < 3
    if ex.code == 401
      self.refresh_oauth
    end
    retry
  else
    raise
  end
end

#updateOption(optionId, value) ⇒ Object



643
644
645
# File 'app/models/zuora_connect/app_instance_base.rb', line 643

def updateOption(optionId, value)
  response = HTTParty.get(ZuoraConnect.configuration.url + "/api/#{self.api_version}/tools/application_options/#{optionId}/edit?value=#{value}",:body => {:access_token => self.username})
end

#upload_to_s3(local_file, s3_path = nil) ⇒ Object



1292
1293
1294
1295
1296
# File 'app/models/zuora_connect/app_instance_base.rb', line 1292

def upload_to_s3(local_file,s3_path = nil)
  s3_path = local_file.split("/").last if s3_path.nil?
  obj = self.s3_client.bucket(ZuoraConnect.configuration.s3_bucket_name).object("#{ZuoraConnect.configuration.s3_folder_name}/#{self.id.to_s}/#{s3_path}}")
  obj.upload_file(local_file, :server_side_encryption => 'AES256')
end

#zuora_loginsObject



432
433
434
435
# File 'app/models/zuora_connect/app_instance_base.rb', line 432

def zuora_logins
  raise  ZuoraConnect::Exceptions::ConnectCommunicationError.new("Zuora Logins is blank, cannot decrypt.") if super.blank?
  return JSON.parse(kms_decrypt(super, field_name: :zuora_logins))
end

#zuora_logins=(val) ⇒ Object



426
427
428
429
430
# File 'app/models/zuora_connect/app_instance_base.rb', line 426

def zuora_logins=(val)
  write_attribute(:zuora_logins, kms_encrypt(val.to_json))
rescue Aws::KMS::Errors::ValidationException, Aws::KMS::Errors::NotFoundException, *AWS_AUTH_ERRORS => ex
  Rails.logger.warn(AWS_AUTH_ERRORS_MSG, ex)
end