github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/broker/logging.go (about)

     1  package broker
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"reflect"
     7  )
     8  
     9  var openKeys = map[string]struct{}{
    10  	"sm_url":           {},
    11  	"xsappname":        {},
    12  	"globalaccount_id": {},
    13  	"subaccount_id":    {},
    14  	"kubeconfig":       {},
    15  }
    16  
    17  func hideSensitiveDataFromRawContext(d []byte) map[string]interface{} {
    18  	var data map[string]interface{}
    19  	err := json.Unmarshal(d, &data)
    20  	if err != nil {
    21  		return map[string]interface{}{}
    22  	}
    23  	for k, v := range data {
    24  		switch reflect.TypeOf(v).Kind() {
    25  		case reflect.String:
    26  			if _, exists := openKeys[k]; !exists {
    27  				data[k] = "*****"
    28  			}
    29  		case reflect.Map:
    30  			data[k] = hideSensitiveDataFromContext(v.(map[string]interface{}))
    31  		}
    32  	}
    33  
    34  	return data
    35  }
    36  
    37  func hideSensitiveDataFromContext(input map[string]interface{}) map[string]interface{} {
    38  	for k, v := range input {
    39  		if reflect.TypeOf(v).Kind() == reflect.String {
    40  			if _, exists := openKeys[k]; !exists {
    41  				input[k] = "*****"
    42  			}
    43  		}
    44  		if reflect.TypeOf(v).Kind() == reflect.Map {
    45  			input[k] = hideSensitiveDataFromContext(v.(map[string]interface{}))
    46  		}
    47  	}
    48  
    49  	return input
    50  }
    51  
    52  func marshallRawContext(d map[string]interface{}) string {
    53  	b, err := json.Marshal(d)
    54  	if err != nil {
    55  		return fmt.Sprintf("unable to marshal context data")
    56  	}
    57  	return string(b)
    58  }