ex-aws / ex_aws

A flexible, easy to use set of clients AWS APIs for Elixir
https://hex.pm/packages/ex_aws
MIT License
1.29k stars 528 forks source link

Crash when HOME is not set #914

Open cdegroot opened 2 years ago

cdegroot commented 2 years ago

We use the "three-step" auth method as documented: env var, ~/.aws profile, ecs/instance profile. This allows our code to run on a variety of platforms with a single configuration and it works perfectly.

Today, I installed said code as a systemctl-managed daemon and that failed:

Oct 27 16:11:21 ip-192-168-0-140 metrist-orchestrator[18820]: 16:11:21.232 [info]  Getting secret /dev1/rds/projections-user#host from region us-east-1
Oct 27 16:11:21 ip-192-168-0-140 metrist-orchestrator[18820]: 16:11:21.233 [error] GenServer ExAws.Config.AuthCache terminating
Oct 27 16:11:21 ip-192-168-0-140 metrist-orchestrator[18820]: ** (FunctionClauseError) no function clause matching in IO.chardata_to_string/1
Oct 27 16:11:21 ip-192-168-0-140 metrist-orchestrator[18820]:     (elixir 1.13.4) lib/io.ex:627: IO.chardata_to_string(nil)
Oct 27 16:11:21 ip-192-168-0-140 metrist-orchestrator[18820]:     (elixir 1.13.4) lib/path.ex:538: Path.join/2
Oct 27 16:11:21 ip-192-168-0-140 metrist-orchestrator[18820]:     (ex_aws 2.4.0) lib/ex_aws/credentials_ini/file.ex:187: ExAws.CredentialsIni.File.profile_from_config/1
Oct 27 16:11:21 ip-192-168-0-140 metrist-orchestrator[18820]:     (ex_aws 2.4.0) lib/ex_aws/credentials_ini/file.ex:13: ExAws.CredentialsIni.File.security_credentials/1
Oct 27 16:11:21 ip-192-168-0-140 metrist-orchestrator[18820]:     (ex_aws 2.4.0) lib/ex_aws/config.ex:158: ExAws.Config.awscli_auth_credentials/2
Oct 27 16:11:21 ip-192-168-0-140 metrist-orchestrator[18820]:     (ex_aws 2.4.0) lib/ex_aws/config/auth_cache.ex:65: ExAws.Config.AuthCache.refresh_awscli_config/3
Oct 27 16:11:21 ip-192-168-0-140 metrist-orchestrator[18820]:     (ex_aws 2.4.0) lib/ex_aws/config/auth_cache.ex:50: ExAws.Config.AuthCache.handle_call/3
Oct 27 16:11:21 ip-192-168-0-140 metrist-orchestrator[18820]:     (stdlib 4.0.1) gen_server.erl:1146: :gen_server.try_handle_call/4
Oct 27 16:11:21 ip-192-168-0-140 metrist-orchestrator[18820]: Last message (from Orchestrator.MonitorSupervisor): {:refresh_awscli_conf

The cause is clear: the code in file.ex uses System.user_home() which ends up basically reading the HOME environment variable.

The fix could be simple as well: System.user_home() || "/" would already do the trick and is sort of the expected behavior on Unix systems - if there is no home set, the root directory will do. In most cases, this should be invisible but I'm filing this as an issue rather than as a PR because it's likely to be one of these things that people want to discuss :)

bernardd commented 2 years ago

That's totally fine with me - any relatively sane behaviour is preferable to a crash :) A PR to that effect would be most welcome. Apologies for the delay getting back to you.

pcapel commented 1 year ago

Seems like this is related to #932. If there's not ongoing work to this effect I'd be happy to include this protection in my PR pending a review of the desirable changes with a maintainer.