Skip to content

Commit 0310b9e

Browse files
committed
ID-29: Add support for environment variables for field and password configuration
1 parent 9d29b55 commit 0310b9e

File tree

7 files changed

+89
-17
lines changed

7 files changed

+89
-17
lines changed

CHANGELOG.adoc

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,39 @@ In case of "source" is equal to "property" the password is retrieved form the pr
111111
<4> specifies a property as the source of the password
112112

113113
NOTE: Configuration files in the old format are automatically converted into the new format.
114+
115+
|https://github.com/Axway-API-Management-Plus/apigw-maven-plugin/issues/29[#29]
116+
|Enhancement
117+
|Add support for environment variables for field and password configuration.
118+
119+
The `source` property new supports a new value `env` to specify an environment variable as the source for field values and certificate passwords.
120+
121+
*Environmentalized Fields*
122+
[source,json]
123+
----
124+
"field#0": {
125+
"source": "env", <1>
126+
"type": string",
127+
"used": true,
128+
"value": "ENV_NAME" <2>
129+
}
130+
----
131+
<1> specifies an environment variable as the source of the field value
132+
<2> field value is retrieved from the environment variable `ENV_NAME`
133+
134+
*Certificates*
135+
[source,json]
136+
----
137+
"update": {
138+
"file": "cert/server.p12",
139+
"password": "SERVER_PASSWORD", <1>
140+
"source": "env" <2>
141+
"type": "p12"
142+
}
143+
----
144+
<1> password is retrieved from the environment variable `SERVER_PASSWORD`
145+
<2> specifies an environment variable as the source of the password
146+
114147
|===
115148

116149

doc/manual/_config-tool.adoc

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ For each configured environmentalized entity a property exists.
157157
The name of the property is the _short hand key_ of the entity.
158158

159159
The value of the field can be specified directly by a `value` property.
160-
It can also be specified indirectly by a property identified by the `property` property.
160+
It can also be specified indirectly by a property or environment variables (controlled via the `source` property).
161161

162162
Properties are name/value pairs.
163163
The value of the property is defined ... _(in order of precedence)_
@@ -221,7 +221,7 @@ The plugin and the configuration tool provides a set of predefined system proper
221221
"value": "_system.build.datetime" <8>
222222
}
223223
}
224-
}
224+
},
225225
"/[CircuitContainer]name=Hello World/[FilterCircuit]name=Hello World Message/[SetAttributeFilter]name=Network zone": {
226226
"description": "Network zone the API Gateway is located.",
227227
"fields": {
@@ -232,9 +232,20 @@ The plugin and the configuration tool provides a set of predefined system proper
232232
"value": "internal" <9>
233233
}
234234
}
235+
},
236+
"/[CircuitContainer]name=Hello World/[FilterCircuit]name=Hello World Message/[SetAttributeFilter]name=User": {
237+
"description": "Name of the user building the .fed",
238+
"fields": {
239+
"attributeValue#0": {
240+
"source": "env" <10>
241+
"type": "string",
242+
"used": true,
243+
"value": "USERNAME" <11>
244+
}
245+
}
235246
}
236247
}
237-
"properties": { <10>
248+
"properties": { <12>
238249
"foobar": "myvalue"
239250
}
240251
}
@@ -250,7 +261,9 @@ The property is automatically maintained by the plugin.
250261
<7> Name of the property containing the value.
251262
<8> Use value of the predefined system property `_system.build.datetime`.
252263
<9> Literal value for the field.
253-
<10> An optional local definition of properties.
264+
<10> Specifies an environment variable as the source for the field value.
265+
<11> Field value is retrieved from the `USERNAME` environment variable.
266+
<12> An optional local definition of properties.
254267
If the same property is defined in a separate property file (see below), the separate property has precedence.
255268

256269
NOTE: If `value` is _null_ the field is treated as undefined and the build will fail.
@@ -306,6 +319,14 @@ It specifies the alias of the certificates within the project and the source of
306319
"source": "property", <14>
307320
"type": "p12"
308321
}
322+
},
323+
"test3": {
324+
"update": {
325+
"file": "cert/server.p12",
326+
"password": "TEST3_PASSWORD", <15>
327+
"source": "env", <16>
328+
"type": "p12"
329+
}
309330
}
310331
}
311332
}
@@ -328,6 +349,8 @@ A missing `update` attribute indicates a new/unconfigured certificate.
328349
This certificate will be added to the certificate store.
329350
<13> Password for the `.p12` file is retrieved from the property configuration file.
330351
<14> Declares the property "password" as the source of the password for the `.p12` file.
352+
<15> The password is retrieved from the `TEST3_PASSWORD` environment variable.
353+
<16> Specifies an environment variable as the source of the password.
331354

