Hide keyboard shortcuts

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

#!/usr/bin/env python3 

# vim: set fileencoding=utf-8 

 

# Copyright © 2020, 2021 Pavel Tisnovsky 

# 

# Licensed under the Apache License, Version 2.0 (the "License"); 

# you may not use this file except in compliance with the License. 

# You may obtain a copy of the License at 

# 

# http://www.apache.org/licenses/LICENSE-2.0 

# 

# Unless required by applicable law or agreed to in writing, software 

# distributed under the License is distributed on an "AS IS" BASIS, 

# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

# See the License for the specific language governing permissions and 

# limitations under the License. 

 

"""Set of custom validators (predicates) used in data schemes.""" 

 

 

import datetime 

import string 

import re 

import json 

import base64 

import math 

from uuid import UUID 

 

from voluptuous import Schema 

from voluptuous import Invalid 

from voluptuous import Required 

 

 

def intTypeValidator(value): 

"""Validate value for any integer.""" 

# check if the given value is an integer 

if type(value) is not int: 

raise Invalid("integer expected, but invalid value type {v}".format(v=value)) 

 

 

def posIntValidator(value): 

"""Validate value for positive integers.""" 

# check if the given value is integer greater than zero 

intTypeValidator(value) 

if value <= 0: 

raise Invalid("positive integer value expected, but got {v} instead".format(v=value)) 

 

 

def posIntOrZeroValidator(value): 

"""Validate value for positive integers or zeroes.""" 

# check if the given value is integer greater than or equal to zero 

intTypeValidator(value) 

if value < 0: 

raise Invalid("positive integer or 0 value expected, but got {v} instead".format(v=value)) 

 

 

def negIntValidator(value): 

"""Validate value for negative integers.""" 

# check if the given value is integer less than zero 

intTypeValidator(value) 

if value >= 0: 

raise Invalid("negative integer value expected, but got {v} instead".format(v=value)) 

 

 

def negIntOrZeroValidator(value): 

"""Validate value for negative integers or zeroes.""" 

# check if the given value is integer less than or equal zero 

intTypeValidator(value) 

if value > 0: 

raise Invalid("negative integer or 0 value expected, but got {v} instead".format(v=value)) 

 

 

def floatTypeValidator(value): 

"""Validate value for any float.""" 

# check if the given value is a float 

if type(value) is not float: 

raise Invalid("invalid value type {value}".format(value=value)) 

 

 

def posFloatValidator(value): 

"""Predicate that checks if the given value is positive float.""" 

# check if the value has the expected type 

floatTypeValidator(value) 

 

# check for NaN 

isNotNaNValidator(value) 

 

if value <= 0.0: 

raise Invalid("invalid value {value}, positive float expected".format(value=value)) 

 

 

def posFloatOrZeroValidator(value): 

"""Predicate that checks if the given value is positive float or zero.""" 

# check if the value has the expected type 

floatTypeValidator(value) 

 

# check for NaN 

isNotNaNValidator(value) 

 

if value < 0.0: 

raise Invalid("invalid value {value}, positive float or zero expected".format(value=value)) 

 

 

def negFloatValidator(value): 

"""Predicate that checks if the given value is positive float.""" 

# check if the value has the expected type 

floatTypeValidator(value) 

 

# check for NaN 

isNotNaNValidator(value) 

 

if value >= 0.0: 

raise Invalid("invalid value {value}, negative float expected".format(value=value)) 

 

 

def negFloatOrZeroValidator(value): 

"""Predicate that checks if the given value is positive float or zero.""" 

# check if the value has the expected type 

floatTypeValidator(value) 

 

# check for NaN 

isNotNaNValidator(value) 

 

if value > 0.0: 

raise Invalid("invalid value {value}, negative float or zero expected".format(value=value)) 

 

 

def isNaNValidator(value): 

"""Predicate that checks if the given value is NaN.""" 

# check if the value has the expected type 

floatTypeValidator(value) 

 

if not math.isnan(value): 

raise Invalid("invalid value {value}, NaN expected".format(value=value)) 

 

 

def isNotNaNValidator(value): 

"""Predicate that checks if the given value is not NaN.""" 

