Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

# Authors: 

#   Jr Aquino <jr.aquino@citrixonline.com> 

#   Pavel Zuna <pzuna@redhat.com> 

# 

# Copyright (C) 2010  Red Hat 

# see file 'COPYING' for use and warranty information 

# 

# This program is free software; you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the GNU General Public License 

# along with this program.  If not, see <http://www.gnu.org/licenses/>. 

""" 

Test the `ipalib/plugins/sudorule.py` module. 

""" 

 

from nose.tools import raises, assert_raises  # pylint: disable=E0611 

 

from xmlrpc_test import XMLRPC_test, assert_attr_equal 

from ipalib import api 

from ipalib import errors 

 

class test_sudorule(XMLRPC_test): 

    """ 

    Test the `sudorule` plugin. 

    """ 

    rule_name = u'testing_sudorule1' 

    rule_name2 = u'testing_sudorule2' 

    rule_command = u'/usr/bin/testsudocmd1' 

    rule_desc = u'description' 

    rule_desc_mod = u'description modified' 

 

    test_user = u'sudorule_test_user' 

    test_external_user = u'external_test_user' 

    test_group = u'sudorule_test_group' 

    test_external_group = u'external_test_group' 

    test_host = u'sudorule.testhost' 

    test_external_host = u'external.testhost' 

    test_hostgroup = u'sudorule_test_hostgroup' 

    test_sudoallowcmdgroup = u'sudorule_test_allowcmdgroup' 

    test_sudodenycmdgroup = u'sudorule_test_denycmdgroup' 

    test_command = u'/usr/bin/testsudocmd1' 

    test_denycommand = u'/usr/bin/testdenysudocmd1' 

    test_runasuser = u'manager' 

    test_runasgroup = u'manager' 

    test_category = u'all' 

    test_option = u'authenticate' 

 

    test_invalid_user = u'+invalid#user' 

    test_invalid_host = u'+invalid&host.nonexist.com' 

    test_invalid_group = u'+invalid#group' 

 

    def test_0_sudorule_add(self): 

        """ 

        Test adding a new Sudo rule using `xmlrpc.sudorule_add`. 

        """ 

        ret = self.failsafe_add(api.Object.sudorule, 

            self.rule_name, 

            description=self.rule_desc, 

        ) 

        entry = ret['result'] 

        assert_attr_equal(entry, 'cn', self.rule_name) 

        assert_attr_equal(entry, 'description', self.rule_desc) 

 

    @raises(errors.DuplicateEntry) 

    def test_1_sudorule_add(self): 

        """ 

        Test adding an duplicate Sudo rule using `xmlrpc.sudorule_add'. 

        """ 

        api.Command['sudorule_add']( 

            self.rule_name 

        ) 

 

    def test_2_sudorule_show(self): 

        """ 

        Test displaying a Sudo rule using `xmlrpc.sudorule_show`. 

        """ 

        entry = api.Command['sudorule_show'](self.rule_name)['result'] 

        assert_attr_equal(entry, 'cn', self.rule_name) 

        assert_attr_equal(entry, 'description', self.rule_desc) 

 

    def test_3_sudorule_mod(self): 

        """ 

        Test modifying a Sudo rule using `xmlrpc.sudorule_mod`. 

        """ 

        ret = api.Command['sudorule_mod']( 

            self.rule_name, description=self.rule_desc_mod 

        ) 

        entry = ret['result'] 

        assert_attr_equal(entry, 'description', self.rule_desc_mod) 

 

    def test_6_sudorule_find(self): 

        """ 

        Test searching for Sudo rules using `xmlrpc.sudorule_find`. 

        """ 

        ret = api.Command['sudorule_find']( 

            cn=self.rule_name, 

            description=self.rule_desc_mod 

        ) 

        assert ret['truncated'] is False 

        entries = ret['result'] 

        assert_attr_equal(entries[0], 'cn', self.rule_name) 

        assert_attr_equal(entries[0], 'description', self.rule_desc_mod) 

 

    def test_7_sudorule_init_testing_data(self): 

        """ 

        Initialize data for more Sudo rule plugin testing. 

        """ 

        self.failsafe_add(api.Object.user, 

            self.test_user, givenname=u'first', sn=u'last' 

        ) 

        self.failsafe_add(api.Object.user, 

            self.test_runasuser, givenname=u'first', sn=u'last' 

        ) 

        self.failsafe_add(api.Object.group, 

            self.test_group, description=u'description' 

        ) 

        self.failsafe_add(api.Object.host, 

            self.test_host, force=True 

        ) 

        self.failsafe_add(api.Object.hostgroup, 

            self.test_hostgroup, description=u'description' 

        ) 

        self.failsafe_add(api.Object.sudocmdgroup, 

            self.test_sudoallowcmdgroup, description=u'desc' 

        ) 

        self.failsafe_add(api.Object.sudocmdgroup, 

            self.test_sudodenycmdgroup, description=u'desc' 

        ) 

        self.failsafe_add(api.Object.sudocmd, 

            self.test_command, description=u'desc' 

        ) 

 

    def test_8_sudorule_add_user(self): 

        """ 

        Test adding user and group to Sudo rule using 

        `xmlrpc.sudorule_add_user`. 

        """ 

        ret = api.Command['sudorule_add_user']( 

            self.rule_name, user=self.test_user, group=self.test_group 

        ) 

        assert ret['completed'] == 2 

        failed = ret['failed'] 

        assert 'memberuser' in failed 

        assert 'user' in failed['memberuser'] 

        assert not failed['memberuser']['user'] 

        assert 'group' in failed['memberuser'] 

        assert not failed['memberuser']['group'] 

        entry = ret['result'] 

        assert_attr_equal(entry, 'memberuser_user', self.test_user) 

        assert_attr_equal(entry, 'memberuser_group', self.test_group) 

 

    def test_9_a_show_user(self): 

        """ 

        Test showing a user to verify Sudo rule membership 

        `xmlrpc.user_show`. 

        """ 

        ret = api.Command['user_show'](self.test_user, all=True) 

        entry = ret['result'] 

        assert_attr_equal(entry, 'memberof_sudorule', self.rule_name) 

 

    def test_9_b_show_group(self): 

        """ 

        Test showing a group to verify Sudo rule membership 

        `xmlrpc.group_show`. 

        """ 

        ret = api.Command['group_show'](self.test_group, all=True) 

        entry = ret['result'] 

        assert_attr_equal(entry, 'memberof_sudorule', self.rule_name) 

 

    def test_9_sudorule_remove_user(self): 

        """ 

        Test removing user and group from Sudo rule using 

        `xmlrpc.sudorule_remove_user'. 

        """ 

        ret = api.Command['sudorule_remove_user']( 

            self.rule_name, user=self.test_user, group=self.test_group 

        ) 

        assert ret['completed'] == 2 

        failed = ret['failed'] 

        assert 'memberuser' in failed 

        assert 'user' in failed['memberuser'] 

        assert not failed['memberuser']['user'] 

        assert 'group' in failed['memberuser'] 

        assert not failed['memberuser']['group'] 

        entry = ret['result'] 

        assert 'memberuser_user' not in entry 

        assert 'memberuser_group' not in entry 

 

    def test_a_sudorule_add_runasuser(self): 

        """ 

        Test adding run as user to Sudo rule using 

        `xmlrpc.sudorule_add_runasuser`. 

        """ 

        ret = api.Command['sudorule_add_runasuser']( 

            self.rule_name, user=self.test_runasuser 

        ) 

        assert ret['completed'] == 1 

        failed = ret['failed'] 

        assert 'ipasudorunas' in failed 

        assert 'user' in failed['ipasudorunas'] 

        assert not failed['ipasudorunas']['user'] 

        entry = ret['result'] 

        assert_attr_equal(entry, 'ipasudorunas_user', self.test_runasuser) 

 

    def test_a_sudorule_add_runasuser_invalid(self): 

        """ 

        Test adding run as invalid user to Sudo rule using 

        `xmlrpc.sudorule_add_runasuser`. 

        """ 

        try: 

            api.Command['sudorule_add_runasuser']( 

                self.rule_name, user=self.test_invalid_user 

            ) 

        except errors.ValidationError: 

            pass 

        else: 

            assert False 

 

    def test_b_sudorule_remove_runasuser(self): 

        """ 

        Test removing run as user to Sudo rule using 

        `xmlrpc.sudorule_remove_runasuser'. 

        """ 

        ret = api.Command['sudorule_remove_runasuser']( 

            self.rule_name, user=self.test_runasuser 

        ) 

        assert ret['completed'] == 1 

        failed = ret['failed'] 

        assert 'ipasudorunas' in failed 

        assert 'user' in failed['ipasudorunas'] 

        assert not failed['ipasudorunas']['user'] 

        entry = ret['result'] 

        assert 'ipasudorunas_user' not in entry 

 

    def test_a_sudorule_add_runasgroup(self): 

        """ 

        Test adding run as group to Sudo rule using 

        `xmlrpc.sudorule_add_runasgroup`. 

        """ 

        ret = api.Command['sudorule_add_runasgroup']( 

            self.rule_name, group=self.test_runasgroup 

        ) 

        assert ret['completed'] == 1 

        failed = ret['failed'] 

        assert 'ipasudorunasgroup' in failed 

        assert 'group' in failed['ipasudorunasgroup'] 

        assert not failed['ipasudorunasgroup']['group'] 

        entry = ret['result'] 

        assert_attr_equal(entry, 'ipasudorunasgroup_group', 

            self.test_runasgroup) 

 

    def test_a_sudorule_add_runasgroup_invalid(self): 

        """ 

        Test adding run as invalid user to Sudo rule using 

        `xmlrpc.sudorule_add_runasuser`. 

        """ 

        try: 

            api.Command['sudorule_add_runasgroup']( 

                self.rule_name, group=self.test_invalid_group 

            ) 

        except errors.ValidationError: 

            pass 

        else: 

            assert False 

 

    def test_b_sudorule_remove_runasgroup(self): 

        """ 

        Test removing run as group to Sudo rule using 

        `xmlrpc.sudorule_remove_runasgroup'. 

        """ 

        ret = api.Command['sudorule_remove_runasgroup']( 

            self.rule_name, group=self.test_runasgroup 

        ) 

        assert ret['completed'] == 1 

        failed = ret['failed'] 

        assert 'ipasudorunasgroup' in failed 

        assert 'group' in failed['ipasudorunasgroup'] 

        assert not failed['ipasudorunasgroup']['group'] 

        entry = ret['result'] 

        assert 'ipasudorunasgroup_group' not in entry 

 

    def test_a_sudorule_add_externaluser(self): 

        """ 

        Test adding an external user to Sudo rule using 

        `xmlrpc.sudorule_add_user`. 

        """ 

        ret = api.Command['sudorule_add_user']( 

            self.rule_name, user=self.test_external_user 

        ) 

        assert ret['completed'] == 1 

        failed = ret['failed'] 

        entry = ret['result'] 

        assert_attr_equal(entry, 'externaluser', self.test_external_user) 

 

    def test_a_sudorule_add_externaluser_invalid(self): 

        """ 

        Test adding an invalid external user to Sudo rule using 

        `xmlrpc.sudorule_add_user`. 

        """ 

        try: 

            api.Command['sudorule_add_user']( 

                self.rule_name, user=self.test_invalid_user 

            ) 

        except errors.ValidationError: 

            pass 

        else: 

            assert False 

 

    def test_b_sudorule_remove_externaluser(self): 

        """ 

        Test removing an external user from Sudo rule using 

        `xmlrpc.sudorule_remove_user'. 

        """ 

        ret = api.Command['sudorule_remove_user']( 

            self.rule_name, user=self.test_external_user 

        ) 

        assert ret['completed'] == 1 

        failed = ret['failed'] 

        entry = ret['result'] 

        assert entry['externaluser'] == () 

 

    def test_a_sudorule_add_runasexternaluser(self): 

        """ 

        Test adding an external runasuser to Sudo rule using 

        `xmlrpc.sudorule_add_runasuser`. 

        """ 

        ret = api.Command['sudorule_add_runasuser']( 

            self.rule_name, user=self.test_external_user 

        ) 

        assert ret['completed'] == 1 

        failed = ret['failed'] 

        entry = ret['result'] 

        assert_attr_equal(entry, 'ipasudorunasextuser', self.test_external_user) 

 

    def test_b_sudorule_remove_runasexternaluser(self): 

        """ 

        Test removing an external runasuser from Sudo rule using 

        `xmlrpc.sudorule_remove_runasuser'. 

        """ 

        ret = api.Command['sudorule_remove_runasuser']( 

            self.rule_name, user=self.test_external_user 

        ) 

        assert ret['completed'] == 1 

        failed = ret['failed'] 

        entry = ret['result'] 

        assert entry['ipasudorunasextuser'] == () 

 

    def test_a_sudorule_add_runasexternalgroup(self): 

        """ 

        Test adding an external runasgroup to Sudo rule using 

        `xmlrpc.sudorule_add_runasgroup`. 

        """ 

        ret = api.Command['sudorule_add_runasgroup']( 

            self.rule_name, group=self.test_external_group 

        ) 

        assert ret['completed'] == 1 

        failed = ret['failed'] 

        entry = ret['result'] 

        assert_attr_equal(entry, 'ipasudorunasextgroup', self.test_external_group) 

 

    def test_b_sudorule_remove_runasexternalgroup(self): 

        """ 

        Test removing an external runasgroup from Sudo rule using 

        `xmlrpc.sudorule_remove_runasgroup'. 

        """ 

        ret = api.Command['sudorule_remove_runasgroup']( 

            self.rule_name, group=self.test_external_group 

        ) 

        assert ret['completed'] == 1 

        failed = ret['failed'] 

        entry = ret['result'] 

        assert entry['ipasudorunasextgroup'] == () 

 

    def test_a_sudorule_add_option(self): 

        """ 

        Test adding an option to Sudo rule using 

        `xmlrpc.sudorule_add_option`. 

        """ 

        ret = api.Command['sudorule_add_option']( 

            self.rule_name, ipasudoopt=self.test_option 

        ) 

        entry = ret['result'] 

        assert_attr_equal(entry, 'ipasudoopt', self.test_option) 

 

    def test_b_sudorule_remove_option(self): 

        """ 

        Test removing an option from Sudo rule using 

        `xmlrpc.sudorule_remove_option'. 

        """ 

        ret = api.Command['sudorule_remove_option']( 

            self.rule_name, ipasudoopt=self.test_option 

        ) 

        entry = ret['result'] 

        assert 'ipasudoopt' not in entry 

 

    def test_a_sudorule_add_host(self): 

        """ 

        Test adding host and hostgroup to Sudo rule using 

        `xmlrpc.sudorule_add_host`. 

        """ 

        ret = api.Command['sudorule_add_host']( 

            self.rule_name, host=self.test_host, hostgroup=self.test_hostgroup 

        ) 

        assert ret['completed'] == 2 

        failed = ret['failed'] 

        assert 'memberhost' in failed 

        assert 'host' in failed['memberhost'] 

        assert not failed['memberhost']['host'] 

        assert 'hostgroup' in failed['memberhost'] 

        assert not failed['memberhost']['hostgroup'] 

        entry = ret['result'] 

        assert_attr_equal(entry, 'memberhost_host', self.test_host) 

        assert_attr_equal(entry, 'memberhost_hostgroup', self.test_hostgroup) 

 

    def test_a_sudorule_show_host(self): 

        """ 

        Test showing host to verify Sudo rule membership 

        `xmlrpc.host_show`. 

        """ 

        ret = api.Command['host_show'](self.test_host, all=True) 

        entry = ret['result'] 

        assert_attr_equal(entry, 'memberof_sudorule', self.rule_name) 

 

    def test_a_sudorule_show_hostgroup(self): 

        """ 

        Test showing hostgroup to verify Sudo rule membership 

        `xmlrpc.hostgroup_show`. 

        """ 

        ret = api.Command['hostgroup_show'](self.test_hostgroup, all=True) 

        entry = ret['result'] 

        assert_attr_equal(entry, 'memberof_sudorule', self.rule_name) 

 

    def test_b_sudorule_remove_host(self): 

        """ 

        Test removing host and hostgroup from Sudo rule using 

        `xmlrpc.sudorule_remove_host`. 

        """ 

        ret = api.Command['sudorule_remove_host']( 

            self.rule_name, host=self.test_host, hostgroup=self.test_hostgroup 

        ) 

        assert ret['completed'] == 2 

        failed = ret['failed'] 

        assert 'memberhost' in failed 

        assert 'host' in failed['memberhost'] 

        assert not failed['memberhost']['host'] 

        assert 'hostgroup' in failed['memberhost'] 

        assert not failed['memberhost']['hostgroup'] 

        entry = ret['result'] 

        assert 'memberhost_host' not in entry 

        assert 'memberhost_hostgroup' not in entry 

 

    def test_a_sudorule_add_externalhost(self): 

        """ 

        Test adding an external host to Sudo rule using 

        `xmlrpc.sudorule_add_host`. 

        """ 

        ret = api.Command['sudorule_add_host']( 

            self.rule_name, host=self.test_external_host 

        ) 

        assert ret['completed'] == 1 

        failed = ret['failed'] 

        entry = ret['result'] 

        assert_attr_equal(entry, 'externalhost', self.test_external_host) 

 

    def test_a_sudorule_add_externalhost_invalid(self): 

        """ 

        Test adding an invalid external host to Sudo rule using 

        `xmlrpc.sudorule_add_host`. 

        """ 

        try: 

            api.Command['sudorule_add_host']( 

                self.rule_name, host=self.test_invalid_host 

            ) 

        except errors.ValidationError: 

            pass 

        else: 

            assert False 

 

    def test_a_sudorule_mod_externalhost_invalid_addattr(self): 

        """ 

        Test adding an invalid external host to Sudo rule using 

        `xmlrpc.sudorule_mod --addattr`. 

        """ 

        try: 

            api.Command['sudorule_mod']( 

                self.rule_name, 

                addattr='externalhost=%s' % self.test_invalid_host 

            ) 

        except errors.ValidationError, e: 

            assert unicode(e) == ("invalid 'externalhost': only letters, " + 

                "numbers, _, and - are allowed. " + 

                "DNS label may not start or end with -") 

        else: 

            assert False 

 

    def test_b_sudorule_remove_externalhost(self): 

        """ 

        Test removing an external host from Sudo rule using 

        `xmlrpc.sudorule_remove_host`. 

        """ 

        ret = api.Command['sudorule_remove_host']( 

            self.rule_name, host=self.test_external_host 

        ) 

        assert ret['completed'] == 1 

        failed = ret['failed'] 

        entry = ret['result'] 

        assert len(entry['externalhost']) == 0 

 

    def test_a_sudorule_add_allow_command(self): 

        """ 

        Test adding allow command and cmdgroup to Sudo rule using 

        `xmlrpc.sudorule_add_allow_command`. 

        """ 

        ret = api.Command['sudorule_add_allow_command']( 

            self.rule_name, sudocmd=self.test_command, 

            sudocmdgroup=self.test_sudoallowcmdgroup 

        ) 

        assert ret['completed'] == 2 

        failed = ret['failed'] 

        assert 'memberallowcmd' in failed 

        assert 'sudocmd' in failed['memberallowcmd'] 

        assert not failed['memberallowcmd']['sudocmd'] 

        assert 'sudocmdgroup' in failed['memberallowcmd'] 

        assert not failed['memberallowcmd']['sudocmdgroup'] 

        entry = ret['result'] 

        assert_attr_equal(entry, 'memberallowcmd_sudocmd', self.test_command) 

        assert_attr_equal(entry, 'memberallowcmd_sudocmdgroup', 

            self.test_sudoallowcmdgroup) 

 

    def test_a_sudorule_remove_allow_command(self): 

        """ 

        Test removing allow command and sudocmdgroup from Sudo rule using 

        `xmlrpc.sudorule_remove_command`. 

        """ 

        ret = api.Command['sudorule_remove_allow_command']( 

            self.rule_name, sudocmd=self.test_command, 

            sudocmdgroup=self.test_sudoallowcmdgroup 

        ) 

        assert ret['completed'] == 2 

        failed = ret['failed'] 

        assert 'memberallowcmd' in failed 

        assert 'sudocmd' in failed['memberallowcmd'] 

        assert not failed['memberallowcmd']['sudocmd'] 

        assert 'sudocmdgroup' in failed['memberallowcmd'] 

        assert not failed['memberallowcmd']['sudocmdgroup'] 

        entry = ret['result'] 

        assert 'memberallowcmd_sudocmd' not in entry 

        assert 'memberallowcmd_sudocmdgroup' not in entry 

 

    def test_b_sudorule_add_deny_command(self): 

        """ 

        Test adding deny command and cmdgroup to Sudo rule using 

        `xmlrpc.sudorule_add_deny_command`. 

        """ 

        ret = api.Command['sudorule_add_deny_command']( 

            self.rule_name, sudocmd=self.test_command, 

            sudocmdgroup=self.test_sudodenycmdgroup 

        ) 

        assert ret['completed'] == 2 

        failed = ret['failed'] 

        assert 'memberdenycmd' in failed 

        assert 'sudocmd' in failed['memberdenycmd'] 

        assert not failed['memberdenycmd']['sudocmd'] 

        assert 'sudocmdgroup' in failed['memberdenycmd'] 

        assert not failed['memberdenycmd']['sudocmdgroup'] 

        entry = ret['result'] 

        assert_attr_equal(entry, 'memberdenycmd_sudocmd', self.test_command) 

        assert_attr_equal(entry, 'memberdenycmd_sudocmdgroup', 

            self.test_sudodenycmdgroup) 

 

    def test_b_sudorule_remove_deny_command(self): 

        """ 

        Test removing deny command and sudocmdgroup from Sudo rule using 

        `xmlrpc.sudorule_remove_deny_command`. 

        """ 

        ret = api.Command['sudorule_remove_deny_command']( 

            self.rule_name, sudocmd=self.test_command, 

            sudocmdgroup=self.test_sudodenycmdgroup 

        ) 

        assert ret['completed'] == 2 

        failed = ret['failed'] 

        assert 'memberdenycmd' in failed 

        assert 'sudocmd' in failed['memberdenycmd'] 

        assert not failed['memberdenycmd']['sudocmd'] 

        assert 'sudocmdgroup' in failed['memberdenycmd'] 

        assert not failed['memberdenycmd']['sudocmdgroup'] 

        entry = ret['result'] 

        assert 'memberdenycmd_sudocmd' not in entry 

        assert 'memberdenycmd_sudocmdgroup' not in entry 

 

    @raises(errors.MutuallyExclusiveError) 

    def test_c_sudorule_exclusiveuser(self): 

        """ 

        Test adding a user to an Sudo rule when usercat='all' 

        """ 

        api.Command['sudorule_mod'](self.rule_name, usercategory=u'all') 

        try: 

            api.Command['sudorule_add_user'](self.rule_name, user=u'admin') 

        finally: 

            api.Command['sudorule_mod'](self.rule_name, usercategory=u'') 

 

    @raises(errors.MutuallyExclusiveError) 

    def test_d_sudorule_exclusiveuser(self): 

        """ 

        Test setting usercat='all' in an Sudo rule when there are users 

        """ 

        api.Command['sudorule_add_user'](self.rule_name, user=u'admin') 

        try: 

            api.Command['sudorule_mod'](self.rule_name, usercategory=u'all') 

        finally: 

            api.Command['sudorule_remove_user'](self.rule_name, user=u'admin') 

 

    @raises(errors.MutuallyExclusiveError) 

    def test_e_sudorule_exclusivehost(self): 

        """ 

        Test adding a host to an Sudo rule when hostcat='all' 

        """ 

        api.Command['sudorule_mod'](self.rule_name, hostcategory=u'all') 

        try: 

            api.Command['sudorule_add_host'](self.rule_name, host=self.test_host) 

        finally: 

            api.Command['sudorule_mod'](self.rule_name, hostcategory=u'') 

 

    @raises(errors.MutuallyExclusiveError) 

    def test_f_sudorule_exclusivehost(self): 

        """ 

        Test setting hostcat='all' in an Sudo rule when there are hosts 

        """ 

        api.Command['sudorule_add_host'](self.rule_name, host=self.test_host) 

        try: 

            api.Command['sudorule_mod'](self.rule_name, hostcategory=u'all') 

        finally: 

            api.Command['sudorule_remove_host'](self.rule_name, host=self.test_host) 

 

    @raises(errors.MutuallyExclusiveError) 

    def test_g_sudorule_exclusivecommand(self): 

        """ 

        Test adding a command to an Sudo rule when cmdcategory='all' 

        """ 

        api.Command['sudorule_mod'](self.rule_name, cmdcategory=u'all') 

        try: 

            api.Command['sudorule_add_allow_command'](self.rule_name, sudocmd=self.test_command) 

        finally: 

            api.Command['sudorule_mod'](self.rule_name, cmdcategory=u'') 

 

    @raises(errors.MutuallyExclusiveError) 

    def test_h_sudorule_exclusivecommand(self): 

        """ 

        Test setting cmdcategory='all' in an Sudo rule when there are commands 

        """ 

        api.Command['sudorule_add_allow_command'](self.rule_name, sudocmd=self.test_command) 

        try: 

            api.Command['sudorule_mod'](self.rule_name, cmdcategory=u'all') 

        finally: 

            api.Command['sudorule_remove_allow_command'](self.rule_name, sudocmd=self.test_command) 

 

    @raises(errors.MutuallyExclusiveError) 

    def test_i_sudorule_exclusiverunas(self): 

        """ 

        Test adding a runasuser to an Sudo rule when ipasudorunasusercategory='all' 

        """ 

        api.Command['sudorule_mod'](self.rule_name, ipasudorunasusercategory=u'all') 

        try: 

            api.Command['sudorule_add_runasuser'](self.rule_name, user=self.test_user) 

        finally: 

            api.Command['sudorule_mod'](self.rule_name, ipasudorunasusercategory=u'') 

 

    @raises(errors.MutuallyExclusiveError) 

    def test_j_1_sudorule_exclusiverunas(self): 

        """ 

        Test setting ipasudorunasusercategory='all' in an Sudo rule when there are runas users 

        """ 

        api.Command['sudorule_add_runasuser'](self.rule_name, user=self.test_user) 

        try: 

            api.Command['sudorule_mod'](self.rule_name, ipasudorunasusercategory=u'all') 

        finally: 

            api.Command['sudorule_remove_runasuser'](self.rule_name, user=self.test_command) 

 

    def test_j_2_sudorule_referential_integrity(self): 

        """ 

        Test adding various links to Sudo rule 

        """ 

        api.Command['sudorule_add_user'](self.rule_name, user=self.test_user) 

        api.Command['sudorule_add_runasuser'](self.rule_name, user=self.test_runasuser, 

                                              group=self.test_group) 

        api.Command['sudorule_add_runasgroup'](self.rule_name, group=self.test_group) 

        api.Command['sudorule_add_host'](self.rule_name, host=self.test_host) 

        api.Command['sudorule_add_allow_command'](self.rule_name, 

                                                  sudocmd=self.test_command) 

        api.Command['sudorule_add_deny_command'](self.rule_name, 

                                                 sudocmdgroup=self.test_sudodenycmdgroup) 

        entry = api.Command['sudorule_show'](self.rule_name)['result'] 

        assert_attr_equal(entry, 'cn', self.rule_name) 

        assert_attr_equal(entry, 'memberuser_user', self.test_user) 

        assert_attr_equal(entry, 'memberallowcmd_sudocmd', self.test_command) 

        assert_attr_equal(entry, 'memberdenycmd_sudocmdgroup', 

            self.test_sudodenycmdgroup) 

        assert_attr_equal(entry, 'memberhost_host', self.test_host) 

        assert_attr_equal(entry, 'ipasudorunas_user', self.test_runasuser) 

        assert_attr_equal(entry, 'ipasudorunas_group', self.test_group) 

        assert_attr_equal(entry, 'ipasudorunasgroup_group', self.test_group) 

 

 

    def test_k_1_sudorule_clear_testing_data(self): 

        """ 

        Clear data for Sudo rule plugin testing. 

        """ 

        api.Command['user_del'](self.test_user) 

        api.Command['user_del'](self.test_runasuser) 

        api.Command['group_del'](self.test_group) 

        api.Command['host_del'](self.test_host) 

        api.Command['hostgroup_del'](self.test_hostgroup) 

        api.Command['sudorule_remove_allow_command'](self.rule_name, 

                                                     sudocmd=self.test_command) 

        api.Command['sudocmd_del'](self.test_command) 

        api.Command['sudocmdgroup_del'](self.test_sudoallowcmdgroup) 

        api.Command['sudocmdgroup_del'](self.test_sudodenycmdgroup) 

 

    def test_k_2_sudorule_referential_integrity(self): 

        """ 

        Test that links in Sudo rule were removed by referential integrity plugin 

        """ 

        entry = api.Command['sudorule_show'](self.rule_name)['result'] 

        assert_attr_equal(entry, 'cn', self.rule_name) 

        assert 'memberuser_user' not in entry 

        assert 'memberallowcmd_sudocmd' not in entry 

        assert 'memberdenycmd_sudocmdgroup' not in entry 

        assert 'memberhost_host' not in entry 

        assert 'ipasudorunas_user' not in entry 

        assert 'ipasudorunas_group' not in entry 

        assert 'ipasudorunasgroup_group' not in entry 

 

    def test_l_sudorule_order(self): 

        """ 

        Test that order uniqueness is maintained 

        """ 

        api.Command['sudorule_mod'](self.rule_name, sudoorder=1) 

 

        api.Command['sudorule_add'](self.rule_name2) 

 

        # mod of rule that has no order and set a duplicate 

        try: 

            api.Command['sudorule_mod'](self.rule_name2, sudoorder=1) 

        except errors.ValidationError: 

            pass 

 

        # Remove the rule so we can re-add it 

        api.Command['sudorule_del'](self.rule_name2) 

 

        # add a new rule with a duplicate order 

        with assert_raises(errors.ValidationError): 

            api.Command['sudorule_add'](self.rule_name2, sudoorder=1) 

 

        # add a new rule with a unique order 

        api.Command['sudorule_add'](self.rule_name2, sudoorder=2) 

        with assert_raises(errors.ValidationError): 

            api.Command['sudorule_mod'](self.rule_name2, sudoorder=1) 

 

        # Try setting both to 0 

        api.Command['sudorule_mod'](self.rule_name2, sudoorder=0) 

        with assert_raises(errors.ValidationError): 

            api.Command['sudorule_mod'](self.rule_name, sudoorder=0) 

 

 

    def test_m_sudorule_del(self): 

        """ 

        Test deleting a Sudo rule using `xmlrpc.sudorule_del`. 

        """ 

        api.Command['sudorule_del'](self.rule_name) 

        # verify that it's gone 

        with assert_raises(errors.NotFound): 

            api.Command['sudorule_show'](self.rule_name) 

        api.Command['sudorule_del'](self.rule_name2)