github.com/google/cloudprober@v0.11.3/validators/integrity/integrity.go (about) 1 // Copyright 2018 The Cloudprober Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package integrity provides data integrity validator for the Cloudprober's 16 // validator framework. 17 package integrity 18 19 import ( 20 "fmt" 21 22 "github.com/google/cloudprober/logger" 23 "github.com/google/cloudprober/probes/probeutils" 24 configpb "github.com/google/cloudprober/validators/integrity/proto" 25 ) 26 27 // Validator implements an integrity validator. 28 type Validator struct { 29 pattern []byte 30 patternNumBytes int32 31 32 l *logger.Logger 33 } 34 35 // Init initializes the integrity validator. 36 func (v *Validator) Init(config interface{}, l *logger.Logger) error { 37 c, ok := config.(*configpb.Validator) 38 if !ok { 39 return fmt.Errorf("%v is not a valid integrity validator config", config) 40 } 41 42 switch c.GetPattern().(type) { 43 case *configpb.Validator_PatternString: 44 v.pattern = []byte(c.GetPatternString()) 45 case *configpb.Validator_PatternNumBytes: 46 v.patternNumBytes = c.GetPatternNumBytes() 47 } 48 49 if len(v.pattern) == 0 && v.patternNumBytes == 0 { 50 return fmt.Errorf("bad integrity validator config (%v): one of pattern_string and pattern_num_bytes should be set", c) 51 } 52 53 v.l = l 54 return nil 55 } 56 57 // Validate validates the provided responseBody for data integrity errors, for 58 // example, data corruption. 59 func (v *Validator) Validate(responseBody []byte) (bool, error) { 60 pattern := v.pattern 61 if len(pattern) == 0 { 62 if len(responseBody) < int(v.patternNumBytes) { 63 return false, fmt.Errorf("response (%s) is smaller than the number of pattern bytes (%d)", responseBody, v.patternNumBytes) 64 } 65 pattern = responseBody[:v.patternNumBytes] 66 } 67 68 err := probeutils.VerifyPayloadPattern(responseBody, pattern) 69 70 if err != nil { 71 v.l.Error(err.Error()) 72 return false, nil 73 } 74 75 return true, nil 76 } 77 78 // PatternNumBytesValidator returns a data integrity validator with number of 79 /// pattern bytes set to patternNumbBytes. 80 func PatternNumBytesValidator(patternNumbBytes int32, l *logger.Logger) (*Validator, error) { 81 v := &Validator{} 82 83 vConfig := &configpb.Validator{ 84 Pattern: &configpb.Validator_PatternNumBytes{ 85 PatternNumBytes: patternNumbBytes, 86 }, 87 } 88 89 if err := v.Init(vConfig, l); err != nil { 90 return nil, fmt.Errorf("error initializing data integrity validator: %v", err) 91 } 92 93 return v, nil 94 }