# check if the value has the expected type 

floatTypeValidator(value) 

 

if math.isnan(value): 

raise Invalid("invalid value {value}, NaN is not expected".format(value=value)) 

 

 

def stringTypeValidator(value): 

"""Validate value for string type.""" 

# check if the given value is a string 

if type(value) is not str: 

raise Invalid("string value expected, but got {t} type instead".format(t=type(value))) 

 

 

def bytesTypeValidator(value): 

"""Validate value for byte array type.""" 

# check if the given value is a byte array 

if type(value) is not bytes: 

raise Invalid("byte array value expected, but got {t} type instead".format(t=type(value))) 

 

 

def emptyStringValidator(value): 

"""Validate value for an empty string.""" 

stringTypeValidator(value) 

 

# check for non-empty string 

if value: 

raise Invalid("Empty string value expected") 

 

 

def notEmptyStringValidator(value): 

"""Validate value for a non-empty string.""" 

stringTypeValidator(value) 

 

# check for empty string 

if not value: 

raise Invalid("empty string should not be used there") 

 

 

def intInStringValidator(value): 

"""Validate value for an int value stored as a string.""" 

stringTypeValidator(value) 

 

# try to parse the string 

x = int(value) 

 

 

def posIntInStringValidator(value): 

"""Validate value for a positive int value stored as a string.""" 

stringTypeValidator(value) 

 

# try to parse the string 

x = int(value) 

posIntValidator(x) 

 

 

def posIntOrZeroInStringValidator(value): 

"""Validate value for a positive int value or zero stored as a string.""" 

stringTypeValidator(value) 

 

# try to parse the string 

x = int(value) 

posIntOrZeroValidator(x) 

 

 

def negIntInStringValidator(value): 

"""Validate value for a negative int value stored as a string.""" 

stringTypeValidator(value) 

 

# try to parse the string 

x = int(value) 

negIntValidator(x) 

 

 

def negIntOrZeroInStringValidator(value): 

"""Validate value for a negative int value or zero stored as a string.""" 

stringTypeValidator(value) 

 

# try to parse the string 

x = int(value) 

negIntOrZeroValidator(x) 

 

 

def posFloatInStringValidator(value): 

"""Validate value for a positive float value stored as a string.""" 

stringTypeValidator(value) 

 

# try to parse the string 

x = float(value) 

posFloatValidator(x) 

 

 

def posFloatOrZeroInStringValidator(value): 

"""Validate value for a positive float value or zero stored as a string.""" 

stringTypeValidator(value) 

 

# try to parse the string 

x = float(value) 

posFloatOrZeroValidator(x) 

 

 

def negFloatInStringValidator(value): 

"""Validate value for a negative float value stored as a string.""" 

stringTypeValidator(value) 

 

# try to parse the string 

x = float(value) 

negFloatValidator(x) 

 

 

def negFloatOrZeroInStringValidator(value): 

"""Validate value for a negative float value or zero stored as a string.""" 

stringTypeValidator(value) 

 

# try to parse the string 

x = float(value) 

negFloatOrZeroValidator(x) 

 

 

def intInBytesValidator(value): 

"""Validate value for an int value stored as a string.""" 

# check if the value has the expected type 

bytesTypeValidator(value) 

 

# use default encoding 

value = value.decode("utf-8") 

 

# check new type 

stringTypeValidator(value) 

 

# try to parse the string 

x = int(value) 

 

 

def posIntInBytesValidator(value): 

"""Validate value for a positive int value stored as a string.""" 

# check if the value has the expected type 

bytesTypeValidator(value) 

 

# use default encoding 

value = value.decode("utf-8") 

 

# check new type 

stringTypeValidator(value) 

 

# try to parse the string 

x = int(value) 

posIntValidator(x) 

 

 

def posIntOrZeroInBytesValidator(value): 

"""Validate value for a positive int value or zero stored as a string.""" 

# check if the value has the expected type 

bytesTypeValidator(value) 

 

# use default encoding 

value = value.decode("utf-8") 

 

# check new type 

stringTypeValidator(value) 

 

# try to parse the string 

x = int(value) 

posIntOrZeroValidator(x) 

 

 

