openc3 7.5.1 → 7.6.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 (169) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +0 -0
  3. data/bin/openc3cli +82 -5
  4. data/data/config/_params.yaml +3 -0
  5. data/data/config/command_modifiers.yaml +5 -3
  6. data/data/config/default_plugins.yaml +1 -0
  7. data/data/config/interface_modifiers.yaml +17 -1
  8. data/data/config/item_modifiers.yaml +24 -1
  9. data/data/config/microservice.yaml +15 -1
  10. data/data/config/parameter_modifiers.yaml +4 -4
  11. data/data/config/plugins.yaml +511 -3
  12. data/data/config/settings.yaml +17 -0
  13. data/data/config/target.yaml +53 -0
  14. data/data/config/telemetry.yaml +58 -2
  15. data/data/config/widgets.yaml +30 -0
  16. data/lib/openc3/accessors/accessor.rb +14 -2
  17. data/lib/openc3/accessors/binary_accessor.rb +25 -5
  18. data/lib/openc3/api/CLAUDE.md +1 -0
  19. data/lib/openc3/api/api.rb +1 -0
  20. data/lib/openc3/api/cmd_api.rb +9 -1
  21. data/lib/openc3/api/interface_api.rb +7 -2
  22. data/lib/openc3/api/limits_api.rb +93 -41
  23. data/lib/openc3/api/router_api.rb +7 -2
  24. data/lib/openc3/api/settings_api.rb +4 -1
  25. data/lib/openc3/api/shared_resource_api.rb +107 -0
  26. data/lib/openc3/api/tlm_api.rb +10 -0
  27. data/lib/openc3/interfaces/http_client_interface.rb +84 -27
  28. data/lib/openc3/interfaces/protocols/preidentified_protocol.rb +6 -2
  29. data/lib/openc3/interfaces/tcpip_server_interface.rb +205 -125
  30. data/lib/openc3/io/json_api.rb +2 -10
  31. data/lib/openc3/io/json_api_object.rb +2 -10
  32. data/lib/openc3/logs/packet_log_writer.rb +8 -3
  33. data/lib/openc3/microservices/decom_common.rb +4 -1
  34. data/lib/openc3/microservices/decom_microservice.rb +52 -6
  35. data/lib/openc3/microservices/interface_decom_common.rb +13 -0
  36. data/lib/openc3/microservices/interface_microservice.rb +50 -11
  37. data/lib/openc3/microservices/limits_group_microservice.rb +513 -0
  38. data/lib/openc3/microservices/log_microservice.rb +22 -8
  39. data/lib/openc3/microservices/microservice.rb +8 -2
  40. data/lib/openc3/microservices/multi_microservice.rb +76 -22
  41. data/lib/openc3/microservices/queue_microservice.rb +29 -24
  42. data/lib/openc3/migrations/20260704000000_default_bridge.rb +1 -2
  43. data/lib/openc3/migrations/20261103000001_limits_group_backfill.rb +193 -0
  44. data/lib/openc3/models/bridge_model.rb +3 -3
  45. data/lib/openc3/models/cvt_model.rb +3 -1
  46. data/lib/openc3/models/host_interface_microservice_model.rb +4 -4
  47. data/lib/openc3/models/interface_model.rb +27 -7
  48. data/lib/openc3/models/microservice_group.rb +167 -0
  49. data/lib/openc3/models/microservice_model.rb +88 -2
  50. data/lib/openc3/models/panel_model.rb +109 -0
  51. data/lib/openc3/models/plugin_model.rb +1006 -26
  52. data/lib/openc3/models/plugin_store_model.rb +6 -1
  53. data/lib/openc3/models/protected_storage_model.rb +64 -0
  54. data/lib/openc3/models/queue_model.rb +19 -10
  55. data/lib/openc3/models/scope_model.rb +80 -3
  56. data/lib/openc3/models/setting_model.rb +2 -2
  57. data/lib/openc3/models/shared_resource_model.rb +381 -0
  58. data/lib/openc3/models/target_model.rb +324 -68
  59. data/lib/openc3/operators/microservice_operator.rb +76 -9
  60. data/lib/openc3/operators/operator.rb +29 -0
  61. data/lib/openc3/packets/commands.rb +16 -16
  62. data/lib/openc3/packets/limits.rb +2 -0
  63. data/lib/openc3/packets/packet.rb +75 -4
  64. data/lib/openc3/packets/packet_config.rb +152 -13
  65. data/lib/openc3/packets/packet_item.rb +35 -14
  66. data/lib/openc3/packets/packet_item_limits.rb +67 -6
  67. data/lib/openc3/packets/parsers/limits_parser.rb +69 -2
  68. data/lib/openc3/packets/parsers/packet_item_parser.rb +6 -0
  69. data/lib/openc3/packets/parsers/packet_parser.rb +45 -0
  70. data/lib/openc3/packets/parsers/state_parser.rb +1 -1
  71. data/lib/openc3/script/CLAUDE.md +1 -0
  72. data/lib/openc3/script/api_shared.rb +17 -9
  73. data/lib/openc3/script/calendar.rb +18 -6
  74. data/lib/openc3/script/commands.rb +11 -9
  75. data/lib/openc3/script/critical_cmd.rb +22 -2
  76. data/lib/openc3/script/metadata.rb +30 -4
  77. data/lib/openc3/script/plugins.rb +8 -0
  78. data/lib/openc3/script/script.rb +8 -24
  79. data/lib/openc3/script/web_socket_api.rb +23 -17
  80. data/lib/openc3/system/system.rb +9 -3
  81. data/lib/openc3/top_level.rb +17 -0
  82. data/lib/openc3/topics/command_topic.rb +7 -1
  83. data/lib/openc3/topics/decom_interface_topic.rb +9 -3
  84. data/lib/openc3/topics/discovery_topic.rb +84 -0
  85. data/lib/openc3/topics/limits_event_topic.rb +236 -17
  86. data/lib/openc3/topics/telemetry_decom_topic.rb +12 -3
  87. data/lib/openc3/topics/topic.rb +2 -1
  88. data/lib/openc3/utilities/authentication.rb +67 -16
  89. data/lib/openc3/utilities/cli_generator.rb +117 -25
  90. data/lib/openc3/utilities/client_ip_header_filter.rb +38 -0
  91. data/lib/openc3/utilities/config_overlay.rb +24 -1
  92. data/lib/openc3/utilities/cosmos_rails_formatter.rb +18 -10
  93. data/lib/openc3/utilities/hardware_discovery.rb +279 -0
  94. data/lib/openc3/utilities/limits_rollup.rb +81 -0
  95. data/lib/openc3/utilities/local_mode.rb +48 -5
  96. data/lib/openc3/utilities/questdb_client.rb +130 -22
  97. data/lib/openc3/utilities/reingest_job.rb +12 -3
  98. data/lib/openc3/utilities/running_script.rb +206 -49
  99. data/lib/openc3/utilities/script.rb +24 -12
  100. data/lib/openc3/utilities/script_env.rb +164 -0
  101. data/lib/openc3/utilities/script_message_signing.rb +133 -0
  102. data/lib/openc3/utilities/store_autoload.rb +32 -1
  103. data/lib/openc3/utilities/store_queued.rb +5 -1
  104. data/lib/openc3/version.rb +6 -6
  105. data/templates/command_validator/command_validator.py +9 -9
  106. data/templates/{tool_react/pnpm-workspace.yaml → panel/.prettierrc.cjs} +7 -5
  107. data/templates/panel/Rakefile +24 -0
  108. data/templates/panel/package.json +17 -0
  109. data/templates/{tool_angular → panel}/pnpm-workspace.yaml +5 -0
  110. data/templates/panel/src/Panel.js +52 -0
  111. data/templates/panel/src/Panel.vue +60 -0
  112. data/templates/panel/src/panelSdk.js +24 -0
  113. data/templates/panel/vite-Panel.config.js +43 -0
  114. data/templates/tool_vue/package.json +3 -3
  115. data/templates/widget/package.json +2 -2
  116. metadata +24 -56
  117. data/templates/tool_angular/.editorconfig +0 -16
  118. data/templates/tool_angular/.gitignore +0 -44
  119. data/templates/tool_angular/.vscode/extensions.json +0 -4
  120. data/templates/tool_angular/.vscode/launch.json +0 -20
  121. data/templates/tool_angular/.vscode/tasks.json +0 -42
  122. data/templates/tool_angular/angular.json +0 -111
  123. data/templates/tool_angular/extra-webpack.config.js +0 -8
  124. data/templates/tool_angular/package.json +0 -48
  125. data/templates/tool_angular/src/app/app-routing.module.ts +0 -15
  126. data/templates/tool_angular/src/app/app.component.html +0 -22
  127. data/templates/tool_angular/src/app/app.component.scss +0 -18
  128. data/templates/tool_angular/src/app/app.component.spec.ts +0 -29
  129. data/templates/tool_angular/src/app/app.component.ts +0 -52
  130. data/templates/tool_angular/src/app/app.module.ts +0 -30
  131. data/templates/tool_angular/src/app/custom-overlay-container.ts +0 -17
  132. data/templates/tool_angular/src/app/empty-route/empty-route.component.ts +0 -7
  133. data/templates/tool_angular/src/app/openc3-api.d.ts +0 -1
  134. data/templates/tool_angular/src/assets/.gitkeep +0 -0
  135. data/templates/tool_angular/src/environments/environment.prod.ts +0 -3
  136. data/templates/tool_angular/src/environments/environment.ts +0 -16
  137. data/templates/tool_angular/src/favicon.ico +0 -0
  138. data/templates/tool_angular/src/index.html +0 -13
  139. data/templates/tool_angular/src/main.single-spa.ts +0 -40
  140. data/templates/tool_angular/src/single-spa/asset-url.ts +0 -12
  141. data/templates/tool_angular/src/single-spa/single-spa-props.ts +0 -8
  142. data/templates/tool_angular/src/styles.scss +0 -1
  143. data/templates/tool_angular/tsconfig.app.json +0 -13
  144. data/templates/tool_angular/tsconfig.json +0 -33
  145. data/templates/tool_angular/tsconfig.spec.json +0 -14
  146. data/templates/tool_react/.eslintrc +0 -7
  147. data/templates/tool_react/.gitignore +0 -71
  148. data/templates/tool_react/.prettierignore +0 -8
  149. data/templates/tool_react/babel.config.json +0 -29
  150. data/templates/tool_react/jest.config.js +0 -12
  151. data/templates/tool_react/package.json +0 -51
  152. data/templates/tool_react/src/openc3-tool_name.js +0 -24
  153. data/templates/tool_react/src/root.component.js +0 -88
  154. data/templates/tool_react/src/root.component.test.js +0 -9
  155. data/templates/tool_react/webpack.config.js +0 -27
  156. data/templates/tool_svelte/.gitignore +0 -71
  157. data/templates/tool_svelte/.prettierignore +0 -8
  158. data/templates/tool_svelte/.prettierrc.js +0 -5
  159. data/templates/tool_svelte/babel.config.js +0 -12
  160. data/templates/tool_svelte/build/smui.css +0 -1
  161. data/templates/tool_svelte/jest.config.js +0 -9
  162. data/templates/tool_svelte/package.json +0 -49
  163. data/templates/tool_svelte/pnpm-workspace.yaml +0 -20
  164. data/templates/tool_svelte/rollup.config.js +0 -74
  165. data/templates/tool_svelte/src/App.svelte +0 -42
  166. data/templates/tool_svelte/src/App.test.js +0 -9
  167. data/templates/tool_svelte/src/theme/_smui-theme.scss +0 -25
  168. data/templates/tool_svelte/src/tool_name.js +0 -17
  169. data/templates/widget/LICENSE.md +0 -20
