def _create_event(self, req): correlation_id = identifier.generate_uuid() action = self.get_action(req) initiator = ClientResource( typeURI=taxonomy.ACCOUNT_USER, id=req.environ.get('HTTP_X_USER_ID', taxonomy.UNKNOWN), name=req.environ.get('HTTP_X_USER_NAME', taxonomy.UNKNOWN), host=host.Host(address=req.client_addr, agent=req.user_agent), credential=KeystoneCredential( token=req.environ.get('HTTP_X_AUTH_TOKEN', ''), identity_status=req.environ.get('HTTP_X_IDENTITY_STATUS', taxonomy.UNKNOWN)), project_id=req.environ.get('HTTP_X_PROJECT_ID', taxonomy.UNKNOWN)) target = self.get_target_resource(req) event = factory.EventFactory().new_event( eventType=cadftype.EVENTTYPE_ACTIVITY, outcome=taxonomy.OUTCOME_PENDING, action=action, initiator=initiator, target=target, observer=resource.Resource(id='target')) event.requestPath = req.path_qs event.add_tag(tag.generate_name_value_tag('correlation_id', correlation_id)) return event
def _create_event(self, req): correlation_id = identifier.generate_uuid() action = self._cadf_audit.get_action(req) initiator = ClientResource( typeURI=taxonomy.ACCOUNT_USER, id=identifier.norm_ns(str(req.environ['HTTP_X_USER_ID'])), name=req.environ['HTTP_X_USER_NAME'], host=host.Host(address=req.client_addr, agent=req.user_agent), credential=KeystoneCredential( token=req.environ['HTTP_X_AUTH_TOKEN'], identity_status=req.environ['HTTP_X_IDENTITY_STATUS']), project_id=identifier.norm_ns(req.environ['HTTP_X_PROJECT_ID'])) target = self._cadf_audit.get_target_resource(req) event = factory.EventFactory().new_event( eventType=cadftype.EVENTTYPE_ACTIVITY, outcome=taxonomy.OUTCOME_PENDING, action=action, initiator=initiator, target=target, observer=resource.Resource(id='target')) event.requestPath = req.path_qs event.add_tag( tag.generate_name_value_tag('correlation_id', correlation_id)) # cache model in request to allow tracking of transistive steps. req.environ['cadf_event'] = event return event
def test_cadfabstracttype_attribute_error(self): """Test an invalid CADFAbstractType attribute is set returns False""" h = host.Host(id=identifier.generate_uuid(), address='192.168.0.1', agent='client', platform='AIX') self.assertEqual(False, h._isset(uuid.uuid4().hex))
def test_host(self): h = host.Host(id=identifier.generate_uuid(), address='192.168.0.1', agent='client', platform='AIX') self.assertEqual(h.is_valid(), True) dict_host = h.as_dict() for key in host.HOST_KEYNAMES: self.assertIn(key, dict_host)
def audit_initiator(self): """A pyCADF initiator describing the current authenticated context.""" pycadf_host = host.Host(address=flask.request.remote_addr, agent=str(flask.request.user_agent)) initiator = resource.Resource(typeURI=taxonomy.ACCOUNT_USER, host=pycadf_host) if self.oslo_context.user_id: initiator.id = utils.resource_uuid(self.oslo_context.user_id) initiator.user_id = self.oslo_context.user_id if self.oslo_context.project_id: initiator.project_id = self.oslo_context.project_id if self.oslo_context.domain_id: initiator.domain_id = self.oslo_context.domain_id return initiator
def build_audit_initiator(): """A pyCADF initiator describing the current authenticated context.""" pycadf_host = host.Host(address=flask.request.remote_addr, agent=str(flask.request.user_agent)) initiator = resource.Resource(typeURI=taxonomy.ACCOUNT_USER, host=pycadf_host) oslo_context = flask.request.environ.get(context.REQUEST_CONTEXT_ENV) if oslo_context.user_id: initiator.id = utils.resource_uuid(oslo_context.user_id) initiator.user_id = oslo_context.user_id if oslo_context.project_id: initiator.project_id = oslo_context.project_id if oslo_context.domain_id: initiator.domain_id = oslo_context.domain_id return initiator
def test_resource(self): res = resource.Resource(typeURI='storage', name='res_name', domain='res_domain', ref='res_ref', credential=credential.Credential( token=identifier.generate_uuid()), host=host.Host(address='192.168.0.1'), geolocation=geolocation.Geolocation(), geolocationId=identifier.generate_uuid()) res.add_attachment(attachment.Attachment(typeURI='attachURI', content='content', name='attachment_name')) res.add_address(endpoint.Endpoint(url='http://192.168.0.1')) self.assertEqual(res.is_valid(), True) dict_res = res.as_dict() for key in resource.RESOURCE_KEYNAMES: self.assertIn(key, dict_res)
def create_event(self, req, correlation_id): action = self._get_action(req) initiator_host = host.Host(address=req.client_addr, agent=req.user_agent) catalog = ast.literal_eval(req.environ['HTTP_X_SERVICE_CATALOG']) service_info = self.Service(type=taxonomy.UNKNOWN, name=taxonomy.UNKNOWN, id=taxonomy.UNKNOWN, admin_endp=None, private_endp=None, public_endp=None) default_endpoint = None for endp in catalog: admin_urlparse = urlparse.urlparse( endp['endpoints'][0]['adminURL']) public_urlparse = urlparse.urlparse( endp['endpoints'][0]['publicURL']) req_url = urlparse.urlparse(req.host_url) if (req_url.netloc == admin_urlparse.netloc or req_url.netloc == public_urlparse.netloc): service_info = self._get_service_info(endp) break elif (self._MAP.default_target_endpoint_type and endp['type'] == self._MAP.default_target_endpoint_type): default_endpoint = endp else: if default_endpoint: service_info = self._get_service_info(default_endpoint) initiator = ClientResource( typeURI=taxonomy.ACCOUNT_USER, id=identifier.norm_ns(str(req.environ['HTTP_X_USER_ID'])), name=req.environ['HTTP_X_USER_NAME'], host=initiator_host, credential=KeystoneCredential( token=req.environ['HTTP_X_AUTH_TOKEN'], identity_status=req.environ['HTTP_X_IDENTITY_STATUS']), project_id=identifier.norm_ns(req.environ['HTTP_X_PROJECT_ID'])) target_typeURI = (self._build_typeURI(req, service_info.type) if service_info.type != taxonomy.UNKNOWN else service_info.type) target = resource.Resource(typeURI=target_typeURI, id=service_info.id, name=service_info.name) if service_info.admin_endp: target.add_address(service_info.admin_endp) if service_info.private_endp: target.add_address(service_info.private_endp) if service_info.public_endp: target.add_address(service_info.public_endp) event = factory.EventFactory().new_event( eventType=cadftype.EVENTTYPE_ACTIVITY, outcome=taxonomy.OUTCOME_PENDING, action=action, initiator=initiator, target=target, observer=resource.Resource(id='target')) event.requestPath = req.path_qs event.add_tag( tag.generate_name_value_tag('correlation_id', correlation_id)) return event
def _create_cadf_event(self, project, res_spec, res_id, res_parent_id, request, response, suffix): action, key = self._get_action_and_key(res_spec, res_id, request, suffix) if not action: return None project_id = request.environ.get('HTTP_X_PROJECT_ID') domain_id = request.environ.get('HTTP_X_DOMAIN_ID') initiator = OpenStackResource( project_id=project_id, domain_id=domain_id, typeURI=taxonomy.ACCOUNT_USER, id=request.environ.get('HTTP_X_USER_ID', taxonomy.UNKNOWN), name=request.environ.get('HTTP_X_USER_NAME', taxonomy.UNKNOWN), domain=request.environ.get('HTTP_X_USER_DOMAIN_NAME', taxonomy.UNKNOWN), host=host.Host(address=request.client_addr, agent=request.user_agent)) action_result = None event_reason = None if response: if 200 <= response.status_int < 400: action_result = taxonomy.OUTCOME_SUCCESS else: action_result = taxonomy.OUTCOME_FAILURE event_reason = reason.Reason(reasonType='HTTP', reasonCode=str(response.status_int)) else: action_result = taxonomy.UNKNOWN target = None if res_id or res_parent_id: target = self._create_target_resource(project, res_spec, res_id, res_parent_id, key=key) else: target = self._create_target_resource(project, res_spec, None, self._service_id, key=key) target.name = self._service_name observer = self._create_observer_resource() event = eventfactory.EventFactory().new_event( eventType=cadftype.EVENTTYPE_ACTIVITY, outcome=action_result, action=action, initiator=initiator, observer=observer, reason=event_reason, target=target) event.requestPath = request.path_qs # add reporter step again? # event.add_reporterstep( # reporterstep.Reporterstep( # role=cadftype.REPORTER_ROLE_MODIFIER, # reporter=resource.Resource(id='observer'), # reporterTime=timestamp.get_utc_now())) return event
def _create_cadf_event(self, project, res_spec, res_id, res_parent_id, request, response, suffix): action = self._get_action(res_spec, res_id, request, suffix) key = None if not action: # ignored unknown actions if suffix == 'action': return None # suffix must be a key key = suffix # determine action from method (never None) action = self._get_action(res_spec, res_id, request, None) action += action_suffix_map[action] project_id = request.environ.get('HTTP_X_PROJECT_ID') domain_id = request.environ.get('HTTP_X_DOMAIN_ID') initiator = OpenStackResource( project_id=project_id, domain_id=domain_id, typeURI=taxonomy.ACCOUNT_USER, id=request.environ.get('HTTP_X_USER_ID', taxonomy.UNKNOWN), name=request.environ.get('HTTP_X_USER_NAME', taxonomy.UNKNOWN), domain=request.environ.get('HTTP_X_USER_DOMAIN_NAME', taxonomy.UNKNOWN), host=host.Host(address=request.client_addr, agent=request.user_agent)) action_result = None event_reason = None if response: if 200 <= response.status_int < 400: action_result = taxonomy.OUTCOME_SUCCESS else: action_result = taxonomy.OUTCOME_FAILURE event_reason = reason.Reason(reasonType='HTTP', reasonCode=str(response.status_int)) else: action_result = taxonomy.UNKNOWN target = None if res_id or res_parent_id: target = self._create_target_resource(project, res_spec, res_id, res_parent_id, key=key) else: target = self._create_target_resource(project, res_spec, None, self._service_id, key=key) target.name = self._service_name observer = self._create_observer_resource(request) event = eventfactory.EventFactory().new_event( eventType=cadftype.EVENTTYPE_ACTIVITY, outcome=action_result, action=action, initiator=initiator, observer=observer, reason=event_reason, target=target) event.requestPath = request.path_qs # if key and request.method[0] == 'P' and self._payloads_enabled and \ res_spec.payloads['enabled']: req_pl = request.json # remove possible wrapper elements req_pl = req_pl.get(res_spec.el_type_name, req_pl) self._attach_payload(event, req_pl, res_spec) # TODO add reporter step again? # event.add_reporterstep( # reporterstep.Reporterstep( # role=cadftype.REPORTER_ROLE_MODIFIER, # reporter=resource.Resource(id='observer'), # reporterTime=timestamp.get_utc_now())) return event