oxidized 0.37.0 → 0.38.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (139) hide show
  1. checksums.yaml +4 -4
  2. data/.github/ISSUE_TEMPLATE/bug_report.md +30 -4
  3. data/.github/ISSUE_TEMPLATE/feature_request.md +10 -3
  4. data/.github/ISSUE_TEMPLATE/support-request.md +30 -4
  5. data/.github/PULL_REQUEST_TEMPLATE.md +3 -1
  6. data/.github/workflows/codeql.yml +4 -4
  7. data/.github/workflows/publishdocker.yml +2 -2
  8. data/.github/workflows/ruby.yml +10 -3
  9. data/.github/workflows/stale.yml +1 -1
  10. data/.rubocop_todo.yml +2 -8
  11. data/AGENTS.md +174 -0
  12. data/CHANGELOG.md +101 -0
  13. data/CONTRIBUTING.md +78 -44
  14. data/Dockerfile +8 -4
  15. data/README.md +8 -1
  16. data/docs/Creating-Models.md +5 -5
  17. data/docs/DeviceSimulation.md +80 -51
  18. data/docs/Docker.md +49 -0
  19. data/docs/Inputs.md +81 -5
  20. data/docs/Issues.md +49 -13
  21. data/docs/Model-Notes/FTD.md +64 -0
  22. data/docs/Model-Notes/HPEAruba.md +5 -0
  23. data/docs/Model-Notes/ITMGOS.md +40 -0
  24. data/docs/Model-Notes/PanOS_API.md +20 -1
  25. data/docs/Model-Notes/TPLink.md +75 -0
  26. data/docs/Outputs.md +1 -0
  27. data/docs/Release.md +43 -21
  28. data/docs/Ruby-API.md +231 -9
  29. data/docs/Supported-OS-Types.md +10 -5
  30. data/docs/Troubleshooting.md +8 -9
  31. data/extra/device2yaml.rb +21 -5
  32. data/extra/hooks/modelrulesadvanced.rb +2 -3
  33. data/extra/oxidized.service +0 -1
  34. data/extra/syslog.rb +1 -1
  35. data/lib/oxidized/cli/support.rb +4 -7
  36. data/lib/oxidized/config/vars.rb +2 -1
  37. data/lib/oxidized/core.rb +20 -0
  38. data/lib/oxidized/hook/ciscosparkdiff.rb +1 -1
  39. data/lib/oxidized/hook/slackdiff.rb +1 -1
  40. data/lib/oxidized/hook/xmppdiff.rb +2 -2
  41. data/lib/oxidized/input/cli.rb +14 -2
  42. data/lib/oxidized/input/http.rb +22 -7
  43. data/lib/oxidized/input/scp.rb +3 -1
  44. data/lib/oxidized/input/sshbase.rb +43 -12
  45. data/lib/oxidized/input/telnet.rb +2 -2
  46. data/lib/oxidized/model/acos.rb +2 -2
  47. data/lib/oxidized/model/acsw.rb +1 -1
  48. data/lib/oxidized/model/adtran.rb +1 -1
  49. data/lib/oxidized/model/aireos.rb +3 -3
  50. data/lib/oxidized/model/aoscx.rb +7 -3
  51. data/lib/oxidized/model/aosw.rb +8 -13
  52. data/lib/oxidized/model/arbos.rb +1 -1
  53. data/lib/oxidized/model/aricentiss.rb +1 -1
  54. data/lib/oxidized/model/asa.rb +1 -1
  55. data/lib/oxidized/model/asternos.rb +1 -1
  56. data/lib/oxidized/model/audiocodesmp.rb +1 -1
  57. data/lib/oxidized/model/awplus.rb +4 -3
  58. data/lib/oxidized/model/bdcom.rb +1 -1
  59. data/lib/oxidized/model/br6910.rb +1 -1
  60. data/lib/oxidized/model/c4cmts.rb +1 -1
  61. data/lib/oxidized/model/ciscosmb.rb +0 -1
  62. data/lib/oxidized/model/cnos.rb +10 -1
  63. data/lib/oxidized/model/comnetms.rb +1 -1
  64. data/lib/oxidized/model/coriantgroove.rb +1 -1
  65. data/lib/oxidized/model/dellx.rb +1 -1
  66. data/lib/oxidized/model/dlink.rb +1 -1
  67. data/lib/oxidized/model/dlinknextgen.rb +1 -1
  68. data/lib/oxidized/model/dnos.rb +2 -2
  69. data/lib/oxidized/model/eatonnetwork.rb +33 -32
  70. data/lib/oxidized/model/eciapollo.rb +1 -1
  71. data/lib/oxidized/model/edgeos.rb +3 -3
  72. data/lib/oxidized/model/enterasys.rb +1 -1
  73. data/lib/oxidized/model/eos.rb +1 -0
  74. data/lib/oxidized/model/fastiron.rb +10 -1
  75. data/lib/oxidized/model/fiberdriver.rb +1 -1
  76. data/lib/oxidized/model/firelinuxos.rb +1 -1
  77. data/lib/oxidized/model/fortigate.rb +15 -2
  78. data/lib/oxidized/model/fortios.rb +1 -1
  79. data/lib/oxidized/model/ftd.rb +183 -0
  80. data/lib/oxidized/model/ftos.rb +2 -2
  81. data/lib/oxidized/model/garderos.rb +16 -5
  82. data/lib/oxidized/model/hpebladesystem.rb +1 -1
  83. data/lib/oxidized/model/ibos.rb +1 -2
  84. data/lib/oxidized/model/ingate.rb +15 -1
  85. data/lib/oxidized/model/iosxr.rb +9 -3
  86. data/lib/oxidized/model/ironware.rb +1 -1
  87. data/lib/oxidized/model/itmgos.rb +74 -0
  88. data/lib/oxidized/model/junos.rb +8 -2
  89. data/lib/oxidized/model/kornfeldos.rb +2 -4
  90. data/lib/oxidized/model/lancom.rb +2 -2
  91. data/lib/oxidized/model/masteros.rb +1 -1
  92. data/lib/oxidized/model/mlnxos.rb +1 -1
  93. data/lib/oxidized/model/model.rb +5 -2
  94. data/lib/oxidized/model/mtrlrfs.rb +1 -1
  95. data/lib/oxidized/model/ndms.rb +1 -1
  96. data/lib/oxidized/model/necix.rb +1 -1
  97. data/lib/oxidized/model/netgear.rb +1 -1
  98. data/lib/oxidized/model/netscaler.rb +1 -1
  99. data/lib/oxidized/model/nxos.rb +11 -2
  100. data/lib/oxidized/model/ocnos.rb +1 -1
  101. data/lib/oxidized/model/onefinity.rb +1 -1
  102. data/lib/oxidized/model/oneos.rb +0 -1
  103. data/lib/oxidized/model/openbsd.rb +1 -1
  104. data/lib/oxidized/model/os10.rb +2 -2
  105. data/lib/oxidized/model/os6.rb +3 -2
  106. data/lib/oxidized/model/panos.rb +1 -1
  107. data/lib/oxidized/model/panos_api.rb +42 -14
  108. data/lib/oxidized/model/procurve.rb +63 -31
  109. data/lib/oxidized/model/routeros.rb +2 -1
  110. data/lib/oxidized/model/screenos.rb +2 -2
  111. data/lib/oxidized/model/sgos.rb +1 -1
  112. data/lib/oxidized/model/siklu.rb +2 -2
  113. data/lib/oxidized/model/slxos.rb +1 -1
  114. data/lib/oxidized/model/sonicos.rb +2 -2
  115. data/lib/oxidized/model/tdre.rb +2 -2
  116. data/lib/oxidized/model/telco.rb +1 -1
  117. data/lib/oxidized/model/tmos.rb +11 -2
  118. data/lib/oxidized/model/tplink.rb +4 -4
  119. data/lib/oxidized/model/viptela.rb +1 -1
  120. data/lib/oxidized/model/voltaire.rb +1 -1
  121. data/lib/oxidized/model/vrp.rb +14 -8
  122. data/lib/oxidized/model/vsololt.rb +93 -0
  123. data/lib/oxidized/model/vyatta.rb +3 -3
  124. data/lib/oxidized/model/vyos.rb +5 -1
  125. data/lib/oxidized/model/xos.rb +1 -1
  126. data/lib/oxidized/model/zhoneolt.rb +1 -1
  127. data/lib/oxidized/node.rb +9 -9
  128. data/lib/oxidized/nodes.rb +5 -13
  129. data/lib/oxidized/output/git.rb +72 -81
  130. data/lib/oxidized/output/gitcrypt.rb +13 -32
  131. data/lib/oxidized/redact.rb +18 -0
  132. data/lib/oxidized/signals.rb +2 -16
  133. data/lib/oxidized/source/source.rb +5 -1
  134. data/lib/oxidized/source/sql.rb +1 -0
  135. data/lib/oxidized/version.rb +2 -2
  136. data/lib/oxidized.rb +1 -0
  137. data/lib/refinements.rb +7 -3
  138. data/oxidized.gemspec +20 -11
  139. metadata +69 -29
