Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
82 views
in Technique[技术] by (71.8m points)

php - Pass output of shell command to inside .env file

I have a .env file for a php application. The contents are similar to this:

#define env
#dev,stage,prod
env=stage

http="https://"

#define debug
debug=true
debug_level=2

#db
mysql_host=staging-rds.cvexrtrtr1.us-east-2.rds.amazonaws.com
mysql_port=3306
mysql_username=dev
mysql_password="existing!secret"

I want to store the secret for mysql_password in the AWS paramater store. The goal is to not have the value existing!secret exposed to env variables or to anyone accessing the machine, Basically I want the output of a bash command similar to the one below, to be passed on directly inside the .env file:

export DB_PASSWORD=$(aws ssm get-parameter 
  --name "secret-for-the-password" 
  --with-decryption 
  | jq -r '.Parameter.Value')

Is it fundamentally possible to pass the output of this command to the .env file? Output should be something similar to:

#define env
#dev,stage,prod
env=stage

http="https://"

#define debug
debug=true
debug_level=2

#db
mysql_host=staging-rds.cvexrtrtr1.us-east-2.rds.amazonaws.com
mysql_port=3306
mysql_username=dev
mysql_password="$(aws ssm get-parameter 
  --name "secret-for-the-password" 
  --with-decryption 
  | jq -r '.Parameter.Value')"
question from:https://stackoverflow.com/questions/65904375/pass-output-of-shell-command-to-inside-env-file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Having the secret stored as an environment variable would seem like a better option but if this fits your needs better, you could use exec to run the bash code, for example:

<?php

$mysql_password = "aws ssm get-parameter 
--name "secret-for-the-password" 
--with-decryption 
| jq -r '.Parameter.Value'";

$retval = exec($mysql_password);

echo $retval;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...