@@ -25,31 +25,185 @@ module OpenC3
25
25
  # While this isn't a clean separation of topics (streams) and models (key-value)
26
26
  # it helps maintain consistency as the topic and model are linked.
27
27
  class LimitsEventTopic < Topic
28
+ LIMITS_GROUP_PACKET = 'LIMITS_GROUP'.freeze
29
+
30
+ # Limits groups are stored in <SCOPE>__limits_groups under <TARGET>__<GROUP>.
31
+ # Group names can't contain a double underscore so the key is unambiguous.
32
+ #
33
+ # @return [String] The store key of the group
34
+ def self.limits_group_key(target_name, group_name)
35
+ "#{target_name}__#{group_name}"
36
+ end
37
+
38
+ # @return [Array(String, String)] The target name and group name of a store key.
39
+ # Groups stored before they were owned by a target have no target name.
40
+ def self.split_limits_group_key(key)
41
+ target_name, group_name = key.split('__', 2)
42
+ return [nil, key] unless group_name
43
+
44
+ [target_name, group_name]
45
+ end
46
+
47
+ # Finds the store key of a limits group from the arguments passed to the
48
+ # limits group APIs: either a target name and group name or just a group name.
49
+ # A group name alone must only be defined by one target. It can also be the
50
+ # TARGET__GROUP key returned by get_limits_groups for a name several targets share.
51
+ #
52
+ # @param target_name [String, nil] Target which defines the group
53
+ # @param group_name [String] Group name
54
+ # @return [String] The store key of the group
55
+ def self.find_limits_group(target_name, group_name, scope:)
56
+ group_name = group_name.to_s.upcase
57
+ keys = Store.hkeys("#{scope}__limits_groups")
58
+ if target_name
59
+ target_name = target_name.to_s.upcase
60
+ key = limits_group_key(target_name, group_name)
61
+ return key if keys.include?(key)
62
+
63
+ raise "LIMITS_GROUP #{target_name} #{group_name} undefined. Ensure target #{target_name} contains the line: LIMITS_GROUP #{group_name}"
64
+ end
65
+
66
+ # An exact match is a TARGET__GROUP key or a group stored before groups were owned by a target
67
+ return group_name if keys.include?(group_name)
68
+
69
+ matches = keys.select { |key| split_limits_group_key(key)[1] == group_name }
70
+ case matches.length
71
+ when 0
72
+ raise "LIMITS_GROUP #{group_name} undefined. Ensure your telemetry definition contains the line: LIMITS_GROUP #{group_name}"
73
+ when 1
74
+ matches[0]
75
+ else
76
+ targets = matches.map { |key| split_limits_group_key(key)[0] }.sort
77
+ raise "LIMITS_GROUP #{group_name} is defined by targets #{targets.join(', ')}. Specify the target name and the group name, e.g. ('#{targets[0]}', '#{group_name}')."
78
+ end
79
+ end
80
+
81
+ # @return [String, nil] The store key of the group named by a limits group event.
82
+ # Events without a target name (written before groups were owned by a target)
83
+ # name a group defined by only one target.
84
+ def self.limits_group_event_key(event, scope:)
85
+ target_name = event[:target_name] || event['target_name']
86
+ group_name = event[:group_name] || event['group_name']
87
+ return limits_group_key(target_name, group_name) if target_name
88
+
89
+ begin
90
+ find_limits_group(nil, group_name, scope: scope)
91
+ rescue RuntimeError
92
+ nil
93
+ end
94
+ end
95
+
96
+ # @return [Boolean] Whether a limits group event applies to the items of nested
97
+ # groups. Only events which say otherwise (cascade: false) don't.
98
+ def self.limits_group_event_cascade?(event)
99
+ cascade = event.key?(:cascade) ? event[:cascade] : event['cascade']
100
+ cascade != false
101
+ end
102
+
28
103
  # Returns the items in a limits group grouped by target and packet, or nil if the