@@ -7,7 +7,7 @@ class Viptela < Oxidized::Model
7
7
  comment '! '
8
8
 
9
9
  cmd :all do |cfg|
10
- cfg.each_line.to_a[1..-2].join
10
+ cfg.cut_both
11
11
  end
12
12
 
13
13
  cmd :secret do |cfg|
@@ -18,7 +18,7 @@ class VOLTAIRE < Oxidized::Model
18
18
  cfg.gsub! /^System memory:\s.+/, '' # Omit constantly changing memory info
19
19
  cfg.gsub! /^Uptime:\s.+/, '' # Omit constantly changing uptime info
20
20
  cfg.gsub! /.+Generated at\s\d+.+/, '' # Omit constantly changing generation time info
21
- cfg.lines.to_a[2..-3].join
21
+ cfg.cut_both(2, 2)
22
22
  end
23
23
 
24
24
  cmd :secret do |cfg|
@@ -16,6 +16,10 @@ class VRP < Oxidized::Model
16
16
  cfg.cut_both
17
17
  end
18
18
 
19
+ cmd :significant_changes do |cfg|
20
+ cfg.reject_lines [/^!Last configuration was (updated|saved) at /]
21
+ end
22
+
19
23
  cfg :telnet do
20
24
  username /^Username:$/
