Class: Kiji::Zipper

Inherits:
Object
  • Object
show all
Defined in:
lib/kiji/zipper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Zipper

Returns a new instance of Zipper.

Yields:

  • (_self)

Yield Parameters:

  • _self (Kiji::Zipper)

    the object that the method was called on



5
6
7
# File 'lib/kiji/zipper.rb', line 5

def initialize
  yield(self) if block_given?
end

Instance Attribute Details

#certObject

Returns the value of attribute cert.



3
4
5
# File 'lib/kiji/zipper.rb', line 3

def cert
  @cert
end

#private_keyObject

Returns the value of attribute private_key.



3
4
5
# File 'lib/kiji/zipper.rb', line 3

def private_key
  @private_key
end

Instance Method Details

#sign(kousei_base_file_path_or_content, app_file_paths) ⇒ Object

構成管理ファイル(kouse.xml)に署名を施す



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/kiji/zipper.rb', line 10

def sign(kousei_base_file_path_or_content, app_file_paths)
  raise 'Please specify cert & private_key' if @cert.nil? || @private_key.nil?

  content = begin
              File.read(kousei_base_file_path_or_content)
            rescue Errno::ENOENT, Errno::ENAMETOOLONG
              kousei_base_file_path_or_content
            end

  kousei_data = Nokogiri::XML(content)
  kousei_doc = kousei_data.to_xml(save_with: 0)

  signer = Signer.new(kousei_doc) do |s|
    s.cert                       = @cert
    s.private_key                = @private_key
    s.digest_algorithm           = :sha256
    s.signature_digest_algorithm = :sha256
  end

  # 構成情報のハッシュ値を求める
  signer.security_node = signer.document.root
  node = signer.document.at_xpath('//構成情報')
  signer.digest!(node, id: '#構成情報')

  # 申請書のハッシュ値を求める
  app_file_paths.each do |app_file_path|
    app_doc = File.read(app_file_path)
    app_file_name = File.basename(app_file_path)
    signer.digest_file!(app_doc, id: app_file_name)
  end

  # 署名の付与
  signer.sign!(issuer_serial: true)
  signer.document.xpath('//ns:Signature', ns: 'http://www.w3.org/2000/09/xmldsig#').wrap('<署名情報></署名情報>')

  # 構成情報 - 署名情報 - その他という順序
  kousei_node = signer.document.at_xpath('//構成情報')
  signature_node = signer.document.at_xpath('//署名情報')
  kousei_node.add_next_sibling(signature_node)

  signer
end

#write_zip(input_dir, output_file) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/kiji/zipper.rb', line 53

def write_zip(input_dir, output_file)
  @input_dir = input_dir
  @output_file = output_file

  entries = Dir.entries(@input_dir) - %w[. .. .DS_Store]
  Zip.sort_entries = true
  Zip::File.open(output_file, Zip::File::CREATE) do |io|
    write_entries(entries, '', io)
  end
end