def send_new_ticket_mail_to_assignees(ticket: Ticket, assignees): recipient_list = [user.email for user in assignees] user = ticket.user if not recipient_list: logger.error("Ticket not has assignees: {}".format(ticket.id)) return subject = '{}: {}'.format(_("New ticket"), ticket.title) # 这里要设置前端地址,因为要直接跳转到页面 if ticket.type == ticket.TYPE.REQUEST_ASSET_PERM: detail_url = urljoin( settings.SITE_URL, f'/tickets/tickets/request-asset-perm/{ticket.id}') else: detail_url = urljoin(settings.SITE_URL, f'/tickets/tickets/{ticket.id}') message = _(""" <div> <p>Your has a new ticket</p> <div> {body} <br/> <a href={url}>click here to review</a> </div> </div> """).format(body=ticket.body, user=user, url=detail_url) send_mail_async.delay(subject, message, recipient_list, html_message=message)
def send_ticket_applied_mail_to_assignees(ticket, assignees): if not assignees: logger.debug( "Not found assignees, ticket: {}({}), assignees: {}".format( ticket, str(ticket.id), assignees)) return subject = _('New Ticket: {} ({})'.format(ticket.title, ticket.get_type_display())) ticket_detail_url = urljoin( settings.SITE_URL, const.TICKET_DETAIL_URL.format(id=str(ticket.id))) message = _("""<div> <p>Your has a new ticket</p> <div> <b>Ticket:</b> <br/> {body} <br/> <a href={ticket_detail_url}>click here to review</a> </div> </div> """.format(body=ticket.body.replace('\n', '<br/>'), ticket_detail_url=ticket_detail_url)) if settings.DEBUG: logger.debug(message) recipient_list = [assignee.email for assignee in assignees] send_mail_async.delay(subject, message, recipient_list, html_message=message)
def send_reset_password_mail(user): subject = _('Reset password') recipient_list = [user.email] message = _(""" Hello %(name)s: <br> Please click the link below to reset your password, if not your request, concern your account security <br> <a href="%(rest_password_url)s?token=%(rest_password_token)s">Click here reset password</a> <br> This link is valid for 1 hour. After it expires, <a href="%(forget_password_url)s?email=%(email)s">request new one</a> <br> --- <br> <a href="%(login_url)s">Login direct</a> <br> """) % { 'name': user.name, 'rest_password_url': reverse('users:reset-password', external=True), 'rest_password_token': user.generate_reset_token(), 'forget_password_url': reverse('users:forgot-password', external=True), 'email': user.email, 'login_url': reverse('authentication:login', external=True), } if settings.DEBUG: logger.debug(message) send_mail_async.delay(subject, message, recipient_list, html_message=message)
def send_reset_password_mail(user): subject = _('Reset password') recipient_list = [user.email] message = _(""" Hello %(name)s: </br> Please click the link below to reset your password, if not your request, concern your account security </br> <a href="%(rest_password_url)s?token=%(rest_password_token)s">Click here reset password</a> </br> This link is valid for 1 hour. After it expires, <a href="%(forget_password_url)s?email=%(email)s">request new one</a> </br> --- </br> <a href="%(login_url)s">Login direct</a> </br> """) % { 'name': user.name, 'rest_password_url': reverse('users:reset-password', external=True), 'rest_password_token': user.generate_reset_token(), 'forget_password_url': reverse('users:forgot-password', external=True), 'email': user.email, 'login_url': reverse('users:login', external=True), } if settings.DEBUG: logger.debug(message) send_mail_async.delay(subject, message, recipient_list, html_message=message)
def send_user_created_mail(user): recipient_list = [user.email] subject = _('Create account successfully') if settings.EMAIL_CUSTOM_USER_CREATED_SUBJECT: subject = settings.EMAIL_CUSTOM_USER_CREATED_SUBJECT honorific = '<p>' + _('Hello %(name)s') % {'name': user.name} + ':</p>' if settings.EMAIL_CUSTOM_USER_CREATED_HONORIFIC: honorific = '<p>' + settings.EMAIL_CUSTOM_USER_CREATED_HONORIFIC + ':</p>' body = construct_user_created_email_body(user) signature = '<p style="float:right">jumpserver</p>' if settings.EMAIL_CUSTOM_USER_CREATED_SIGNATURE: signature = '<p style="float:right">' + settings.EMAIL_CUSTOM_USER_CREATED_SIGNATURE + '</p>' message = honorific + body + signature if settings.DEBUG: try: print(message) except OSError: pass send_mail_async.delay(subject, message, recipient_list, html_message=message)
def user_add_success_next(user): subject = _('Create account successfully') recipient_list = [user.email] message = _(""" Hello %(name)s: </br> Your account has been created successfully </br> <a href="%(rest_password_url)s?token=%(rest_password_token)s">click here to set your password</a> </br> This link is valid for 1 hour. After it expires, <a href="%(forget_password_url)s?email=%(email)s">request new one</a> </br> --- </br> <a href="%(login_url)s">Login direct</a> </br> """) % { 'name': user.name, 'rest_password_url': reverse('users:reset-password', external=True), 'rest_password_token': user.generate_reset_token(), 'forget_password_url': reverse('users:forgot-password', external=True), 'email': user.email, 'login_url': reverse('users:login', external=True), } send_mail_async.delay(subject, message, recipient_list, html_message=message)
def send_command_execution_alert_mail(command): subject = _("Insecure Web Command Execution Alert: [%(name)s]") % { 'name': command['user'], } input = command['input'] input = input.replace('\n', '<br>') recipient_list = settings.SECURITY_INSECURE_COMMAND_EMAIL_RECEIVER.split(',') assets = ', '.join([str(asset) for asset in command['assets']]) message = _(""" <br> Assets: %(assets)s <br> User: %(user)s <br> Level: %(risk_level)s <br> ----------------- Commands ---------------- <br> %(command)s <br> ----------------- Commands ---------------- <br> """) % { 'command': input, 'assets': assets, 'user': command['user'], 'risk_level': Command.get_risk_level_str(command['risk_level']), } send_mail_async.delay(subject, message, recipient_list, html_message=message)
def send_ticket_applied_mail_to_assignees(ticket): if not ticket.assignees: logger.debug( "Not found assignees, ticket: {}({}), assignees: {}".format( ticket, str(ticket.id), ticket.assignees)) return ticket_detail_url = urljoin( settings.SITE_URL, const.TICKET_DETAIL_URL.format(id=str(ticket.id))) subject = _('New Ticket - {} ({})').format(ticket.title, ticket.get_type_display()) message = EMAIL_TEMPLATE.format( title=_('Your has a new ticket, applicant - {}').format( str(ticket.applicant_display)), ticket_detail_url=ticket_detail_url, ticket_detail_url_description=_('click here to review'), body=ticket.body.replace('\n', '<br/>'), ) if settings.DEBUG: logger.debug(message) recipient_list = [assignee.email for assignee in ticket.assignees.all()] send_mail_async.delay(subject, message, recipient_list, html_message=message)
def send_command_alert_mail(command): session_obj = Session.objects.get(id=command['session']) subject = _("Insecure Command Alert: [%(name)s->%(login_from)s@%(remote_addr)s] $%(command)s") % { 'name': command['user'], 'login_from': session_obj.get_login_from_display(), 'remote_addr': session_obj.remote_addr, 'command': command['input'] } recipient_list = settings.SECURITY_INSECURE_COMMAND_EMAIL_RECEIVER.split(',') message = _(""" Command: %(command)s <br> Asset: %(host_name)s (%(host_ip)s) <br> User: %(user)s <br> Level: %(risk_level)s <br> Session: <a href="%(session_detail_url)s">session detail</a> <br> """) % { 'command': command['input'], 'host_name': command['asset'], 'host_ip': session_obj.asset_obj.ip, 'user': command['user'], 'risk_level': Command.get_risk_level_str(command['risk_level']), 'session_detail_url': reverse('api-terminal:session-detail', kwargs={'pk': command['session']}, external=True, api_to_ui=True), } logger.debug(message) send_mail_async.delay(subject, message, recipient_list, html_message=message)
def user_add_success_next(user): subject = _('帐号创建成功') recipient_list = [user.email] message = _(""" 您好 %(name)s: </br> 您的帐号已经创建成功。 </br> <a href="%(rest_password_url)s?token=%(rest_password_token)s">点击些链接设置登录密码</a> </br> 此链接一小时内有效, 已经失效? <a href="%(forget_password_url)s?email=%(email)s">重新发起请求</a> </br> --- </br> <a href="%(login_url)s">登录</a> </br> """) % { 'name': user.name, 'rest_password_url': reverse('users:reset-password', external=True), 'rest_password_token': user.generate_reset_token(), 'forget_password_url': reverse('users:forgot-password', external=True), 'email': user.email, 'login_url': reverse('users:login', external=True) } send_mail_async.delay(subject, message, recipient_list, html_message=message)
def send_user_created_mail(user): subject = _('Create account successfully') recipient_list = [user.email] message = _(""" Hello %(name)s: </br> Your account has been created successfully </br> <a href="%(rest_password_url)s?token=%(rest_password_token)s">click here to set your password</a> </br> This link is valid for 1 hour. After it expires, <a href="%(forget_password_url)s?email=%(email)s">request new one</a> </br> --- </br> <a href="%(login_url)s">Login direct</a> </br> """) % { 'name': user.name, 'rest_password_url': reverse('users:reset-password', external=True), 'rest_password_token': user.generate_reset_token(), 'forget_password_url': reverse('users:forgot-password', external=True), 'email': user.email, 'login_url': reverse('users:login', external=True), } if settings.DEBUG: print(message) send_mail_async.delay(subject, message, recipient_list, html_message=message)
def send_ticket_processed_mail_to_applicant(ticket): if not ticket.applicant: logger.error("Not found applicant: {}({})".format( ticket.title, ticket.id)) return subject = _('Ticket has processed: {} ({})').format( ticket.title, ticket.get_type_display()) message = _(""" <div> <p>Your ticket has been processed</p> <div> <b>Ticket:</b> <br/> {body} <br/> </div> </div> """.format(body=ticket.body.replace('\n', '<br/>'), )) if settings.DEBUG: logger.debug(message) recipient_list = [ticket.applicant.email] send_mail_async.delay(subject, message, recipient_list, html_message=message)
def send_user_created_mail(user): from .notifications import UserCreatedMsg recipient_list = [user.email] msg = UserCreatedMsg.html_msg subject = msg['subject'] message = msg['message'] if settings.DEBUG: try: print(message) except OSError: pass send_mail_async.delay(subject, message, recipient_list, html_message=message)
def send_password_expiration_reminder_mail(user): subject = _('Security notice') recipient_list = [user.email] message = _(""" Hello %(name)s: </br> Your password will expire in %(date_password_expired)s, </br> For your account security, please click on the link below to update your password in time </br> <a href="%(update_password_url)s">Click here update password</a> </br> If your password has expired, please click <a href="%(forget_password_url)s?email=%(email)s">Password expired</a> to apply for a password reset email. </br> --- </br> <a href="%(login_url)s">Login direct</a> </br> """) % { 'name': user.name, 'date_password_expired': datetime.fromtimestamp(datetime.timestamp( user.date_password_expired)).strftime('%Y-%m-%d %H:%M'), 'update_password_url': reverse('users:user-password-update', external=True), 'forget_password_url': reverse('users:forgot-password', external=True), 'email': user.email, 'login_url': reverse('users:login', external=True), } if settings.DEBUG: logger.debug(message) send_mail_async.delay(subject, message, recipient_list, html_message=message)
def send_reset_password_success_mail(request, user): subject = _('Reset password success') recipient_list = [user.email] message = _(""" Hi %(name)s: <br> <br> Your JumpServer password has just been successfully updated. <br> <br> If the password update was not initiated by you, your account may have security issues. It is recommended that you log on to the JumpServer immediately and change your password. <br> <br> If you have any questions, you can contact the administrator. <br> <br> --- <br> <br> IP Address: %(ip_address)s <br> <br> Browser: %(browser)s <br> """) % { 'name': user.name, 'ip_address': get_request_ip_or_data(request), 'browser': get_request_user_agent(request), } if settings.DEBUG: logger.debug(message) send_mail_async.delay(subject, message, recipient_list, html_message=message)
def send_user_expiration_reminder_mail(user): subject = _('Expiration notice') recipient_list = [user.email] message = _(""" Hello %(name)s: <br> Your account will expire in %(date_expired)s, <br> In order not to affect your normal work, please contact the administrator for confirmation. <br> """) % { 'name': user.name, 'date_expired': datetime.fromtimestamp(datetime.timestamp( user.date_expired)).strftime('%Y-%m-%d %H:%M'), } if settings.DEBUG: logger.debug(message) send_mail_async.delay(subject, message, recipient_list, html_message=message)
def check_schedule_result(): assets = Asset.objects.filter(labels__value__in=['生产环境', '测试环境']) for asset in assets: ip = asset.ip system_user = asset.systemuser_set.first() username = system_user.username password = system_user.password log_dir = asset.log_dir ssh = SSHClient(hostname=ip, port=22, username=username, password=password) for i in range(3): ssh.connect() if ssh.client_state: break else: if not ssh.client_state: return False commands = Command.objects.filter(schedule__database__asset=asset, status=1) sch_num = cache.get('SCHEDULE_NUM_%s' % ip, 0) for command in commands: cmd = 'cat %s/%s' % (log_dir + timezone.now().strftime('%Y%m%d'), str(command.id)) stdout, stderr = ssh.run_cmd(cmd) if stderr: send_mail_async.delay( 'Check schedule result error', 'asset: %s\n' % ip + 'command: %s\n' % cmd + 'err: %s' % stderr, [item[-1] for item in settings.ADMINS]) command.status = 3 command.save(update_fields=['status']) sch_num -= 1 continue status = get_status(log_info=stdout, content=command.content) if status: command.status = status command.save(update_fields=['status']) sch_num -= 1 cache.set('SCHEDULE_NUM_%s' % ip, sch_num, 60 * 60 * 24 * 5)
def send_reset_ssh_key_mail(user): subject = _('SSH Key Reset') recipient_list = [user.email] message = _(""" Hello %(name)s: </br> Your ssh public key has been reset by site administrator. Please login and reset your ssh public key. </br> <a href="%(login_url)s">Login direct</a> </br> """) % { 'name': user.name, 'login_url': reverse('users:login', external=True), } if settings.DEBUG: logger.debug(message) send_mail_async.delay(subject, message, recipient_list, html_message=message)
def send_reset_ssh_key_mail(user): subject = _('SSH Key Reset') recipient_list = [user.email] message = _(""" Hello %(name)s: <br> Your ssh public key has been reset by site administrator. Please login and reset your ssh public key. <br> <a href="%(login_url)s">Login direct</a> <br> """) % { 'name': user.name, 'login_url': reverse('authentication:login', external=True), } if settings.DEBUG: logger.debug(message) send_mail_async.delay(subject, message, recipient_list, html_message=message)
def send_user_created_mail(user): subject = _('Create account successfully') recipient_list = [user.email] message = _(""" Hello %(name)s: </br> Your account has been created successfully </br> Username: %(username)s </br> <a href="%(rest_password_url)s?token=%(rest_password_token)s">If you have forgotten your password, please click here to reset it.</a> </br> This link is valid for 1 hour. After it expires, <a href="%(forget_password_url)s?email=%(email)s">request new one</a> </br> --- </br> <a href="%(login_url)s">Login direct</a> </br> """) % { 'name': user.name, 'username': user.username, 'rest_password_url': reverse('users:reset-password', external=True), 'rest_password_token': user.generate_reset_token(), 'forget_password_url': reverse('users:forgot-password', external=True), 'email': user.email, 'login_url': reverse('users:login', external=True), } if settings.DEBUG: try: print(message) except OSError: pass send_mail_async.delay(subject, message, recipient_list, html_message=message)
def send_new_ticket_mail_to_assignees(ticket, assignees): recipient_list = [user.email for user in assignees] user = ticket.user if not recipient_list: logger.error("Ticket not has assignees: {}".format(ticket.id)) return subject = '{}: {}'.format(_("New ticket"), ticket.title) detail_url = reverse('tickets:ticket-detail', kwargs={'pk': ticket.id}, external=True) message = _(""" <div> <p>Your has a new ticket</p> <div> {body} <br/> <a href={url}>click here to review</a> </div> </div> """).format(body=ticket.body, user=user, url=detail_url) send_mail_async.delay(subject, message, recipient_list, html_message=message)
def send_ticket_action_mail_to_user(ticket): if not ticket.user: logger.error("Ticket not has user: {}".format(ticket.id)) return user = ticket.user recipient_list = [user.email] subject = '{}: {}'.format(_("Ticket has been reply"), ticket.title) message = _(""" <div> <p>Your ticket has been replay</p> <div> <b>Title:</b> {ticket.title} <br/> <b>Assignee:</b> {ticket.assignee_display} <br/> <b>Status:</b> {ticket.status_display} <br/> </div> </div> """).format(ticket=ticket) send_mail_async.delay(subject, message, recipient_list, html_message=message)
def send_password_expiration_reminder_mail(user): subject = _('Security notice') recipient_list = [user.email] message = _(""" Hello %(name)s: </br> Your password will expire in %(date_password_expired)s, </br> For your account security, please click on the link below to update your password in time </br> <a href="%(update_password_url)s">Click here update password</a> </br> If your password has expired, please click <a href="%(forget_password_url)s?email=%(email)s">Password expired</a> to apply for a password reset email. </br> --- </br> <a href="%(login_url)s">Login direct</a> </br> """) % { 'name': user.name, 'date_password_expired': datetime.fromtimestamp(datetime.timestamp( user.date_password_expired)).strftime('%Y-%m-%d %H:%M'), 'update_password_url': reverse('users:user-password-update', external=True), 'forget_password_url': reverse('users:forgot-password', external=True), 'email': user.email, 'login_url': reverse('authentication:login', external=True), } if settings.DEBUG: logger.debug(message) send_mail_async.delay(subject, message, recipient_list, html_message=message)
def send_ticket_processed_mail_to_applicant(ticket): if not ticket.applicant: logger.error("Not found applicant: {}({})".format( ticket.title, ticket.id)) return ticket_detail_url = urljoin( settings.SITE_URL, const.TICKET_DETAIL_URL.format(id=str(ticket.id))) subject = _('Ticket has processed - {} ({})').format( ticket.title, ticket.processor_display) message = EMAIL_TEMPLATE.format( title=_('Your ticket has been processed, processor - {}').format( ticket.processor_display), ticket_detail_url=ticket_detail_url, ticket_detail_url_description=_('click here to review'), body=ticket.body.replace('\n', '<br/>'), ) if settings.DEBUG: logger.debug(message) recipient_list = [ticket.applicant.email] send_mail_async.delay(subject, message, recipient_list, html_message=message)
def send_new_ticket_mail_to_assignees(ticket: Ticket, assignees): recipient_list = [user.email for user in assignees] user = ticket.user if not recipient_list: logger.error("Ticket not has assignees: {}".format(ticket.id)) return subject = '{}: {}'.format(_("New ticket"), ticket.title) # 这里要设置前端地址,因为要直接跳转到页面 detail_url = urljoin(settings.SITE_URL, TICKET_DETAIL.format(id=ticket.id)) message = _(""" <div> <p>Your has a new ticket</p> <div> {body} <br/> <a href={url}>click here to review</a> </div> </div> """).format(body=ticket.body, user=user, url=detail_url) send_mail_async.delay(subject, message, recipient_list, html_message=message)