go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/cli/execruntime/environment.go (about)

     1  // Copyright (c) Mondoo, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package execruntime
     5  
     6  import (
     7  	"path"
     8  	"strings"
     9  
    10  	"github.com/rs/zerolog/log"
    11  )
    12  
    13  var (
    14  	environmentProvider envProvider
    15  	environmentDef      CiConfig
    16  )
    17  
    18  func init() {
    19  	environmentProvider = &osEnvProvider{}
    20  	environmentDef = CiConfig{
    21  		GITHUB:        githubEnv,
    22  		GITLAB:        gitlabEnv,
    23  		K8S_OPERATOR:  kubernetesEnv,
    24  		CIRCLE:        circleciEnv,
    25  		AZUREPIPELINE: azurePipelineEnv,
    26  		JENKINS:       jenkinsEnv,
    27  		// The following detections have been deactivated in scope of the v6 release
    28  		// to only support the CI/CD detections that we can show in the UI.
    29  		// At the time of writing, the ones supported are listed above.
    30  		// We will be adding CI/CDs back throughout
    31  		// TRAVIS:              travisEnv,
    32  		// AWS_CODEBUILD:       awscodebuildEnv,
    33  		// AWS_RUN_COMMAND:     awsruncommandEnv,
    34  		// GOOGLE_CLOUD_BUILD:  googleCloudBuildEnv,
    35  		// TERRAFORM:           terraformEnv,
    36  		// PACKER:              packerEnv,
    37  		// TEAMCITY:            teamcityEnv,
    38  		// MONDOO_CI:           mondooCIEnv,
    39  		// MONDOO_AWS_OPERATOR: mondooAwsOperatorEnv,
    40  	}
    41  }
    42  
    43  type CiConfig map[string]*RuntimeEnv
    44  
    45  type RuntimeEnv struct {
    46  	Id        string
    47  	Name      string `json:"name"`
    48  	Prefix    string `json:"prefix"`
    49  	Namespace string `json:"slug"`
    50  	// Identify holds all env variables that must be set to
    51  	// identify the CI environment
    52  	Identify  []Variable `json:"identify"`
    53  	Variables []Variable `json:"vars"`
    54  }
    55  
    56  type Variable struct {
    57  	Name  string `json:"name"`
    58  	Desc  string `json:"desc"`
    59  	Value string `json:"value"`
    60  	Type  string `json:"type"`
    61  }
    62  
    63  // Detect determines if we are running within the CI environment or not
    64  func (c *RuntimeEnv) Detect() bool {
    65  	for i := range c.Identify {
    66  		id := c.Identify[i].Name
    67  		if len(environmentProvider.Getenv(id)) != 0 {
    68  			return true
    69  		}
    70  	}
    71  
    72  	return false
    73  }
    74  
    75  func (c *RuntimeEnv) IsAutomatedEnv() bool {
    76  	return c.Id != CLIENT_ENV
    77  }
    78  
    79  // Annotations returns env variables as key value pairs
    80  func (c *RuntimeEnv) Labels() map[string]string {
    81  	labels := map[string]string{}
    82  
    83  	// store used ci environment in labels
    84  	labels["mondoo.com/exec-environment"] = c.Namespace
    85  	// iterate over all known ENV variables and fetch the data
    86  	for i := range c.Variables {
    87  		key := c.Variables[i].Name
    88  		val := environmentProvider.Getenv(key)
    89  		log.Debug().Msgf("cicd asset env var> %s : %s", key, val)
    90  
    91  		// only store data if a value is set
    92  		if len(val) > 0 {
    93  			// replace prefix of variable name and make it lowercase
    94  			valkey := strings.Replace(key, c.Prefix+"_", "", 1)
    95  			slug := strings.ToLower(valkey)
    96  			slug = strings.Replace(slug, "_", "-", -1)
    97  
    98  			// check if the env var has the generic prefix
    99  			if strings.HasPrefix(slug, "ci-") {
   100  				slug = strings.Replace(slug, "ci-", "", 1)
   101  			}
   102  
   103  			// check if the env var has the env prefix already
   104  			// if !strings.HasPrefix(slug, c.Id+"-") {
   105  			// 	slug = fmt.Sprintf("%s-%s", c.Id, slug)
   106  			// }
   107  
   108  			log.Debug().Str("namespace", c.Namespace).Str("slug", slug).Msg("labels")
   109  			labels[path.Join(c.Namespace, slug)] = val
   110  		}
   111  	}
   112  	return labels
   113  }