21
25
  password /^Password:$/
@@ -27,19 +31,21 @@ class VRP < Oxidized::Model
27
31
  end
28
32
 
29
33
  cmd 'display version' do |cfg|
30
- cfg = cfg.each_line.reject do |l|
31
- l.match /uptime|^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d(\.\d\d\d)? ?(\+\d\d:\d\d)?$/
32
- end.join
33
- comment cfg
34
+ comment cfg.reject_lines [
35
+ /uptime/,
36
+ /^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d(\.\d\d\d)? ?(\+\d\d:\d\d)?$/
37
+ ]
34
38
  end
35
39
 
36
40
  cmd 'display device' do |cfg|
37
- cfg = cfg.each_line.reject { |l| l.match /^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d(\.\d\d\d)? ?(\+\d\d:\d\d)?$/ }.join
38
- comment cfg
41
+ comment cfg.reject_lines [
42
+ /^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d(\.\d\d\d)? ?(\+\d\d:\d\d)?$/
43
+ ]
39
44
  end
40
45
 
41
46
  cmd 'display current-configuration all' do |cfg|
42
- cfg = cfg.each_line.reject { |l| l.match /^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d(\.\d\d\d)? ?(\+\d\d:\d\d)?$/ }.join
43
- cfg
47
+ cfg.reject_lines [
48
+ /^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d(\.\d\d\d)? ?(\+\d\d:\d\d)?$/
49
+ ]
44
50
  end
45
51
  end
