t2-server 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +1 -1
- data/lib/t2server/exceptions.rb +11 -0
- data/lib/t2server/server.rb +66 -19
- data/test/tc_server.rb +0 -4
- metadata +5 -5
data/README.rdoc
CHANGED
data/lib/t2server/exceptions.rb
CHANGED
@@ -139,6 +139,17 @@ module T2Server
|
|
139
139
|
end
|
140
140
|
end
|
141
141
|
|
142
|
+
# Access to the server is denied to this username
|
143
|
+
class AuthorizationError < T2ServerError
|
144
|
+
attr_reader :username
|
145
|
+
|
146
|
+
# Create a new AuthorizationError with the rejected username
|
147
|
+
def initialize(username)
|
148
|
+
@username = username
|
149
|
+
super "The username '#{@username}' is not authorized to connect to this server"
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
142
153
|
# Raised if an operation is performed on a run when it is in the wrong
|
143
154
|
# state. Trying to start a run if it is the finished state would cause this
|
144
155
|
# exception to be raised.
|
data/lib/t2server/server.rb
CHANGED
@@ -32,7 +32,7 @@
|
|
32
32
|
|
33
33
|
require 'base64'
|
34
34
|
require 'uri'
|
35
|
-
require 'net/
|
35
|
+
require 'net/https'
|
36
36
|
require 'rexml/document'
|
37
37
|
include REXML
|
38
38
|
|
@@ -50,19 +50,33 @@ module T2Server
|
|
50
50
|
# Runs in any state (+Initialized+, +Running+ and +Finished+) are counted
|
51
51
|
# against this maximum.
|
52
52
|
attr_reader :run_limit
|
53
|
-
|
53
|
+
|
54
54
|
# list of servers we know about
|
55
55
|
@@servers = []
|
56
56
|
|
57
57
|
# :stopdoc:
|
58
58
|
# New is private but rdoc does not get it right! Hence :stopdoc: section.
|
59
|
-
def initialize(uri)
|
59
|
+
def initialize(uri, username, password)
|
60
60
|
@uri = uri.strip_path
|
61
61
|
uri = URI.parse(@uri)
|
62
62
|
@host = uri.host
|
63
63
|
@port = uri.port
|
64
64
|
@base_path = uri.path
|
65
65
|
@rest_path = uri.path + "/rest"
|
66
|
+
|
67
|
+
# set up http connection
|
68
|
+
@http = Net::HTTP.new(@host, @port)
|
69
|
+
|
70
|
+
# use ssl?
|
71
|
+
@ssl = uri.scheme == "https"
|
72
|
+
if ssl?
|
73
|
+
@username = uri.user || username
|
74
|
+
@password = uri.password || password
|
75
|
+
|
76
|
+
@http.use_ssl = true
|
77
|
+
@http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
78
|
+
end
|
79
|
+
|
66
80
|
@links = parse_description(get_attribute(@rest_path))
|
67
81
|
#@links.each {|key, val| puts "#{key}: #{val}"}
|
68
82
|
|
@@ -76,19 +90,20 @@ module T2Server
|
|
76
90
|
# :startdoc:
|
77
91
|
|
78
92
|
# :call-seq:
|
79
|
-
# Server.connect(uri) -> server
|
93
|
+
# Server.connect(uri, username="", password="") -> server
|
80
94
|
#
|
81
95
|
# Connect to the server specified by _uri_ which should be of the form:
|
82
|
-
# http://example.com:8888/blah
|
96
|
+
# http://example.com:8888/blah or https://user:pass@example.com:8888/blah
|
83
97
|
#
|
98
|
+
# The username and password can also be passed in separately.
|
84
99
|
# A Server instance is returned that represents the connection.
|
85
|
-
def Server.connect(uri)
|
100
|
+
def Server.connect(uri, username="", password="")
|
86
101
|
# see if we've already got this server
|
87
102
|
server = @@servers.find {|s| s.uri == uri}
|
88
103
|
|
89
104
|
if !server
|
90
105
|
# no, so create new one and return it
|
91
|
-
server = new(uri)
|
106
|
+
server = new(uri, username, password)
|
92
107
|
@@servers << server
|
93
108
|
end
|
94
109
|
|
@@ -112,10 +127,11 @@ module T2Server
|
|
112
127
|
def initialize_run(workflow)
|
113
128
|
request = Net::HTTP::Post.new("#{@links[:runs]}")
|
114
129
|
request.content_type = "application/xml"
|
130
|
+
if ssl?
|
131
|
+
request.basic_auth @username, @password
|
132
|
+
end
|
115
133
|
begin
|
116
|
-
response =
|
117
|
-
http.request(request, Fragments::WORKFLOW % workflow)
|
118
|
-
end
|
134
|
+
response = @http.request(request, Fragments::WORKFLOW % workflow)
|
119
135
|
rescue InternalHTTPError => e
|
120
136
|
raise ConnectionError.new(e)
|
121
137
|
end
|
@@ -127,11 +143,21 @@ module T2Server
|
|
127
143
|
epr.path[-36..-1]
|
128
144
|
when Net::HTTPForbidden
|
129
145
|
raise ServerAtCapacityError.new(@run_limit)
|
146
|
+
when Net::HTTPUnauthorized
|
147
|
+
raise AuthorizationError.new(@username)
|
130
148
|
else
|
131
149
|
raise UnexpectedServerResponse.new(response)
|
132
150
|
end
|
133
151
|
end
|
134
152
|
|
153
|
+
# :call-seq:
|
154
|
+
# server.ssl? -> bool
|
155
|
+
#
|
156
|
+
# Is this server using SSL?
|
157
|
+
def ssl?
|
158
|
+
@ssl
|
159
|
+
end
|
160
|
+
|
135
161
|
# :call-seq:
|
136
162
|
# server.runs -> [runs]
|
137
163
|
#
|
@@ -154,8 +180,11 @@ module T2Server
|
|
154
180
|
# Delete the specified run from the server, discarding all of its state.
|
155
181
|
def delete_run(uuid)
|
156
182
|
request = Net::HTTP::Delete.new("#{@links[:runs]}/#{uuid}")
|
183
|
+
if ssl?
|
184
|
+
request.basic_auth @username, @password
|
185
|
+
end
|
157
186
|
begin
|
158
|
-
response =
|
187
|
+
response = @http.request(request)
|
159
188
|
rescue InternalHTTPError => e
|
160
189
|
raise ConnectionError.new(e)
|
161
190
|
end
|
@@ -169,6 +198,8 @@ module T2Server
|
|
169
198
|
raise RunNotFoundError.new(uuid)
|
170
199
|
when Net::HTTPForbidden
|
171
200
|
raise AccessForbiddenError.new("run #{uuid}")
|
201
|
+
when Net::HTTPUnauthorized
|
202
|
+
raise AuthorizationError.new(@username)
|
172
203
|
else
|
173
204
|
raise UnexpectedServerResponse.new(response)
|
174
205
|
end
|
@@ -223,10 +254,11 @@ module T2Server
|
|
223
254
|
raise AccessForbiddenError.new("subdirectories (#{dir})") if dir.include? ?/
|
224
255
|
request = Net::HTTP::Post.new("#{@links[:runs]}/#{uuid}/#{root}")
|
225
256
|
request.content_type = "application/xml"
|
257
|
+
if ssl?
|
258
|
+
request.basic_auth @username, @password
|
259
|
+
end
|
226
260
|
begin
|
227
|
-
response =
|
228
|
-
http.request(request, Fragments::MKDIR % dir)
|
229
|
-
end
|
261
|
+
response = @http.request(request, Fragments::MKDIR % dir)
|
230
262
|
rescue InternalHTTPError => e
|
231
263
|
raise ConnectionError.new(e)
|
232
264
|
end
|
@@ -239,6 +271,8 @@ module T2Server
|
|
239
271
|
raise RunNotFoundError.new(uuid)
|
240
272
|
when Net::HTTPForbidden
|
241
273
|
raise AccessForbiddenError.new("#{dir} on run #{uuid}")
|
274
|
+
when Net::HTTPUnauthorized
|
275
|
+
raise AuthorizationError.new(@username)
|
242
276
|
else
|
243
277
|
raise UnexpectedServerResponse.new(response)
|
244
278
|
end
|
@@ -254,10 +288,11 @@ module T2Server
|
|
254
288
|
rename = filename.split('/')[-1] if rename == ""
|
255
289
|
request = Net::HTTP::Post.new("#{@links[:runs]}/#{uuid}/#{location}")
|
256
290
|
request.content_type = "application/xml"
|
291
|
+
if ssl?
|
292
|
+
request.basic_auth @username, @password
|
293
|
+
end
|
257
294
|
begin
|
258
|
-
response =
|
259
|
-
http.request(request, Fragments::UPLOAD % [rename, contents])
|
260
|
-
end
|
295
|
+
response = @http.request(request, Fragments::UPLOAD % [rename, contents])
|
261
296
|
rescue InternalHTTPError => e
|
262
297
|
raise ConnectionError.new(e)
|
263
298
|
end
|
@@ -270,6 +305,8 @@ module T2Server
|
|
270
305
|
raise RunNotFoundError.new(uuid)
|
271
306
|
when Net::HTTPForbidden
|
272
307
|
raise AccessForbiddenError.new("run #{uuid}")
|
308
|
+
when Net::HTTPUnauthorized
|
309
|
+
raise AuthorizationError.new(@username)
|
273
310
|
else
|
274
311
|
raise UnexpectedServerResponse.new(response)
|
275
312
|
end
|
@@ -306,8 +343,11 @@ module T2Server
|
|
306
343
|
private
|
307
344
|
def get_attribute(path)
|
308
345
|
request = Net::HTTP::Get.new(path)
|
346
|
+
if ssl?
|
347
|
+
request.basic_auth @username, @password
|
348
|
+
end
|
309
349
|
begin
|
310
|
-
response =
|
350
|
+
response = @http.request(request)
|
311
351
|
rescue InternalHTTPError => e
|
312
352
|
raise ConnectionError.new(e)
|
313
353
|
end
|
@@ -319,6 +359,8 @@ module T2Server
|
|
319
359
|
raise AttributeNotFoundError.new(path)
|
320
360
|
when Net::HTTPForbidden
|
321
361
|
raise AccessForbiddenError.new("attribute #{path}")
|
362
|
+
when Net::HTTPUnauthorized
|
363
|
+
raise AuthorizationError.new(@username)
|
322
364
|
else
|
323
365
|
raise UnexpectedServerResponse.new(response)
|
324
366
|
end
|
@@ -327,8 +369,11 @@ module T2Server
|
|
327
369
|
def set_attribute(path, value, type)
|
328
370
|
request = Net::HTTP::Put.new(path)
|
329
371
|
request.content_type = type
|
372
|
+
if ssl?
|
373
|
+
request.basic_auth @username, @password
|
374
|
+
end
|
330
375
|
begin
|
331
|
-
response =
|
376
|
+
response = @http.request(request, value)
|
332
377
|
rescue InternalHTTPError => e
|
333
378
|
raise ConnectionError.new(e)
|
334
379
|
end
|
@@ -341,6 +386,8 @@ module T2Server
|
|
341
386
|
raise AttributeNotFoundError.new(path)
|
342
387
|
when Net::HTTPForbidden
|
343
388
|
raise AccessForbiddenError.new("attribute #{path}")
|
389
|
+
when Net::HTTPUnauthorized
|
390
|
+
raise AuthorizationError.new(@username)
|
344
391
|
else
|
345
392
|
raise UnexpectedServerResponse.new(response)
|
346
393
|
end
|
data/test/tc_server.rb
CHANGED
@@ -40,10 +40,6 @@ class TestServer < Test::Unit::TestCase
|
|
40
40
|
@server = T2Server::Server.connect($address)
|
41
41
|
end
|
42
42
|
assert_not_nil(@server)
|
43
|
-
assert_raise(T2Server::ConnectionError) do
|
44
|
-
uri = URI.parse($address)
|
45
|
-
T2Server::Server.connect("http://#{uri.host}:22")
|
46
|
-
end
|
47
43
|
|
48
44
|
# run creation
|
49
45
|
assert_nothing_raised(T2Server::T2ServerError) do
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: t2-server
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Robert Haines
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-09-
|
18
|
+
date: 2010-09-22 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|