def users_create(request): """ Returns users list """ form = forms.UserCreateForm(MultiDict(request.safe_json_body or {}), csrf_context=request) if form.validate(): log.info("registering user") # probably not needed in the future since this requires root anyways # lets keep this here in case we lower view permission in the future # if request.registry.settings['appenlight.disable_registration']: # return HTTPUnprocessableEntity(body={'error': 'Registration is currently disabled.'}) user = User() # insert new user here DBSession.add(user) form.populate_obj(user) UserService.regenerate_security_code(user) UserService.set_password(user, user.user_password) user.status = 1 if form.status.data else 0 request.session.flash(_("User created")) DBSession.flush() return user.get_dict(exclude_keys=[ "security_code_date", "notes", "security_code", "user_password", ]) else: return HTTPUnprocessableEntity(body=form.errors_json)
def alert_channels_rule_POST(request): """ Creates new notification rule for specific alert channel """ user = request.user alert_action = AlertChannelAction(owner_id=request.user.id, type="report") DBSession.add(alert_action) DBSession.flush() return alert_action.get_dict()
def default_application(default_user): from appenlight.models.application import Application transaction.begin() application = Application( resource_id=1, resource_name='testapp', api_key='xxxx') DBSession.add(application) default_user.resources.append(application) DBSession.flush() transaction.commit() return application
def assign_users(request): """ Assigns specific report group to user for review - send email notification """ report_group = request.context.report_group application = request.context.resource currently_assigned = [u.user_name for u in report_group.assigned_users] new_assigns = request.unsafe_json_body # first unassign old users for user_name in new_assigns["unassigned"]: if user_name in currently_assigned: user = UserService.by_user_name(user_name) report_group.assigned_users.remove(user) comment = ReportComment(owner_id=request.user.id, report_time=report_group.first_timestamp) comment.body = "Unassigned group from @%s" % user_name report_group.comments.append(comment) # assign new users for user_name in new_assigns["assigned"]: if user_name not in currently_assigned: user = UserService.by_user_name(user_name) if user in report_group.assigned_users: report_group.assigned_users.remove(user) DBSession.flush() assignment = ReportAssignment( owner_id=user.id, report_time=report_group.first_timestamp, group_id=report_group.id, ) DBSession.add(assignment) comment = ReportComment(owner_id=request.user.id, report_time=report_group.first_timestamp) comment.body = "Assigned report_group to @%s" % user_name report_group.comments.append(comment) email_vars = { "user": user, "request": request, "application": application, "report_group": report_group, "email_title": "AppEnlight :: Assigned Report", } UserService.send_email( request, recipients=[user.email], variables=email_vars, template="/email_templates/assigned_report.jinja2", ) return True
def set_default_values(): row = PluginConfigService.by_query(plugin_name=PLUGIN_DEFINITION["name"], section="global").first() if not row: plugin = PluginConfig() plugin.config = {"uptime_regions_map": [], "json_config_version": 1} plugin.section = "global" plugin.plugin_name = PLUGIN_DEFINITION["name"] plugin.config["json_config_version"] = 1 DBSession.add(plugin) DBSession.flush()
def charts_event_rules_POST(request): json_body = copy.deepcopy(request.unsafe_json_body) alert_action = AlertChannelAction( name=json_body.get("name"), owner_id=request.user.id, resource_id=request.context.resource.resource_id, other_id=request.context.chart.uuid, rule=json_body["rule"], config=json_body["mappings"], type="chart", ) DBSession.add(alert_action) DBSession.flush() return alert_action.get_dict(extended_info=True)
def default_user(): from appenlight.models.user import User from appenlight.models.auth_token import AuthToken transaction.begin() user = User(id=1, user_name='testuser', status=1, email='*****@*****.**') DBSession.add(user) token = AuthToken(token='1234') user.auth_tokens.append(token) DBSession.flush() transaction.commit() return user
def default_application(default_user): from appenlight.models import DBSession from appenlight.models.application import Application transaction.begin() session = DBSession() application = Application(resource_id=1, resource_name="testapp", api_key="xxxx") session.add(application) default_user.resources.append(application) session.execute("SELECT nextval('resources_resource_id_seq')") transaction.commit() return application
def default_user(): from appenlight.models import DBSession from appenlight.models.user import User from appenlight.models.auth_token import AuthToken transaction.begin() session = DBSession() user = User(id=1, user_name="testuser", status=1, email="*****@*****.**") session.add(user) token = AuthToken(token="1234") user.auth_tokens.append(token) session.execute("SELECT nextval('users_id_seq')") transaction.commit() return user
def groups_create(request): """ Returns groups list """ form = forms.GroupCreateForm(MultiDict(request.safe_json_body or {}), csrf_context=request) if form.validate(): log.info("registering group") group = Group() # insert new group here DBSession.add(group) form.populate_obj(group) request.session.flash(_("Group created")) DBSession.flush() return group.get_dict(include_perms=True) else: return HTTPUnprocessableEntity(body=form.errors_json)
def add_uptime_stats(params, metric): proto_version = parse_proto(params.get("protocol_version")) try: application = ApplicationService.by_id_cached()(metric["resource_id"]) application = DBSession.merge(application, load=False) if not application: return start_interval = convert_date(metric["timestamp"]) start_interval = start_interval.replace(second=0, microsecond=0) new_metric = UptimeMetric( start_interval=start_interval, response_time=metric["response_time"], status_code=metric["status_code"], is_ok=metric["is_ok"], location=metric.get("location", 1), tries=metric["tries"], resource_id=application.resource_id, owner_user_id=application.owner_user_id, ) DBSession.add(new_metric) DBSession.flush() add_metrics_uptime([new_metric.es_doc()]) if metric["is_ok"]: event_types = [Event.types["uptime_alert"]] statuses = [Event.statuses["active"]] # get events older than 5 min events = EventService.by_type_and_status( event_types, statuses, older_than=(datetime.utcnow() - timedelta(minutes=6)), app_ids=[application.resource_id], ) for event in events: event.close() else: UptimeMetricService.check_for_alert(application, metric=metric) action = "METRICS UPTIME" metrics_msg = "%s: %s, proto:%s" % (action, str(application), proto_version) log.info(metrics_msg) session = DBSession() mark_changed(session) return True except Exception as exc: print_traceback(log) add_uptime_stats.retry(exc=exc)
def application_create(request): """ Creates new application instances """ user = request.user form = forms.ApplicationCreateForm(MultiDict(request.unsafe_json_body), csrf_context=request) if form.validate(): session = DBSession() resource = Application() DBSession.add(resource) form.populate_obj(resource) resource.api_key = resource.generate_api_key() user.resources.append(resource) request.session.flash(_('Application created')) DBSession.flush() mark_changed(session) else: return HTTPUnprocessableEntity(body=form.errors_json) return resource.get_dict()
def users_create(request): """ Returns users list """ form = forms.UserCreateForm(MultiDict(request.safe_json_body or {}), csrf_context=request) if form.validate(): log.info('registering user') user = User() # insert new user here DBSession.add(user) form.populate_obj(user) user.regenerate_security_code() user.set_password(user.user_password) user.status = 1 if form.status.data else 0 request.session.flash(_('User created')) DBSession.flush() return user.get_dict(exclude_keys=[ 'security_code_date', 'notes', 'security_code', 'user_password' ]) else: return HTTPUnprocessableEntity(body=form.errors_json)
def post(request): schema = UptimeConfigSchema() json_body = request.unsafe_json_body plugin = PluginConfig() plugin.config = {} plugin.plugin_name = PLUGIN_DEFINITION["name"] plugin.owner_id = request.user.id if json_body["section"] == "global": # admin config plugin.config = json_body["config"] plugin.section = "global" else: # handle user uptime_url deserialized = schema.deserialize(json_body["config"]) plugin.config = deserialized plugin.section = "resource" if request.context.resource: plugin.resource_id = request.context.resource.resource_id plugin.config["json_config_version"] = 1 DBSession.add(plugin) DBSession.flush() return plugin
def register(request): """ Render register page with form Also handles oAuth flow for registration """ login_url = request.route_url('ziggurat.routes.sign_in') if request.query_string: query_string = '?%s' % request.query_string else: query_string = '' referrer = '%s%s' % (request.path, query_string) if referrer in [login_url, '/register', '/register?sign_in=1']: referrer = '/' # never use the login form itself as came_from sign_in_form = forms.SignInForm(came_from=request.params.get( 'came_from', referrer), csrf_context=request) # populate form from oAuth session data returned by authomatic social_data = request.session.get('zigg.social_auth') if request.method != 'POST' and social_data: log.debug(social_data) user_name = social_data['user'].get('user_name', '').split('@')[0] form_data = { 'user_name': user_name, 'email': social_data['user'].get('email') } form_data['user_password'] = str(uuid.uuid4()) form = forms.UserRegisterForm(MultiDict(form_data), csrf_context=request) form.user_password.widget.hide_value = False else: form = forms.UserRegisterForm(request.POST, csrf_context=request) if request.method == 'POST' and form.validate(): log.info('registering user') # insert new user here if request.registry.settings['appenlight.disable_registration']: request.session.flash(_('Registration is currently disabled.')) return HTTPFound(location=request.route_url('/')) new_user = User() DBSession.add(new_user) form.populate_obj(new_user) new_user.regenerate_security_code() new_user.status = 1 new_user.set_password(new_user.user_password) new_user.registration_ip = request.environ.get('REMOTE_ADDR') if social_data: handle_social_data(request, new_user, social_data) email_vars = { 'user': new_user, 'request': request, 'email_title': "AppEnlight :: Start information" } UserService.send_email(request, recipients=[new_user.email], variables=email_vars, template='/email_templates/registered.jinja2') request.session.flash(_('You have successfully registered.')) DBSession.flush() headers = security.remember(request, new_user.id) return HTTPFound(location=request.route_url('/'), headers=headers) settings = request.registry.settings social_plugins = {} if settings.get('authomatic.pr.twitter.key', ''): social_plugins['twitter'] = True if settings.get('authomatic.pr.google.key', ''): social_plugins['google'] = True if settings.get('authomatic.pr.github.key', ''): social_plugins['github'] = True if settings.get('authomatic.pr.bitbucket.key', ''): social_plugins['bitbucket'] = True return { "form": form, "sign_in_form": sign_in_form, "social_plugins": social_plugins }
def alert_chart(pkey, chart_uuid): start = datetime.utcnow() request = get_current_request() alert_action = AlertChannelActionService.by_pkey(pkey) chart = DashboardChartService.by_uuid(chart_uuid) chart.migrate_json_config() resource = chart.dashboard json_body = chart.config ids_to_override = [json_body["resource"]] filter_settings = build_filter_settings_from_chart_config( request, json_body, override_app_ids=ids_to_override ) log.warning("alert_chart, resource:{}, chart:{}".format(resource, chart_uuid)) # determine start and end date for dataset start_date, end_date = determine_date_boundries_json(json_body) if not filter_settings["start_date"]: filter_settings["start_date"] = start_date.replace( hour=0, minute=0, second=0, microsecond=0 ) if not filter_settings["end_date"]: filter_settings["end_date"] = end_date event_type = Event.types["chart_alert"] open_event = None latest_closed_event = None events_query = EventService.for_resource( [resource.resource_id], event_type=event_type, target_uuid=chart_uuid, limit=20 ) for event in events_query: if event.status == Event.statuses["active"] and not open_event: open_event = event if event.status == Event.statuses["closed"] and not latest_closed_event: latest_closed_event = event if latest_closed_event: filter_settings["start_date"] = latest_closed_event.end_date es_config = transform_json_to_es_config( request, json_body, filter_settings, ids_to_override=ids_to_override ) if not es_config["index_names"]: return result = Datastores.es.search( body=es_config["query"], index=es_config["index_names"], doc_type="log", size=0 ) series, info_dict = parse_es_result(result, es_config, json_config=json_body) # we need to make a deepcopy since we will mutate it rule_config = copy.deepcopy(alert_action.rule) field_mappings = alert_action.config rule_obj = RuleService.rule_from_config( rule_config, field_mappings, info_dict["system_labels"] ) matched_interval = None finished_interval = None for step in reversed(series): if rule_obj.match(step): log.info("matched start") if not matched_interval: matched_interval = step break else: finished_interval = step if matched_interval: if open_event: log.info("ALERT: PROGRESS: %s %s" % (event_type, resource)) if finished_interval: open_event.values = copy.deepcopy(open_event.values) end_interval = finished_interval["key"].strftime(DATE_FORMAT) open_event.values["end_interval"] = end_interval open_event.close() else: log.warning("ALERT: OPEN: %s %s" % (event_type, resource)) step_size = None parent_agg = json_body.get("parentAgg") if parent_agg and parent_agg["type"] == "time_histogram": step_size = time_deltas[parent_agg["config"]["interval"]][ "delta" ].total_seconds() matched_step_values = { "values": matched_interval, "labels": info_dict["system_labels"], } values_dict = { "matched_rule": alert_action.get_dict(), "matched_step_values": matched_step_values, "start_interval": step["key"], "end_interval": None, "resource": chart.config.get("resource"), "chart_name": chart.name, "chart_uuid": chart_uuid, "step_size": step_size, "action_name": alert_action.name, } new_event = Event( resource_id=resource.resource_id, event_type=event_type, status=Event.statuses["active"], values=values_dict, target_uuid=chart_uuid, ) DBSession.add(new_event) DBSession.flush() new_event.send_alerts(request=request, resource=resource) elif open_event: if finished_interval: open_event.values = copy.deepcopy(open_event.values) end_interval = finished_interval["key"].strftime(DATE_FORMAT) open_event.values["end_interval"] = end_interval open_event.close() took = datetime.utcnow() - start log.warning("chart alert rule check took: {}".format(took))
def main(): parser = argparse.ArgumentParser( description="Populate AppEnlight database", add_help=False ) parser.add_argument( "-c", "--config", required=True, help="Configuration ini file of application" ) parser.add_argument("--username", default=None, help="User to create") parser.add_argument("--password", default=None, help="Password for created user") parser.add_argument("--email", default=None, help="Email for created user") parser.add_argument( "--auth-token", default=None, help="Auth token for created user" ) args = parser.parse_args() config_uri = args.config setup_logging(config_uri) env = bootstrap(config_uri) request = env["request"] with get_current_request().tm: group = GroupService.by_id(1) if not group: group = Group( id=1, group_name="Administrators", description="Top level permission owners", ) DBSession.add(group) permission = GroupPermission(perm_name="root_administration") group.permissions.append(permission) create_user = True if args.username else None while create_user is None: response = input("Do you want to create a new admin? (n)\n").lower() if is_yes(response or "n"): create_user = True elif is_no(response or "n"): create_user = False if create_user: csrf_token = request.session.get_csrf_token() user_name = args.username print("*********************************************************") while user_name is None: response = input("What is the username of new admin?\n") form = UserRegisterForm( user_name=response, csrf_token=csrf_token, csrf_context=request ) form.validate() if form.user_name.errors: print(form.user_name.errors[0]) else: user_name = response print('The admin username is "{}"\n'.format(user_name)) print("*********************************************************") email = args.email while email is None: response = input("What is the email of admin account?\n") form = UserRegisterForm( email=response, csrf_token=csrf_token, csrf_context=request ) form.validate() if form.email.errors: print(form.email.errors[0]) else: email = response print('The admin email is "{}"\n'.format(email)) print("*********************************************************") user_password = args.password confirmed_password = args.password while user_password is None or confirmed_password is None: response = getpass.getpass("What is the password for admin account?\n") form = UserRegisterForm( user_password=response, csrf_token=csrf_token, csrf_context=request ) form.validate() if form.user_password.errors: print(form.user_password.errors[0]) else: user_password = response response = getpass.getpass("Please confirm the password.\n") if user_password == response: confirmed_password = response else: print("Passwords do not match. Please try again") print("*********************************************************") with get_current_request().tm: if create_user: group = GroupService.by_id(1) user = User(user_name=user_name, email=email, status=1) UserService.regenerate_security_code(user) UserService.set_password(user, user_password) DBSession.add(user) token = AuthToken(description="Uptime monitoring token") if args.auth_token: token.token = args.auth_token user.auth_tokens.append(token) group.users.append(user) print("USER CREATED") print(json.dumps(user.get_dict())) print("*********************************************************") print("AUTH TOKEN") print(json.dumps(user.auth_tokens[0].get_dict()))
def register(request): """ Render register page with form Also handles oAuth flow for registration """ login_url = request.route_url("ziggurat.routes.sign_in") if request.query_string: query_string = "?%s" % request.query_string else: query_string = "" referrer = "%s%s" % (request.path, query_string) if referrer in [login_url, "/register", "/register?sign_in=1"]: referrer = "/" # never use the login form itself as came_from sign_in_form = forms.SignInForm( came_from=request.params.get("came_from", referrer), csrf_context=request ) # populate form from oAuth session data returned by authomatic social_data = request.session.get("zigg.social_auth") if request.method != "POST" and social_data: log.debug(social_data) user_name = social_data["user"].get("user_name", "").split("@")[0] form_data = {"user_name": user_name, "email": social_data["user"].get("email")} form_data["user_password"] = str(uuid.uuid4()) form = forms.UserRegisterForm(MultiDict(form_data), csrf_context=request) form.user_password.widget.hide_value = False else: form = forms.UserRegisterForm(request.POST, csrf_context=request) if request.method == "POST" and form.validate(): log.info("registering user") # insert new user here if request.registry.settings["appenlight.disable_registration"]: request.session.flash(_("Registration is currently disabled.")) return HTTPFound(location=request.route_url("/")) new_user = User() DBSession.add(new_user) form.populate_obj(new_user) UserService.regenerate_security_code(new_user) new_user.status = 1 UserService.set_password(new_user, new_user.user_password) new_user.registration_ip = request.environ.get("REMOTE_ADDR") if social_data: handle_social_data(request, new_user, social_data) email_vars = { "user": new_user, "request": request, "email_title": "AppEnlight :: Start information", } UserService.send_email( request, recipients=[new_user.email], variables=email_vars, template="/email_templates/registered.jinja2", ) request.session.flash(_("You have successfully registered.")) DBSession.flush() headers = security.remember(request, new_user.id) return HTTPFound(location=request.route_url("/"), headers=headers) settings = request.registry.settings social_plugins = {} if settings.get("authomatic.pr.twitter.key", ""): social_plugins["twitter"] = True if settings.get("authomatic.pr.google.key", ""): social_plugins["google"] = True if settings.get("authomatic.pr.github.key", ""): social_plugins["github"] = True if settings.get("authomatic.pr.bitbucket.key", ""): social_plugins["bitbucket"] = True return { "form": form, "sign_in_form": sign_in_form, "social_plugins": social_plugins, }