...

Source file src/k8s.io/component-base/logs/datapol/datapol.go

Documentation: k8s.io/component-base/logs/datapol

     1  /*
     2  Copyright 2020 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  // Package datapol contains functions to determine if objects contain sensitive
    18  // data to e.g. make decisions on whether to log them or not.
    19  package datapol
    20  
    21  import (
    22  	"reflect"
    23  	"strings"
    24  
    25  	"k8s.io/klog/v2"
    26  )
    27  
    28  // Verify returns a list of the datatypes contained in the argument that can be
    29  // considered sensitive w.r.t. to logging
    30  func Verify(value interface{}) []string {
    31  	defer func() {
    32  		if r := recover(); r != nil {
    33  			//TODO maybe export a metric
    34  			klog.Warningf("Error while inspecting arguments for sensitive data: %v", r)
    35  		}
    36  	}()
    37  	t := reflect.ValueOf(value)
    38  	if t.Kind() == reflect.Pointer {
    39  		t = t.Elem()
    40  	}
    41  	return datatypes(t)
    42  }
    43  
    44  func datatypes(v reflect.Value) []string {
    45  	if types := byType(v.Type()); len(types) > 0 {
    46  		// Slices, and maps can be nil or empty, only the nil case is zero
    47  		switch v.Kind() {
    48  		case reflect.Slice, reflect.Map:
    49  			if !v.IsZero() && v.Len() > 0 {
    50  				return types
    51  			}
    52  		default:
    53  			if !v.IsZero() {
    54  				return types
    55  			}
    56  		}
    57  	}
    58  	switch v.Kind() {
    59  	case reflect.Interface:
    60  		return datatypes(v.Elem())
    61  	case reflect.Slice, reflect.Array:
    62  		for i := 0; i < v.Len(); i++ {
    63  			if types := datatypes(v.Index(i)); len(types) > 0 {
    64  				return types
    65  			}
    66  		}
    67  	case reflect.Map:
    68  		mapIter := v.MapRange()
    69  		for mapIter.Next() {
    70  			k := mapIter.Key()
    71  			v := mapIter.Value()
    72  			if types := datatypes(k); len(types) > 0 {
    73  				return types
    74  			}
    75  			if types := datatypes(v); len(types) > 0 {
    76  				return types
    77  			}
    78  		}
    79  	case reflect.Struct:
    80  		t := v.Type()
    81  		numField := t.NumField()
    82  
    83  		for i := 0; i < numField; i++ {
    84  			f := t.Field(i)
    85  			if f.Type.Kind() == reflect.Pointer {
    86  				continue
    87  			}
    88  			if reason, ok := f.Tag.Lookup("datapolicy"); ok {
    89  				if !v.Field(i).IsZero() {
    90  					return strings.Split(reason, ",")
    91  				}
    92  			}
    93  			if types := datatypes(v.Field(i)); len(types) > 0 {
    94  				return types
    95  			}
    96  		}
    97  	}
    98  	return nil
    99  }
   100  

View as plain text