def negIntInBytesValidator(value): 

"""Validate value for a negative int value stored as a string.""" 

# check if the value has the expected type 

bytesTypeValidator(value) 

 

# use default encoding 

value = value.decode("utf-8") 

 

# check new type 

stringTypeValidator(value) 

 

# try to parse the string 

x = int(value) 

negIntValidator(x) 

 

 

def negIntOrZeroInBytesValidator(value): 

"""Validate value for a negative int value or zero stored as a string.""" 

# check if the value has the expected type 

bytesTypeValidator(value) 

 

# use default encoding 

value = value.decode("utf-8") 

 

# check new type 

stringTypeValidator(value) 

 

# try to parse the string 

x = int(value) 

negIntOrZeroValidator(x) 

 

 

def posFloatInBytesValidator(value): 

"""Validate value for a positive float value stored as a string.""" 

# check if the value has the expected type 

bytesTypeValidator(value) 

 

# use default encoding 

value = value.decode("utf-8") 

 

# check new type 

stringTypeValidator(value) 

 

# try to parse the string 

x = float(value) 

posFloatValidator(x) 

 

 

def posFloatOrZeroInBytesValidator(value): 

"""Validate value for a positive float value or zero stored as a string.""" 

# check if the value has the expected type 

bytesTypeValidator(value) 

 

# use default encoding 

value = value.decode("utf-8") 

 

# check new type 

stringTypeValidator(value) 

 

# try to parse the string 

x = float(value) 

posFloatOrZeroValidator(x) 

 

 

def negFloatInBytesValidator(value): 

"""Validate value for a negative float value stored as a string.""" 

# check if the value has the expected type 

bytesTypeValidator(value) 

 

# use default encoding 

value = value.decode("utf-8") 

 

# check new type 

stringTypeValidator(value) 

 

# try to parse the string 

x = float(value) 

negFloatValidator(x) 

 

 

def negFloatOrZeroInBytesValidator(value): 

"""Validate value for a negative float value or zero stored as a string.""" 

# check if the value has the expected type 

bytesTypeValidator(value) 

 

# use default encoding 

value = value.decode("utf-8") 

 

# check new type 

stringTypeValidator(value) 

 

# try to parse the string 

x = float(value) 

negFloatOrZeroValidator(x) 

 

 

def hexaString32Validator(value): 

"""Validate value for string containign exactly 32 hexadecimal digits.""" 

stringTypeValidator(value) 

 

if len(value) == 0: 

raise Invalid("string is empty".format(len(value))) 

if len(value) != 32: 

raise Invalid("wrong number of digits: {}".format(len(value))) 

if not all(c in string.hexdigits for c in value): 

raise Invalid("non-hexadecimal char detected in: {}".format(value)) 

 

 

def timestampValidator(value): 

"""Validate value for timestamps.""" 

stringTypeValidator(value) 

 

timeformat = '%Y-%m-%dT%H:%M:%SZ' 

try: 

# try to parse the input value 

datetime.datetime.strptime(value, timeformat) 

except ValueError: 

raise Invalid("invalid datetime value {value}".format(value=value)) 

 

 

def timestampValidatorOffset(value): 

"""Validate value for timestamps.""" 

stringTypeValidator(value) 

 

timeformat = '%Y-%m-%dT%H:%M:%S.%f+00:00' 

try: 

# try to parse the input value 

datetime.datetime.strptime(value, timeformat) 

except ValueError: 

raise Invalid("invalid datetime value {value}".format(value=value)) 

 

 

def timestampValidatorNoZ(value): 

"""Validate value for timestamps.""" 

stringTypeValidator(value) 

 

timeformat = '%Y-%m-%dT%H:%M:%S' 

try: 

# try to parse the input value 

datetime.datetime.strptime(value, timeformat) 

except ValueError: 

raise Invalid("invalid datetime value {value}".format(value=value)) 

 

 

def timestampValidatorMs(value): 

"""Validate value for timestamps without ms part, but with TZ info.""" 

stringTypeValidator(value) 

 

timeformat = '%Y-%m-%dT%H:%M:%S.%f' 

try: 

