Skip to content

Commit b17e4c6

Browse files
committed
allow setting use_secret from Markdown meta keys.
ignore_missing_secret: allow use of password usage from config or meta key if secret environment variable is empty or undefined
1 parent a3350fd commit b17e4c6

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

encryptcontent/plugin.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class encryptContentPlugin(BasePlugin):
6161
# password feature
6262
('global_password', config_options.Type(string_types, default=None)),
6363
('use_secret', config_options.Type(string_types, default=None)),
64+
('ignore_missing_secret', config_options.Type(bool, default=False)),
6465
('password', config_options.Type(string_types, default=None)),
6566
('remember_password', config_options.Type(bool, default=False)),
6667
('session_storage', config_options.Type(bool, default=True)),
@@ -189,10 +190,16 @@ def on_config(self, config, **kwargs):
189190
if os.environ.get(str(self.config['use_secret'])):
190191
self.config['global_password'] = os.environ.get(str(self.config['use_secret']))
191192
else:
192-
logger.error('Cannot get global password from environment variable: {var}. Abort !'.format(
193-
var=str(self.config['use_secret']))
194-
)
195-
os._exit(1) # prevent build without password to avoid leak
193+
if self.config['ignore_missing_secret'] and self.config['global_password']:
194+
logger.warning('Cannot get global password from environment variable: {var}. Using global_password as fallback!'.format(
195+
var=str(self.config['use_secret']))
196+
)
197+
else:
198+
logger.error('Cannot get global password from environment variable: {var}. Abort !'.format(
199+
var=str(self.config['use_secret']))
200+
)
201+
os._exit(1) # prevent build without password to avoid leak
202+
196203
# Check if hljs feature need to be enabled, based on theme configuration
197204
if ('highlightjs' in config['theme']._vars
198205
and config['theme']._vars['highlightjs'] # noqa: W503
@@ -297,6 +304,21 @@ def on_page_markdown(self, markdown, page, config, **kwargs):
297304
self.config['password'] = None if page_password == '' else page_password
298305
# Delete meta password information before rendering to avoid leak :]
299306
del page.meta['password']
307+
308+
if 'use_secret' in page.meta.keys():
309+
if os.environ.get(str(page.meta.get('use_secret'))):
310+
self.config['password'] = os.environ.get(str(page.meta.get('use_secret')))
311+
else:
312+
if self.config['ignore_missing_secret'] and self.config['password']:
313+
logger.warning('Cannot get password from environment variable: {var}. Using password from config or meta key as fallback!'.format(
314+
var=str(page.meta.get('use_secret')))
315+
)
316+
else:
317+
logger.error('Cannot get password from environment variable: {var}. Abort !'.format(
318+
var=str(page.meta.get('use_secret')))
319+
)
320+
os._exit(1) # prevent build without password to avoid leak0
321+
300322
return markdown
301323

302324
def on_page_content(self, html, page, config, **kwargs):

0 commit comments

Comments
 (0)