openc2lib.core.target

OpenC2 Target

This module implements the Target types defined in Sec. 3.4.1 [OpenC2 Languate specification].

 1""" OpenC2 Target
 2
 3	This module implements the Target types defined in Sec. 3.4.1 [OpenC2 Languate specification].
 4"""
 5
 6import aenum
 7
 8from openc2lib.types.basetypes import Choice
 9from openc2lib.types.datatypes import TargetEnum
10from openc2lib.core.register import Register
11
12
13class TargetRegister(Register):
14	""" Target registration
15	
16		This class registers all available `Target`s, both provided by the openc2lib and by Profiles.
17		The extension of the base class `Register` is necessary to add the nsid prefix in front of the
18		`Target` name.
19	"""
20	
21	def add(self, name: str, target, identifier=None, nsid=None):
22		""" Add a new `Target`
23	
24			Register a new `Target` and make it available within the system. This method is expected to
25			be called by any `Profile` that defines additional `Target`s. Additionally, the name is added 
26			to the Target enumeration `TargetEnum`.
27			
28			This method throw an Exception if the `Target` is already registered.
29
30			:param name: The name used for the `Target`.
31			:param target: The class that defines the `Target`.
32			:param identifier: A numeric value associated to the standard by the Specification.
33			:param nsid: The Namespace Identifier where the `Target` is defined. It is prepended to the target `name`.
34			:return: None
35		"""
36		if nsid is not None:
37			name = nsid + ':' + name
38		try:
39			list(self.keys())[list(self.values()).index(target)]
40		except ValueError:
41			# The item is not in the list
42			self[name] = target
43			aenum.extend_enum(TargetEnum, name, identifier)
44			return
45		raise ValueError("Target already registered")
46
47Targets = TargetRegister()
48""" List of available `Target`s
49
50	Include base Targets defined by the Language Specification and additional Targets defined by Profiles.
51"""
52
53class Target(Choice):
54	""" OpenC2 Target in `Command`
55
56		This is the definition of the `target` carried in OpenC2 `Command`.
57	"""
58	register = Targets
59	""" Keeps the list of registered `Target`s """
60
61	def getName(self):
62		""" Returns the identifier associated to the Target type."""
63		return self.choice
class TargetRegister(openc2lib.core.register.Register):
14class TargetRegister(Register):
15	""" Target registration
16	
17		This class registers all available `Target`s, both provided by the openc2lib and by Profiles.
18		The extension of the base class `Register` is necessary to add the nsid prefix in front of the
19		`Target` name.
20	"""
21	
22	def add(self, name: str, target, identifier=None, nsid=None):
23		""" Add a new `Target`
24	
25			Register a new `Target` and make it available within the system. This method is expected to
26			be called by any `Profile` that defines additional `Target`s. Additionally, the name is added 
27			to the Target enumeration `TargetEnum`.
28			
29			This method throw an Exception if the `Target` is already registered.
30
31			:param name: The name used for the `Target`.
32			:param target: The class that defines the `Target`.
33			:param identifier: A numeric value associated to the standard by the Specification.
34			:param nsid: The Namespace Identifier where the `Target` is defined. It is prepended to the target `name`.
35			:return: None
36		"""
37		if nsid is not None:
38			name = nsid + ':' + name
39		try:
40			list(self.keys())[list(self.values()).index(target)]
41		except ValueError:
42			# The item is not in the list
43			self[name] = target
44			aenum.extend_enum(TargetEnum, name, identifier)
45			return
46		raise ValueError("Target already registered")

Target registration

This class registers all available Targets, both provided by the openc2lib and by Profiles. The extension of the base class Register is necessary to add the nsid prefix in front of the Target name.

def add(self, name: str, target, identifier=None, nsid=None):
22	def add(self, name: str, target, identifier=None, nsid=None):
23		""" Add a new `Target`
24	
25			Register a new `Target` and make it available within the system. This method is expected to
26			be called by any `Profile` that defines additional `Target`s. Additionally, the name is added 
27			to the Target enumeration `TargetEnum`.
28			
29			This method throw an Exception if the `Target` is already registered.
30
31			:param name: The name used for the `Target`.
32			:param target: The class that defines the `Target`.
33			:param identifier: A numeric value associated to the standard by the Specification.
34			:param nsid: The Namespace Identifier where the `Target` is defined. It is prepended to the target `name`.
35			:return: None
36		"""
37		if nsid is not None:
38			name = nsid + ':' + name
39		try:
40			list(self.keys())[list(self.values()).index(target)]
41		except ValueError:
42			# The item is not in the list
43			self[name] = target
44			aenum.extend_enum(TargetEnum, name, identifier)
45			return
46		raise ValueError("Target already registered")

Add a new Target

Register a new Target and make it available within the system. This method is expected to be called by any Profile that defines additional Targets. Additionally, the name is added to the Target enumeration TargetEnum.

This method throw an Exception if the Target is already registered.

Parameters
  • name: The name used for the Target.
  • target: The class that defines the Target.
  • identifier: A numeric value associated to the standard by the Specification.
  • nsid: The Namespace Identifier where the Target is defined. It is prepended to the target name.
Returns

None

Inherited Members
openc2lib.core.register.Register
get
getName
builtins.dict
setdefault
pop
popitem
keys
items
values
update
fromkeys
clear
copy
Targets = {'features': <class 'openc2lib.types.targettypes.Features'>, 'ipv4_net': <class 'openc2lib.types.targettypes.IPv4Net'>, 'ipv4_connection': <class 'openc2lib.types.targettypes.IPv4Connection'>, 'slpf:rule_number': <class 'openc2lib.profiles.slpf.targettypes.RuleID'>}

List of available Targets

Include base Targets defined by the Language Specification and additional Targets defined by Profiles.

class Target(openc2lib.types.basetypes.Choice):
54class Target(Choice):
55	""" OpenC2 Target in `Command`
56
57		This is the definition of the `target` carried in OpenC2 `Command`.
58	"""
59	register = Targets
60	""" Keeps the list of registered `Target`s """
61
62	def getName(self):
63		""" Returns the identifier associated to the Target type."""
64		return self.choice

OpenC2 Target in Command

This is the definition of the target carried in OpenC2 Command.

register = {'features': <class 'openc2lib.types.targettypes.Features'>, 'ipv4_net': <class 'openc2lib.types.targettypes.IPv4Net'>, 'ipv4_connection': <class 'openc2lib.types.targettypes.IPv4Connection'>, 'slpf:rule_number': <class 'openc2lib.profiles.slpf.targettypes.RuleID'>}

Keeps the list of registered Targets

def getName(self):
62	def getName(self):
63		""" Returns the identifier associated to the Target type."""
64		return self.choice

Returns the identifier associated to the Target type.