332355
=== Properties
333356

example/config-tool/config/gateway.certs.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
{
22
"certificates": {
3+
"new-server": {
4+
"update": {
5+
"file": "config/certs/staged-server.p12",
6+
"password": "NEW_SERVER_PASSWORD",
7+
"source": "env",
8+
"type": "p12"
9+
}
10+
},
311
"server": {
412
"origin": {
513
"info": {
@@ -9,10 +17,6 @@
917
},
1018
"update": {
1119
"file": "config/certs/staged-server.p12",
12-
"info": {
13-
"not_after": "2020-08-23T20:26:00+02:00",
14-
"subject": "CN=staged, O=ACME Inc., C=EX"
15-
},
1620
"password": "cert.password.staged.server",
1721
"source": "property",
1822
"type": "p12"
@@ -27,10 +31,6 @@
2731
},
2832
"update": {
2933
"file": "config/certs/staged-root-ca.crt",
30-
"info": {
31-
"not_after": "2029-08-23T20:22:00+02:00",
32-
"subject": "CN=staged-acme-inc-example, O=ACME Inc. (staged), C=EX"
33-
},
3434
"type": "crt"
3535
}
3636
}

example/config-tool/config/gateway.config.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@
3232
"value": "service.address"
3333
},
3434
"port#0": {
35-
"source": "property",
35+
"source": "env",
3636
"type": "string",
3737
"used": true,
38-
"value": "service.port"
38+
"value": "SERVICE_PORT"
3939
}
4040
}
4141
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"properties": {
3-
"service.address": "localhost",
4-
"service.port": "18443"
3+
"service.address": "localhost"
54
}
65
}

example/config-tool/run.cmd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,9 @@ SET CMD_HOME=%~dp0
44
CD /d "%CMD_HOME%"
55
SET BUILDFED="..\..\src\main\resources\scripts\buildfed.cmd"
66

7+
REM Define environment variables for field value and password configuration
8+
SET SERVICE_PORT=18443
9+
SET NEW_SERVER_PASSWORD=changeme
10+
711
CALL %BUILDFED% -e src\gateway.env -p src\gateway.pol -c config\gateway.config.json --cert=config\gateway.certs.json --prop=config\gateway.props.json --prop=config\passwords.props.json -D artifact:demo-1.0.0 --output-fed=gateway.fed --passphrase-in=changeme --passphrase-out=changed
812
ENDLOCAL

src/main/resources/scripts/lib/envconfig.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,13 @@ def get_value(self, entity, entity_field):
167167
fk = FieldKey(self.to_shorthandkey(entity), entity_field.getEntityFieldName(), entity_field.getIndex(), entity_field.getType())
168168
f = self.__get_field(fk)
169169

170-
value = None
171170
if "source" not in f:
172171
raise ValueError("Missing 'source' property in field '%s#%s' of entity '%s'" % (fk.name, str(fk.index), fk.short_hand_key))
173172

173+
if "value" not in f:
174+
raise ValueError("Missing 'value' property in field '%s#%s' of entity '%s'" % (fk.name, str(fk.index), fk.short_hand_key))
175+
176+
value = None
174177
if "property" == f["source"]:
175178
if f["value"]:
176179
p = f["value"]
@@ -179,6 +182,10 @@ def get_value(self, entity, entity_field):
179182
raise ValueError("Missing configured property '%s'" % (p))
180183
elif "value" == f["source"]:
181184
value = f["value"]
185+
elif "env" == f["source"]:
186+
if f["value"]:
187+
e = f["value"]
188+
value = os.environ[e]
182189
else:
183190
raise ValueError("Invalid source property '%s'" % f["source"])
184191

@@ -350,6 +357,9 @@ def get_certificates(self):
350357

351358
password = None
352359
if "password" in cert:
360+
if not cert["password"]:
361+
raise ValueError("Missing value for 'password' property in 'update' for alias '%s'!" % alias)
362+
353363
if "source" not in cert:
354364
raise ValueError("Missing 'source' property in 'update' for alias '%s'!" % alias)
355365

@@ -360,6 +370,9 @@ def get_certificates(self):
360370
raise ValueError("Missing configured property '%s' for alias '%s'!" % (p, alias))
361371
elif "password" == cert["source"]:
362372
password = cert["password"]
373+
elif "env" == cert["source"]:
374+
e = cert["password"]
375+
password = os.environ[e]
363376
else:
364377
raise ValueError("Invalid password source '%s' for alias '%s'!" % (cert["source"], alias))
365378

0 commit comments

Comments
 (0)