Class: Shibkit::MetaMeta

Inherits:
Object
  • Object
show all
Defined in:
lib/shibkit/meta_meta.rb,
lib/shibkit/meta_meta/sp.rb,
lib/shibkit/meta_meta/idp.rb,
lib/shibkit/meta_meta/logo.rb,
lib/shibkit/meta_meta/config.rb,
lib/shibkit/meta_meta/entity.rb,
lib/shibkit/meta_meta/source.rb,
lib/shibkit/meta_meta/contact.rb,
lib/shibkit/meta_meta/service.rb,
lib/shibkit/meta_meta/provider.rb,
lib/shibkit/meta_meta/attribute.rb,
lib/shibkit/meta_meta/federation.rb,
lib/shibkit/meta_meta/organisation.rb,
lib/shibkit/meta_meta/metadata_item.rb,
lib/shibkit/meta_meta/provisioning/base.rb,
lib/shibkit/meta_meta/mixin/xpath_chores.rb,
lib/shibkit/meta_meta/requested_attribute.rb,
lib/shibkit/meta_meta/mixin/cached_downloads.rb

Overview

Simple library to parse Shibboleth metadata files into Ruby objects

Defined Under Namespace

Modules: Mixin, Provisioning Classes: Attribute, Config, Contact, Entity, Federation, IDP, Logo, MetadataItem, Organisation, Provider, RequestedAttribute, SP, Service, Source

Class Method Summary collapse

Class Method Details

.add_source(data) ⇒ Object

Convenience method to add a source from a hash or object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/shibkit/meta_meta.rb', line 97

def self.add_source(data)
  
  @additional_sources ||= Hash.new
  
  case data
  when Hash
    source = Source.from_hash(data)
  when ::Shibkit::MetaMeta::Source
    source = data
  else 
    raise "Expected either hash or Source object!"
  end

  log.info "Added a source for #{source.uri}"
  
  @additional_sources[source.uri] = source
  
end

.config(&block) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/shibkit/meta_meta.rb', line 41

def self.config(&block)

  if block
    return ::Shibkit::MetaMeta::Config.instance.configure(&block)
  else
    return ::Shibkit::MetaMeta::Config.instance
  end

end

.delete_all_cached_files!Object

Delete all cache files



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/shibkit/meta_meta.rb', line 78

def self.delete_all_cached_files!
  
  dir = config.cache_root
  
  if config.can_delete?
    
    log.info "Deleting all files at #{dir}..."
    FileUtils.rm_rf   dir
    FileUtils.mkdir_p dir
    
  else
    
    log.warn "Cannot delete files at #{dir} - check config settings."
    
  end
  
end

.entitiesObject

All primary entities from all federations



346
347
348
349
350
351
352
353
354
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
395
# File 'lib/shibkit/meta_meta.rb', line 346

def self.entities
  
  return [] if @entities.nil? and ! config.autoload?
  
  ## Populate memoised array of entities if it's empty
  unless @entities and @entities.size > 0
    
    ## Array for memoising primary entities
    @entities ||= Array.new
    
    ## For keeping track of already processed entities & marking them as primary
    processed = Hash.new 
    
    self.federations.each do |f|
      
      f.entities.each do |e|
        
        ## If we've already found the primary version of the entity
        if processed[e.uri]
          
          ## Add this federation's URI to the primary
          primary = processed[e.uri]              
          primary.other_federation_uris << f.uri
          
          ## Add tags from the non-primary to the primary
          primary.tags << e.tags if config.merge_primary_tags?
          
          next
       
        end
        
        ## Mark this entity as the primary and remember it as already processed.
        e.primary = true
        processed[e.uri] = e
        
        ## Collect entity
        @entities << e 

      end
      
    end

    ## BODGE: Needs a better fix. Issue 14
    #@entities = @entities.compact
    
  end
  
  return @entities
  
end

.federationsObject

Return list of Federations objects (filtered if select_federations is set)



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/shibkit/meta_meta.rb', line 329

def self.federations
  
  return [] if @federations.nil? and ! config.autoload?
  
  self.stockup
   
  if self.filtered_sources?
    
    return @federations.select { |f| self.selected_federation_uris.include? f.uri }
  
  end

  return @federations
  
end

.filtered_sources?Boolean

Has a limited subset of federations/sources been selected?

Returns:

  • (Boolean)


