Class: Shibkit::MetaMeta::Source

Inherits:
Object
  • Object
show all
Includes:
Mixin::CachedDownloads
Defined in:
lib/shibkit/meta_meta/source.rb

Constant Summary collapse

REAL_SOURCES_FILE =

Location of default real sources list (contains real-world federation details) # REMOVE

"#{::File.dirname(__FILE__)}/data/real_sources.yml"
DEV_SOURCES_FILE =

Location of default mock sources list (contains small fictional federations) # REMOVE

"#{::File.dirname(__FILE__)}/data/dev_sources.yml"
PERCENTAGE_PATTERN =
/(\d+)\s*%/
NAMESPACES =

Additional namespaces that Nokogiri needs to know about # TODO Move to Config?

{
  'ukfedlabel' => 'http://ukfederation.org.uk/2006/11/label',
  'elab'       => 'http://eduserv.org.uk/labels',
  'wayf'       => 'http://sdss.ac.uk/2006/06/WAYF',
  'mdui'       => 'urn:oasis:names:tc:SAML:metadata:ui',
  'saml'       => 'urn:oasis:names:tc:SAML:2.0:assertion',
  'shibmd'     => 'urn:mace:shibboleth:metadata:1.0'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixin::CachedDownloads

#fetch_local, #fetch_remote, included

Constructor Details

#initialize(&block) ⇒ Source

New Source object with default values



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/shibkit/meta_meta/source.rb', line 133

def initialize(&block)
  
  @name_uri   = ""
  @created_at = Time.new
  @name       = "Unnown"
  @refresh_delay = 86400
  @display_name = "Unknown"
  @type      = "federation"
  @countries = []
  @metadata_source = nil
  @certificate_source = nil
  @fingerprint = nil
  @refeds_info = nil
  @homepage  = nil
  @languages = []
  @support_email = nil
  @description = ""
  @certificate_tmpfile = nil
  @metadata_tmpfile    = nil
  @active = true
  @trustiness = 1
  @groups = []
  @tags   = []
  
  self.instance_eval(&block) if block
  
end

Instance Attribute Details

#activeObject Also known as: active?

Returns the value of attribute active.



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

def active
  @active
end

#certificate_sourceString

Returns URL or filesystem path of the metadata certificate to be used.

Returns:

  • (String)

    URL or filesystem path of the metadata certificate to be used



86
87
88
# File 'lib/shibkit/meta_meta/source.rb', line 86

def certificate_source
  @certificate_source
end

#countriesArray

Returns country codes for areas served by the federation.

Returns:

  • (Array)

    country codes for areas served by the federation



79
80
81
# File 'lib/shibkit/meta_meta/source.rb', line 79

def countries
  @countries
end

#created_atObject (readonly)

Returns the value of attribute created_at.



122
123
124
# File 'lib/shibkit/meta_meta/source.rb', line 122

def created_at
  @created_at
end

#descriptionString

Returns Brief description of the federation or collection.

Returns:

  • (String)

    Brief description of the federation or collection



106
107
108
# File 'lib/shibkit/meta_meta/source.rb', line 106

def description
  @description
end

#display_nameString

Returns the common, friendler name of the federation or collection.

Returns:

  • (String)

    the common, friendler name of the federation or collection



69
70
71
# File 'lib/shibkit/meta_meta/source.rb', line 69

def display_name
  @display_name
end

#fetched_atString (readonly)

Note:

This is not persistent between uses of this class

Returns Time the metadata for this federation was last fetched.

Returns:

  • (String)

    Time the metadata for this federation was last fetched



110
111
112
# File 'lib/shibkit/meta_meta/source.rb', line 110

def fetched_at
  @fetched_at
end

#fingerprintString?

Returns Fingerprint of the federation certificate.

Returns:

  • (String, nil)

    Fingerprint of the federation certificate



89
90
91
# File 'lib/shibkit/meta_meta/source.rb', line 89

def fingerprint
  @fingerprint
end

#homepageString Also known as: homepage_url

Returns URL of the federation or collection’s home page.

Returns:

  • (String)

    URL of the federation or collection’s home page



96
97
98
# File 'lib/shibkit/meta_meta/source.rb', line 96

def homepage
  @homepage
end

#languagesArray

Returns Array of languages supported by the federation or collection.

Returns:

  • (Array)

    Array of languages supported by the federation or collection



100
101
102
# File 'lib/shibkit/meta_meta/source.rb', line 100

def languages
  @languages
end

#messagesString (readonly)

Deprecated.

Not actually used at present, not sure if this is needed…

Returns Message returned during processing.

Returns:

  • (String)

    Message returned during processing



114
115
116
# File 'lib/shibkit/meta_meta/source.rb', line 114

def messages
  @messages
end

#metadata_sourceString Also known as: url

Returns URL or filesystem path of the metadata file to be used.

Returns:

  • (String)

    URL or filesystem path of the metadata file to be used



82
83
84
# File 'lib/shibkit/meta_meta/source.rb', line 82

def 
  @metadata_source
end

#nameString

Returns the full name of the federation or collection.

Returns:

  • (String)

    the full name of the federation or collection



66
67
68
# File 'lib/shibkit/meta_meta/source.rb', line 66

def name
  @name
end

#name_uriString Also known as: uri

Returns the URI identifier for the federation or collection.

Returns:

  • (String)

    the URI identifier for the federation or collection



62
63
64
# File 'lib/shibkit/meta_meta/source.rb', line 62

def name_uri
  @name_uri
end

#refeds_urlString? Also known as: refeds_info

Returns URL of the federation’s Refeds wiki entry.

Returns:

  • (String, nil)

    URL of the federation’s Refeds wiki entry



92
93
94
# File 'lib/shibkit/meta_meta/source.rb', line 92

def refeds_url
  @refeds_url
end

#refresh_delayFixednum

Returns the recommended refresh period for the federation, in seconds.

Returns:

  • (Fixednum)

    the recommended refresh period for the federation, in seconds



76
77
78
# File 'lib/shibkit/meta_meta/source.rb', line 76

def refresh_delay
  @refresh_delay
end

#statusString (readonly)

Returns Status of the source: indicates success of last operation.

Returns:

  • (String)

    Status of the source: indicates success of last operation



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

def status
  @status
end

#support_emailString

Returns Main contact email address for the federation.

Returns:

  • (String)

    Main contact email address for the federation



103
104
105
# File 'lib/shibkit/meta_meta/source.rb', line 103

def support_email
  @support_email
end

#typeString

Returns :federation for proper federations, :collection for simple collections of entities.

Returns:

  • (String)

    :federation for proper federations, :collection for simple collections of entities.



73
74
75
# File 'lib/shibkit/meta_meta/source.rb', line 73

def type
  @type
end

Class Method Details

.from_hash(data, uri = nil) ⇒ Object

Create a new source from a hash



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

def self.from_hash(data, uri=nil)

  raise "#{data.class} is not a hash" if not data.instance_of? Hash

  data = data.inject({}){|m,(k,v)| m[k.to_sym] = v; m}
  
  new_source = self.new do |source|
    
    source.name_uri           = data[:uri]  || uri
    source.name               = data[:name] || uri
    source.refresh_delay      = data[:refresh].to_i || 86400
    source.display_name       = data[:display_name] || data['name'] || uri
    source.type               = data[:type].to_sym  || :collection

    source.    = data[:metadata]
    source.certificate_source = data[:certificate]
    source.fingerprint        = data[:fingerprint]
    source.refeds_url         = data[:refeds_info]
    source.homepage           = data[:homepage]

    source.support_email      = data[:support_email] || nil
    source.description        = data[:description]   || ""
    source.trustiness         = data[:trustiness].to_f || 1
    
    source.languages = data[:languages].inject([]){|m,v| m << v.to_s.downcase.to_sym } || [:en]
    source.countries = data[:countries].inject([]){|m,v| m << v.to_s.downcase.to_sym } || []
    source.groups    = data[:groups] || []
    source.tags      = data[:tags]   || []
    
  end
  
  return new_source
  
end

.load(source_list = :auto, *selected_groups) ⇒ Array

Load a metadata source list from a YAML file

Parameters:

  • source_list (String) (defaults to: :auto)

    Filesystem path of a sources YAML file or :real for included list of real sources, :dev for mock sources, or :auto for either :real or :dev, based on environment

Returns:

  • (Array)

    Array of metadata source objects



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

def self.load(source_list=:auto, *selected_groups)
  
  selected_groups = selected_groups.empty? ? ::Shibkit::MetaMeta.config.selected_groups : []
  
  Shibkit::MetaMeta.log.debug "Load sources from #{source_list.to_s}"
  
  file = self.locate_sources_file(source_list)
  
  sources = Array.new
  source_data = YAML::load(File.open(file))
  Shibkit::MetaMeta.log.debug "Source YAML:\n#{source_data.inspect}"
  
  ## Load records from the YAML-derived hash rather than directly to process them first
  source_data.each_pair do |id, data|
    
    case data
    when Source
      sources << data
    when Hash
      sources << Source.from_hash(data,id)
    else
      raise "Don't know how to convert #{source_data.class} to Source"
    end
  end

  ## If groups are specified then trim off any non-matching sources
  unless selected_groups.empty? or selected_groups.include? :all
    
    Shibkit::MetaMeta.log.info "Filtering source/federations by selected groups"          
    
    selected_groups.inject([]){|m,v| m << v.to_s.downcase.to_sym }
    
    group_set = Set.new selected_groups
    sources = sources.delete_if { |s| group_set.intersection(s.groups).empty? }
    
    Shibkit::MetaMeta.log.debug "Rejected sources that aren't in #{selected_groups.join(', ')}"
  
  end
  
  return sources 
  
end

.locate_sources_file(source_list) ⇒ Object

Return appropriate file path for



537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
# File 'lib/shibkit/meta_meta/source.rb', line 537

def self.locate_sources_file(source_list)

  config = ::Shibkit::MetaMeta.config

  case source_list
  when :auto
    file_path = config.in_production? ? REAL_SOURCES_FILE : DEV_SOURCES_FILE
  when :dev, :test
    file_path = DEV_SOURCES_FILE
  when :real, :prod, :production
    file_path = REAL_SOURCES_FILE
  else
    file_path = source_list
  end
  
  return file_path
  
end

Instance Method Details

#certificate_pemString?

The content of the certificate associated with the metadata

Returns:

  • (String, nil)


413
414
415
416
417
418
419
420
# File 'lib/shibkit/meta_meta/source.rb', line 413

def certificate_pem
  
  ## Deal with caching locally, downloading, etc
  refresh if ::Shibkit::MetaMeta.config.auto_refresh? and @certificate_tmpfile == nil
  
  return IO.read(certificate_tmpfile.path)
  
end

#contentString

Return raw source string from the file

Returns:

  • (String)

    Metadata XML as text



424
425
426
427
428
429
430
431
432
433
434
# File 'lib/shibkit/meta_meta/source.rb', line 424

def content
          
  ## Deal with caching locally, downloading, etc
  refresh if ::Shibkit::MetaMeta.config.auto_refresh? and @metadata_tmpfile == nil
  
  raise "No content is available, source has not been downloaded" unless 
    .path
  
  return IO.read(.path)
    
end

#fetch_certificateFile

Fetch remote file and store locally

Returns:

  • (File)

    open filehandle for the local copy of certificate file



356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/shibkit/meta_meta/source.rb', line 356

def fetch_certificate
   
   @certificate_tmpfile = case certificate_source
     when /^http/
       fetch_remote(certificate_source)
     else
       fetch_local(certificate_source)
    end
   
   return @certificate_tmpfile
   
end

#fetch_metadataFile

Fetch remote file and store locally without validation

Returns:

  • (File)

    Open filehandle for the local copy of metadata file



339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/shibkit/meta_meta/source.rb', line 339

def 
   
  @metadata_tmpfile = case 
    when /^http/
      fetch_remote()
    else
      fetch_local()
   end
   
   @fetched_at = Time.new
   
   return @metadata_tmpfile
   
end

#groupsObject



265
266
267
268
269
# File 'lib/shibkit/meta_meta/source.rb', line 265

def groups
  
  return @groups
  
end

#groups=(group_names) ⇒ Object



258
259
260
261
262
263
# File 'lib/shibkit/meta_meta/source.rb', line 258

def groups=(group_names)
  
  @groups ||= Array.new
  @groups = [group_names].flatten.inject([]) {|m,v| m << v.to_s.downcase.to_sym }
  
end

#ok?TrueClass, FalseClass

Does the source object look sensible?

Returns:

  • (TrueClass, FalseClass)

    True or false



463
464
465
466
467
468
469
# File 'lib/shibkit/meta_meta/source.rb', line 463

def ok?
    
  return false unless  and .size > 1
    
  return true
    
end

#parse_xmlNokogiri::XML::Document

Return Nokogiri object tree for the metadata

Returns:

  • (Nokogiri::XML::Document)

    Nokogiri document



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

def parse_xml

  ## Parse the entire file as an XML document
  doc = Nokogiri::XML.parse(content) do |config|
    #config.strict.noent.dtdvalid
  end
  
  ## Select the top node  
  xml  = doc.root
  
  ## Add exotic namespaces to make sure we can deal with all metadata # TODO
  NAMESPACES.each_pair { |label, uri| xml.add_namespace_definition(label,uri) }
  
  return xml
 
end

#readObject



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

def read
  
  Nokogiri::XML::Reader(content)
   
end

#refreshTrueClass, FalseClass

Redownload and revalidate remote files for the source

Returns:

  • (TrueClass, FalseClass)


317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/shibkit/meta_meta/source.rb', line 317

def refresh
  
  unless selected?
    
    log.info "Content for #{ uri} - skipping refresh as not selected."
    return false
    
  end
  
  log.info "Content for #{ uri} is being refreshed..."
  
  
  fetch_certificate
  
  raise "Validation error" unless valid?
  
  return true
  
end

#selected?Boolean

Has this federation/source been selected by the user?

Returns:

  • (Boolean)


399
400
401
402
403
404
405
406
407
408
409
# File 'lib/shibkit/meta_meta/source.rb', line 399

def selected?
  
  ## If nothing has been specified then everything has been selected.
  return true if ::Shibkit::MetaMeta.config.selected_federation_uris.empty?
  
  ## If this source's uri is present in the list, then yup.
  return true if ::Shibkit::MetaMeta.config.selected_federation_uris.include?(uri)
  
  return false
  
end

#tagsObject



278
279
280
281
282
# File 'lib/shibkit/meta_meta/source.rb', line 278

def tags
  
  return @tags
  
end

#tags=(tag_names) ⇒ Object



271
272
273
274
275
276
# File 'lib/shibkit/meta_meta/source.rb', line 271

def tags=(tag_names)
  
  @tags ||= Array.new
  @tags = [tag_names].flatten.inject([]) {|m,v| m << v.to_s.downcase.to_sym }
  
end

#to_federationShibkit::MetaMeta::Federation

Build a parsed Federation object containing Entitiess



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

def to_federation
  
  fx = self.parse_xml

  federation = ::Shibkit::MetaMeta::Federation.new(fx)
              
  ## Pass additional information from source into federation object  
  federation.name          = name || uri
  federation.display_name  = display_name || name
  federation.type          = type 
  federation.refeds_url    = refeds_info 
  federation.countries     = countries
  federation.languages     = languages
  federation.support_email = support_email
  federation.homepage_url  = homepage
  federation.description   = description
  federation.groups        = groups
  federation.tags          = tags
  federation.trustiness    = trustiness
  
  #federation.from_xml(fx)
  
  return federation
  
end

#to_hashObject

Create a new hash from a source object



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

def to_hash

  data = Hash.new
  
  data['uri']           = name_uri.strip
  data['name']          = name
  data['refresh']       = refresh_delay.to_i
  data['display_name']  = display_name
  data['type']          = type.to_s
  data['countries']     = countries
  data['metadata']      = 
  data['certificate']   = certificate_source
  data['fingerprint']   = fingerprint
  data['refeds_info']   = refeds_url
  data['homepage']      = homepage
  data['languages']     = languages
  data['support_email'] = support_email
  data['description']   = description.strip
  data['trustiness']    = trustiness 
  data['groups']        = groups 
  data['tags']          = tags 
  
  return data
  
end

#to_sObject



161
162
163
164
165
# File 'lib/shibkit/meta_meta/source.rb', line 161

def to_s
  
  return  || nil
  
end

#trustinessObject



309
310
311
312
313
# File 'lib/shibkit/meta_meta/source.rb', line 309

def trustiness
  
  return @trustiness || 1.0 # TODO should be able to configure default trustiness
  
end

#trustiness=(level) ⇒ Object



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/shibkit/meta_meta/source.rb', line 284

def trustiness=(level)

  ## Convert strings
  if level.kind_of? String 
    
    ## Percentages as strings become decimal fractions, otherwise directly converted.
    level = level.match(PERCENTAGE_PATTERN) ? level.to_f / 100 : level.to_f

  end
  
  case 
  when level > 1
    log.warn "Setting trustiness greater than 1 is ambiguous, so storing as 1. Use decimal fraction or percentage."
    @trustiness = 1.0
  when level < 0
    log.warn "Setting trustiness less than 1 is ambiguous, so storing as 0. Use decimal fraction or percentage."
    @trustiness = 0.0
  else
    @trustiness = level.to_f
  end  
  
  return @trustiness
  
end

#valid?TrueClass, FalseClass

Checks validity of metadata and certificate without raising exceptions

Returns:

  • (TrueClass, FalseClass)


388
389
390
391
392
393
394
395
396
# File 'lib/shibkit/meta_meta/source.rb', line 388

def valid?
  
  begin
    return true if validate
  rescue
    return false
  end
  
end

#validateTrueClass, FalseClass

Validates metadata and certificate or raises an exception

Returns:

  • (TrueClass, FalseClass)


371
372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/shibkit/meta_meta/source.rb', line 371

def validate
  
  ## Check that XML is valid
  # ...
  
  ## Check that certificate is OK
  # ...
  
  ## Check that metadata has been signed OK, prob. Using XMLSecTool?
  # ...
  
  return true
  
end