|
2 | 2 | require_relative '../util/ExceptionHandler.rb' |
3 | 3 | require_relative '../logging/log_factory.rb' |
4 | 4 | require_relative '../logging/log_configuration.rb' |
| 5 | +require_relative '../util/CertificateUtility.rb' |
5 | 6 |
|
6 | 7 | public |
7 | 8 | # This fuction has all the merchantConfig properties getters and setters methods |
8 | 9 | class Merchantconfig |
9 | 10 | def initialize(cybsPropertyObj) |
10 | | - # Common Parameters |
11 | | - @merchantId = cybsPropertyObj['merchantID'] |
12 | | - @runEnvironment = cybsPropertyObj['runEnvironment'] |
13 | | - @intermediateHost = cybsPropertyObj['intermediateHost'] |
14 | | - @defaultDeveloperId = cybsPropertyObj['defaultDeveloperId'] |
15 | | - @authenticationType = cybsPropertyObj['authenticationType'] |
16 | | - @proxyAddress = cybsPropertyObj['proxyAddress'] |
17 | | - @proxyPort = cybsPropertyObj['proxyPort'] |
18 | | - @getId = '' |
19 | | - @requestHost = '' |
20 | | - @requestTarget = '' |
21 | | - @requestJsonData = '' |
22 | | - # HTTP Parameters |
23 | | - @merchantSecretKey = cybsPropertyObj['merchantsecretKey'] |
24 | | - @merchantKeyId = cybsPropertyObj['merchantKeyId'] |
25 | | - # JWT Parameters |
26 | | - @keysDirectory = cybsPropertyObj['keysDirectory'] |
27 | | - @keyAlias = cybsPropertyObj['keyAlias'] |
28 | | - @keyPass = cybsPropertyObj['keyPass'] |
29 | | - @keyFilename = cybsPropertyObj['keyFilename'] |
30 | | - @useMetaKey = cybsPropertyObj['useMetaKey'] |
31 | | - @portfolioID = cybsPropertyObj['portfolioID'] |
32 | | - @solutionId = cybsPropertyObj['solutionId'] |
33 | | - # MutualAuth & OAuth Parameters |
34 | | - @enableClientCert = cybsPropertyObj['enableClientCert'] |
35 | | - @clientCertDirectory = cybsPropertyObj['clientCertDirectory'] |
36 | | - @sslClientCert = cybsPropertyObj['sslClientCert'] |
37 | | - @privateKey = cybsPropertyObj['privateKey'] |
38 | | - @sslKeyPassword = cybsPropertyObj['sslKeyPassword'] |
39 | | - @clientId = cybsPropertyObj['clientId'] |
40 | | - @clientSecret = cybsPropertyObj['clientSecret'] |
41 | | - @accessToken = cybsPropertyObj['accessToken'] |
42 | | - @refreshToken = cybsPropertyObj['refreshToken'] |
43 | | - # LogConfiguration |
44 | | - @log_config = LogConfiguration.new(cybsPropertyObj['logConfiguration']) |
45 | | - # Custom Default Headers |
46 | | - @defaultCustomHeaders = cybsPropertyObj['defaultCustomHeaders'] |
47 | | - # Path to client JWE pem file directory |
48 | | - @pemFileDirectory = cybsPropertyObj['pemFileDirectory'] |
49 | | - @mleKeyAlias = cybsPropertyObj['mleKeyAlias'] |
50 | | - @useMLEGlobally = cybsPropertyObj['useMLEGlobally'] |
51 | | - |
52 | | - if !cybsPropertyObj['mleForRequestPublicCertPath'].nil? && !cybsPropertyObj['mleForRequestPublicCertPath'].to_s.strip.empty? |
53 | | - @mleForRequestPublicCertPath = cybsPropertyObj['mleForRequestPublicCertPath'].to_s.strip |
54 | | - end |
| 11 | + # Common Parameters |
| 12 | + @merchantId = cybsPropertyObj['merchantID'] |
| 13 | + @runEnvironment = cybsPropertyObj['runEnvironment'] |
| 14 | + @intermediateHost = cybsPropertyObj['intermediateHost'] |
| 15 | + @defaultDeveloperId = cybsPropertyObj['defaultDeveloperId'] |
| 16 | + @authenticationType = cybsPropertyObj['authenticationType'] |
| 17 | + @proxyAddress = cybsPropertyObj['proxyAddress'] |
| 18 | + @proxyPort = cybsPropertyObj['proxyPort'] |
| 19 | + @getId = '' |
| 20 | + @requestHost = '' |
| 21 | + @requestTarget = '' |
| 22 | + @requestJsonData = '' |
| 23 | + # HTTP Parameters |
| 24 | + @merchantSecretKey = cybsPropertyObj['merchantsecretKey'] |
| 25 | + @merchantKeyId = cybsPropertyObj['merchantKeyId'] |
| 26 | + # JWT Parameters |
| 27 | + @keysDirectory = cybsPropertyObj['keysDirectory'] |
| 28 | + @keyAlias = cybsPropertyObj['keyAlias'] |
| 29 | + @keyPass = cybsPropertyObj['keyPass'] |
| 30 | + @keyFilename = cybsPropertyObj['keyFilename'] |
| 31 | + @useMetaKey = cybsPropertyObj['useMetaKey'] |
| 32 | + @portfolioID = cybsPropertyObj['portfolioID'] |
| 33 | + @solutionId = cybsPropertyObj['solutionId'] |
| 34 | + @p12KeyFilePath = nil |
| 35 | + # MutualAuth & OAuth Parameters |
| 36 | + @enableClientCert = cybsPropertyObj['enableClientCert'] |
| 37 | + @clientCertDirectory = cybsPropertyObj['clientCertDirectory'] |
| 38 | + @sslClientCert = cybsPropertyObj['sslClientCert'] |
| 39 | + @privateKey = cybsPropertyObj['privateKey'] |
| 40 | + @sslKeyPassword = cybsPropertyObj['sslKeyPassword'] |
| 41 | + @clientId = cybsPropertyObj['clientId'] |
| 42 | + @clientSecret = cybsPropertyObj['clientSecret'] |
| 43 | + @accessToken = cybsPropertyObj['accessToken'] |
| 44 | + @refreshToken = cybsPropertyObj['refreshToken'] |
| 45 | + # LogConfiguration |
| 46 | + @log_config = LogConfiguration.new(cybsPropertyObj['logConfiguration']) |
| 47 | + # Custom Default Headers |
| 48 | + @defaultCustomHeaders = cybsPropertyObj['defaultCustomHeaders'] |
| 49 | + # Path to client JWE pem file directory |
| 50 | + @pemFileDirectory = cybsPropertyObj['pemFileDirectory'] |
| 51 | + @mleKeyAlias = cybsPropertyObj['mleKeyAlias'] |
| 52 | + @useMLEGlobally = cybsPropertyObj['useMLEGlobally'] |
| 53 | + |
| 54 | + if !cybsPropertyObj['mleForRequestPublicCertPath'].nil? && !cybsPropertyObj['mleForRequestPublicCertPath'].to_s.strip.empty? |
| 55 | + @mleForRequestPublicCertPath = cybsPropertyObj['mleForRequestPublicCertPath'].to_s.strip |
| 56 | + end |
55 | 57 |
|
56 | | - @mapToControlMLEonAPI = cybsPropertyObj['mapToControlMLEonAPI'] |
57 | | - validateMerchantDetails |
58 | | - validateMLEConfiguration |
59 | | - logAllProperties(cybsPropertyObj) |
| 58 | + @mapToControlMLEonAPI = cybsPropertyObj['mapToControlMLEonAPI'] |
| 59 | + validateMerchantDetails |
| 60 | + validateMLEConfiguration |
| 61 | + @p12KeyFilePath = File.join(@keysDirectory, @keyFilename + ".p12") |
| 62 | + logAllProperties(cybsPropertyObj) |
60 | 63 | end |
61 | 64 |
|
62 | 65 | #fall back logic |
@@ -162,6 +165,9 @@ def validateMerchantDetails() |
162 | 165 | elsif !@keyFilename.instance_of? String |
163 | 166 | @keyFilename=@keyFilename.to_s |
164 | 167 | end |
| 168 | + if !check_key_file |
| 169 | + @log_obj.logger.error(ExceptionHandler.new.new_custom_error "Error finding or accessing the Key Directory or Key File. Please review the values in the merchant configuration.") |
| 170 | + end |
165 | 171 | end |
166 | 172 | if @authenticationType.upcase == Constants::AUTH_TYPE_MUTUAL_AUTH |
167 | 173 | if @clientId.to_s.empty? |
@@ -262,15 +268,24 @@ def validateMLEConfiguration |
262 | 268 | @mleKeyAlias = Constants::DEFAULT_ALIAS_FOR_MLE_CERT |
263 | 269 | end |
264 | 270 |
|
265 | | - # verify the input path for mle Cert should be correct else throw error in both case mle=true/false |
266 | 271 | if @mleForRequestPublicCertPath && !@mleForRequestPublicCertPath.to_s.strip.empty? |
267 | | - unless File.exist?(@mleForRequestPublicCertPath) && File.readable?(@mleForRequestPublicCertPath) |
268 | | - err = StandardError.new(Constants::ERROR_PREFIX + "Invalid mleForRequestPublicCertPath: file does not exist or is not readable") |
| 272 | + begin |
| 273 | + CertificateUtility.validatePathAndFile(@mleForRequestPublicCertPath, "mleForRequestPublicCertPath", @log_config) |
| 274 | + rescue => err |
269 | 275 | @log_obj.logger.error(ExceptionHandler.new.new_api_exception err) |
270 | 276 | raise err |
271 | 277 | end |
272 | 278 | end |
273 | 279 |
|
| 280 | + # # verify the input path for mle Cert should be correct else throw error in both case mle=true/false |
| 281 | + # if @mleForRequestPublicCertPath && [email protected]_s.strip.empty? |
| 282 | + # unless File.exist?(@mleForRequestPublicCertPath) && File.readable?(@mleForRequestPublicCertPath) |
| 283 | + # err = StandardError.new(Constants::ERROR_PREFIX + "Invalid mleForRequestPublicCertPath: file does not exist or is not readable") |
| 284 | + # @log_obj.logger.error(ExceptionHandler.new.new_api_exception err) |
| 285 | + # raise err |
| 286 | + # end |
| 287 | + # end |
| 288 | + |
274 | 289 | # mle_configured = @useMLEGlobally |
275 | 290 | |
276 | 291 | # @mapToControlMLEonAPI.each do |_, value| |
@@ -306,6 +321,35 @@ def logAllProperties(merchantPropertyObj) |
306 | 321 | @log_obj.logger.info('Merchant Configuration :\n' + propertyObj.to_s) |
307 | 322 | end |
308 | 323 |
|
| 324 | + def check_key_file |
| 325 | + # Directory exists? |
| 326 | + unless Dir.exist?(@keysDirectory) |
| 327 | + @log_obj.logger.error("Keys Directory not found. Entered directory : #{@keysDirectory}") |
| 328 | + return false |
| 329 | + end |
| 330 | + |
| 331 | + key_file_pathname = File.join(@keysDirectory, @keyFilename + ".p12") |
| 332 | + |
| 333 | + # File exists? |
| 334 | + unless File.exist?(key_file_pathname) |
| 335 | + @log_obj.logger.error("Key File not found. Check path/filename entered. Entered path/filename : #{key_file_pathname}") |
| 336 | + return false |
| 337 | + end |
| 338 | + |
| 339 | + @log_obj.logger.info("Entered value for Key File Path : #{key_file_pathname}") |
| 340 | + |
| 341 | + # Can file be opened for reading? |
| 342 | + begin |
| 343 | + File.open(key_file_pathname, 'rb') do |f| |
| 344 | + # Just open and close |
| 345 | + end |
| 346 | + return true |
| 347 | + rescue => e |
| 348 | + @log_obj.logger.info("File cannot be accessed. Permission denied : #{key_file_pathname}") |
| 349 | + return false |
| 350 | + end |
| 351 | + end |
| 352 | + |
309 | 353 | # getter and setter methods |
310 | 354 | attr_accessor :merchantId |
311 | 355 | attr_accessor :merchantSecretKey |
@@ -346,4 +390,5 @@ def logAllProperties(merchantPropertyObj) |
346 | 390 | attr_accessor :mleForRequestPublicCertPath |
347 | 391 | attr_accessor :mapToControlMLEonAPI |
348 | 392 | attr_accessor :mleKeyAlias |
| 393 | + attr_accessor :p12KeyFilePath |
349 | 394 | end |
0 commit comments