# the following timestamp can't be parsed directly by Python 

# "2020-12-09T16:17:42.822020204Z" 

if len(value) >= 26: 

value = value[0:26] 

# try to parse the input value 

datetime.datetime.strptime(value, timeformat) 

except ValueError: 

raise Invalid("invalid datetime value {value}".format(value=value)) 

 

 

def keyValueValidator(value): 

"""Validate if value conformns to a key used in Insights Results.""" 

stringTypeValidator(value) 

 

KEY_VALUE_RE = re.compile(r"[A-Z0-9]+([_][A-Z0-9]+)+") 

if not KEY_VALUE_RE.fullmatch(value): 

raise Invalid("wrong key value '{}'".format(value)) 

 

 

def ruleFQDNValidator(value): 

"""Validate if value contains FQDN (fully-qualified name).""" 

stringTypeValidator(value) 

 

FQDN_VALUE_RE = re.compile(r"([a-zA-Z0-9_]+[.])+[a-z0-9_]+") 

if not FQDN_VALUE_RE.fullmatch(value): 

raise Invalid("wrong FQDN '{}'".format(value)) 

 

 

def ruleFQDNInBytesValidator(value): 

"""Validate if value contains FQDN (fully-qualified name).""" 

# check if the value has the expected type 

bytesTypeValidator(value) 

 

# use default encoding 

value = value.decode("utf-8") 

 

stringTypeValidator(value) 

 

FQDN_VALUE_RE = re.compile(r"([a-z0-9_]+[.])+[a-z0-9_]+") 

if not FQDN_VALUE_RE.fullmatch(value): 

raise Invalid("wrong FQDN '{}'".format(value)) 

 

 

def ruleIDValidator(value): 

"""Validate if value contains rule ID.""" 

stringTypeValidator(value) 

 

FQDN_VALUE_RE = re.compile(r"[a-zA-Z0-9_]+([_][a-z0-9_]+)+\|[A-Z0-9_]+([_][A-Z0-9_]+)+") 

if not FQDN_VALUE_RE.fullmatch(value): 

raise Invalid("wrong FQDN '{}'".format(value)) 

 

 

def ruleIDInBytesValidator(value): 

"""Validate if value contains rule ID.""" 

# check if the value has the expected type 

bytesTypeValidator(value) 

 

# use default encoding 

value = value.decode("utf-8") 

 

stringTypeValidator(value) 

 

FQDN_VALUE_RE = re.compile(r"[A-Za-z0-9_]+([_][A-Za-z0-9_]+)+\|[A-Z0-9_]+([_][A-Z0-9_]+)+") 

if not FQDN_VALUE_RE.fullmatch(value): 

raise Invalid("wrong FQDN '{}'".format(value)) 

 

 

def urlToAWSValidator(value): 

"""Validate if value conformns to AWS S3 URL.""" 

stringTypeValidator(value) 

 

# https://<hostname>/service_id/file_id?<credentials and other params> 

HTTP_RE = re.compile( 

r"^(?:https://[^/]+\.s3\.amazonaws\.com/[0-9a-zA-Z/\-]+|" 

r"https://s3\.[0-9a-zA-Z\-]+\.amazonaws\.com/[0-9a-zA-Z\-]+/[0-9a-zA-Z/\-]+|" 

r"http://minio:9000/insights-upload-perma/[0-9a-zA-Z\.\-]+/[0-9a-zA-Z\-]+)\?" 

r"X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=[^/]+$" 

) 

if not HTTP_RE.fullmatch(value): 

raise Invalid("wrong URL") 

 

 

def domainValidator(value): 

"""Validate if value conformns to (e-mail) domain.""" 

stringTypeValidator(value) 

 

DOMAIN_RE = re.compile(r"((?!-)[A-Za-z0-9-]{1,63}(?<!-)\.)+[A-Za-z]{2,6}") 

if not DOMAIN_RE.fullmatch(value): 

raise Invalid("wrong e-mail domain:" + value) 

 

 

def domainInBytesValidator(value): 

"""Validate if value conformns to (e-mail) domain.""" 

bytesTypeValidator(value) 

 

value = value.decode("utf-8") 

 

