@@ -26,7 +26,8 @@ protected function configure()
26
26
->addOption ('file ' , 'f ' , InputOption::VALUE_REQUIRED , 'The filename for the archive ' )
27
27
->addOption ('exclude-services ' , null , InputOption::VALUE_NONE , 'Exclude services ' )
28
28
->addOption ('exclude-mounts ' , null , InputOption::VALUE_NONE , 'Exclude mounts ' )
29
- ->addOption ('include-variables ' , null , InputOption::VALUE_NONE , 'Include variables ' );
29
+ ->addOption ('include-variables ' , null , InputOption::VALUE_NONE , 'Include variables ' )
30
+ ->addOption ('include-sensitive-values ' , null , InputOption::VALUE_NONE , 'Include sensitive variable values ' );
30
31
$ this ->addProjectOption ();
31
32
$ this ->addEnvironmentOption ();
32
33
}
@@ -189,21 +190,56 @@ protected function execute(InputInterface $input, OutputInterface $output)
189
190
];
190
191
191
192
if ($ includeVariables ) {
193
+ $ includeSensitive = $ input ->getOption ('include-sensitive-values ' );
192
194
$ this ->stdErr ->writeln ('' );
193
195
$ this ->stdErr ->writeln ('Copying project-level variables ' );
194
196
foreach ($ this ->getSelectedProject ()->getVariables () as $ var ) {
195
197
$ metadata ['variables ' ]['project ' ][$ var ->name ] = $ var ->getProperties ();
196
- if ($ var ->is_sensitive ) {
197
- $ this ->stdErr ->writeln (sprintf (' Warning: cannot save value for sensitive project-level variable <comment>%s</comment> ' , $ var ->name ));
198
+ if ($ var ->is_sensitive && !$ var ->hasProperty ('value ' )) {
199
+ if ($ var ->visible_runtime ) {
200
+ if ($ includeSensitive ) {
201
+ $ value = false ;
202
+ foreach ($ apps as $ app ) {
203
+ try {
204
+ $ value = $ this ->fetchSensitiveValue ($ app ->getSshUrl (), $ var ->name , $ var ->is_json );
205
+ } catch (\RuntimeException $ e ) {
206
+ continue ;
207
+ }
208
+ break ;
209
+ }
210
+ if ($ value !== false ) {
211
+ $ metadata ['variables ' ]['project ' ][$ var ->name ]['value ' ] = $ value ;
212
+ }
213
+ } else {
214
+ $ this ->stdErr ->writeln (sprintf (' Warning: cannot save value for sensitive project-level variable <comment>%s</comment> ' , $ var ->name ));
215
+ $ this ->stdErr ->writeln (' Use --include-sensitive-values to try to fetch this via SSH ' );
216
+ }
217
+ }
198
218
}
199
219
}
200
220
201
221
$ this ->stdErr ->writeln ('' );
202
222
$ this ->stdErr ->writeln ('Copying environment-level variables ' );
203
223
foreach ($ environment ->getVariables () as $ envVar ) {
204
224
$ metadata ['variables ' ]['environment ' ][$ envVar ->name ] = $ envVar ->getProperties ();
205
- if ($ envVar ->is_sensitive ) {
206
- $ this ->stdErr ->writeln (sprintf (' Warning: cannot save value for sensitive environment-level variable <comment>%s</comment> ' , $ envVar ->name ));
225
+ if ($ envVar ->is_sensitive && !$ envVar ->hasProperty ('value ' )) {
226
+ if ($ includeSensitive ) {
227
+ $ value = false ;
228
+ foreach ($ apps as $ app ) {
229
+ try {
230
+ $ value = $ this ->fetchSensitiveValue ($ app ->getSshUrl (), $ envVar ->name , $ envVar ->is_json );
231
+ } catch (\RuntimeException $ e ) {
232
+ continue ;
233
+ }
234
+ break ;
235
+ }
236
+ if ($ value !== false ) {
237
+ $ metadata ['variables ' ]['environment ' ][$ envVar ->name ]['value ' ] = $ value ;
238
+ }
239
+ } else {
240
+ $ this ->stdErr ->writeln (sprintf (' Warning: cannot save value for sensitive environment-level variable <comment>%s</comment> ' , $ envVar ->name ));
241
+ $ this ->stdErr ->writeln (' Use --include-sensitive-values to try to fetch this via SSH ' );
242
+ }
207
243
}
208
244
}
209
245
}
@@ -302,6 +338,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
302
338
$ mountService = $ this ->getService ('mount ' );
303
339
/** @var \Platformsh\Cli\Service\Rsync $rsync */
304
340
$ rsync = $ this ->getService ('rsync ' );
341
+ $ rsyncOptions = [
342
+ 'verbose ' => $ output ->isVeryVerbose (),
343
+ 'quiet ' => !$ output ->isVerbose (),
344
+ ];
305
345
foreach ($ apps as $ app ) {
306
346
$ sourcePaths = [];
307
347
$ mounts = $ mountService ->normalizeMounts ($ app ->getMounts ());
@@ -319,7 +359,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
319
359
$ this ->stdErr ->writeln ('Copying from mount <info> ' . $ path . '</info> ' );
320
360
$ destination = $ archiveDir . '/mounts/ ' . trim ($ path , '/ ' );
321
361
mkdir ($ destination , 0755 , true );
322
- $ rsync ->syncDown ($ app ->getSshUrl (), ltrim ($ path , '/ ' ), $ destination );
362
+ $ rsync ->syncDown ($ app ->getSshUrl (), ltrim ($ path , '/ ' ), $ destination, $ rsyncOptions );
323
363
$ metadata ['mounts ' ][$ path ] = [
324
364
'app ' => $ app ->getName (),
325
365
'path ' => 'mounts/ ' . trim ($ path , '/ ' ),
@@ -402,4 +442,27 @@ private function getSchemas(Service $service, $relationshipName)
402
442
403
443
return $ schemas ;
404
444
}
445
+
446
+ /**
447
+ * @param string $sshUrl
448
+ * @param string $varName
449
+ * @param bool $is_json
450
+ *
451
+ * @return mixed
452
+ */
453
+ private function fetchSensitiveValue ($ sshUrl , $ varName , $ is_json )
454
+ {
455
+ /** @var \Platformsh\Cli\Service\RemoteEnvVars $remoteEnvVars */
456
+ $ remoteEnvVars = $ this ->getService ('remote_env_vars ' );
457
+ if (substr ($ varName , 0 , 4 ) === 'env: ' ) {
458
+ return $ remoteEnvVars ->getEnvVar (substr ($ varName , 4 ), $ sshUrl , true , 3600 , false );
459
+ }
460
+
461
+ $ variables = $ remoteEnvVars ->getArrayEnvVar ('VARIABLES ' , $ sshUrl );
462
+ if (array_key_exists ($ varName , $ variables )) {
463
+ return $ is_json ? json_encode ($ variables [$ varName ]) : $ variables [$ varName ];
464
+ }
465
+
466
+ throw new \RuntimeException ('Variable not found: ' . $ varName );
467
+ }
405
468
}
0 commit comments