Governança de TI
repositório 1
repositório 2
repositório 3
repositório ...
repositório n
.env
e imprime na tela
$file = __DIR__ . DIRECTORY_SEPARATOR . '.env';
$handler = fopen($file, 'r');
while (($line = fgets($handler)) !== false) {
list($name, $value) = explode('=', $line, 2);
$_ENV[$name] = trim($value);
}
var_dump($_ENV);
Em sistemas reais, utilize um pacote para isso, como o vlucas/phpdotenv
↓
array (size=7) 'AWS_ACCESS_KEY_ID' => string 'id' (length=2) 'AWS_SECRET_ACCESS_KEY' => string 'secret' (length=6) 'AWS_DEFAULT_REGION' => string 'us-east-1' (length=9) 'MONGO_HOST' => string 'localhost' (length=9) 'MONGO_USER' => string 'mymongo' (length=7) 'MONGO_PASSWORD' => string '"CDyV_n,$(3x/=`ogv9R' (length=20)ver código-fonte do script
before_script:
- apt-get -y update && apt-get install -y openssh-client
- eval $(ssh-agent -s)
deploy_stage:
script:
- ssh-add <(echo "$SSH_PRIVATE_KEY")
- scp -r . $SSH_USER@$SSH_HOST:$SSH_APP_DIRECTORY
ver código-fonte do .gitlab-ci.yml
{
"applications": {
"myrepository": {
"name": "My Repository",
"credentials": {
"staging": [1, 2, 3, 4, 5]
}
}
},
"credentials": {
"1": {
"name": "AWS_ACCESS_KEY_ID",
"value": "e829fb6fee0136da5967ff0a68efd44c"
},
"2": {
"name": "AWS_SECRET_ACCESS_KEY",
"value": "b9bafa4b4dd120b33b8eedbe1325e54f9650fd4deedb"
},
"3": {
"name": "MONGO_HOST",
"value": "localhost"
},
"4": {
"name": "MONGO_USER",
"value": "mongo"
},
"5": {
"name": "MONGO_PASS",
"value": "veez9aeg5OquiuT1aimek4xo"
}
}
}
ver código-fonte do serviço
#!/usr/bin/env python3
import requests
import sys
VAULT_HOST = sys.argv[1]
APP_NAME = sys.argv[2]
APP_ENVIRONMENT = sys.argv[3]
response = requests.get(
"%s/application/%s/credentials/%s" %
(VAULT_HOST, APP_NAME, APP_ENVIRONMENT)
)
response.raise_for_status()
response = response.json()
if 'data' in response and 'credentials' in response['data']:
credentials = response['data']['credentials']
if credentials:
for credential in credentials:
print("%s=%s" % (credential['name'], credential['value']))
ver script que consome a API do vault
before_script:
- apt-get -y update && apt-get install -y openssh-client
- eval $(ssh-agent -s)
deploy_stage:
script:
- ssh-add <(echo "$SSH_PRIVATE_KEY")
- scp -r . $SSH_USER@$SSH_HOST:$SSH_APP_DIRECTORY
ver .gitlab-ci.yml
modificado para invocar o script
before_script:
- apt-get -y update && apt-get install -y openssh-client
- eval $(ssh-agent -s)
deploy_stage:
script:
- vault-client $VAULT_HOST $CI_PROJECT_NAME staging > .env
- ssh-add <(echo "$SSH_PRIVATE_KEY")
- scp -r . $SSH_USER@$SSH_HOST:$SSH_APP_DIRECTORY
ver .gitlab-ci.yml
modificado para invocar o script