github.com/DataDog/datadog-agent/pkg/security/secl@v0.55.0-devel.0.20240517055856-10c4965fea94/model/args_envs.go (about)

     1  // Unless explicitly stated otherwise all files in this repository are licensed
     2  // under the Apache License Version 2.0.
     3  // This product includes software developed at Datadog (https://www.datadoghq.com/).
     4  // Copyright 2016-present Datadog, Inc.
     5  
     6  // Package model holds model related files
     7  package model
     8  
     9  import (
    10  	"slices"
    11  	"strings"
    12  )
    13  
    14  const (
    15  	// MaxArgEnvSize maximum size of one argument or environment variable
    16  	MaxArgEnvSize = 256
    17  	// MaxArgsEnvsSize maximum number of args and/or envs
    18  	MaxArgsEnvsSize = 256
    19  )
    20  
    21  // ArgsEnvs raw value for args and envs
    22  type ArgsEnvs struct {
    23  	ID        uint32
    24  	Size      uint32
    25  	ValuesRaw [MaxArgEnvSize]byte
    26  }
    27  
    28  // ArgsEntry defines a args cache entry
    29  type ArgsEntry struct {
    30  	Values    []string
    31  	Truncated bool
    32  }
    33  
    34  // Equals compares two ArgsEntry
    35  func (p *ArgsEntry) Equals(o *ArgsEntry) bool {
    36  	if p == o {
    37  		return true
    38  	} else if p == nil || o == nil {
    39  		return false
    40  	}
    41  
    42  	return slices.Equal(p.Values, o.Values)
    43  }
    44  
    45  // EnvsEntry defines a args cache entry
    46  type EnvsEntry struct {
    47  	Values    []string
    48  	Truncated bool
    49  
    50  	filteredEnvs []string
    51  	kv           map[string]string
    52  }
    53  
    54  // FilterEnvs returns an array of envs, only the name of each variable is returned unless the variable name is part of the provided filter
    55  func (p *EnvsEntry) FilterEnvs(envsWithValue map[string]bool) ([]string, bool) {
    56  	if p.filteredEnvs != nil {
    57  		return p.filteredEnvs, p.Truncated
    58  	}
    59  
    60  	if len(p.Values) == 0 {
    61  		return nil, p.Truncated
    62  	}
    63  
    64  	p.filteredEnvs = make([]string, 0, len(p.Values))
    65  
    66  	for _, value := range p.Values {
    67  		k, _, found := strings.Cut(value, "=")
    68  		if found {
    69  			if envsWithValue[k] {
    70  				p.filteredEnvs = append(p.filteredEnvs, value)
    71  			} else {
    72  				p.filteredEnvs = append(p.filteredEnvs, k)
    73  			}
    74  		} else {
    75  			p.filteredEnvs = append(p.filteredEnvs, value)
    76  		}
    77  	}
    78  
    79  	return p.filteredEnvs, p.Truncated
    80  }
    81  
    82  // FilterEnvs returns an array of environment variable key value pairs matching the desired keys
    83  func FilterEnvs(allEnvVars []string, desiredKeys map[string]bool) []string {
    84  	if len(allEnvVars) == 0 {
    85  		return nil
    86  	}
    87  
    88  	filteredEnvs := make([]string, 0, len(desiredKeys))
    89  
    90  	for _, value := range allEnvVars {
    91  		k, _, _ := strings.Cut(value, "=")
    92  		if desiredKeys[k] {
    93  			filteredEnvs = append(filteredEnvs, value)
    94  		}
    95  	}
    96  
    97  	return filteredEnvs
    98  }
    99  
   100  func (p *EnvsEntry) toMap() {
   101  	if p.kv != nil {
   102  		return
   103  	}
   104  
   105  	p.kv = make(map[string]string, len(p.Values))
   106  
   107  	for _, value := range p.Values {
   108  		k, v, found := strings.Cut(value, "=")
   109  		if found {
   110  			p.kv[k] = v
   111  		}
   112  	}
   113  }
   114  
   115  // Get returns the value for the given key
   116  func (p *EnvsEntry) Get(key string) string {
   117  	p.toMap()
   118  	return p.kv[key]
   119  }
   120  
   121  // Equals compares two EnvsEntry
   122  func (p *EnvsEntry) Equals(o *EnvsEntry) bool {
   123  	if p == o {
   124  		return true
   125  	} else if p == nil || o == nil {
   126  		return false
   127  	}
   128  
   129  	return slices.Equal(p.Values, o.Values)
   130  }