63
63
64
64
local pl_path = require " pl.path"
65
65
local pl_config = require " pl.config"
66
+ local pl_utils = require ' pl.utils'
66
67
68
+ local sub = string.sub
69
+ local os_getenv = os.getenv
70
+ local assert_string = pl_utils .assert_string
71
+ local is_windows = pl_utils .is_windows
67
72
68
73
69
74
-- Convention: variable values are stored in the config table by the name of
@@ -154,6 +159,14 @@ local env_vars = {
154
159
HTTP_PROXY = { name = " http_proxy" , default = nil },
155
160
HTTPS_PROXY = { name = " https_proxy" , default = nil },
156
161
NO_PROXY = { name = " no_proxy" , default = nil },
162
+
163
+ -- Environment variables for expanding user home path
164
+ -- Nix specific
165
+ HOME = { name = " HOME" , default = nil },
166
+ -- Windows specific
167
+ USERPROFILE = { name = " USERPROFILE" , default = nil },
168
+ HOMEPATH = { name = " HOMEPATH" , default = nil },
169
+ HOMEDRIVE = { name = " HOMEDRIVE" , default = nil },
157
170
}
158
171
159
172
-- populate the env vars with their values, or defaults
@@ -173,18 +186,72 @@ local config = {
173
186
env_vars = env_vars
174
187
}
175
188
189
+
190
+ --- Returns the environment variable value or the cached
191
+ --- environment variable value in the `env_vars` table.
192
+ -- @string var_name The environment variable name
193
+ -- @treturn [1] string The environment variable value or the cached value in `env_vars` table
194
+ -- @treturn [2] nil If the environment variable is not set and the cached value is not available
195
+ local function getenv (var_name )
196
+ return os_getenv (var_name ) or env_vars [var_name ].value
197
+ end
198
+
199
+
200
+ --- Replace a starting '~' with the user's home directory.
201
+ --- This is a patched version of the original `pl.path.expanduser` function.
202
+ --- In lua-resty-aws the environment variables must be fetched in `init_phase`
203
+ --- So we need to cache those home path related values and fall back to them
204
+ --- if expanduser function failed to fetch the environment variables.
205
+ -- @string P A file path
206
+ -- @treturn [1] string The file path with the `~` prefix substituted, or the input path if it had no prefix.
207
+ -- @treturn [2] nil
208
+ -- @treturn [2] string Error message if the environment variables were unavailable.
209
+ local function expanduser (P )
210
+ assert_string (1 ,P )
211
+ if P :sub (1 ,1 ) ~= ' ~' then
212
+ return P
213
+ end
214
+
215
+ local home = getenv (' HOME' )
216
+ if (not home ) and (not is_windows ) then
217
+ -- no more options to try on Nix
218
+ return nil , " failed to expand '~' (HOME not set)"
219
+ end
220
+
221
+ if (not home ) then
222
+ -- try alternatives on Windows
223
+ home = getenv (' USERPROFILE' )
224
+ if not home then
225
+ local hd = getenv (' HOMEDRIVE' )
226
+ local hp = getenv (' HOMEPATH' )
227
+ if not (hd and hp ) then
228
+ return nil , " failed to expand '~' (HOME, USERPROFILE, and HOMEDRIVE and/or HOMEPATH not set)"
229
+ end
230
+ home = hd .. hp
231
+ end
232
+ end
233
+
234
+ return home .. sub (P ,2 )
235
+ end
236
+
176
237
do
177
238
-- load a config file. If section given returns section only, otherwise full file.
178
239
-- returns an empty table if the section does not exist
179
240
local function load_file (filename , section )
180
241
assert (type (filename ) == " string" , " expected filename to be a string" )
181
- if not pl_path .isfile (pl_path .expanduser (filename )) then
242
+
243
+ local expanded_filename , err = expanduser (filename )
244
+ if not expanded_filename then
245
+ return nil , " failed expanding path '" .. filename .. " ': " .. tostring (err )
246
+ end
247
+
248
+ if not pl_path .isfile (expanded_filename ) then
182
249
return nil , " not a file: '" .. filename .. " '"
183
250
end
184
251
185
- local contents , err = pl_config .read (filename , { variabilize = false })
252
+ local contents , err = pl_config .read (expanded_filename , { variabilize = false })
186
253
if not contents then
187
- return nil , " failed reading file '" .. filename .. " ': " .. tostring (err )
254
+ return nil , " failed reading file '" .. filename .. " '(expanded: ' " .. expanded_filename .. " ') : " .. tostring (err )
188
255
end
189
256
190
257
if not section then
193
260
194
261
assert (type (section ) == " string" , " expected section to be a string or falsy" )
195
262
if not contents [section ] then
196
- ngx .log (ngx .DEBUG , " section '" ,section ," ' does not exist in file '" ,filename ," '" )
263
+ ngx .log (ngx .DEBUG , " section '" ,section ," ' does not exist in file '" ,filename ," '(expanded: ' " .. expanded_filename .. " ') " )
197
264
return {}
198
265
end
199
266
200
- ngx .log (ngx .DEBUG , " loaded section '" ,section ," ' from file '" ,filename ," '" )
267
+ ngx .log (ngx .DEBUG , " loaded section '" ,section ," ' from file '" ,filename ," '(expanded: ' " .. expanded_filename .. " ') " )
201
268
return contents [section ]
202
269
end
203
270
237
304
-- table if the config file does not exist.
238
305
-- @return options table as gotten from the configuration file, or nil+err.
239
306
function config .load_config ()
240
- if not pl_path .isfile (pl_path .expanduser (env_vars .AWS_CONFIG_FILE .value )) then
307
+ local expanded_path , err = expanduser (env_vars .AWS_CONFIG_FILE .value )
308
+ if not (expanded_path and pl_path .isfile (expanded_path )) then
309
+ ngx .log (ngx .DEBUG , " failed to expand config file path or file does not exist: " , err )
241
310
-- file doesn't exist
242
311
return {}
243
312
end
@@ -252,11 +321,15 @@ end
252
321
-- @return credentials table as gotten from the credentials file, or a table
253
322
-- with the key, id, and token from the configuration file, table can be empty.
254
323
function config .load_credentials ()
255
- if pl_path .isfile (pl_path .expanduser (env_vars .AWS_SHARED_CREDENTIALS_FILE .value )) then
324
+ local expanded_path , err = expanduser (env_vars .AWS_SHARED_CREDENTIALS_FILE .value )
325
+ if expanded_path and pl_path .isfile (expanded_path ) then
256
326
local creds = config .load_credentials_file (env_vars .AWS_SHARED_CREDENTIALS_FILE .value , env_vars .AWS_PROFILE .value )
257
327
if creds then -- ignore error, already logged
258
328
return creds
259
329
end
330
+
331
+ else
332
+ ngx .log (ngx .DEBUG , " failed to expand credential file path or file does not exist: " , err )
260
333
end
261
334
262
335
-- fall back to config file
288
361
function config .get_config ()
289
362
local cfg = config .load_config () or {} -- ignore error, already logged
290
363
291
- if pl_path .isfile (pl_path .expanduser (env_vars .AWS_SHARED_CREDENTIALS_FILE .value )) then
364
+ local expanded_path , err = expanduser (env_vars .AWS_SHARED_CREDENTIALS_FILE .value )
365
+ if expanded_path and pl_path .isfile (expanded_path ) then
292
366
-- there is a creds file, so override creds with creds file
293
367
local creds = config .load_credentials_file (
294
368
env_vars .AWS_SHARED_CREDENTIALS_FILE .value , env_vars .AWS_PROFILE .value ) -- ignore error, already logged
@@ -297,6 +371,9 @@ function config.get_config()
297
371
cfg .aws_secret_access_key = creds .aws_secret_access_key
298
372
cfg .aws_session_token = creds .aws_session_token
299
373
end
374
+
375
+ else
376
+ ngx .log (ngx .DEBUG , " failed to expand credential file path or file does not exist: " , err )
300
377
end
301
378
302
379
-- add environment variables
0 commit comments