186
187
188
189
190
# File 'lib/shibkit/meta_meta.rb', line 186

def self.filtered_sources?
  
  return self.selected_federation_uris.empty? ? false : true 
  
end

.flushObject

Clear all loaded entity & federation data



66
67
68
69
70
71
72
73
74
75
# File 'lib/shibkit/meta_meta.rb', line 66

def self.flush
  
  log.info "Flushing all loaded objects"
  
  @orgs        = Array.new
  @entities    = Array.new
  @federations = Array.new
  @by_uri      = Hash.new
  
end

.from_uri(uri) ⇒ Object



441
442
443
444
445
446
447
448
449
450
451
452
453
454
# File 'lib/shibkit/meta_meta.rb', line 441

def self.from_uri(uri)
  
  unless @by_uri and @by_uri.size > 0
    
    @by_uri ||= Hash.new
    
    self.federations.each { |f| @by_uri[f.uri] = f unless @by_uri[f.uri] }
    self.entities.each    { |e| @by_uri[e.uri] = e unless @by_uri[e.uri] }
      
  end
  
  return @by_uri[uri]
  
end

.idpsObject



428
429
430
431
432
# File 'lib/shibkit/meta_meta.rb', line 428

def self.idps
  
  return entities.select { |e| e.idp? }
  
end

.load_cache_file(file_or_url, format = :yaml) ⇒ Object

Loads federation metadata contents



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/shibkit/meta_meta.rb', line 193

def self.load_cache_file(file_or_url, format=:yaml)
    
    self.reset
    
    log.info "Loading object cache file from #{file_or_url} as #{format} data..."
    
    @federations = case format
    when :yaml
      YAML.load(File.open(file_or_url))
    when :marshal
      Marshal.load(File.open(file_or_url))
    else
      raise "Unexpected cache file format requested! Please use :yaml or :marshal"
    end
    
    self.entities      
    
    log.info "Processing complete."
    
    return true
    
end

.load_sources(filename = self.config.sources_file) ⇒ Object

Load sources from a YAML file



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/shibkit/meta_meta.rb', line 124

def self.load_sources(filename=self.config.sources_file)
  
  log.info "Loading sources from disk..."
  
  @loaded_sources = Hash.new
  
  Source.load(filename).each do |source|
    
    ## More than one definition for a source is a problem 
    raise "Duplicate source for #{source.uri}!" if @loaded_sources[source.uri]
    
    @loaded_sources[source.uri] = source
    
  end 
  
end

.loaded_sources?Boolean

Have we loaded any sources?

Returns:

  • (Boolean)


117
118
119
120
121
# File 'lib/shibkit/meta_meta.rb', line 117

def self.loaded_sources?
  
  return @loaded_sources ? true : false
  
end

.orgsObject



397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# File 'lib/shibkit/meta_meta.rb', line 397

def self.orgs
  
  unless @orgs and @orgs.size > 0
    
    @orgs ||= Array.new
    
    processed = Hash.new
    
    self.entities.each do |e|
       
       org = e.organisation
       
       next unless org
       next if processed[org.druid]
       
       @orgs << org
       
      
       processed[org.druid] = true
       
    end
    
    @orgs.sort! {|a,b| a.druid <=> b.druid }
    
  end
  
  return @orgs
  
end

.process_sourcesObject

Parses sources and returns an array of all federation object



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
289
290
# File 'lib/shibkit/meta_meta.rb', line 248

def self.process_sources
  
  if config.smartcache_active?
    return if self.smartcache_load
  end
  
  log.info "Processing content of sources into objects..."
  
  raise "MetaMeta sources are not an Array! (Should not be a #{self.sources.class})" unless
    self.sources.kind_of? Array
  
  self.flush
  
  self.sources.each do |source|
    
    if self.filtered_sources?
      
      next unless self.selected_federation_uris.include? source.uri
    
    end
    
    start_time = Time.new
      
    federation = source.to_federation
    
    ## Store all federations in array
    @federations << federation
    
    log.info "Loaded #{federation.entities.count} entities from #{federation} metadata file in #{Time.new - start_time} seconds."
    
    
  end
  
  ## Bodge to make sure primary ents are set, multifederation calculated, etc
  #self.entities # Issue 14
  
  log.info "Processing complete. #{@federations.count} sets of metadata have been loaded."
  
  self.smartcache_save if config.smartcache_active?
  
  return @federations
     