stringTypeValidator(value) 

domainValidator(value) 

 

 

def uuidValidator(value, version=4): 

"""Check if value conforms to UUID.""" 

# check if the value has the expected type 

stringTypeValidator(value) 

 

# UUID version 4 is the most common version, but it is possible to specify 

# other version as well 

UUID(value, version=version) 

 

 

def uuidInBytesValidator(value, version=4): 

"""Check if value conforms to UUID.""" 

# check if the value has the expected type 

bytesTypeValidator(value) 

 

# use default encoding 

value = value.decode("utf-8") 

 

# UUID version 4 is the most common version, but it is possible to specify 

# other version as well 

UUID(value, version=version) 

 

 

def md5Validator(value): 

"""Predicate that checks if the given value seems to be MD5 hash.""" 

# check if the value has the expected type 

stringTypeValidator(value) 

 

# MD5 hash has 32 hexadecimal characters 

if not re.fullmatch(r"^[a-f0-9]{32}$", value): 

raise Invalid("the value '{value}' does not seem to be MD5 hash".format(value=value)) 

 

 

def sha1Validator(value): 

"""Predicate that checks if the given value seems to be SHA1 hash.""" 

# check if the value has the expected type 

stringTypeValidator(value) 

 

# SHA-1 hash has 40 hexadecimal characters 

if not re.fullmatch(r"^[a-f0-9]{40}$", value): 

raise Invalid("the value '{value}' does not seem to be SHA1 hash".format(value=value)) 

 

 

def sha224Validator(value): 

"""Predicate that checks if the given value seems to be SHA224 hash.""" 

# check if the value has the expected type 

stringTypeValidator(value) 

 

# SHA-224 hash has 56 hexadecimal characters 

if not re.fullmatch(r"^[a-fA-F0-9]{56}$", value): 

raise Invalid("the value '{value}' does not seem to be SHA224 hash".format(value=value)) 

 

 

def sha256Validator(value): 

"""Predicate that checks if the given value seems to be SHA256 hash.""" 

# check if the value has the expected type 

stringTypeValidator(value) 

 

# SHA-256 hash has 64 hexadecimal characters 

if not re.fullmatch(r"^[a-fA-F0-9]{64}$", value): 

raise Invalid("the value '{value}' does not seem to be SHA256 hash".format(value=value)) 

 

 

def sha384Validator(value): 

"""Predicate that checks if the given value seems to be SHA384 hash.""" 

# check if the value has the expected type 

stringTypeValidator(value) 

 

# SHA-384 hash has 64 hexadecimal characters 

if not re.fullmatch(r"^[a-fA-F0-9]{96}$", value): 

raise Invalid("the value '{value}' does not seem to be SHA384 hash".format(value=value)) 

 

 

def sha512Validator(value): 

"""Predicate that checks if the given value seems to be SHA512 hash.""" 

# check if the value has the expected type 

stringTypeValidator(value) 

 

# SHA-512 hash has 128 hexadecimal characters 

if not re.fullmatch(r"^[a-fA-F0-9]{128}$", value): 

raise Invalid("the value '{value}' does not seem to be SHA512 hash".format(value=value)) 

 

 

def sha3_224Validator(value): 

"""Predicate that checks if the given value seems to be SHA-3 224 hash.""" 

# check if the value has the expected type 

stringTypeValidator(value) 

 

# SHA-3 224 hash has 56 hexadecimal characters 

if not re.fullmatch(r"^[a-fA-F0-9]{56}$", value): 

raise Invalid("the value '{value}' does not seem to be SHA-3 224 hash".format(value=value)) 

 

 

def sha3_256Validator(value): 

"""Predicate that checks if the given value seems to be SHA-3 256 hash.""" 

# check if the value has the expected type 

stringTypeValidator(value) 

 

# SHA-3 256 hash has 64 hexadecimal characters 

if not re.fullmatch(r"^[a-fA-F0-9]{64}$", value): 

raise Invalid("the value '{value}' does not seem to be SHA-3 256 hash".format(value=value)) 

 

 

def sha3_384Validator(value): 

"""Predicate that checks if the given value seems to be SHA-3 384 hash.""" 