29
104
  # group is undefined. Reads the store directly to avoid a circular reference to TargetModel.
105
+ #
106
+ # @param key [String] Store key of the group (see limits_group_key)
107
+ # @param cascade [Boolean] Whether to include the items of nested groups
30
108
  # @return [Hash{String => Hash{String => Array<String>}}, nil]
31
- def self.limits_group_items(group_name, scope:)
32
- json = Store.hget("#{scope}__limits_groups", group_name)
33
- return nil unless json
109
+ def self.limits_group_items(key, cascade: true, scope:)
110
+ return nil unless key
111
+
112
+ groups = Store.hgetall("#{scope}__limits_groups")
113
+ return nil unless groups[key]
34
114
 
35
115
  items = {}
116
+ _expand_limits_group(key, groups, items, Set.new, Set.new, cascade)
117
+ items
118
+ end
119
+
120
+ # Returns the store keys of a group and, with cascade, of every defined group
121
+ # nested below it. These are the groups whose own enabled flag a
122
+ # LIMITS_GROUP_ENABLE_STATE event sets or clears (see limits_group_settings).
123
+ #
124
+ # @param key [String] Store key of the group (see limits_group_key)
125
+ # @param cascade [Boolean] Whether to include the nested groups
126
+ # @return [Array<String>] Group keys, empty if the group is undefined
127
+ def self.limits_group_keys(key, cascade: true, scope:)
128
+ return [] unless key
129
+
130
+ groups = Store.hgetall("#{scope}__limits_groups")
131
+ return [] unless groups[key]
132
+ return [key] unless cascade
133
+
134
+ visited = Set.new
135
+ _expand_limits_group(key, groups, {}, visited, Set.new, true)
136
+ visited.select { |group| groups[group] }
137
+ end
138
+
139
+ # A group can be disabled itself (by disable_limits_group with cascade false)
140
+ # independent of its members. Groups without a setting are enabled.
141
+ #
142
+ # @return [Hash{String => Hash}] Settings keyed by group store key, e.g. { 'enabled' => false }
143
+ def self.limits_group_settings(scope:)
144
+ Store.hgetall("#{scope}__limits_group_settings").transform_values do |json|
145
+ JSON.parse(json, allow_nan: true, create_additions: true)
146
+ end
147
+ end
148
+
149
+ # Nested groups are members named <TARGET> LIMITS_GROUP <GROUP>. They have no limits
150
+ # of their own so expand them into the items of the child group, unless cascade
151
+ # is false in which case they're skipped. Children which aren't defined (yet) are
152
+ # ignored and each group is only expanded once which also guards against cycles.
153
+ #
154
+ # @param seen [Set<Array(String, String, String)>] Items already added
155
+ def self._expand_limits_group(key, groups, items, visited, seen, cascade = true)
156
+ return if visited.include?(key)
157
+
158
+ visited << key
159
+ json = groups[key]
160
+ return unless json
161
+
36
162
  JSON.parse(json, allow_nan: true, create_additions: true).each do |target_name, packet_name, item_name|
