capistrano / capistrano/sshkit

Exports added by :with are ineffective when :user is specified

Aperta
#490 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
Ruby
Stelle
1.2k
Fork
257
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

When a command is mapped, (`:should_map?`), both `:with` and `:user` are invoked.

In both builder methods, the environment variables specified are intended to be inherited by the subshell.

Say that we have the env var `MYVAR=myvar`, and the command `mycommand`, which needs to be run as the user `myuser`. The relevant builder methods are:

```
def with(&_block)
env_string = environment_string
return yield if env_string.empty?
"( export #{env_string} ; #{yield} )"
end

def user(&_block)
return yield unless options[:user]
env_string = environment_string
"sudo -u #{options[:user].to_s.shellescape} #{env_string + " " unless env_string.empty?}-- sh -c #{yield.shellescape}"
end
```

this will generate something like:

```sh
( export MYVAR=myvar ; sudo -u myuser MYVAR=myvar -- sh -c command )
```

The problem is that the export is ineffective, because exports are not inherited by the sudo subshell.

While this is harmless, it clutters the generated commands, and it's also semantically incorrect (because it's ineffective) and confusing.

This could be avoided by just yielding the the builder block if `options[:user]` is set:

```ruby
def with(&_block)
> # Exports are ineffective on non-login (sudo) shells; in this case, the :user builder takes
> # care of setting them.
> return yield if options[:user]
env_string = environment_string
return yield if env_string.empty?
"( export #{env_string} ; #{yield} )"
end
```

This makes the (internal) commands output more readable (and understandable). While this has no effect on the end users, it helps understanding those who approach Capistrano/Sshkit development (at least, it did it for me :grimacing:).

If this is approved (either in this form or another), I can easily open a PR.

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Inizia individuando i metodi builder with e user mostrati nell’issue e verifica come compongono il comando shell generato quando vengono usate entrambe le opzioni. La modifica è completata quando il builder with non aggiunge una subshell export per un comando utente, mentre il builder user continua a fornire le variabili d’ambiente.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
ruby
Ambito
devops
Tipo di issue
Bug
Difficoltà
2/5
Tempo stimato
1-3 ore
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
48/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.