|
7 | 7 | let(:subject) do
|
8 | 8 | mod = Module.new
|
9 | 9 | mod.extend described_class
|
10 |
| - stubs = [ :vprint_status, :print_status, :vprint_good, :print_good, :print_error ] |
| 10 | + stubs = [ :vprint_status, :print_status, :vprint_good, :print_good, :print_error, :print_warning ] |
11 | 11 | stubs.each { |meth| mod.stub(meth) }
|
12 | 12 | mod.stub(:service_info).and_return({})
|
13 | 13 | mod
|
|
235 | 235 | end
|
236 | 236 |
|
237 | 237 | it "should identify only a named SQL instance" do
|
238 |
| - allow(subject).to receive(:each_service).and_yield(normal_service).and_yield(running_analysis_service). |
239 |
| - and_yield(running_2k_sql_instance).and_yield(running_named_2k_sql_instance) |
| 238 | + allow(subject).to receive(:each_service).and_yield(normal_service).and_yield(running_analysis_service) |
| 239 | + .and_yield(running_2k_sql_instance).and_yield(running_named_2k_sql_instance) |
240 | 240 | result = subject.check_for_sqlserver(instance)
|
241 | 241 | result.should eq running_named_2k_sql_instance
|
242 | 242 | end
|
|
248 | 248 | end
|
249 | 249 |
|
250 | 250 | it "should identify only a named SQL instance" do
|
251 |
| - allow(subject).to receive(:each_service).and_yield(normal_service).and_yield(running_analysis_service). |
252 |
| - and_yield(running_2k5_sql_instance).and_yield(running_named_2k5_sql_instance) |
| 251 | + allow(subject).to receive(:each_service).and_yield(normal_service).and_yield(running_analysis_service) |
| 252 | + .and_yield(running_2k5_sql_instance).and_yield(running_named_2k5_sql_instance) |
253 | 253 | result = subject.check_for_sqlserver(instance)
|
254 | 254 | result.should eq running_named_2k5_sql_instance
|
255 | 255 | end
|
|
261 | 261 | end
|
262 | 262 |
|
263 | 263 | it "should identify only a named SQL instance" do
|
264 |
| - allow(subject).to receive(:each_service).and_yield(normal_service).and_yield(running_analysis_service). |
265 |
| - and_yield(running_2k8_sql_instance).and_yield(running_named_2k8_sql_instance) |
| 264 | + allow(subject).to receive(:each_service).and_yield(normal_service).and_yield(running_analysis_service) |
| 265 | + .and_yield(running_2k8_sql_instance).and_yield(running_named_2k8_sql_instance) |
266 | 266 | result = subject.check_for_sqlserver(instance)
|
267 | 267 | result.should eq running_named_2k8_sql_instance
|
268 | 268 | end
|
269 | 269 | end
|
270 | 270 | end
|
271 | 271 |
|
| 272 | + describe "#impersonate_sql_user" do |
| 273 | + let(:pid) do |
| 274 | + 8787 |
| 275 | + end |
| 276 | + |
| 277 | + let(:user) do |
| 278 | + 'sqluser' |
| 279 | + end |
| 280 | + |
| 281 | + let(:service) do |
| 282 | + { pid: pid } |
| 283 | + end |
| 284 | + |
| 285 | + let(:process) do |
| 286 | + { 'pid' => pid, 'user' => user } |
| 287 | + end |
| 288 | + |
| 289 | + it 'should return false if service is invalid or pid is invalid' do |
| 290 | + subject.impersonate_sql_user(nil).should be_falsey |
| 291 | + subject.impersonate_sql_user(pid: nil).should be_falsey |
| 292 | + subject.impersonate_sql_user(pid: 0).should be_falsey |
| 293 | + end |
| 294 | + |
| 295 | + context 'user has privs to impersonate' do |
| 296 | + before(:each) do |
| 297 | + subject.stub_chain('session.sys.config.getuid').and_return('Superman') |
| 298 | + subject.stub_chain('client.sys.config.getprivs').and_return(['SeAssignPrimaryTokenPrivilege']) |
| 299 | + subject.stub_chain('session.incognito').and_return(true) |
| 300 | + subject.stub_chain('session.sys.process.each_process').and_yield(process) |
| 301 | + end |
| 302 | + |
| 303 | + it 'should return true if successful impersonating' do |
| 304 | + subject.stub_chain('session.incognito.incognito_impersonate_token').with(user).and_return('Successfully') |
| 305 | + subject.impersonate_sql_user(service).should be true |
| 306 | + end |
| 307 | + |
| 308 | + it 'should return false if fails impersonating' do |
| 309 | + subject.stub_chain('session.incognito.incognito_impersonate_token').with(user).and_return('guff') |
| 310 | + subject.impersonate_sql_user(service).should be false |
| 311 | + end |
| 312 | + |
| 313 | + it 'should return false if unable to find process username' do |
| 314 | + subject.stub_chain('session.sys.process.each_process').and_yield('pid' => 0) |
| 315 | + subject.impersonate_sql_user(service).should be false |
| 316 | + end |
| 317 | + end |
| 318 | + |
| 319 | + context 'user does not have privs to impersonate' do |
| 320 | + before(:each) do |
| 321 | + subject.stub_chain('session.sys.config.getuid').and_return('Superman') |
| 322 | + subject.stub_chain('client.sys.config.getprivs').and_return([]) |
| 323 | + end |
| 324 | + |
| 325 | + it 'should return true if successful' do |
| 326 | + expect(subject).to receive(:print_warning) |
| 327 | + subject.stub_chain('session.core.migrate').with(pid).and_return(true) |
| 328 | + subject.impersonate_sql_user(service).should be true |
| 329 | + end |
| 330 | + |
| 331 | + it 'should rescue an exception if migration fails' do |
| 332 | + expect(subject).to receive(:print_warning) |
| 333 | + subject.stub_chain('session.core.migrate').with(pid).and_raise(Rex::RuntimeError) |
| 334 | + subject.impersonate_sql_user(service).should be false |
| 335 | + end |
| 336 | + end |
| 337 | + end |
| 338 | + |
| 339 | + describe "#get_system" do |
| 340 | + it 'should return true if already SYSTEM' do |
| 341 | + expect(subject).to receive(:is_system?).and_return(true) |
| 342 | + subject.get_system.should be_truthy |
| 343 | + end |
| 344 | + |
| 345 | + it 'should return true if able to get SYSTEM and print a warning' do |
| 346 | + expect(subject).to receive(:is_system?).and_return(false) |
| 347 | + expect(subject).to receive(:print_warning) |
| 348 | + subject.stub_chain('session.priv.getsystem').and_return([true]) |
| 349 | + subject.get_system.should be_truthy |
| 350 | + end |
| 351 | + |
| 352 | + it 'should return false if unable to get SYSTEM and print a warning' do |
| 353 | + expect(subject).to receive(:is_system?).and_return(false) |
| 354 | + expect(subject).to receive(:print_warning) |
| 355 | + subject.stub_chain('session.priv.getsystem').and_return([false]) |
| 356 | + subject.get_system.should be_falsey |
| 357 | + end |
| 358 | + end |
| 359 | + |
272 | 360 | describe "#run_cmd" do
|
273 | 361 | it 'should return a string' do
|
274 | 362 | p = double('process')
|
|
377 | 465 | expect(subject).to receive(:run_cmd).and_return('SQL Server Command Line Tool')
|
378 | 466 | subject.check_sqlcmd.should be_truthy
|
379 | 467 | end
|
380 |
| - |
381 | 468 | end
|
382 | 469 |
|
383 | 470 | describe "#get_sql_client" do
|
|
0 commit comments