end

.purge_xml!Object



462
463
464
465
466
# File 'lib/shibkit/meta_meta.rb', line 462

def self.purge_xml! 
  
  @federations.each { |f| f.purge_xml(true) }
  
end

.refresh(force = false) ⇒ Object

Downloads and reprocesses metadata files



293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/shibkit/meta_meta.rb', line 293

def self.refresh(force=false)
  
  log.info "Refreshing all selected federations"
  
  ## Reload source lists overwriting previous set
  self.load_sources
  
  ## Reprocess sources to create fresh set of federation and entity objects
  self.process_sources 
  
  return true
  
end

.resetObject

Flush out all available sources, metadata caches, etc.



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/shibkit/meta_meta.rb', line 52

def self.reset
  
  log.info "Resetting all sources, metadata caches, etc."
  
  ## Clear the source data
  @additional_sources   = Hash.new
  @loaded_sources       = Hash.new
   
  ## Clear federation entity data
  self.flush
  
end

.save_cache_file(file_path, format = :yaml) ⇒ Object

 Save entity data into a YAML file.



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
# File 'lib/shibkit/meta_meta.rb', line 217

def self.save_cache_file(file_path, format=:yaml)
  
  log.info "Saving object cache file to #{file_path} as #{format} data..."
  
  ## Will *not* overwrite the example/default file in gem! TODO: this code is awful.
  gem_data_path = "#{::File.dirname(__FILE__)}/data"
  if file_path.include? gem_data_path 
    raise "Attempt to overwrite gem's default metadata cache! Please specify your own file to save cache in"
  end
  
  self.textify_xml!
  
  ## Write the YAML to disk
  File.open(file_path, 'w') do |out|
    
    case format
    when :yaml
      YAML.dump(@federations, out)
    when :marshal        
      Marshal.dump(@federations, out)
    else
      raise "Unexpected cache file format requested! Please use :yaml or :marshal"
    end
    
  end
    
  return true
    
end

.save_sources(filename) ⇒ Object

Save all known sources to sources list file



142
143
144
145
146
147
148
149
150
151
# File 'lib/shibkit/meta_meta.rb', line 142

def self.save_sources(filename)
  
  log.info "Saving sources to #{filename}..."
  
  src_dump = Hash.new
  self.sources.each { |s| src_dump[s.uri] = s.to_hash }
  
  File.open(filename, 'w') { |out| YAML.dump(src_dump, out) }
    
end

.selected_federation_urisObject

List of federation/collection uris



179
180
181
182
183
# File 'lib/shibkit/meta_meta.rb', line 179

def self.selected_federation_uris
  
  return self.config.selected_federation_uris
  
end

.sourcesObject

List all sources as an array



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/shibkit/meta_meta.rb', line 154

def self.sources
  
  if self.config.autoload? and loaded_sources.size == 0 and additional_sources.size == 0
    
    self.load_sources
  
  end
  
  all_sources_indexed = loaded_sources.merge(additional_sources)

  sources = all_sources_indexed.values

  sources = sources.sort {|a,b| a.created_at <=> b.created_at }

  if self.filtered_sources?
    
    sources = sources.select { |s| self.selected_federation_uris.include? s.uri }
  
  end

  return sources 
  
end

.spsObject



435
436
437
438
439
# File 'lib/shibkit/meta_meta.rb', line 435

def self.sps
  
  return entities.select { |e| e.sp? }
  
end

.stocked?Boolean

Have objects been loaded from metadata?

Returns:

  • (Boolean)


319
320
321
322
323
324
325
326
# File 'lib/shibkit/meta_meta.rb', line 319

def self.stocked?
  
  return false unless @federations
  return false if @federations.empty? 
  
  return true

end

.stockup(force = false) ⇒ Object



307
308
309
310
311
312
313
314
315
316
# File 'lib/shibkit/meta_meta.rb', line 307

def self.stockup(force=false)
  
  if self.config.autoload?
  
    self.process_sources unless @federations
    self.process_sources if @federations.empty?
    
  end
  
end

.textify_xml!Object



456
457
458
459
460
# File 'lib/shibkit/meta_meta.rb', line 456

def self.textify_xml! 
  
  @federations.each { |f| f.textify_xml(true) }
  
end