bitovi/bitops

ENV variables are not taking precedence over the plugin config (schema)

arm4b opened this issue · 1 comments

arm4b commented

During the plugin deploy stage, if the ENV variable is set for the respective bitops plugin config option, it'll be ignored and the config setting will be applied instead. The expectation is that the ENV always takes precedence.

Example

Example Terraform schema:
https://github.com/bitops-plugins/terraform/blob/ce701be1616cec9420b9ae484e2533ba131cddbf/bitops.schema.yaml#L19-L23
https://bitops.sh/tool-configuration/configuration-terraform/#terraform-bitops-schema

With the following bitops.config.yaml:

terraform:
    cli:
        stack-action: apply
    options: {}

and BITOPS_TERRAFORM_COMMAND=destroy, the terraform apply is executed which is undesired.

Example Docker CLI command:

docker run \
--rm \
-e BITOPS_ENVIRONMENT="test" \
-e BITOPS_LOGGING_LEVEL=DEBUG \
-e AWS_ACCESS_KEY_ID="$AWS_ACCESS_KEY_ID" \
-e AWS_SECRET_ACCESS_KEY="$AWS_SECRET_ACCESS_KEY" \
-e AWS_DEFAULT_REGION="us-east-2" \
-e TF_STATE_BUCKET="heyemoji" \
-e BITOPS_TERRAFORM_COMMAND="destroy" \
-e HEYEMOJI_SLACK_API_TOKEN="$HUBOT_SLACK_TOKEN" \
-v $(pwd):/opt/bitops_deployment \
bitovi/bitops:latest

Will run terraform apply instead of destroy.

Expected behavior

The ENV variable if set should always override the respective bitops.config section based on the plugin schema.

Logs

Based on the logs, BITOPS_TERRAFORM_COMMAND=apply is derived from config:

        NEW SCHEMA:
                Name:         [stack-action]            
                Schema Key:   [terraform.properties.cli.properties.stack-action]            
                Config_Key:   [terraform.cli.stack-action]            
                Schema Type:  [cli]            
                                  
                Export Env:   [TERRAFORM_COMMAND]            
                Default:      [plan]            
                Enabled:      [None]            
                Type:         [string]            
                Parameter:    [None]            
                Dash Type:    [None]            
                Required:     [True]            
                                  
                Value Set:    []
2022-12-07 01:28:49,378 root         DEBUG 
                SEARCHING FOR KEY:  [terraform.cli.stack-action]                      
                SEARCH_DICT:        [{'terraform': {'cli': {'stack-action': 'apply'}, 'options': {}}}]
2022-12-07 01:28:49,378 root         DEBUG 
                KEY [terraform.cli.stack-action] 
                RESULT FOUND:   [apply]
2022-12-07 01:28:49,392 root         INFO 
        Searching for: [terraform.cli.stack-action]
                Result Found: [apply]
2022-12-07 01:28:49,394 root         INFO Override found for: [stack-action], default: [plan], new value: [apply]
[...]
2022-12-07 01:28:49,469 root         DEBUG 
        SCHEMA:
                Name:         [stack-action]            
                Schema Key:   [terraform.properties.cli.properties.stack-action]            
                Config_Key:   [terraform.cli.stack-action]            
                Schema Type:  [cli]            
                                  
                Export Env:   [TERRAFORM_COMMAND]            
                Default:      [plan]            
                Enabled:      [None]            
                Type:         [string]            
                Parameter:    [None]            
                Dash Type:    [None]            
                Required:     [True]            
                                  
                Value Set:    [apply]

~#~#~#~BITOPS ENVIRONMENT VARIABLES~#~#~#~
        BITOPS_TERRAFORM_COMMAND=apply

Seems to me that this is an issue caused by overriding the env var..

Something like

def add_value_to_env(export_env, value):
    ...
    if value is None or value == "" or value == "None" or export_env is None or export_env == "":
        return

    if os.environ.get('BITOPS_'+export_env):
        logger.info("Environment Varible alredy set. Configuration value ignored.")
        return
    ...

Should fix that