@@ -0,0 +1,93 @@
1
+ class VSOLOLT < Oxidized::Model
2
+ using Refinements
3
+
4
+ # Tested with VSOL V1600G0-B GPON OLT, software V1.4.13R
5
+
6
+ # The device runs its CLI in three modes, all matched here:
7
+ # user mode: RT_01|192.0.2.4-Site_OLT>
8
+ # enable mode: RT_01|192.0.2.4-Site_OLT#
9
+ # config mode: RT_01|192.0.2.4-Site_OLT(config)#
10
+ # The hostname can contain '|', '.', '-' and the prompt appends '(config)'.
11
+ prompt /^([\w.@|()\/-]+[#>]\s?)$/
12
+ comment '! '
13
+
14
+ # The OLT continuously prints log events into the SSH session and there is no
15
+ # known way to disable them. They all start with a "YYYY/MM/DD HH:MM:SS"
16
+ # timestamp, e.g.:
17
+ # 2026/05/25 17:08:24 ONU Port Los PON 0/3 ONU 50 sn ... LAN1 LINK DOWN
18
+ # Strip them so they don't pollute the collected configuration.
19
+ cmd :all do |cfg|
20
+ cfg.gsub! /^\d{4}\/\d{2}\/\d{2}\s+\d{2}:\d{2}:\d{2}\s+.*$\n?/, ''
21
+ # Pager redraw orphans: after we answer the " --More-- " pager with a space,
22
+ # the device erases its 10-character prompt by emitting 10x BACKSPACE +
23
+ # 10x SPACE + 10x BACKSPACE before redrawing the next line in its place.
24
+ # Left untouched, this glues the last line of a page to the first line of
25
+ # the next one. (See tnsr.rb for an analogous case with a different byte
26
+ # count: there the prompt is "--More--" cleared with 8x \x08\x20\x08.)
27
+ cfg.gsub! /\x08{10}\x20{10}\x08{10}/, ''
28
+ # NUL byte trailing the " --More-- " marker, in case expect didn't catch it.
29
+ cfg.gsub! "\x00", ''
30
+ cfg.cut_both
31
+ end
32
+
33
+ # Pager handling. The device paginates with a " --More-- \x00" line (note the
34
+ # leading/trailing space and the trailing NUL byte). Advance with a space.
35
+ # Only consume the spaces on the pager's own line (and the NUL) here, NOT the
36
+ # preceding newline, so the previous config line stays on its own line. The
37
+ # backspace/space redraw orphans are cleaned in cmd :all above.
38
+ expect /\x20*--More--\x20*\x00?/ do |data, re|
39
+ send ' '
40
+ data.sub re, ''
41
+ end
42
+
43
+ cmd :secret do |cfg|
44
+ # SNMP community string. It appears both as "snmp-server community <X> ro|rw"
45
+ # and inside "snmp-server host ... version 2c community <X>"; anchor to
46
+ # "snmp-server" so we don't touch the word "community" elsewhere.
47
+ cfg.gsub! /^(snmp-server .*community) \S+/, '\\1 <secret hidden>'
48
+ # OLT admin accounts:
49
+ # user add admin login-password X
50
+ # user role admin ADMIN enable-password X
51
+ cfg.gsub! /(login-password)\s+\S+/, '\\1 <secret hidden>'
52
+ cfg.gsub! /(enable-password)\s+\S+/, '\\1 <secret hidden>'
53
+ # client account passwords on ONUs, e.g.
54
+ # ... username admin_control enable admin PW1 user_control enable user PW2
55
+ # PW1 follows "enable <role>", PW2 follows "user_control enable <role>".
56
+ cfg.gsub! /(username \S+ enable \S+)\s+\S+/, '\\1 <secret hidden>'
57
+ cfg.gsub! /(user_control enable \S+)\s+\S+/, '\\1 <secret hidden>'
58
+ # client (ONU) Wi-Fi PSK: ... shared_key PW rekey_interval ...
59
+ cfg.gsub! /(shared_key)\s+\S+/, '\\1 <secret hidden>'
60
+ cfg
61
+ end
62
+
63
+ cmd 'show version' do |cfg|
64
+ comment cfg
65
+ end
66
+
67
+ cmd 'show pon transceiver-info all' do |cfg|
68
+ comment cfg
69
+ end
70
+
71
+ cmd 'show running-config' do |cfg|
72
+ # Filtering the injected log messages (see cmd :all) leaves behind the
73
+ # blank line(s) the device printed around each async message. The
74
+ # running-config uses '!' as section separators, not blank lines, so
75
+ # collapse any run of empty lines to drop those artifacts.
76
+ cfg.gsub! /(?:\r?\n[ \t]*){2,}/, "\n"
77
+ cfg
78
+ end
79
+
80
+ cfg :ssh do
81
+ # The show commands are only available after entering enable + config mode.
82
+ # The enable password equals the SSH login password (vars(:enable) overrides).
83
+ post_login do
84
+ send "enable\n"
85
+ expect /[pP]assword:\s*$/
86
+ send (vars(:enable) || @node.auth[:password]).to_s + "\n"
87
+ expect /[#>]\s*$/
88
+ end
89
+ post_login 'configure terminal'
90
+ pre_logout 'end'
91
+ pre_logout 'exit'
92
+ end
93
+ end
@@ -6,7 +6,7 @@ class Vyatta < Oxidized::Model
6
6
  prompt /@.*(:~\$|>)\s/
7
7
 
8
8
  cmd :all do |cfg|
9
- cfg.lines.to_a[1..-2].join
9
+ cfg.cut_both
10
10
  end
11
11
 
12
12
  cmd :secret do |cfg|
@@ -27,8 +27,8 @@ class Vyatta < Oxidized::Model
27
27
  cmd 'show configuration commands | no-more'
28
28
 
29
29
  cfg :telnet do
30
- username /login:\s/
31
- password /^Password:\s/
30
+ username /login:\s/
31
+ password /^Password:\s/
32
32
  end
33
33
 
34
34
  cfg :telnet, :ssh do
@@ -9,7 +9,7 @@ class Vyos < Oxidized::Model
9
9
  clean :escape_codes
10
10
 
11
11
  cmd :all do |cfg|
12
- cfg.lines.to_a[1..-2].join
12
+ cfg.cut_both
13
13
  end
14
14
 
15
15
  cmd :secret do |cfg|
@@ -29,6 +29,10 @@ class Vyos < Oxidized::Model
29
29
  comment cfg
30
30
  end
31
31
 
32
+ cmd 'show system commit | no-more' do |cfg|
33
+ comment "# Last commits:\n#{cfg.lines.first(4).join}"
34
+ end
35
+
32
36
  cmd 'show configuration commands | no-more'
33
37
 
34
38
  cfg :ssh do
@@ -9,7 +9,7 @@ class XOS < Oxidized::Model
9
9
  cmd :all do |cfg|
10
10
  # xos inserts leading \r characters and other trailing white space.
11
11
  # this deletes extraneous \r and trailing white space.
12
- cfg.each_line.to_a[1..-2].map { |line| line.delete("\r").rstrip }.join("\n") + "\n"
12
+ cfg.delete("\r").cut_both.rstrip_lines
13
13
  end
14
14
 
15
15
  cmd :secret do |cfg|
@@ -20,7 +20,7 @@ class ZhoneOLT < Oxidized::Model
20
20
  end
21
21
 
22
22
  cmd :all do |cfg|
23
- cfg.each_line.to_a[1..-2].map { |line| line.delete("\r").rstrip }.join("\n") + "\n"
23
+ cfg.delete("\r").cut_both.rstrip_lines
24
24
  end
25
25
 
26
26
  cmd 'swversion' do |cfg|
data/lib/oxidized/node.rb CHANGED
@@ -95,7 +95,7 @@ module Oxidized
95
95
  return false
96
96
  end
97
97
 
98
- crashdir = Oxidized.config.crash.directory
98
+ crashdir = File.expand_path(Oxidized.config.crash.directory)
99
99
  crashfile = Oxidized.config.crash.hostnames? ? name : ip.to_s
100
100
  FileUtils.mkdir_p(crashdir) unless File.directory?(crashdir)
101
101
 
@@ -225,40 +225,40 @@ module Oxidized
225
225
  key_sym = key.to_sym
226
226
  key_str = key.to_s
227
227
  model_name = @model.class.name.to_s.downcase
228
- logger.debug "resolving node key '#{key}', with passed global value of '#{global}' " \
229
- "and node value '#{opt[key_sym]}'"
228
+ logger.debug "resolving node key '#{key}', with passed global value of '#{Redact.value(key, global)}' " \
229
+ "and node value '#{Redact.value(key, opt[key_sym])}'"
230
230
 
231
231
  # Node
232
232
  if opt[key_sym]
233
233
  value = opt[key_sym]
234
- logger.debug "setting node key '#{key}' to value '#{value}' from node"
234
+ logger.debug "setting node key '#{key}' to value '#{Redact.value(key, value)}' from node"
235
235
 
236
236
  # Group specific model
237
237
  elsif Oxidized.config.groups.has_key?(@group) &&
238
238
  Oxidized.config.groups[@group].models.has_key?(model_name) &&
239
239
  Oxidized.config.groups[@group].models[model_name].has_key?(key_str)
240
240
  value = Oxidized.config.groups[@group].models[model_name][key_str]
241
- logger.debug "setting node key '#{key}' to value '#{value}' from model in group"
241
+ logger.debug "setting node key '#{key}' to value '#{Redact.value(key, value)}' from model in group"
242
242
 
243
243
  # Group
244
244
  elsif Oxidized.config.groups.has_key?(@group) && Oxidized.config.groups[@group].has_key?(key_str)
245
245
  value = Oxidized.config.groups[@group][key_str]
246
- logger.debug "setting node key '#{key}' to value '#{value}' from group"
246
+ logger.debug "setting node key '#{key}' to value '#{Redact.value(key, value)}' from group"
247
247
 
248
248
  # Model
249
249
  elsif Oxidized.config.models.has_key?(model_name) && Oxidized.config.models[model_name].has_key?(key_str)
250
250
  value = Oxidized.config.models[model_name][key_str]
251
- logger.debug "setting node key '#{key}' to value '#{value}' from model"
251
+ logger.debug "setting node key '#{key}' to value '#{Redact.value(key, value)}' from model"
252
252
 
253
253
  # Global passed
254
254
  elsif global
255
255
  value = global
256
- logger.debug "setting node key '#{key}' to value '#{value}' from passed global value"
256
+ logger.debug "setting node key '#{key}' to value '#{Redact.value(key, value)}' from passed global value"
257
257
 
258
258
  # Global
259
259
  elsif Oxidized.config.has_key?(key_str)
260
260
  value = Oxidized.config[key_str]
261
- logger.debug "setting node key '#{key}' to value '#{value}' from global"
261
+ logger.debug "setting node key '#{key}' to value '#{Redact.value(key, value)}' from global"
262
262
  end
263
263
  value
264
264
  end
@@ -38,19 +38,11 @@ module Oxidized
38
38
  def node_want?(node_want, node)
39
39
  return true unless node_want
40
40
 
41
- # rubocop:disable Style/RedundantParentheses
42
- node_want_ip = (IPAddr.new(node_want) rescue false)
43
- name_is_ip = (IPAddr.new(node[:name]) rescue false)
44
- # rubocop:enable Style/RedundantParentheses
45
- # rubocop:todo Lint/DuplicateBranch
46
- if name_is_ip && (node_want_ip == node[:name])
47
- true
48
- elsif node[:ip] && (node_want_ip == node[:ip])
49
- true
50
- elsif node_want.match node[:name]
51
- true unless name_is_ip
52
- end
53
- # rubocop:enable Lint/DuplicateBranch
41
+ node_want_ip = IPAddr.new(node_want) rescue false
42
+ name_is_ip = IPAddr.new(node[:name]) rescue false
43
+ (name_is_ip && node_want_ip == node[:name]) ||
44
+ (node[:ip] && node_want_ip == node[:ip]) ||
45
+ node_want == node[:name]
54
46
  end
55
47
 
56
48
  def list
@@ -74,17 +74,10 @@ module Oxidized
74
74
  # Returns the configuration of group/node_name
75
75
  #
76
76
  # #fetch is called by Nodes#fetch
77
- # Nodes#fetch creates a new Output object each time, so it not easy
78
- # to cache the repo index in memory. But as we keep the repo index up
79
- # to date on disk in #update_repo, we can read it from disk instead of
80
- # rebuilding it each time.
81
77
  def fetch(node, group)
82
78
  repo, path = yield_repo_and_path(node, group)
83
79
  repo = Rugged::Repository.new repo
84
- # Read the index from disk
85
- index = repo.index
86
-
87
- repo.read(index.get(path)[:oid]).data
80
+ repo.blob_at(repo.head.target_id, path).content
88
81
  rescue StandardError
89
82
  'node not found'
90
83
  end
@@ -110,29 +103,16 @@ module Oxidized
110
103
  end
111
104
 
112
105
  # give a hash with the patch of a diff between 2 revision and the stats (added and deleted lines)
106
+ # The diff is limited to the node, as a commit may contain several nodes
113
107
  def get_diff(node, group, oid1, oid2)
114
- diff_commits = nil
115
- repo, = yield_repo_and_path(node, group)
108
+ repo, path = yield_repo_and_path(node, group)
116
109
  repo = Rugged::Repository.new repo
117
110
  commit = repo.lookup(oid1)
111
+ commit_old = oid2 ? repo.lookup(oid2) : commit.parents[0]
118
112
 
119
- if oid2
120
- commit_old = repo.lookup(oid2)
121
- diff = repo.diff(commit_old, commit)
122
- diff.each do |patch|
123
- if /#{node.name}\s+/ =~ patch.to_s.lines.first
124
- diff_commits = { patch: patch.to_s, stat: patch.stat }
125
- break
126
- end
127
- end
128
- else
129
- stat = commit.parents[0].diff(commit).stat
130
- stat = [stat[1], stat[2]]
131
- patch = commit.parents[0].diff(commit).patch
132
- diff_commits = { patch: patch, stat: stat }
133
- end
134
-
135
- diff_commits
113
+ diff = repo.diff(commit_old, commit, paths: [path], disable_pathspec_match: true)
114
+ _files, additions, deletions = diff.stat
115
+ { patch: diff.patch, stat: [additions, deletions] }
136
116
  rescue StandardError
137
117
  'no diffs'
138
118
  end
@@ -143,66 +123,78 @@ module Oxidized
143
123
  @gitcache[repo_path][:nodes][node_path] || []
144
124
  end
145
125
 
126
+ # As oxidized-web works with multiple threads, we need a mutex to protect the cache
127
+ @cache_mutex = Mutex.new
128
+
146
129
  # Update @gitcache, a class instance variable, ensuring persistence
147
130
  # by saving the cache independently of object instances
148
131
  def self.update_cache(repo_path)
149
- # initialize our cache as a class instance variable
150
- @gitcache ||= {}
151
- # When single_repo == false, we have multiple repositories
152
- unless @gitcache[repo_path]
153
- @gitcache[repo_path] = {}
154
- @gitcache[repo_path][:nodes] = {}
155
- @gitcache[repo_path][:last_commit] = nil
156
- end
157
-
158
- repo = Rugged::Repository.new repo_path
132
+ @cache_mutex.synchronize do
133
+ # @gitcache (class instance variable) is keyed by repo_path:
134
+ # with single_repo == false, each group has its own repo
135
+ @gitcache ||= {}
136
+ repo = Rugged::Repository.new repo_path
137
+ # Read HEAD only once, as a worker may commit while we walk the logs
138
+ head_oid = repo.head.target.oid
139
+ cache = @gitcache[repo_path]
140
+
141
+ if cache && cache[:last_commit] == head_oid
142
+ logger.debug "update_cache: #{repo_path} unchanged since last update, using cached versions"
143
+ return cache
144
+ end
159
145
 
160
- walker = Rugged::Walker.new(repo)
161
- walker.sorting(Rugged::SORT_DATE)
162
- walker.push(repo.head.target.oid)
163
-
164
- # We store the commits into a temporary cache. It will be prepended
165
- # to @gitcache to preserve the order of the commits.
166
- cache = {}
167
- walker.each do |commit|
168
- if commit.oid == @gitcache[repo_path][:last_commit]
169
- # we have reached the last cached commit, so we're done
170
- break
146
+ walker = Rugged::Walker.new(repo)
147
+ walker.sorting(Rugged::SORT_TOPO | Rugged::SORT_DATE)
148
+ walker.push(head_oid)
149
+ if cache.nil?
150
+ logger.debug "update_cache: building cache for #{repo_path}"
151
+ cache = { nodes: {}, last_commit: nil }
152
+ elsif descendant?(repo, head_oid, cache[:last_commit])
153
+ logger.debug "update_cache: walking new commits in #{repo_path}"
154
+ walker.hide(cache[:last_commit])
155
+ else
156
+ logger.warn "update_cache: history of #{repo_path} rewritten, rebuilding cache"
157
+ cache = { nodes: {}, last_commit: nil }
171
158
  end
172
159
 
173
- commit.diff.each_delta do |delta|
174
- next unless delta.added? || delta.modified?
175
-
176
- hash = {}
177
- # We keep :date for reverse compatibility on oxidized-web <= 0.15.1
178
- hash[:date] = commit.time.to_s
179
- # date as a Time instance for more flexibility in oxidized-web
180
- hash[:time] = commit.time
181
- hash[:oid] = commit.oid
182
- hash[:author] = commit.author
183
- hash[:message] = commit.message
184
-
185
- filename = delta.new_file[:path]
186
- if cache[filename]
187
- cache[filename].append hash
188
- else
189
- cache[filename] = [hash]
160
+ new_commits = Hash.new { |hash, key| hash[key] = [] }
161
+ walker.each do |commit|
162
+ # Deduplicate repeated strings (String#-@) to reduce the memory
163
+ # footprint of large repositories
164
+ author = commit.author
165
+ author[:name] = -author[:name]
166
+ author[:email] = -author[:email]
167
+ message = -commit.message
168
+
169
+ commit.diff.each_delta do |delta|
170
+ next unless delta.added? || delta.modified?
171
+
172
+ new_commits[delta.new_file[:path]] << {
173
+ time: commit.time,
174
+ oid: commit.oid,
175
+ author: author,
176
+ message: message
177
+ }
190
178
  end
191
179
  end
192
- end
193
180
 
194
- cache.each_pair do |filename, hashlist|
195
- if @gitcache[repo_path][:nodes][filename]
196
- # using the splat operator (*) should be OK as hashlist should
197
- # not be very big when working on deltas
198
- @gitcache[repo_path][:nodes][filename].prepend(*hashlist)
199
- else
200
- @gitcache[repo_path][:nodes][filename] = hashlist
181
+ # Build new arrays instead of modifying them, as lists returned
182
+ # by hash_list may be in use by another thread
183
+ new_commits.each_pair do |filename, hashlist|
184
+ cache[:nodes][filename] = hashlist + (cache[:nodes][filename] || [])
201
185
  end
186
+
187
+ cache[:last_commit] = head_oid
188
+ @gitcache[repo_path] = cache
202
189
  end
190
+ end
203
191
 
204
- # Store the most recent commit
205
- @gitcache[repo_path][:last_commit] = repo.head.target.oid
192
+ # True if ancestor is in the history of commit, false if not or if
193
+ # ancestor does not exist (e.g. after a history rewrite)
194
+ def self.descendant?(repo, commit, ancestor)
195
+ repo.descendant_of?(commit, ancestor)
196
+ rescue Rugged::Error
197
+ false
206
198
  end
207
199
 
208
200
  # Currently only used in unit tests
@@ -249,6 +241,7 @@ module Oxidized
249
241
  logger.info "clean_obsolete_nodes: removing " \
250
242
  "#{files_to_delete.size} obsolete configs"
251
243
  index = repo.index
244
+ index.read_tree(tree)
252
245
 
253
246
  files_to_delete.each { |file_path| index.remove(file_path) }
254
247
 
@@ -307,18 +300,16 @@ module Oxidized
307
300
 
308
301
  # Uploads data into file in the repository repo
309
302
  #
310
- # update_repo caches the index on disk. An index is usually used in a
311
- # working directory and not in a bare repository, which confuses users.
312
- # The alternative would be to rebuild the index each time, which a little
313
- # time consuming. Caching the index in memory is difficult because a new
314
- # Output object is created each time #store is called.
303
+ # The index is rebuilt from HEAD, as HEAD may have been changed without
304
+ # updating the index (e.g. by the githubrepo hook). It is still written
305
+ # to disk, so that older versions of Oxidized do not use a stale index.
315
306
  def update_repo(repo, file, data) # rubocop:disable Naming/PredicateMethod
316
307
  oid_old = repo.blob_at(repo.head.target_id, file) rescue nil
317
308
  return false if oid_old && (oid_old.content.b == data.b)
318
309
 
319
310
  oid = repo.write data, :blob
320
- # Read the index from disk
321
311
  index = repo.index
312
+ repo.empty? ? index.clear : index.read_tree(repo.head.target.tree)
322
313
  index.add path: file, oid: oid, mode: 0o100644
323
314
 
324
315
  repo.config['user.name'] = @user
@@ -91,7 +91,7 @@ module Oxidized
91
91
 
92
92
  def fetch(node, group)
93
93
  repo, path = yield_repo_and_path(node, group)
94
- repo = Git.open repo
94
+ repo = ::Git.open repo
95
95
  unlock repo
96
96
  index = repo.index
97
97
  # Empty repo ?
@@ -107,16 +107,13 @@ module Oxidized
107
107
  def version(node, group)
108
108
  repo, path = yield_repo_and_path(node, group)
109
109
 
110
- repo = Git.open repo
110
+ repo = ::Git.open repo
111
111
  unlock repo
112
112
  walker = repo.log.path(path)
113
113
  i = -1
114
114
  tab = []
115
115
  walker.each do |commit|
116
116
  hash = {}
117
- # We keep :date for reverse compatibility on oxidized-web <= 0.15.1
118
- hash[:date] = commit.date.to_s
119
- # date as a Time instance for more flexibility in oxidized-web
120
117
  hash[:time] = commit.date
121
118
  hash[:oid] = commit.objectish
122
119
  hash[:author] = commit.author
@@ -132,7 +129,7 @@ module Oxidized
132
129
  # give the blob of a specific revision
133
130
  def get_version(node, group, oid)
134
131
  repo, path = yield_repo_and_path(node, group)
135
- repo = Git.open repo
132
+ repo = ::Git.open repo
136
133
  unlock repo
137
134
  repo.gtree(oid).files[path].contents
138
135
  rescue StandardError
@@ -142,31 +139,15 @@ module Oxidized
142
139
  end
143
140
 
144
141
  # give a hash with the patch of a diff between 2 revision and the stats (added and deleted lines)
142
+ # The diff is limited to the node, as a commit may contain several nodes
145
143
  def get_diff(node, group, oid1, oid2)
146
- diff_commits = nil
147
- repo, _path = yield_repo_and_path(node, group)
148
- repo = Git.open repo
144
+ repo, path = yield_repo_and_path(node, group)
145
+ repo = ::Git.open repo
149
146
  unlock repo
150
- commit = repo.gcommit(oid1)
151
-
152
- if oid2
153
- commit_old = repo.gcommit(oid2)
154
- diff = repo.diff(commit_old, commit)
155
- stats = [diff.stats[:files][node.name][:insertions], diff.stats[:files][node.name][:deletions]]
156
- diff.each do |patch|
157
- if /#{node.name}\s+/ =~ patch.patch.to_s.lines.first
158
- diff_commits = { patch: patch.patch.to_s, stat: stats }
159
- break
160
- end
161
- end
162
- else
163
- stat = commit.parents[0].diff(commit).stats
164
- stat = [stat[:files][node.name][:insertions], stat[:files][node.name][:deletions]]
165
- patch = commit.parents[0].diff(commit).patch
166
- diff_commits = { patch: patch, stat: stat }
167
- end
168
- lock repo
169
- diff_commits
147
+
148
+ diff = repo.diff(oid2 || "#{oid1}^", oid1).path(path)
149
+ total = diff.stats[:total]
150
+ { patch: diff.patch, stat: [total[:insertions], total[:deletions]] }
170
151
  rescue StandardError
171
152
  'no diffs'
172
153
  ensure
@@ -201,10 +182,10 @@ module Oxidized
201
182
 
202
183
  begin
203
184
  update_repo repo, file, data, @msg, @user, @email
204
- rescue Git::GitExecuteError, ArgumentError => e
185
+ rescue ::Git::GitExecuteError, ArgumentError => e
205
186
  logger.debug "open_error #{e} #{file}"
206
187
  begin
207
- grepo = Git.init repo
188
+ grepo = ::Git.init repo
208
189
  crypt_init grepo
209
190
  rescue StandardError => create_error
210
191
  raise GitCryptError, "first '#{e.message}' was raised while opening git repo, then " \
@@ -215,7 +196,7 @@ module Oxidized
215
196
  end
216
197
 
217
198
  def update_repo(repo, file, data, msg, user, email)
218
- grepo = Git.open repo
199
+ grepo = ::Git.open repo
219
200
  grepo.config('user.name', user)
220
201
  grepo.config('user.email', email)
221
202
  grepo.chdir do
@@ -0,0 +1,18 @@
1
+ module Oxidized
2
+ # Hides values of sensitive keys (passwords, tokens, ...) in logs and outputs
3
+ module Redact
4
+ SENSITIVE_NAME_RE = /(password|passphrase|secret|token|enable|
5
+ (private|api|access)_?key|
6
+ community|credential|auth
7
+ )/ix
8
+ REDACTED = '[REDACTED]'.freeze
9
+
10
+ def self.sensitive?(name)
11
+ name.to_s.match?(SENSITIVE_NAME_RE)
12
+ end
13
+
14
+ def self.value(name, value)
15
+ sensitive?(name) ? REDACTED : value
16
+ end
17
+ end
18
+ end
@@ -1,17 +1,3 @@
1
- # Monkey patch Signal.trap for Puma to keep it from overriding our handlers
2
- # Also prevent Puma from registering its own SIGHUP handler
3
- module Puma
4
- class Signal
5
- class << self
6
- alias os_trap trap
7
- def Signal.trap(sig, &block)
8
- sigshortname = sig.gsub "SIG", ''
9
- Oxidized::Signals.register_signal(sig, block) unless sigshortname.eql? 'HUP'
10
- end
11
- end
12
- end
13
- end
14
-
15
1
  module Oxidized
16
2
  class Signals
17
3
  @handlers = Hash.new { |h, k| h[k] = [] }
@@ -21,10 +7,10 @@ module Oxidized
21
7
  def register_signal(sig, procobj)
22
8
  # Compute short name of the signal (without SIG prefix)
23
9
  sigshortname = sig.gsub "SIG", ''
24
- signum = Signal.list[sigshortname]
10
+ signum = ::Signal.list[sigshortname]
25
11
 
26
12
  # Register the handler with OS
27
- Signal.trap signum do
13
+ ::Signal.trap signum do
28
14
  Oxidized::Signals.handle_signal(signum)
29
15
  end
30
16