37
- ((items[target_name] ||= {})[packet_name] ||= []) << item_name
163
+ if packet_name == LIMITS_GROUP_PACKET
164
+ _expand_limits_group(limits_group_key(target_name, item_name), groups, items, visited, seen, cascade) if cascade
165
+ # A Set rather than include? on the item list since groups can contain many thousands of items
166
+ elsif seen.add?([target_name, packet_name, item_name])
167
+ ((items[target_name] ||= {})[packet_name] ||= []) << item_name
168
+ end
38
169
  end
39
- items
40
170
  end
41
171
 
172
+ # How long the set of active db_shards is cached. Every limits event is
173
+ # written to every active db_shard, and looking them up reads and parses every
174
+ # target, which is too slow to do per event when many items change state.
175
+ # Only LIMITS_CHANGE events use the cache, so a target added on a new db_shard
176
+ # gets them within this time. Decom microservices ignore LIMITS_CHANGE events,
177
+ # and the events that change their limits always look up the current db_shards.
178
+ ACTIVE_DB_SHARDS_CACHE_TIMEOUT = 10 # seconds
179
+ @active_db_shards_cache = {}
180
+
42
181
  # Collect all unique target db_shards from TargetModel
43
- def self._active_db_shards(scope:)
182
+ # @param use_cache [Boolean] Whether to return the cached db_shards if they haven't timed out.
183
+ # The db_shards are cached either way.
184
+ def self._active_db_shards(scope:, use_cache: true)
185
+ now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
186
+ cached = @active_db_shards_cache[scope]
187
+ return cached[1] if use_cache and cached and now < cached[0]
188
+
44
189
  db_shards = Set.new([0])
