# Yaml types and Nameko environment variable substitution

**URL:** <https://discourse.nameko.io/t/yaml-types-and-nameko-environment-variable-substitution/176>\
**Category:** googlegroup\
**Created:** [March 17, 2017, 4:27pm UTC](https://discourse.nameko.io/t/yaml-types-and-nameko-environment-variable-substitution/176 "2017-03-17T16:27:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trex\_Boolat](https://avatars.discourse-cdn.com/v4/letter/t/edb3f5/32.png) [@Trex\_Boolat](https://discourse.nameko.io/u/Trex_Boolat)\
**Post date:** [March 17, 2017, 4:27pm UTC](https://discourse.nameko.io/t/yaml-types-and-nameko-environment-variable-substitution/176/1 "2017-03-17T16:27:11Z")

</div>

When we use flow  
Jijnja2 -\> Nameko  
yaml files receive substitution values BEFORE PyYaml type resolver  
#before Jinja2 processing  
b: {{var\_b}}

#after Jinja2 processing  
b: 4242

#in nameko config  
{ 'a': True, 'b': 4242}

When we start using  
[http://nameko.readthedocs.io/en/stable/cli.html#environment-variable-substitution](http://nameko.readthedocs.io/en/stable/cli.html#environment-variable-substitution)  
- we receive always string

#Yaml  
b: ${var\_b}

#in nameko config  
{ 'a': 'true', 'b': '4242'}

Maybe trow this strin back to PyYaml resolver?  
My fixes in main.py

def \_env\_var\_constructor(loader, node):  
&nbsp;&nbsp;&nbsp;&nbsp;raw\_value = loader.construct\_scalar(node)  
&nbsp;&nbsp;&nbsp;&nbsp;value = ENV\_VAR\_MATCHER.sub(\_replace\_env\_var, raw\_value)  
&nbsp;&nbsp;&nbsp;&nbsp;new\_tag = loader.resolve(yaml.ScalarNode, value, (True, False)) # \<- re-resolve yaml tag  
&nbsp;&nbsp;&nbsp;&nbsp;return loader.yaml\_constructors[new\_tag](loader, yaml.ScalarNode(new\_tag, value)) # \<- rebuild yaml node

> **···**
>
> a: {{var\_a}}  
> a: true  
> a: ${var\_a}

---

<div class="post-metadata">

**Author:** ![mattbennett](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.nameko.io/mattbennett/32/13_2.png) [@mattbennett](https://discourse.nameko.io/u/mattbennett)\
**Post date:** [March 18, 2017, 11:28am UTC](https://discourse.nameko.io/t/yaml-types-and-nameko-environment-variable-substitution/176/2 "2017-03-18T11:28:39Z")

</div>

This seems like a good idea to me. Again, you want to raise a pull request?

> **···**
>
> On Friday, March 17, 2017 at 4:27:12 PM UTC, trex....@gmail.com wrote:
> 
> > When we use flow  
> > Jijnja2 -\> Nameko  
> > yaml files receive substitution values BEFORE PyYaml type resolver  
> > #before Jinja2 processing  
> > a: {{var\_a}}  
> > b: {{var\_b}}
> > 
> > #after Jinja2 processing  
> > a: true  
> > b: 4242
> > 
> > #in nameko config  
> > { 'a': True, 'b': 4242}
> > 
> > When we start using  
> > [Command Line Interface — nameko 2.12.0 documentation](http://nameko.readthedocs.io/en/stable/cli.html#environment-variable-substitution)  
> > - we receive always string
> > 
> > #Yaml  
> > a: ${var\_a}  
> > b: ${var\_b}
> > 
> > #in nameko config  
> > { 'a': 'true', 'b': '4242'}
> > 
> > Maybe trow this strin back to PyYaml resolver?  
> > My fixes in main.py
> > 
> > def \_env\_var\_constructor(loader, node):  
> > &nbsp;&nbsp;&nbsp;&nbsp;raw\_value = loader.construct\_scalar(node)  
> > &nbsp;&nbsp;&nbsp;&nbsp;value = ENV\_VAR\_MATCHER.sub(\_replace\_env\_var, raw\_value)  
> > &nbsp;&nbsp;&nbsp;&nbsp;new\_tag = loader.resolve(yaml.ScalarNode, value, (True, False)) # \<- re-resolve yaml tag  
> > &nbsp;&nbsp;&nbsp;&nbsp;return loader.yaml\_constructors[new\_tag](loader, yaml.ScalarNode(new\_tag, value)) # \<- rebuild yaml node

---

<div class="post-metadata">

**Author:** ![Trex\_Boolat](https://avatars.discourse-cdn.com/v4/letter/t/edb3f5/32.png) [@Trex\_Boolat](https://discourse.nameko.io/u/Trex_Boolat)\
**Post date:** [March 18, 2017, 4:26pm UTC](https://discourse.nameko.io/t/yaml-types-and-nameko-environment-variable-substitution/176/3 "2017-03-18T16:26:57Z")

</div>

Yes, if this change not broke any plans and no unwanted side effect.

> **···**
>
> On Saturday, March 18, 2017 at 1:28:39 PM UTC+2, Matt Yule-Bennett wrote:
> 
> > This seems like a good idea to me. Again, you want to raise a pull request?

---

<div class="post-metadata">

**Author:** ![David\_Szotten](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.nameko.io/david_szotten/32/6_2.png) [@David\_Szotten](https://discourse.nameko.io/u/David_Szotten)\
**Post date:** [March 18, 2017, 5:10pm UTC](https://discourse.nameko.io/t/yaml-types-and-nameko-environment-variable-substitution/176/4 "2017-03-18T17:10:53Z")

</div>

are there any security implications to consider?

if we go ahead, we should make sure we document the behaviour

thanks for the suggestion!

best,  
david

> **···**
>
> On Saturday, 18 March 2017 16:26:57 UTC, trex....@gmail.com wrote:
> 
> > On Saturday, March 18, 2017 at 1:28:39 PM UTC+2, Matt Yule-Bennett wrote:
> > 
> > > This seems like a good idea to me. Again, you want to raise a pull  
> > > request?
> > 
> > Yes, if this change not broke any plans and no unwanted side effect.

---

<div class="post-metadata">

**Author:** ![mattbennett](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.nameko.io/mattbennett/32/13_2.png) [@mattbennett](https://discourse.nameko.io/u/mattbennett)\
**Post date:** [March 19, 2017, 4:49pm UTC](https://discourse.nameko.io/t/yaml-types-and-nameko-environment-variable-substitution/176/5 "2017-03-19T16:49:31Z")

</div>

Thinking about the security -- we're loading the contents of an environment  
variable as yaml. That doesn't seem to be any more dangerous than loading  
the contents of a file.

> **···**
>
> On Saturday, March 18, 2017 at 5:10:54 PM UTC, David Szotten wrote:
> 
> > are there any security implications to consider?
> > 
> > if we go ahead, we should make sure we document the behaviour
> > 
> > thanks for the suggestion!
> > 
> > best,  
> > david
> > 
> > On Saturday, 18 March 2017 16:26:57 UTC, trex....@gmail.com wrote:
> > 
> > > On Saturday, March 18, 2017 at 1:28:39 PM UTC+2, Matt Yule-Bennett wrote:
> > > 
> > > > This seems like a good idea to me. Again, you want to raise a pull  
> > > > request?
> > > 
> > > Yes, if this change not broke any plans and no unwanted side effect.

---

<div class="post-metadata">

**Author:** ![Trex\_Boolat](https://avatars.discourse-cdn.com/v4/letter/t/edb3f5/32.png) [@Trex\_Boolat](https://discourse.nameko.io/u/Trex_Boolat)\
**Post date:** [March 19, 2017, 9:33pm UTC](https://discourse.nameko.io/t/yaml-types-and-nameko-environment-variable-substitution/176/6 "2017-03-19T21:33:17Z")

</div>

> <https://github.com/nameko/nameko/pull/421>
>
> Using http://nameko.readthedocs.io/en/stable/cli.html#environment-variable-subst…itution - always receive string in nameco config. Via re-throw PyYaml resolver - receive int/bool/float in config.
> 
> Based on: https://groups.google.com/forum/#!topic/nameko-dev/oC2JtEflLtM

> **···**
>
> On Sunday, March 19, 2017 at 6:49:31 PM UTC+2, Matt Yule-Bennett wrote:
> 
> > Thinking about the security -- we're loading the contents of an  
> > environment variable as yaml. That doesn't seem to be any more dangerous  
> > than loading the contents of a file.
> > 
> > On Saturday, March 18, 2017 at 5:10:54 PM UTC, David Szotten wrote:
> > 
> > > are there any security implications to consider?
> > > 
> > > if we go ahead, we should make sure we document the behaviour
> > > 
> > > thanks for the suggestion!
> > > 
> > > best,  
> > > david
> > > 
> > > On Saturday, 18 March 2017 16:26:57 UTC, trex....@gmail.com wrote:
> > > 
> > > > On Saturday, March 18, 2017 at 1:28:39 PM UTC+2, Matt Yule-Bennett wrote:
> > > > 
> > > > > This seems like a good idea to me. Again, you want to raise a pull  
> > > > > request?
> > > > 
> > > > Yes, if this change not broke any plans and no unwanted side effect.
