Skip to content

Commit 0f6e17e

Browse files
authored
Merge pull request #1138 from OpenC3/disabled
Implement DISABLED for commands
2 parents ddfb0ca + 2f20988 commit 0f6e17e

File tree

21 files changed

+355
-69
lines changed

21 files changed

+355
-69
lines changed

openc3-cosmos-cmd-tlm-api/app/controllers/auth_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
class AuthController < ApplicationController
2727
def token_exists
28-
result = OpenC3::AuthModel.is_set?
28+
result = OpenC3::AuthModel.set?
2929
render :json => {
3030
result: result
3131
}

openc3/lib/openc3/api/cmd_api.rb

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# GNU Affero General Public License for more details.
1515

1616
# Modified by OpenC3, Inc.
17-
# All changes Copyright 2022, OpenC3, Inc.
17+
# All changes Copyright 2024, OpenC3, Inc.
1818
# All Rights Reserved
1919
#
2020
# This file may also be used under the terms of a commercial license
@@ -41,6 +41,8 @@ module Api
4141
'cmd_raw_no_checks',
4242
'build_cmd',
4343
'build_command', # DEPRECATED
44+
'enable_cmd',
45+
'disable_cmd',
4446
'send_raw',
4547
'get_all_cmds',
4648
'get_all_commands', # DEPRECATED
@@ -131,6 +133,30 @@ def build_cmd(*args, range_check: true, raw: false, scope: $openc3_scope, token:
131133
# build_command is DEPRECATED
132134
alias build_command build_cmd
133135

136+
# Helper method for disable_cmd / enable_cmd
137+
def _get_and_set_cmd(method, *args, scope: $openc3_scope, token: $openc3_token)
138+
target_name, command_name = _extract_target_command_names(method, *args)
139+
authorize(permission: 'admin', target_name: target_name, packet_name: command_name, scope: scope, token: token)
140+
command = yield TargetModel.packet(target_name, command_name, type: :CMD, scope: scope)
141+
TargetModel.set_packet(target_name, command_name, command, type: :CMD, scope: scope)
142+
end
143+
144+
# @since 5.15.1
145+
def enable_cmd(*args, scope: $openc3_scope, token: $openc3_token)
146+
_get_and_set_cmd('enable_cmd', *args, scope: scope, token: token) do |command|
147+
command['disabled'] = false
148+
command
149+
end
150+
end
151+
152+
# @since 5.15.1
153+
def disable_cmd(*args, scope: $openc3_scope, token: $openc3_token)
154+
_get_and_set_cmd('disable_cmd', *args, scope: scope, token: token) do |command|
155+
command['disabled'] = true
156+
command
157+
end
158+
end
159+
134160
# Send a raw binary string to the specified interface.
135161
#
136162
# @param interface_name [String] The interface to send the raw binary
@@ -456,6 +482,12 @@ def _cmd_implementation(method_name, *args, range_check:, hazardous_check:, raw:
456482
cmd_params = cmd_params.transform_keys(&:upcase)
457483
authorize(permission: 'cmd', target_name: target_name, packet_name: cmd_name, scope: scope, token: token)
458484
packet = TargetModel.packet(target_name, cmd_name, type: :CMD, scope: scope)
485+
if packet['disabled']
486+
error = DisabledError.new
487+
error.target_name = target_name
488+
error.cmd_name = cmd_name
489+
raise error
490+
end
459491

460492
command = {
461493
'target_name' => target_name,

openc3/lib/openc3/logs/packet_log_reader.rb

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# GNU Affero General Public License for more details.
1515

1616
# Modified by OpenC3, Inc.
17-
# All changes Copyright 2022, OpenC3, Inc.
17+
# All changes Copyright 2024, OpenC3, Inc.
1818
# All Rights Reserved
1919
#
2020
# This file may also be used under the terms of a commercial license
@@ -96,9 +96,9 @@ def open(filename)
9696
@max_read_size = @file.size
9797
@max_read_size = MAX_READ_SIZE if @max_read_size > MAX_READ_SIZE
9898
return read_file_header()
99-
rescue => err
99+
rescue => e
100100
close()
101-
raise err
101+
raise e
102102
end
103103

104104
# Closes the current log file
@@ -134,9 +134,8 @@ def read(identify_and_define = true)
134134

135135
if flags & OPENC3_ENTRY_TYPE_MASK == OPENC3_JSON_PACKET_ENTRY_TYPE_MASK
136136
packet_index, time_nsec_since_epoch = entry[2..11].unpack('nQ>')
137-
next_offset = 12
138137
received_time_nsec_since_epoch, extra, json_data = handle_received_time_extra_and_data(entry, time_nsec_since_epoch, includes_received_time, includes_extra, cbor)
139-
lookup_cmd_or_tlm, target_name, packet_name, id, key_map = @packets[packet_index]
138+
lookup_cmd_or_tlm, target_name, packet_name, _id, key_map = @packets[packet_index]
140139
if cmd_or_tlm != lookup_cmd_or_tlm
141140
raise "Packet type mismatch, packet:#{cmd_or_tlm}, lookup:#{lookup_cmd_or_tlm}"
142141
end
@@ -148,8 +147,8 @@ def read(identify_and_define = true)
148147
end
149148
elsif flags & OPENC3_ENTRY_TYPE_MASK == OPENC3_RAW_PACKET_ENTRY_TYPE_MASK
150149
packet_index, time_nsec_since_epoch = entry[2..11].unpack('nQ>')
151-
received_time_nsec_since_epoch, extra, packet_data = handle_received_time_extra_and_data(entry, time_nsec_since_epoch, includes_received_time, includes_extra, cbor)
152-
lookup_cmd_or_tlm, target_name, packet_name, id = @packets[packet_index]
150+
received_time_nsec_since_epoch, _extra, packet_data = handle_received_time_extra_and_data(entry, time_nsec_since_epoch, includes_received_time, includes_extra, cbor)
151+
lookup_cmd_or_tlm, target_name, packet_name, _id = @packets[packet_index]
153152
if cmd_or_tlm != lookup_cmd_or_tlm
154153
raise "Packet type mismatch, packet:#{cmd_or_tlm}, lookup:#{lookup_cmd_or_tlm}"
155154
end
@@ -220,9 +219,9 @@ def read(identify_and_define = true)
220219
else
221220
raise "Invalid Entry Flags: #{flags}"
222221
end
223-
rescue => err
222+
rescue => e
224223
close()
225-
raise err
224+
raise e
226225
end
227226

228227
# @return [Integer] The size of the log file being processed

openc3/lib/openc3/microservices/trigger_group_microservice.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# GNU Affero General Public License for more details.
1515

1616
# Modified by OpenC3, Inc.
17-
# All changes Copyright 2022, OpenC3, Inc.
17+
# All changes Copyright 2024, OpenC3, Inc.
1818
# All Rights Reserved
1919
#
2020
# This file may also be used under the terms of a commercial license
@@ -418,7 +418,7 @@ def evaluate(name:, left:, operator:, right:)
418418
when 'OR'
419419
return left || right ? 1 : 0
420420
end
421-
rescue ArgumentError => error
421+
rescue ArgumentError
422422
message = "invalid evaluate: (#{left} #{operator} #{right})"
423423
notify(name: name, severity: 'error', message: message)
424424
return -1
@@ -447,15 +447,15 @@ def evaluate_trigger(head:, trigger:, visited:, triggers:)
447447
if visited["#{head.name}__P"][trigger.name]
448448
# Not sure if this is posible as on create it validates that the dependents are already created
449449
message = "loop detected from #{head.name} -> #{trigger.name} path: #{visited["#{head.name}__P"]}"
450-
notify(name: trigger.name, severity: 'error', message: error.message)
450+
notify(name: trigger.name, severity: 'error', message: message)
451451
return visited["#{trigger.name}__R"] = -1
452452
end
453453
trigger.roots.each do | root_trigger_name |
454454
next if visited["#{root_trigger_name}__R"]
455455
root_trigger = triggers[root_trigger_name]
456456
if head.name == root_trigger.name
457457
message = "loop detected from #{head.name} -> #{root_trigger_name} path: #{visited["#{head.name}__P"]}"
458-
notify(name: trigger.name, severity: 'error', message: error.message)
458+
notify(name: trigger.name, severity: 'error', message: message)
459459
return visited["#{trigger.name}__R"] = -1
460460
end
461461
result = evaluate_trigger(
@@ -474,9 +474,9 @@ def evaluate_trigger(head:, trigger:, visited:, triggers:)
474474
else
475475
right = operand_value(operand: trigger.right, other: trigger.left, visited: visited)
476476
end
477-
rescue => error
477+
rescue => e
478478
# This will primarily happen when the user inputs a bad Regexp
479-
notify(name: trigger.name, severity: 'error', message: error.message)
479+
notify(name: trigger.name, severity: 'error', message: e.message)
480480
return visited["#{trigger.name}__R"] = -1
481481
end
482482
# Convert the standard '==' and '!=' into Ruby Regexp operators
@@ -560,7 +560,7 @@ def block_for_updates
560560
while @read_topic
561561
begin
562562
Topic.read_topics(@topics) do |topic, _msg_id, msg_hash, _redis|
563-
@logger.debug "TriggerGroupManager block_for_updates: #{topic} #{msg_hash.to_s}"
563+
@logger.debug "TriggerGroupManager block_for_updates: #{topic} #{msg_hash}"
564564
if topic != @share.trigger_base.autonomic_topic
565565
packet = JsonPacket.new(:TLM, msg_hash['target_name'], msg_hash['packet_name'], msg_hash['time'].to_i, false, msg_hash["json_data"])
566566
@share.packet_base.add(topic: topic, packet: packet)
@@ -580,7 +580,7 @@ def refresh
580580
def shutdown
581581
@read_topic = false
582582
@cancel_thread = true
583-
@worker_count.times do | i |
583+
@worker_count.times do | _i |
584584
@queue << nil
585585
end
586586
end
@@ -635,7 +635,7 @@ def block_for_updates
635635
begin
636636
AutonomicTopic.read_topics(@topics) do |_topic, _msg_id, msg_hash, _redis|
637637
break if @cancel_thread
638-
@logger.debug "TriggerGroupMicroservice block_for_updates: #{msg_hash.to_s}"
638+
@logger.debug "TriggerGroupMicroservice block_for_updates: #{msg_hash}"
639639
# Process trigger notifications created by TriggerModel notify
640640
if msg_hash['type'] == 'trigger'
641641
data = JSON.parse(msg_hash['data'], :allow_nan => true, :create_additions => true)

openc3/lib/openc3/models/auth_model.rb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# GNU Affero General Public License for more details.
1515

1616
# Modified by OpenC3, Inc.
17-
# All changes Copyright 2022, OpenC3, Inc.
17+
# All changes Copyright 2024, OpenC3, Inc.
1818
# All Rights Reserved
1919
#
2020
# This file may also be used under the terms of a commercial license
@@ -31,11 +31,11 @@ class AuthModel
3131
@@token_cache = nil
3232
@@token_cache_time = nil
3333

34-
def self.is_set?(key = PRIMARY_KEY)
34+
def self.set?(key = PRIMARY_KEY)
3535
Store.exists(key) == 1
3636
end
3737

38-
def self.verify(token, permission: nil)
38+
def self.verify(token)
3939
return false if token.nil? or token.empty?
4040

4141
token_hash = hash(token)
@@ -47,23 +47,21 @@ def self.verify(token, permission: nil)
4747

4848
# Handle a service password - Generally only used by ScriptRunner
4949
service_password = ENV['OPENC3_SERVICE_PASSWORD']
50-
return true if service_password and service_password == token and permission != 'admin'
50+
return true if service_password and service_password == token
5151

5252
return false
5353
end
5454

5555
def self.set(token, old_token, key = PRIMARY_KEY)
5656
raise "token must not be nil or empty" if token.nil? or token.empty?
5757

58-
if is_set?(key)
58+
if set?(key)
5959
raise "old_token must not be nil or empty" if old_token.nil? or old_token.empty?
6060
raise "old_token incorrect" unless verify(old_token)
6161
end
6262
Store.set(key, hash(token))
6363
end
6464

65-
private
66-
6765
def self.hash(token)
6866
Digest::SHA2.hexdigest token
6967
end

openc3/lib/openc3/script/commands.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# GNU Affero General Public License for more details.
1515

1616
# Modified by OpenC3, Inc.
17-
# All changes Copyright 2022, OpenC3, Inc.
17+
# All changes Copyright 2024, OpenC3, Inc.
1818
# All Rights Reserved
1919
#
2020
# This file may also be used under the terms of a commercial license
@@ -96,7 +96,7 @@ def _cmd_disconnect(cmd, raw, no_range, no_hazardous, *args, scope: $openc3_scop
9696

9797
# Get the command and validate the parameters
9898
command = $api_server.get_cmd(target_name, cmd_name, scope: scope)
99-
cmd_params.each do |param_name, param_value|
99+
cmd_params.each do |param_name, _param_value|
100100
param = command['items'].find { |item| item['name'] == param_name }
101101
unless param
102102
raise "Packet item '#{target_name} #{cmd_name} #{param_name}' does not exist"

openc3/lib/openc3/system/system.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# GNU Affero General Public License for more details.
1515

1616
# Modified by OpenC3, Inc.
17-
# All changes Copyright 2022, OpenC3, Inc.
17+
# All changes Copyright 2024, OpenC3, Inc.
1818
# All Rights Reserved
1919
#
2020
# This file may also be used under the terms of a commercial license
@@ -31,7 +31,6 @@
3131
require 'openc3/utilities/bucket'
3232
require 'openc3/utilities/zip'
3333
require 'openc3/topics/limits_event_topic'
34-
require 'thread'
3534
require 'fileutils'
3635

3736
module OpenC3
@@ -109,7 +108,7 @@ def self.setup_targets(target_names, base_dir, scope:)
109108
# target.txt must be configured to either use all files in cmd_tlm folder (default)
110109
# or have a predetermined empty file like dynamic_tlm.txt
111110
bucket_path = "#{scope}/targets_modified/#{target_name}/cmd_tlm"
112-
dirs, files = bucket.list_files(bucket: ENV['OPENC3_CONFIG_BUCKET'], path: bucket_path)
111+
_, files = bucket.list_files(bucket: ENV['OPENC3_CONFIG_BUCKET'], path: bucket_path)
113112
files.each do |file|
114113
bucket_key = File.join(bucket_path, file['name'])
115114
local_path = "#{base_dir}/targets/#{target_name}/cmd_tlm/#{file['name']}"
@@ -180,8 +179,8 @@ def add_target(target_name, target_config_dir)
180179
errors = [] # Store all errors processing the cmd_tlm files
181180
target.cmd_tlm_files.each do |cmd_tlm_file|
182181
@packet_config.process_file(cmd_tlm_file, target.name, target.language)
183-
rescue Exception => error
184-
errors << "Error processing #{cmd_tlm_file}:\n#{error.message}"
182+
rescue Exception => e
183+
errors << "Error processing #{cmd_tlm_file}:\n#{e.message}"
185184
end
186185
unless errors.empty?
187186
raise errors.join("\n")

0 commit comments

Comments
 (0)