[why]
for forwarded email from security admins, the email content will contain
my_name/my_email(s) waiting to be assigned an account.
a forwarded email may contain multi my_name/my_email(s), just loop them.
add store lab to ES
Signed-off-by: Luan Shengde <shdluan(a)163.com>
---
container/mail-robot/lib/apply-account.rb | 49 ++++++++++++++++++++---
1 file changed, 43 insertions(+), 6 deletions(-)
diff --git a/container/mail-robot/lib/apply-account.rb b/container/mail-robot/lib/apply-account.rb
index e8288d7..bc340ad 100755
--- a/container/mail-robot/lib/apply-account.rb
+++ b/container/mail-robot/lib/apply-account.rb
@@ -54,6 +54,8 @@ class ApplyAccount
@mail_content = mail_content
@es_host = @send_mail_host
@es_port = ES_PORT
+ # email address file for security admins.
+ @sec_admins = YAML.load_file '/etc/compass-ci/conf/sec_admins.yaml'
@my_info = {}
end
@@ -63,11 +65,31 @@ class ApplyAccount
# in order to successfully send email for failed parsing
# firstly get my_email before execute parse_mail_content is needed
@my_info['my_email'] = @mail_content.from[0]
- parse_mail_content
- apply_my_account
- store_account_info
- send_mail('')
+ # for the forwarded email, we will get the email/name from the mail_content
+ # all emails from security admins will be treated as forwarded emails.
+ # the mail_content may like:
+ # ---
+ # my_name_1: my_email_1
+ # my_name_2: my_email_2
+ # ---
+ # the forwarded email allowed to contain multi emails/names
+ # in this case, we will loop them
+ if @sec_admins.include? @my_info['my_email']
+ users = forward_users
+ users.each do |user|
+ @my_info.clear
+ next if user.match?(/---/)
+
+ @my_info['my_name'], @my_info['my_email'] = user.split(/:\s+/)
+ @my_info['my_ssh_pubkey'] = []
+ applying_account
+ sleep 10
+ end
+ else
+ parse_mail_content
+ applying_account
+ end
rescue StandardError => e
puts e.message
puts e.backtrace
@@ -75,6 +97,17 @@ class ApplyAccount
send_mail(e.message)
end
+ def forward_users
+ forward_email_content = ParseApplyAccountEmail.new(@mail_content)
+ forward_email_content.extract_users
+ end
+
+ def applying_account
+ apply_my_account
+ store_account_info
+ send_mail('')
+ end
+
def parse_mail_content
parse_apply_account_email = ParseApplyAccountEmail.new(@mail_content)
@@ -93,7 +126,9 @@ class ApplyAccount
apply_info['my_token'] = my_account_es['my_uuid'] if my_account_es['my_token'].nil?
apply_info.update my_account_es
apply_info.update @my_info
- apply_info['my_ssh_pubkey'] = (apply_info['my_ssh_pubkey'] + my_ssh_pubkey_new).uniq
+ if my_ssh_pubkey_new
+ apply_info['my_ssh_pubkey'] = (apply_info['my_ssh_pubkey'] + my_ssh_pubkey_new).uniq
+ end
@my_info.update apply_info
apply_info['is_update_account'] = true
apply_info
@@ -102,6 +137,9 @@ class ApplyAccount
def apply_my_account
my_account_es = read_my_account_es
apply_info = {}
+
+ # lab will also be stored to ES.
+ @my_info['lab'] = ENV['lab']
if my_account_es
build_apply_info(apply_info, my_account_es)
else
@@ -109,7 +147,6 @@ class ApplyAccount
@my_info['my_token'] = my_token
apply_info.update @my_info
end
- apply_info['lab'] = ENV['lab']
apply_new_account(apply_info, my_account_es)
end
--
2.23.0