Produced by Araxis Merge on 7/6/2017 8:23:28 PM Central Daylight Time. See www.araxis.com for information about Merge. This report uses XHTML and CSS2, and is best viewed with a modern standards-compliant browser. For optimum results when printing this report, use landscape orientation and enable printing of background images and colours in your browser.
| # | Location | File | Last Modified |
|---|---|---|---|
| 1 | OSCIF_CTT_v4_build 1.zip\rails_prisme\lib\utilities | prisme_utilities.rb | Wed May 31 03:59:08 2017 UTC |
| 2 | OSCIF_CTT_v4_build 1.zip\rails_prisme\lib\utilities | prisme_utilities.rb | Mon Jul 3 23:36:10 2017 UTC |
| Description | Between Files 1 and 2 |
|
|---|---|---|
| Text Blocks | Lines | |
| Unchanged | 3 | 946 |
| Changed | 3 | 10 |
| Inserted | 0 | 0 |
| Removed | 0 | 0 |
| Whitespace | |
|---|---|
| Character case | Differences in character case are significant |
| Line endings | Differences in line endings (CR and LF characters) are ignored |
| CR/LF characters | Not shown in the comparison detail |
No regular expressions were active.
| 1 | require 'u ri' | |
| 2 | ||
| 3 | #methods f or Prisme, but not K omet go he re, for Ko met visibi lity see h elpers.rb in rails c ommon. | |
| 4 | module Pri smeUtiliti es | |
| 5 | ||
| 6 | module R outeHelper | |
| 7 | includ e Rails.ap plication. routes.url _helpers | |
| 8 | ||
| 9 | def se lf.route(u rl_or_path , proxify = false, * *params) | |
| 10 | host = PRISME_ ENVIRONMEN T.eql?(Pri smeConstan ts::ENVIRO NMENT::DEV _BOX.to_s) ? 'localh ost' : Soc ket.gethos tname | |
| 11 | url = Rails.ap plication. routes.url _helpers.s end(url_or _path.to_s ym, {port: PrismeCons tants::URL ::PORT, pr otocol: Pr ismeConsta nts::URL:: SCHEME, ho st: host, relative_u rl_root: ' /' + Prism eConstants ::URL::CON TEXT}.merg e(params)) | |
| 12 | if p roxify | |
| 13 | ro ot = Rails .applicati on.routes. url_helper s.send(:ro ot_url, {p ort:Prisme Constants: :URL::PORT , protocol : PrismeCo nstants::U RL::SCHEME , host: ho st, relati ve_url_roo t: '/' + P rismeConst ants::URL: :CONTEXT}) | |
| 14 | ur l = url.gs ub(root,UR I(root).pr oxify.to_s ) | |
| 15 | end | |
| 16 | url | |
| 17 | end | |
| 18 | end | |
| 19 | ||
| 20 | KOMET_AP PLICATION = /^rails_ komet/ | |
| 21 | ISAAC_AP PLICATION = /^isaac- rest/ | |
| 22 | ALL_APPL ICATION = /.*/ | |
| 23 | VALID_AP PLICATIONS = [KOMET_ APPLICATIO N, ISAAC_A PPLICATION , ALL_APPL ICATION] | |
| 24 | VUID_DB_ FILE = $PR OPS['PRISM E.data_dir ectory'] + '/' + $PR OPS['PRISM E.vuid_db_ file'] | |
| 25 | ||
| 26 | ||
| 27 | class << self | |
| 28 | #these do leak i nternal st ate. In m ost cases you should call the correspond ing method below to get a defe nsive copy | |
| 29 | #of yo ur data st ructure | |
| 30 | attr_a ccessor :s soi_logout _url | |
| 31 | attr_a ccessor :c onfig #ser ver_config .yml | |
| 32 | attr_a ccessor :v uid_config #VUID_con fig.yml | |
| 33 | attr_a ccessor :a itc_env #a itc_enviro nment.yml | |
| 34 | end | |
| 35 | ||
| 36 | def self .localize_ host(host) | |
| 37 | host.g sub!('0:0: 0:0:0:0:0: 1', 'local host') | |
| 38 | host.g sub!('127. 0.0.1', 'l ocalhost') | |
| 39 | host | |
| 40 | end | |
| 41 | ||
| 42 | def self .get_proxy _contexts( tomcat_ar: , applicat ion_type:) | |
| 43 | raise ArgumentEr ror.new "A pplication _type must be a memb er of Pris meUtilitie s::VALID_A PPLICATION S!" unless VALID_APP LICATIONS. include? a pplication _type | |
| 44 | uri = nil | |
| 45 | tomcat _ar.servic e_properti es.each do |sp| | |
| 46 | if ( sp.key.eql ? PrismeSe rvice::CAR GO_REMOTE_ URL) | |
| 47 | ur i = URI sp .value | |
| 48 | br eak | |
| 49 | end | |
| 50 | end | |
| 51 | host = uri.host | |
| 52 | port = uri.port | |
| 53 | contex ts = [] | |
| 54 | Prisme Utilities. proxy_urls .each do | k| | |
| 55 | uri = URI k['i ncoming_ur l_path'] | |
| 56 | port = uri.por t | |
| 57 | $log .trace(uri .host + " : " + port .to_s) | |
| 58 | cont exts << ur i.path if (host.eql? (uri.host) && port.t o_s.eql?(p ort.to_s)) | |
| 59 | $log .warn("ser ver_config .yaml has a configur ation with no contex t! Prisme cannot us e it for a deploy.") if uri.pa th.eql? '/ ' | |
| 60 | end | |
| 61 | $log.i nfo("conte xts has #{ contexts}" ) | |
| 62 | $log.w arn("I cou ld not fin d a valid proxy conf ig for hos t #{host} with port #{port}. Check pris me's serve r_config.y ml") if co ntexts.emp ty? | |
| 63 | contex ts.map do |e| | |
| 64 | e[0] .eql?('/') ? e.rever se.chop.re verse : e | |
| 65 | end.re ject do |e | | |
| 66 | e.em pty? | |
| 67 | end.re ject do |e | | |
| 68 | e !~ applicati on_type | |
| 69 | end | |
| 70 | end | |
| 71 | ||
| 72 | def self .proxy_map pings | |
| 73 | URI.pr oxy_mappin gs | |
| 74 | end | |
| 75 | ||
| 76 | def self .load_yml_ file(confi g_file, er ror_messag e) | |
| 77 | begin | |
| 78 | if F ile.exists ?(config_f ile) | |
| 79 | ha sh = YAML. load_file( config_fil e) | |
| 80 | else | |
| 81 | $l og.debug(" Config fil e #{config _file} doe s not exis t!!") | |
| 82 | end | |
| 83 | rescue => ex | |
| 84 | $log .error(err or_message ) | |
| 85 | $log .error(ex. backtrace. join("\n") ) | |
| 86 | end | |
| 87 | hash | |
| 88 | end | |
| 89 | ||
| 90 | def self .synch_gro up_data(du mp_data = false) | |
| 91 | persis tent_group _file = "# {$PROPS['P RISME.data _directory ']}/group_ data.yml" | |
| 92 | group_ file = './ config/gro up_data.ym l' | |
| 93 | config _file = Fi le.exists? (persisten t_group_fi le) ? pers istent_gro up_file : group_file | |
| 94 | groups = PrismeU tilities:: load_yml_f ile(config _file, "Gr oup data m ight not h ave been c reated but PRISME wi ll continu e to start .") | |
| 95 | if gro ups.nil? | |
| 96 | $log .info("No group yaml file aval iable for loading!") | |
| 97 | retu rn | |
| 98 | end | |
| 99 | create d_groups = 0 | |
| 100 | skippe d_groups = 0 | |
| 101 | update d_sites = 0 | |
| 102 | group_ ids = grou ps.map do |group| | |
| 103 | grou p['id'] | |
| 104 | end | |
| 105 | db_gro up_ids = V aGroup.all .to_a.map do |e| | |
| 106 | e.id | |
| 107 | end | |
| 108 | groups _to_delete = db_grou p_ids - gr oup_ids | |
| 109 | $log.a lways("Att empting to delete th e followin g groups: #{groups_t o_delete}" ) | |
| 110 | delete d = VaGrou p.where('i d in (?)', groups_to _delete).d estroy_all .length | |
| 111 | $log.a lways("Del eted #{del eted} grou ps.") | |
| 112 | groups .each do | group_hash | | |
| 113 | grou p = VaGrou p.new(grou p_hash) | |
| 114 | if ( VaGroup.ex ists? grou p.id) | |
| 115 | db _group = V aGroup.fin d(group.id ) | |
| 116 | if (db_group .eql? grou p) | |
| 117 | skipped_gr oups += 1 | |
| 118 | $log.debug ("I am ski pping the group from #{config_ file} with group id #{group.id }. It alr eady exist s.") | |
| 119 | el se | |
| 120 | #we need t o update d b_site, an d record t he update | |
| 121 | begin | |
| 122 | db_group .update! n ame: group .name, mem ber_sites: group.mem ber_sites, member_gr oups: grou p.member_g roups | |
| 123 | updated_ sites += 1 | |
| 124 | $log.alw ays("I upd ated the g roup with group id # {db_group. id}") | |
| 125 | rescue => ex | |
| 126 | $log.alw ays("Updat e failed f or the gro up with gr oup id #{d b_group.id }") | |
| 127 | $log.alw ays(ex.mes sage) | |
| 128 | end | |
| 129 | en d | |
| 130 | else | |
| 131 | sa ved = true | |
| 132 | be gin | |
| 133 | group.save ! | |
| 134 | $log.alway s("I saved the group with grou p id #{gro up.id}") | |
| 135 | re scue => ex | |
| 136 | saved = fa lse | |
| 137 | $log.warn( "Save fail ed for #{g roup.inspe ct}") | |
| 138 | $log.warn( ex.message ) | |
| 139 | en d | |
| 140 | cr eated_grou ps += 1 if saved | |
| 141 | sk ipped_grou ps += 1 un less saved | |
| 142 | end | |
| 143 | end | |
| 144 | if dum p_data | |
| 145 | dump _file = Fi le.basenam e(config_f ile, '.*') | |
| 146 | dump _file = "# {$PROPS['P RISME.data _directory ']}/#{dump _file}_#{T ime.now.to _i}.yml" | |
| 147 | File .open(dump _file, 'w' ) do |f| | |
| 148 | f. write(VaGr oup.all.to _a.map do |e| | |
| 149 | {'id' => e .va_site_i d, 'name' => e.name, 'member_s ites' => e .get_site_ ids, 'memb er_groups' => e.get_ group_ids} | |
| 150 | en d.to_yaml) | |
| 151 | end | |
| 152 | end | |
| 153 | r_val = {created _groups: c reated_gro ups, skipp ed_groups: skipped_g roups, upd ated_group s: updated _sites, de leted_grou ps: delete d} | |
| 154 | $log.a lways("Gro up final r esults: #{ r_val}") | |
| 155 | r_val | |
| 156 | end | |
| 157 | ||
| 158 | #todo Wh en the val idators fo r the site model gro w change t o bang(!) methods li ke synch_g roup_data above | |
| 159 | def self .synch_sit e_data(dum p_data = f alse) | |
| 160 | persis tent_site_ file = "#{ $PROPS['PR ISME.data_ directory' ]}/site_da ta.yml" | |
| 161 | site_f ile = './c onfig/site _data.yml' | |
| 162 | config _file = Fi le.exists? (persisten t_site_fil e) ? persi stent_site _file : si te_file | |
| 163 | sites = PrismeUt ilities::l oad_yml_fi le(config_ file, "Sit e data mig ht not hav e been cre ated but P RISME will continue to start." ) | |
| 164 | if sit es.nil? | |
| 165 | $log .info("No site yaml file avali able for l oading!") | |
| 166 | retu rn | |
| 167 | end | |
| 168 | create d_sites = 0 | |
| 169 | skippe d_sites = 0 | |
| 170 | update d_sites = 0 | |
| 171 | site_i ds = sites .map do |s ite| | |
| 172 | site ['va_site_ id'] | |
| 173 | end | |
| 174 | db_sit e_ids = Va Site.all.t o_a.map do |e| | |
| 175 | e.va _site_id | |
| 176 | end | |
| 177 | sites_ to_delete = db_site_ ids - site _ids | |
| 178 | $log.a lways("Att empting to delete th e followin g sites: # {sites_to_ delete}") | |
| 179 | delete d = VaSite .where('va _site_id i n (?)', si tes_to_del ete).destr oy_all.len gth | |
| 180 | $log.a lways("Del eted #{del eted} site s.") | |
| 181 | sites. each do |s ite_hash| | |
| 182 | site = VaSite. new(site_h ash) | |
| 183 | if ( VaSite.exi sts? site. va_site_id ) | |
| 184 | db _site = Va Site.find( site.va_si te_id) | |
| 185 | if (db_site. eql? site) | |
| 186 | skipped_si tes += 1 | |
| 187 | $log.debug ("I am ski pping the site from #{config_f ile} with site id #{ site.va_si te_id}. I t already exists") | |
| 188 | el se | |
| 189 | #we need t o update d b_site, an d record t he update | |
| 190 | updated = db_site.up date site_ hash | |
| 191 | updated_si tes += 1 i f updated | |
| 192 | $log.alway s("I updat ed the sit e with sit e id #{db_ site.va_si te_id}") | |
| 193 | en d | |
| 194 | ||
| 195 | else | |
| 196 | sa ved = site .save | |
| 197 | cr eated_site s += 1 if saved | |
| 198 | sk ipped_site s += 1 unl ess saved | |
| 199 | end | |
| 200 | end | |
| 201 | if dum p_data | |
| 202 | dump _file = Fi le.basenam e(config_f ile, '.*') | |
| 203 | dump _file = "# {$PROPS['P RISME.data _directory ']}/#{dump _file}_#{T ime.now.to _i}.yml" | |
| 204 | File .open(dump _file, 'w' ) do |f| | |
| 205 | f. write(VaSi te.all.to_ a.map do | e| | |
| 206 | {'va_site_ id' => e.v a_site_id, 'name' => e.name, ' site_type' => e.site _type, 'me ssage_type ' => e.mes sage_type} | |
| 207 | en d.to_yaml) | |
| 208 | end | |
| 209 | end | |
| 210 | r_val = {created _sites: cr eated_site s, skipped _sites: sk ipped_site s, updated _sites: up dated_site s, deleted _sites: de leted} | |
| 211 | $log.a lways("Sit e final re sults: #{r _val}") | |
| 212 | r_val | |
| 213 | end | |
| 214 | ||
| 215 | def self .prisme_su per_user | |
| 216 | users = load_yml _file("#{$ PROPS['PRI SME.data_d irectory'] }/prisme_s uper_user. yml", "Adm inistrativ e Users ha ve not bee n created but PRISME will cont inue to st art.") | |
| 217 | return if users. nil? | |
| 218 | begin | |
| 219 | # if the confi g file doe s not exis t then fal lback to t he file in the confi g director y. | |
| 220 | user s['users'] .each do | u| | |
| 221 | su = User.fi nd_or_crea te_by(emai l: u['emai l'], admin _role_chec k: true) | |
| 222 | su .password = u['passw ord'] | |
| 223 | su .save! | |
| 224 | su .add_role( Roles::SUP ER_USER) | |
| 225 | end | |
| 226 | rescue => ex | |
| 227 | $log .error("No t all admi nistrative Users hav e been cre ated but P RISME will continue to start. The except ion is #{e x}") | |
| 228 | $log .error(ex. backtrace. join("\n") ) | |
| 229 | end | |
| 230 | end | |
| 231 | ||
| 232 | def self .applicati on_urls | |
| 233 | server _config['a pplication _urls'] | |
| 234 | end | |
| 235 | ||
| 236 | def self .get_proxy | |
| 237 | server _config['p roxy_confi g_root'][' apache_url _proxy'] | |
| 238 | end | |
| 239 | ||
| 240 | def self .proxy_url s | |
| 241 | server _config['p roxy_confi g_root'][' proxy_urls '] | |
| 242 | end | |
| 243 | ||
| 244 | def self .aitc_envi ronment | |
| 245 | return (HashWith Indifferen tAccess.ne w PrismeUt ilities.ai tc_env).de ep_dup unl ess Prisme Utilities. aitc_env.n il? | |
| 246 | Prisme Utilities. aitc_env = PrismeUti lities.fet ch_yml 'ai tc_environ ment.yml' | |
| 247 | (HashW ithIndiffe rentAccess .new Prism eUtilities .aitc_env) .deep_dup | |
| 248 | end | |
| 249 | ||
| 250 | def self .aitc_prod uction? | |
| 251 | PRISME _ENVIRONME NT.eql? 'P ROD' | |
| 252 | end | |
| 253 | ||
| 254 | def self .real_vuid s? | |
| 255 | return false if $database. eql?(Rails Prisme::H2 ) #H2 is n ever real | |
| 256 | boolea n PrismeUt ilities.fe tch_vuid_c onfig[:rea l_vuids] | |
| 257 | end | |
| 258 | ||
| 259 | def self .fetch_vui d_config | |
| 260 | return (HashWith Indifferen tAccess.ne w PrismeUt ilities.vu id_config) .deep_dup unless Pri smeUtiliti es.vuid_co nfig.nil? | |
| 261 | Prisme Utilities. vuid_confi g = Prisme Utilities. fetch_yml 'VUID_conf ig.yml' | |
| 262 | (HashW ithIndiffe rentAccess .new Prism eUtilities .vuid_conf ig).deep_d up | |
| 263 | end | |
| 264 | ||
| 265 | def self .server_co nfig | |
| 266 | return (HashWith Indifferen tAccess.ne w PrismeUt ilities.co nfig).deep _dup unles s PrismeUt ilities.co nfig.nil? | |
| 267 | Prisme Utilities. config = P rismeUtili ties.fetch _yml 'serv er_config. yml' | |
| 268 | (HashW ithIndiffe rentAccess .new Prism eUtilities .config).d eep_dup | |
| 269 | end | |
| 270 | ||
| 271 | def self .ssoi_logo ut_path | |
| 272 | logout _url = Pri smeUtiliti es.ssoi_lo gout_url | |
| 273 | return logout_ur l unless l ogout_url. nil? | |
| 274 | config _file = Pr ismeUtilit ies.server _config | |
| 275 | return nil unles s config_f ile | |
| 276 | logout _url = con fig_file[A PPLICATION _URLS][SSO I_LOGOUT] | |
| 277 | Prisme Utilities. ssoi_logou t_url = UR I.valid_ur l?(url_str ing: logou t_url) ? l ogout_url : nil | |
| 278 | end | |
| 279 | ||
| 280 | #Tells i f the give n uri is u p. If bas ic authent ication is required and not pr ovided fal se will be returned. | |
| 281 | #The met hod is lig ht weight and only f etches the headers o f a given url. | |
| 282 | #Sample invocation : | |
| 283 | #PrismeU tilities.u ri_up?(uri : 'http:// www.google .com') | |
| 284 | #PrismeUti lities.uri _up?(uri: "https:/ URL . DNS : PORT gov/vha/is aac/db/vet s/1.0/vets -1.0-all.c radle.zip" , user: 'u ser', pass word: 'pas sword') | |
| 285 | def self .uri_up?(u ri:, user: nil, pass word: nil) | |
| 286 | if uri .is_a? Str ing | |
| 287 | uri = URI uri | |
| 288 | else | |
| 289 | rais e "URI mus t be a a S tring URI or a URI o bject" unl ess uri.is _a? URI | |
| 290 | end | |
| 291 | result = false | |
| 292 | begin | |
| 293 | path = uri.pat h.empty? ? '/' : uri .path | |
| 294 | resu lt = Net:: HTTP.new(u ri.host, u ri.port) | |
| 295 | resu lt.use_ssl = uri.sch eme.eql?(' https') | |
| 296 | head = Net::HT TP::Head.n ew(path, n il) | |
| 297 | head .basic_aut h(user, pa ssword) if user | |
| 298 | head ers = resu lt.request head | |
| 299 | resu lt = heade rs.kind_of ?(Net::HTT PSuccess) | |
| 300 | # Net::HTTP. new(u, p). head('/'). kind_of? N et::HTTPOK | |
| 301 | rescue => ex | |
| 302 | $log .warn("I c ould not c heck the U RL #{uri.p ath} at po rt #{uri.p ort} again st path #{ uri.path} because #{ ex}.") | |
| 303 | resu lt = false | |
| 304 | end | |
| 305 | result | |
| 306 | end | |
| 307 | ||
| 308 | def self .fetch_yml (base_file ) | |
| 309 | yml_fi le = Prism eUtilities .which_fil e(base_fil e) | |
| 310 | if yml _file.nil? | |
| 311 | $log .warn("No yml file f ound! #{ba se_file}") | |
| 312 | retu rn nil | |
| 313 | end | |
| 314 | # read the yml f ile | |
| 315 | begin | |
| 316 | retu rn HashWit hIndiffere ntAccess.n ew YAML.lo ad(ERB.new (File.read (yml_file) ).result) | |
| 317 | rescue => ex | |
| 318 | $log .error("Er ror readin g #{yml_fi le}, error is #{ex}" ) | |
| 319 | $log .error(ex. backtrace. join("\n") ) | |
| 320 | end | |
| 321 | nil | |
| 322 | end | |
| 323 | ||
| 324 | #given a base file selects t he prismeD ata varian t over the config va riant | |
| 325 | def self .which_fil e(base_fil e) | |
| 326 | # defa ult the co nfig file location b ased on th e data_dir ectory pro perty | |
| 327 | file = "#{$PROPS ['PRISME.d ata_direct ory']}/#{b ase_file}" | |
| 328 | ||
| 329 | # if t he config file does not exist then fallb ack to the file in t he config directory. | |
| 330 | unless File.exis ts?(file) | |
| 331 | file = "./conf ig/#{base_ file}" | |
| 332 | end | |
| 333 | unless File.exis ts? file | |
| 334 | $log .warn("#{b ase_file} not found. ") | |
| 335 | retu rn nil | |
| 336 | end | |
| 337 | file | |
| 338 | end | |
| 339 | ||
| 340 | ## | |
| 341 | # writes the json data to a file | |
| 342 | # @param json - th e JSON dat a to write out | |
| 343 | # @param file_name - the fil ename to w rite out t o the /tmp directory | |
| 344 | def self .json_to_y aml_file(j son, file_ name) | |
| 345 | prefix = '#Fixtu re created on ' + Ti me.now.str ftime('%F %H:%M:%S') + "\n" | |
| 346 | File.w rite("#{fi le_name}", prefix + json.to_ya ml) | |
| 347 | $log.t race{"Writ ing yaml f ile #{file _name}"} | |
| 348 | end | |
| 349 | ||
| 350 | #only wo rks post i nit. | |
| 351 | def self .write_vui d_db | |
| 352 | json = Rails.con figuration .database_ configurat ion[Rails. env] | |
| 353 | json.m erge! Pris meUtilitie s.fetch_vu id_config | |
| 354 | json.m erge!({'ep och_time_s econds' => Time.now. to_i}) | |
| 355 | json.m erge!({'ep och_time_s econds' => Time.now. to_i}) | |
| 356 | json.m erge!({'lo g_events_u rl' => Pr ismeUtilit ies::Route Helper.rou te(:log_ev ent_url, s ecurity_to ken: Token Support.in stance.gen erate_secu rity_token )}) | |
| 357 | begin | |
| 358 | json _to_yaml_f ile(json, VUID_DB_FI LE) | |
| 359 | rescue => ex | |
| 360 | $log .error("Th e file #{V UID_DB_FIL E} was not written. This will impact th e vuid ser ver!") | |
| 361 | $log .error(ex. to_s) | |
| 362 | $log .error(ex. backtrace. join("\n") ) | |
| 363 | end | |
| 364 | end | |
| 365 | ||
| 366 | def self .remove_vu id_db | |
| 367 | begin | |
| 368 | File .unlink VU ID_DB_FILE | |
| 369 | rescue => ex | |
| 370 | $log .error("I could not delete the file #{VU ID_DB_FILE }, reason is #{ex}") | |
| 371 | end | |
| 372 | end | |
| 373 | ||
| 374 | end | |
| 375 | ||
| 376 | module URI | |
| 377 | # these keys are u sed in the server_co nfig yaml file to ma p incoming urls to t heir corre sponding p roxy locat ion | |
| 378 | PROXY_CO NFIG_ROOT = 'proxy_c onfig_root ' | |
| 379 | APACHE_U RL_PROXY = 'apache_u rl_proxy' | |
| 380 | PROXY_UR LS = 'prox y_urls' | |
| 381 | INCOMING _URL_PATH = 'incomin g_url_path ' | |
| 382 | PROXY_LO CATION = ' proxy_loca tion' | |
| 383 | ||
| 384 | class << self | |
| 385 | attr_a ccessor :p roxy_mappi ngs | |
| 386 | end | |
| 387 | ||
| 388 | #** | |
| 389 | # this m ethod take s the modi fied match ing url pa ths and re turns the proxy path | |
| 390 | def prox ify | |
| 391 | # prox y_mappings are loade d one time once all of the url s listed a re valid. | |
| 392 | if URI .proxy_map pings.nil? | |
| 393 | conf ig_file = PrismeUtil ities.serv er_config | |
| 394 | retu rn self un less confi g_file | |
| 395 | ||
| 396 | # re ad the con fig yml fi le and pul l out the proxy_root propertie s | |
| 397 | $log .debug('in itializing the proxy mappings to:') | |
| 398 | URI. proxy_mapp ings = con fig_file[P ROXY_CONFI G_ROOT] | |
| 399 | $log .debug("PR OXY MAPPIN GS ARE: #{ URI.proxy_ mappings.i nspect}") | |
| 400 | ||
| 401 | # pu ll out the apache ho st url and validate it | |
| 402 | apac he_host = URI.proxy_ mappings[A PACHE_URL_ PROXY] | |
| 403 | $log .debug("ap ache host is #{apach e_host}") | |
| 404 | vali d_urls = U RI.valid_u rl?(url_st ring: apac he_host) | |
| 405 | ||
| 406 | # pu ll out all of the in coming url s and vali date them | |
| 407 | URI. proxy_mapp ings[PROXY _URLS].eac h do |url_ hash| | |
| 408 | ur l = url_ha sh[INCOMIN G_URL_PATH ] | |
| 409 | va lid_urls = valid_url s & URI.va lid_url?(u rl_string: url) # & will not s hort circu it | |
| 410 | end | |
| 411 | ||
| 412 | # ba il out if we have an y invalid urls confi gured | |
| 413 | unle ss valid_u rls | |
| 414 | UR I.proxy_ma ppings = n il | |
| 415 | re turn self | |
| 416 | end | |
| 417 | ||
| 418 | # so rt the url s from lon gest to sh ortest for proxifyi ng | |
| 419 | URI. proxy_mapp ings[PROXY _URLS].sor t! do |a, b| | |
| 420 | b[ INCOMING_U RL_PATH].l ength <=> a[INCOMING _URL_PATH] .length | |
| 421 | end | |
| 422 | ||
| 423 | # fr eeze the c onfigurati on | |
| 424 | URI. proxy_mapp ings.freez e | |
| 425 | $log .debug(URI .proxy_map pings.insp ect) | |
| 426 | end | |
| 427 | ||
| 428 | # ensu re that th e apache u rl has lea ding and t railing sl ashes | |
| 429 | apache _path = UR I.proxy_ma ppings[APA CHE_URL_PR OXY].clone | |
| 430 | apache _path << ' /' unless apache_pat h.last.eql ? '/' | |
| 431 | ||
| 432 | # iter ate the in coming url s for a ma tch that n eeds to be proxified | |
| 433 | URI.pr oxy_mappin gs[PROXY_U RLS].each do |url_ha sh| | |
| 434 | inco ming_url = url_hash[ INCOMING_U RL_PATH] | |
| 435 | loca tion = url _hash[PROX Y_LOCATION ] | |
| 436 | clon e = self.c lone | |
| 437 | ||
| 438 | if ( clone.to_s .starts_wi th?(incomi ng_url) || (clone.to _s + '/'). starts_wit h?(incomin g_url)) | |
| 439 | #w e found ou r match!! | |
| 440 | ap ache_uri = URI(apach e_path) | |
| 441 | cl one.path < < '/' unle ss clone.p ath.last.e ql? '/' | |
| 442 | ||
| 443 | # get the in coming url as a URI, check for matting an d pull out the conte xt | |
| 444 | ma tched_uri = URI(inco ming_url) | |
| 445 | ma tched_uri. path << '/ ' if match ed_uri.pat h.empty? | |
| 446 | ma tched_uri. path << '/ ' unless m atched_uri .path.last .eql? '/' | |
| 447 | co ntext = ma tched_uri. path | |
| 448 | ||
| 449 | # if we have a locatio n that con tains a co ntext then ensure le ading and trailing s lashes | |
| 450 | un less locat ion.eql? ' /' | |
| 451 | location = '/' + loc ation unle ss locatio n.first.eq l? '/' | |
| 452 | location < < '/' unle ss locatio n.last.eql ? '/' | |
| 453 | en d | |
| 454 | ||
| 455 | # operating off of the clone sub stitute th e context with the l ocation an d ensure t hat the | |
| 456 | # scheme, po rt, and ho st for the clone lin es up with the proxy configura tion for a pache | |
| 457 | cl one.path.s ub!(contex t, locatio n) | |
| 458 | cl one.scheme = apache_ uri.scheme | |
| 459 | cl one.port = apache_ur i.port | |
| 460 | cl one.host = apache_ur i.host | |
| 461 | re turn clone | |
| 462 | end | |
| 463 | end | |
| 464 | $log.w arn("No pr oxy mappin g found fo r #{self}, returning self.") | |
| 465 | self | |
| 466 | end | |
| 467 | end | |
| 468 | #load('./l ib/utiliti es/prisme_ utilities. rb') | |
| 469 | # PrismeUt ilities.ai tc_environ ment | |
| 470 | # URI('htt ps://cris. com').prox ify | |
| 471 | # URI.prox y_mappings = nil | |
| 472 | ||
| 473 | #works: | |
| 474 | # URI('htt ps://DNS :PORT | |
| 475 | ||
| 476 | #irb(main) :011:0> UR I('https:/ / DNS : PORT /').proxif y | |
| 477 | #=> #<URI: :HTTPS htt ps:// DNS /> | |
| 478 | # irb(main): 012:0> URI ('https:// DNS : PORT ').proxify |
Araxis Merge (but not the data content of this report) is Copyright © 1993-2016 Araxis Ltd (www.araxis.com). All rights reserved.