##// END OF EJS Templates
Adds an option to rdm-mailhandler to disable server certificate verification (#9496)....
Jean-Philippe Lang -
r7833:949c9a5b2fe8
parent child
Show More
@@ -1,168 +1,175
1 1 #!/usr/bin/env ruby
2 2
3 3 # == Synopsis
4 4 #
5 5 # Reads an email from standard input and forward it to a Redmine server
6 6 # through a HTTP request.
7 7 #
8 8 # == Usage
9 9 #
10 10 # rdm-mailhandler [options] --url=<Redmine URL> --key=<API key>
11 11 #
12 12 # == Arguments
13 13 #
14 14 # -u, --url URL of the Redmine server
15 15 # -k, --key Redmine API key
16 16 #
17 17 # General options:
18 18 # --unknown-user=ACTION how to handle emails from an unknown user
19 19 # ACTION can be one of the following values:
20 20 # ignore: email is ignored (default)
21 21 # accept: accept as anonymous user
22 22 # create: create a user account
23 23 # --no-permission-check disable permission checking when receiving
24 24 # the email
25 # --no-check-certificate do not check server certificate
25 26 # -h, --help show this help
26 27 # -v, --verbose show extra information
27 28 # -V, --version show version information and exit
28 29 #
29 30 # Issue attributes control options:
30 31 # -p, --project=PROJECT identifier of the target project
31 32 # -s, --status=STATUS name of the target status
32 33 # -t, --tracker=TRACKER name of the target tracker
33 34 # --category=CATEGORY name of the target category
34 35 # --priority=PRIORITY name of the target priority
35 36 # -o, --allow-override=ATTRS allow email content to override attributes
36 37 # specified by previous options
37 38 # ATTRS is a comma separated list of attributes
38 39 #
39 40 # == Examples
40 41 # No project specified. Emails MUST contain the 'Project' keyword:
41 42 #
42 43 # rdm-mailhandler --url http://redmine.domain.foo --key secret
43 44 #
44 45 # Fixed project and default tracker specified, but emails can override
45 46 # both tracker and priority attributes using keywords:
46 47 #
47 48 # rdm-mailhandler --url https://domain.foo/redmine --key secret \\
48 49 # --project foo \\
49 50 # --tracker bug \\
50 51 # --allow-override tracker,priority
51 52
52 53 require 'net/http'
53 54 require 'net/https'
54 55 require 'uri'
55 56 require 'getoptlong'
56 57 require 'rdoc/usage'
57 58
58 59 module Net
59 60 class HTTPS < HTTP
60 def self.post_form(url, params, headers)
61 def self.post_form(url, params, headers, options={})
61 62 request = Post.new(url.path)
62 63 request.form_data = params
63 64 request.basic_auth url.user, url.password if url.user
64 65 request.initialize_http_header(headers)
65 66 http = new(url.host, url.port)
66 67 http.use_ssl = (url.scheme == 'https')
68 if options[:no_check_certificate]
69 http.verify_mode = OpenSSL::SSL::VERIFY_NONE
70 end
67 71 http.start {|h| h.request(request) }
68 72 end
69 73 end
70 74 end
71 75
72 76 class RedmineMailHandler
73 77 VERSION = '0.1'
74 78
75 attr_accessor :verbose, :issue_attributes, :allow_override, :unknown_user, :no_permission_check, :url, :key
79 attr_accessor :verbose, :issue_attributes, :allow_override, :unknown_user, :no_permission_check, :url, :key, :no_check_certificate
76 80
77 81 def initialize
78 82 self.issue_attributes = {}
79 83
80 84 opts = GetoptLong.new(
81 85 [ '--help', '-h', GetoptLong::NO_ARGUMENT ],
82 86 [ '--version', '-V', GetoptLong::NO_ARGUMENT ],
83 87 [ '--verbose', '-v', GetoptLong::NO_ARGUMENT ],
84 88 [ '--url', '-u', GetoptLong::REQUIRED_ARGUMENT ],
85 89 [ '--key', '-k', GetoptLong::REQUIRED_ARGUMENT],
86 90 [ '--project', '-p', GetoptLong::REQUIRED_ARGUMENT ],
87 91 [ '--status', '-s', GetoptLong::REQUIRED_ARGUMENT ],
88 92 [ '--tracker', '-t', GetoptLong::REQUIRED_ARGUMENT],
89 93 [ '--category', GetoptLong::REQUIRED_ARGUMENT],
90 94 [ '--priority', GetoptLong::REQUIRED_ARGUMENT],
91 95 [ '--allow-override', '-o', GetoptLong::REQUIRED_ARGUMENT],
92 96 [ '--unknown-user', GetoptLong::REQUIRED_ARGUMENT],
93 [ '--no-permission-check', GetoptLong::NO_ARGUMENT]
97 [ '--no-permission-check', GetoptLong::NO_ARGUMENT],
98 [ '--no-check-certificate', GetoptLong::NO_ARGUMENT]
94 99 )
95 100
96 101 opts.each do |opt, arg|
97 102 case opt
98 103 when '--url'
99 104 self.url = arg.dup
100 105 when '--key'
101 106 self.key = arg.dup
102 107 when '--help'
103 108 usage
104 109 when '--verbose'
105 110 self.verbose = true
106 111 when '--version'
107 112 puts VERSION; exit
108 113 when '--project', '--status', '--tracker', '--category', '--priority'
109 114 self.issue_attributes[opt.gsub(%r{^\-\-}, '')] = arg.dup
110 115 when '--allow-override'
111 116 self.allow_override = arg.dup
112 117 when '--unknown-user'
113 118 self.unknown_user = arg.dup
114 119 when '--no-permission-check'
115 120 self.no_permission_check = '1'
121 when '--no-check-certificate'
122 self.no_check_certificate = true
116 123 end
117 124 end
118 125
119 126 RDoc.usage if url.nil?
120 127 end
121 128
122 129 def submit(email)
123 130 uri = url.gsub(%r{/*$}, '') + '/mail_handler'
124 131
125 132 headers = { 'User-Agent' => "Redmine mail handler/#{VERSION}" }
126 133
127 134 data = { 'key' => key, 'email' => email,
128 135 'allow_override' => allow_override,
129 136 'unknown_user' => unknown_user,
130 137 'no_permission_check' => no_permission_check}
131 138 issue_attributes.each { |attr, value| data["issue[#{attr}]"] = value }
132 139
133 140 debug "Posting to #{uri}..."
134 response = Net::HTTPS.post_form(URI.parse(uri), data, headers)
141 response = Net::HTTPS.post_form(URI.parse(uri), data, headers, :no_check_certificate => no_check_certificate)
135 142 debug "Response received: #{response.code}"
136 143
137 144 case response.code.to_i
138 145 when 403
139 146 warn "Request was denied by your Redmine server. " +
140 147 "Make sure that 'WS for incoming emails' is enabled in application settings and that you provided the correct API key."
141 148 return 77
142 149 when 422
143 150 warn "Request was denied by your Redmine server. " +
144 151 "Possible reasons: email is sent from an invalid email address or is missing some information."
145 152 return 77
146 153 when 400..499
147 154 warn "Request was denied by your Redmine server (#{response.code})."
148 155 return 77
149 156 when 500..599
150 157 warn "Failed to contact your Redmine server (#{response.code})."
151 158 return 75
152 159 when 201
153 160 debug "Proccessed successfully"
154 161 return 0
155 162 else
156 163 return 1
157 164 end
158 165 end
159 166
160 167 private
161 168
162 169 def debug(msg)
163 170 puts msg if verbose
164 171 end
165 172 end
166 173
167 174 handler = RedmineMailHandler.new
168 175 exit(handler.submit(STDIN.read))
General Comments 0
You need to be logged in to leave comments. Login now