45
190
  Store.hgetall("#{scope}__openc3_targets").each do |_name, json|
46
191
  parsed = JSON.parse(json, allow_nan: true, create_additions: true)
47
192
  db_shards << (parsed['db_shard'] || 0).to_i
48
193
  end
194
+ db_shards.freeze
195
+ @active_db_shards_cache[scope] = [now + ACTIVE_DB_SHARDS_CACHE_TIMEOUT, db_shards]
49
196
  db_shards
50
197
  end
51
198
 
52
- def self.write(event, scope:)
199
+ def self.clear_active_db_shards_cache
200
+ @active_db_shards_cache = {}
201
+ end
202
+
203
+ # @param publish [Boolean] Whether to write the event to the limits events stream.
204
+ # When false only the store is updated, which a decom microservice uses while
205
+ # resyncing so the resync can't flood the stream it is recovering from.
206
+ def self.write(event, scope:, publish: true)
53
207
  case event[:type]
54
208
  when :LIMITS_CHANGE
55
209
  # The current_limits hash keeps only the current limits state of items
@@ -98,11 +252,26 @@ module OpenC3
98
252
  end
99
253
  limits_settings['enabled'] = event[:enabled]
100
254
  Store.hset("#{scope}__current_limits_settings", field, JSON.generate(limits_settings, allow_nan: true))
255
+ clear_disabled_current_limits([field], scope: scope) unless ConfigParser.handle_true_false_nil(event[:enabled])
101
256
 
102
257
  when :LIMITS_GROUP_ENABLE_STATE
103
258
  # Groups can contain many thousands of items so read and write the settings in bulk
104
259
  fields = []
105
- (limits_group_items(event[:group_name], scope: scope) || {}).each do |target_name, packets|
260
+ key = limits_group_event_key(event, scope: scope)
261
+ cascade = limits_group_event_cascade?(event)
262
+ # Only disabling with cascade false disables the group itself, so a group of
263
+ # only nested groups can be disabled on its own. Otherwise the group, and with
264
+ # cascade every group below it, follows its members again: with cascade they're
265
+ # all changed too, and a later change to any member then shows in the group.
266
+ group_keys = limits_group_keys(key, cascade: cascade, scope: scope)
267
+ unless group_keys.empty?
268
+ if !cascade and !event[:enabled]
269
+ Store.hset("#{scope}__limits_group_settings", key, JSON.generate({ 'enabled' => false }, allow_nan: true))
270
+ else
271
+ Store.hdel("#{scope}__limits_group_settings", *group_keys)
272
+ end
273
+ end
274
+ (limits_group_items(key, cascade: cascade, scope: scope) || {}).each do |target_name, packets|
106
275
  packets.each do |packet_name, item_names|
107
276
  item_names.each { |item_name| fields << "#{target_name}__#{packet_name}__#{item_name}" }
108
277
  end
@@ -121,6 +290,7 @@ module OpenC3
121
290
  updates[field] = JSON.generate(limits_settings, allow_nan: true)
122
291
  end
123
292
  Store.hmset("#{scope}__current_limits_settings", *updates.flatten)
293
+ clear_disabled_current_limits(fields, scope: scope) unless ConfigParser.handle_true_false_nil(event[:enabled])
124
294
  end
125
295
 
126
296
  when :LIMITS_STATE_COLOR
@@ -133,7 +303,8 @@ module OpenC3
133
303
  limits_settings = {}
134
304
  end
135
305
  limits_settings['state_colors'] ||= {}
136
- limits_settings['state_colors'][event[:state_name].to_s] = event[:color].to_s
306
+ # A cleared color is saved as nil (not "") so sync_system clears it
307
+ limits_settings['state_colors'][event[:state_name].to_s] = event[:color]&.to_s
137
308
  Store.hset("#{scope}__current_limits_settings", field, JSON.generate(limits_settings, allow_nan: true))
138
309
 
139
310
  when :LIMITS_SET
@@ -148,9 +319,13 @@ module OpenC3
148
319
  raise "Invalid limits event type '#{event[:type]}'"
149
320
  end
150
321
 
151
- # Write to all active db_shards so each decom microservice can read limits events inline
322
+ return unless publish
323
+
324
+ # Write to all active db_shards so each decom microservice can read limits events inline.
325
+ # Only LIMITS_CHANGE events are frequent enough to need the cache. The rest change
326
+ # decom limits so they must reach a decom on a db_shard that was just added.
152
327
  msg_hash = { event: JSON.generate(event, allow_nan: true) }
153
- _active_db_shards(scope: scope).each do |db_shard|
328
+ _active_db_shards(scope: scope, use_cache: event[:type] == :LIMITS_CHANGE).each do |db_shard|
154
329
  Topic.write_topic("#{scope}__openc3_limits_events", msg_hash, '*', 1000, db_shard: db_shard)
155
330
  end
156
331
  end
@@ -229,6 +404,13 @@ module OpenC3
229
404
  end
230
405
  end
231
406
  end