# check if the value has the expected type 

stringTypeValidator(value) 

 

# SHA-3 384 hash has 64 hexadecimal characters 

if not re.fullmatch(r"^[a-fA-F0-9]{96}$", value): 

raise Invalid("the value '{value}' does not seem to be SHA-3 384 hash".format(value=value)) 

 

 

def sha3_512Validator(value): 

"""Predicate that checks if the given value seems to be SHA-3 512 hash.""" 

# check if the value has the expected type 

stringTypeValidator(value) 

 

# SHA-3 512 hash has 128 hexadecimal characters 

if not re.fullmatch(r"^[a-fA-F0-9]{128}$", value): 

raise Invalid("the value '{value}' does not seem to be SHA-3 512 hash".format(value=value)) 

 

 

def shake128Validator(value): 

"""Predicate that checks if the given value seems to be SHAKE128 256-bit hash.""" 

# check if the value has the expected type 

stringTypeValidator(value) 

 

# SHAKE128 256-bit hash has 64 hexadecimal characters 

if not re.fullmatch(r"^[a-fA-F0-9]{64}$", value): 

raise Invalid("the value '{v}' does not seem to be SHAKE128 256-bit hash".format(v=value)) 

 

 

def shake256Validator(value): 

"""Predicate that checks if the given value seems to be SHAKE256 256-bit hash.""" 

# check if the value has the expected type 

stringTypeValidator(value) 

 

# SHAKE256 256-bit hash has 64 hexadecimal characters 

if not re.fullmatch(r"^[a-fA-F0-9]{64}$", value): 

raise Invalid("the value '{v}' does not seem to be SHAKE256 256-bit hash".format(v=value)) 

 

 

def BLAKE2Validator(value): 

"""Predicate that checks if the given value seems to be BLAKE2 256-bit hash.""" 

# check if the value has the expected type 

stringTypeValidator(value) 

 

# BLAKE2 256-bit hash has 128 hexadecimal characters 

if not re.fullmatch(r"^[a-fA-F0-9]{128}$", value): 

raise Invalid("the value '{v}' does not seem to be BLAKE2 256-bit hash".format(v=value)) 

 

 

def b64IdentityValidator(identitySchema, value): 

"""Validate identity encoded by base64 encoding.""" 

# input must be a string 

stringTypeValidator(value) 

 

# decode from BASE64 encoding 

value = base64.b64decode(value).decode('utf-8') 

 

# parse JSON 

identity = json.loads(value) 

 

# validate schema 

identitySchema(identity) 

 

 

def jsonInStrValidator(value): 

"""Validate if the value is JSON stored in string.""" 

# input must be a string 

stringTypeValidator(value) 

 

# try to parse into JSON 

decoded = json.loads(value) 

 

assert decoded is not None 

 

 

def versionInBytesValidator(value): 

"""Check if value conforms to version.""" 

# check if the value has the expected type 

bytesTypeValidator(value) 

 

# use default encoding 

value = value.decode("utf-8") 

 

VERSION_RE = re.compile(r"[0-9]+\.[0-9]+\.[0-9]+.*") 

if not VERSION_RE.fullmatch(value): 

raise Invalid("wrong version value '{}'".format(value)) 

 

 

def pathToCephValidator(value): 

"""Check if value conforms to path to Ceph.""" 

# check if the value has the expected type 

stringTypeValidator(value) 

 

# construct regexp for path 

PREFIX = r"archives/compressed/" 

SELECTOR = r"[0-9a-f]{2}/" 

UUID = r"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/" 

DATETIME = r"[0-9]{6}/[0-9]{2}/" 

TARBALL = r"[0-9]+\.tar\.gz" 

 

PATH_RE = re.compile(PREFIX+SELECTOR+UUID+DATETIME+TARBALL) 

if not PATH_RE.fullmatch(value): 

raise Invalid("wrong path value '{}'".format(value)) 

 

 

def pathToCephInBytesValidator(value): 

"""Check if value conforms to path to Ceph.""" 

# check if the value has the expected type 

bytesTypeValidator(value) 

 

# use default encoding 

value = value.decode("utf-8") 

 

pathToCephValidator(value)