def validate_gbs_file(self, gbs_file): file = open(gbs_file, 'rb') gbs = file.read() if len(constants.METADATA_GUID) >= len(gbs): raise Exception("Invalid GBS file") self.guid = gbs[:constants.GUID_LEN] if self.guid != constants.METADATA_GUID: raise Exception("Unsupported GBS format") metadata_index = constants.GUID_LEN + constants.SIZEOF_LEN_FIELD metadata_len = struct.unpack("<I", gbs[constants.GUID_LEN:metadata_index]) self.metadata_len = metadata_len[0] if self.metadata_len != 0: self.gbs_info = json.loads(gbs[metadata_index:(metadata_index + self.metadata_len)]) rbf_index = metadata_index + metadata_len[0] if rbf_index == len(gbs): raise Exception("No RBF in GBS file!") self.rbf = gbs[rbf_index:] self.metadata = metadata.get_metadata(self.gbs_info)
def validate_gbs_file(self, gbs_file): file = open(gbs_file, 'rb') gbs = file.read() if len(constants.METADATA_GUID) >= len(gbs): raise Exception("Invalid GBS file") self.guid = gbs[:constants.GUID_LEN] if self.guid != constants.METADATA_GUID: raise Exception("Unsupported GBS format") metadata_index = constants.GUID_LEN + constants.SIZEOF_LEN_FIELD metadata_len = struct.unpack( "<I", gbs[constants.GUID_LEN:metadata_index]) self.metadata_len = metadata_len[0] if self.metadata_len != 0: self.gbs_info = json.loads( gbs[metadata_index:(metadata_index + self.metadata_len)]) rbf_index = metadata_index + metadata_len[0] if rbf_index == len(gbs): raise Exception("No RBF in GBS file!") self.rbf = gbs[rbf_index:] self.metadata = metadata.get_metadata(self.gbs_info)
def test_get_metadata(get_os, is_freebsd, is_linux, config_get, time_time, uuid_getnode, platform_node): res = { 'agentVersion': '0.99.99', 'apiKey': 'my_api_key', 'uuid': '9ef19c3f1a4c5493825695cd864dc2c3', 'internalHostname': 'test', 'os': 'test_os', 'python': sys.version, 'systemStats': { 'machine': platform.machine(), 'platform': sys.platform, 'processor': platform.processor(), 'pythonV': platform.python_version(), 'cpuCores': psutil.cpu_count(), }, 'meta': { 'socket-hostname': socket.gethostname(), 'timezones': time.tzname, 'host_aliases': [], 'socket-fqdn': socket.getfqdn(), 'hostname': 'test', }, 'host-tags': {'system': ['tag1', 'tag2']}, 'resources': { 'meta': {'host': 'test'}, 'processes': {'snaps': []}, }, 'events': {}, } assert res == metadata.get_metadata('test', '0.99.99') res['events'].update({ 'System': [ { 'api_key': 'my_api_key', 'event_type': 'Agent Startup', 'host': 'test', 'msg_text': 'Version 0.99.99', 'timestamp': 1234567890.12 } ] }) assert res == metadata.get_metadata('test', '0.99.99', start_event=True)
def create_gbs_from_afu_info(cls, rbf_file, afu_json): gbs = cls() rbf = open(rbf_file, 'rb') rbf_content = rbf.read() gbs.guid = constants.METADATA_GUID gbs.metadata_len = len(afu_json) gbs.gbs_info = afu_json gbs.rbf = rbf_content gbs.metadata = metadata.get_metadata(afu_json) gbs.filename = os.path.splitext(os.path.basename(rbf_file))[0] return gbs
def test_get_metadata(get_os, is_freebsd, is_linux, config_get, uuid_getnode, platform_node): res = { "agentVersion": "6.0.0", "apiKey": "my_api_key", "uuid": "9ef19c3f1a4c5493825695cd864dc2c3", "internalHostname": "test", "os": "test_os", "python": sys.version, "systemStats": { "machine": platform.machine(), "platform": sys.platform, "processor": platform.processor(), "pythonV": platform.python_version(), "cpuCores": psutil.cpu_count(), }, "meta": { "socket-hostname": socket.gethostname(), "timezones": time.tzname, "host_aliases": [], "socket-fqdn": socket.getfqdn(), "hostname": "test", }, "host-tags": { "system": ["tag1", "tag2"] }, "resources": { "meta": { "host": "test" }, "processes": { "snaps": [] }, }, } assert res == metadata.get_metadata("test")
def update_gbs_info(self, gbs_info): self.gbs_info = gbs_info self.metadata = metadata.get_metadata(self.gbs_info)