407
+
408
+ # Removing the target removes its groups so a reinstalled target, or a new
409
+ # group with the same name, doesn't start disabled
410
+ unless packet_name
411
+ groups = Store.hkeys("#{scope}__limits_group_settings").select { |group| group.start_with?("#{target_name}__") }
412
+ Store.hdel("#{scope}__limits_group_settings", *groups) unless groups.empty?
413
+ end
232
414
  end
233
415
 
234
416
  # Removes a limits set from the limits_sets hash and from the
@@ -268,15 +450,20 @@ module OpenC3
268
450
  end
269
451
 
270
452
  # Reload all limits state from the store, used to recover from missed limits events
271
- def self.resync_system(scope:)
453
+ # @param telemetry [Hash] Packets by target to update, default all in System
454
+ def self.resync_system(scope:, telemetry: nil)
272
455
  System.limits_set = current_set(scope: scope)
273
- sync_system(scope: scope)
456
+ sync_system(scope: scope, telemetry: telemetry)
274
457
  end
275
458
 
276
459
  # Update the local System based on overall state
277
- def self.sync_system(scope:)
460
+ # @param telemetry [Hash] Packets by target to update, default all in System.
461
+ # Decom microservices sharing a process pass just their own targets because
462
+ # disabling limits calls the target's limits change callback every time.
463
+ def self.sync_system(scope:, telemetry: nil)
278
464
  all_limits_settings = Store.hgetall("#{scope}__current_limits_settings")
279
- telemetry = System.telemetry.all
465
+ telemetry ||= System.telemetry.all
466
+ disabled_fields = []
280
467
  all_limits_settings.each do |item, limits_settings|
281
468
  target_name, packet_name, item_name = item.split('__')
282
469
  target = telemetry[target_name]
@@ -286,14 +473,24 @@ module OpenC3
286
473
  limits_settings = JSON.parse(limits_settings, allow_nan: true, create_additions: true)
287
474
  enabled = limits_settings['enabled']
288
475
  persistence = limits_settings['persistence_setting']
476
+ # Items with change limits (SEQUENCE, CHANGE, STATIC) don't have numeric limits. Saved
477
+ # numeric settings can be left over from before the item definition changed.
478
+ packet_item = packet.items[item_name]
479
+ change_type = packet_item&.limits&.change_type
289
480
  limits_settings.each do |limits_set, settings|
290
481
  next unless Hash === settings
291
482
  if limits_set == 'state_colors'
292
483
  settings.each do |state_name, color|
484
+ # Earlier versions saved a cleared color as "" so treat it as cleared
485
+ color = nil if color.to_s.empty?
293
486
  System.limits.set_state_color(target_name, packet_name, item_name, state_name, color)
294
487
  end
295
488
  next
296
489
  end
490
+ if change_type
491
+ Logger.warn("Ignoring saved #{limits_set} limits settings for #{target_name} #{packet_name} #{item_name} which has #{change_type} limits")
492
+ next
493
+ end
297
494
  System.limits.set(target_name, packet_name, item_name, settings['red_low'], settings['yellow_low'], settings['yellow_high'], settings['red_high'], settings['green_low'], settings['green_high'], limits_set.to_s.intern, persistence, enabled)
298
495
  end
299
496
  if not enabled.nil?
@@ -301,11 +498,29 @@ module OpenC3
301
498
  System.limits.enable(target_name, packet_name, item_name)
302
499
  else
303
500
  System.limits.disable(target_name, packet_name, item_name)
501
+ disabled_fields << item
304
502
  end
305
503
  end
306
504
  end
307
505
  end
308
506
  end
507
+ clear_disabled_current_limits(disabled_fields, scope: scope)
508
+ end
509
+
510
+ # Clear any limits state left in current_limits for disabled items in one pass.
511
+ # Disabling an item only publishes a LIMITS_CHANGE (which clears its state) when
512
+ # the decom microservice has a local state for it. After a decom restart the local
513
+ # state is nil until the next packet, while current_limits can still hold the state
514
+ # from before the restart. An item disabled while the decom wasn't running is the same.
515
+ def self.clear_disabled_current_limits(fields, scope:)
516
+ return if fields.empty?
517
+
518
+ states = Store.hmget("#{scope}__current_limits", *fields)
519
+ updates = []
520
+ fields.each_with_index do |field, index|
521
+ updates.push(field, '') if states[index] and !states[index].empty?
522
+ end
523
+ Store.hmset("#{scope}__current_limits", *updates) unless updates.empty?
309
524
  end
310
525
 
311
526
  # Process a single limits event hash and update the local System accordingly.
@@ -350,7 +565,8 @@ module OpenC3
350
565
 
351
566
  when 'LIMITS_GROUP_ENABLE_STATE'
352
567
  enabled = ConfigParser.handle_true_false_nil(event['enabled'])
353
- (limits_group_items(event['group_name'], scope: scope) || {}).each do |target_name, packets|
568
+ key = limits_group_event_key(event, scope: scope)
569
+ (limits_group_items(key, cascade: limits_group_event_cascade?(event), scope: scope) || {}).each do |target_name, packets|
354
570
  target = telemetry[target_name]
355
571
  next unless target
356
572
 
@@ -389,7 +605,10 @@ module OpenC3
389
605
  end
390
606
 
391
607
  when 'LIMITS_SET'
392
- System.limits_set = event['set']
608
+ # Read the current set rather than the event's set. Decoms sharing a process
609
+ # share System.limits_set, so a decom still catching up on old events must
610
+ # not switch the others back to an obsolete set.
611
+ System.limits_set = current_set(scope: scope)
393
612
  end
394
613
  end
395
614
 
@@ -16,11 +16,14 @@
16
16
  # if purchased from OpenC3, Inc.
17
17
 
18
18
  require 'openc3/topics/topic'
19
+ require 'openc3/utilities/store_queued'
19
20
  require 'openc3/utilities/open_telemetry'
20
21
 
21
22
  module OpenC3
22
23
  class TelemetryDecomTopic < Topic
23
- def self.write_packet(packet, id: nil, include_limits_states: true, scope:)
24
+ # @param queued [Boolean] Queue the stream and CVT writes to be flushed in a pipelined
25
+ # batch (EphemeralStoreQueued / StoreQueued) rather than writing them directly
26
+ def self.write_packet(packet, id: nil, include_limits_states: true, queued: false, scope:)
24
27
  OpenC3.in_span("write_packet") do
25
28
  # Need to build a JSON hash of the decommutated data
26
29
  # Support "downward typing"
@@ -45,12 +48,18 @@ module OpenC3
45
48
  }
46
49
  msg_hash[:extra] = JSON.generate(packet.extra.as_json, allow_nan: true) if packet.extra
47
50
  db_shard = Store.db_shard_for_target(packet.target_name, scope: scope)
48
- Topic.write_topic("#{scope}__DECOM__{#{packet.target_name}}__#{packet.packet_name}", msg_hash, id, minid: Topic.stream_safety_minid(id), db_shard: db_shard)
51
+ topic = "#{scope}__DECOM__{#{packet.target_name}}__#{packet.packet_name}"
52
+ minid = Topic.stream_safety_minid(id)
53
+ if queued
54
+ EphemeralStoreQueued.instance(db_shard: db_shard).write_topic(topic, msg_hash, id, minid: minid)
55
+ else
56
+ Topic.write_topic(topic, msg_hash, id, minid: minid, db_shard: db_shard)
57
+ end
49
58
 
50
59
  unless packet.stored
51
60
  # Also update the current value table with the latest decommutated data
52
61
  # Pass pre-serialized JSON to avoid calling as_json twice
53
- CvtModel.set_json(json_data, json_safe_hash, target_name: packet.target_name, packet_name: packet.packet_name, scope: scope)
62
+ CvtModel.set_json(json_data, json_safe_hash, target_name: packet.target_name, packet_name: packet.packet_name, queued: queued, scope: scope)
54
63
  end
55
64
  end
56
65
  end
@@ -224,7 +224,8 @@ module OpenC3
224
224
  # Extract the target name from the {TARGET} hash tag in a topic like
225
225
  # DEFAULT__TELEMETRY__{INST}__HEALTH_STATUS. Returns nil if there is none.
226
226
  def self.target_name_from_topic(topic)
227
- match = topic.match(/__\{([^}]+)\}__/)
227
+ # [^{}] (not [^}]) keeps this linear: target names never contain braces
228
+ match = topic.match(/__\{([^{}]+)\}__/)
228
229
  match ? match[1] : nil
229
230
  end
230
231
 
@@ -117,6 +117,7 @@ module OpenC3
117
117
  # "scope": "openid email profile"
118
118
  # }
119
119
 
120
+ FORM_URLENCODED_CONTENT_TYPE = 'application/x-www-form-urlencoded'
120
121
  REFRESH_OFFSET_SECONDS = 60
121
122
 
122
123
  attr_reader :refresh_token
@@ -131,6 +132,11 @@ module OpenC3
131
132
  @token = nil
132
133
  @log = [nil, nil]
133
134
  @http = Faraday.new
135
+ # Capture the credentials now: script processes remove OPENC3_API_PASSWORD
136
+ # from ENV (ScriptEnv.clear_credentials) before user code runs, which is
137
+ # before the first token request and any later re-login.
138
+ @api_user = ENV.fetch('OPENC3_API_USER', nil)
139
+ @api_password = ENV.fetch('OPENC3_API_PASSWORD', nil)
134
140
  end
135
141
 
136
142
  # Load the token from the environment
@@ -138,7 +144,11 @@ module OpenC3
138
144
  @auth_mutex.synchronize do
139
145
  @log = [nil, nil]
140
146
  current_time = Time.now.to_i
141
- if @token.nil?
147
+ if client_secret
148
+ if @token.nil? || @expires_at.nil? || @expires_at < current_time
149
+ _make_token(current_time, openid_scope: openid_scope)
150
+ end
151
+ elsif @token.nil?
142
152
  _make_token(current_time, openid_scope: openid_scope)
143
153
  elsif @refresh_expires_at < current_time
144
154
  _make_token(current_time, openid_scope: openid_scope)
@@ -166,20 +176,59 @@ module OpenC3
166
176
 
167
177
  private
168
178
 
179
+ def client_secret
180
+ secret = ENV.fetch('OPENC3_API_CLIENT_SECRET', nil)
181
+ return nil if secret.nil? || secret.empty?
182
+ secret
183
+ end
184
+
185
+ # Credentials captured at initialize, falling back to ENV when they were set later
186
+ def api_user
187
+ @api_user || ENV.fetch('OPENC3_API_USER', nil)
188
+ end
189
+
190
+ def api_password
191
+ @api_password || ENV.fetch('OPENC3_API_PASSWORD', nil)
192
+ end
193
+
194
+ def redacted_log_data(data)
195
+ log_data = data.dup
196
+ %w[password client_secret refresh_token].each do |secret_key|
197
+ log_data[secret_key] = '***' if log_data.key?(secret_key)
198
+ end
199
+ log_data.inspect
200
+ end
201
+
169
202
  # Make the token and save token to instance
170
203
  def _make_token(current_time, openid_scope: 'openid')
171
204
  client_id = ENV['OPENC3_API_CLIENT'] || 'api'
172
- if ENV['OPENC3_API_USER'] and ENV['OPENC3_API_PASSWORD']
205
+ if client_secret
206
+ data = {
207
+ 'client_id' => client_id,
208
+ 'client_secret' => client_secret,
209
+ 'grant_type' => 'client_credentials',
210
+ 'scope' => openid_scope
211
+ }
212
+ headers = {
213
+ 'Content-Type' => FORM_URLENCODED_CONTENT_TYPE,
214
+ 'User-Agent' => "OpenC3KeycloakAuthorization / #{OPENC3_VERSION} (ruby/openc3/lib/utilities/authentication)",
215
+ }
216
+ oath = _make_request(headers, data)
217
+ @token = oath['access_token']
218
+ @refresh_token = nil
219
+ @expires_at = current_time + oath['expires_in'] - REFRESH_OFFSET_SECONDS
220
+ @refresh_expires_at = nil
221
+ elsif api_user and api_password
173
222
  # Username and password
174
223
  data = {
175
- 'username' => ENV.fetch('OPENC3_API_USER', nil),
176
- 'password' => ENV.fetch('OPENC3_API_PASSWORD', nil),
224
+ 'username' => api_user,
225
+ 'password' => api_password,
177
226
  'client_id' => client_id,
178
227
  'grant_type' => 'password',
179
228
  'scope' => openid_scope
180
229
  }
181
230
  headers = {
182
- 'Content-Type' => 'application/x-www-form-urlencoded',
231
+ 'Content-Type' => FORM_URLENCODED_CONTENT_TYPE,
183
232
  'User-Agent' => "OpenC3KeycloakAuthorization / #{OPENC3_VERSION} (ruby/openc3/lib/utilities/authentication)",
184
233
  }
185
234
  oath = _make_request(headers, data)
@@ -203,7 +252,7 @@ module OpenC3
203
252
  'grant_type' => 'refresh_token'
204
253
  }
205
254
  headers = {
206
- 'Content-Type' => 'application/x-www-form-urlencoded',
255
+ 'Content-Type' => FORM_URLENCODED_CONTENT_TYPE,
207
256
  'User-Agent' => "OpenC3KeycloakAuthorization / #{OPENC3_VERSION} (ruby/openc3/lib/utilities/authentication)",
208
257
  }
209
258
  oath = _make_request(headers, data)
@@ -217,16 +266,8 @@ module OpenC3
217
266
  def _make_request(headers, data)
218
267
  realm = ENV['OPENC3_KEYCLOAK_REALM'] || 'openc3'
219
268
  uri = URI("#{@url}/realms/#{realm}/protocol/openid-connect/token")
220
- # Obfuscate password and refresh token in logs unless debug mode is enabled
221
- if JsonDRb.debug?
222
- log_data = data.inspect
223
- else
224
- # Create a copy of the hash with sensitive values obfuscated
225
- log_data = data.dup
226
- log_data['password'] = '***' if log_data.key?('password')
227
- log_data['refresh_token'] = '***' if log_data.key?('refresh_token')
228
- log_data = log_data.inspect
229
- end
269
+ # Obfuscate password, client secret, and refresh token before logging.
270
+ log_data = redacted_log_data(data)
230
271
  @log[0] = "request uri: #{uri} header: #{headers} body: #{log_data}"
231
272
  STDOUT.puts @log[0] if JsonDRb.debug?
232
273
  saved_verbose = $VERBOSE; $VERBOSE = nil
@@ -246,4 +287,14 @@ module OpenC3
246
287
  end
247
288
  end
248
289
  end
290
+
291
+ # Build the appropriate authentication object from standard environment variables.
292
+ def self.authentication_from_environment
293
+ client_secret = ENV.fetch('OPENC3_API_CLIENT_SECRET', nil)
294
+ if ENV['OPENC3_API_TOKEN'] || ENV['OPENC3_API_USER'] || (client_secret && !client_secret.empty?)
295
+ OpenC3KeycloakAuthentication.new(ENV['OPENC3_KEYCLOAK_URL'])
296
+ elsif ENV['OPENC3_API_PASSWORD']
297
+ OpenC3Authentication.new()
298
+ end
299
+ end
249
300
  end