Skip to content

Commit edeccc5

Browse files
committed
ID-56: read properties from stdin
1 parent b1037b0 commit edeccc5

File tree

4 files changed

+57
-12
lines changed

4 files changed

+57
-12
lines changed

CHANGELOG.adoc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,33 @@
77
|https://github.com/Axway-API-Management-Plus/apigw-maven-plugin/issues/55[#55]
88
|Enhancement
99
|Add support for multiple base directories for certificates.
10+
11+
|https://github.com/Axway-API-Management-Plus/apigw-maven-plugin/issues/56[#56]
12+
|Enhancement
13+
|Read properties from stdin.
14+
15+
If the filename of the `--prop` parameter is a single dash `-`, stdin will be used to read properties from a flat key/value JSON document.
16+
17+
.Example of flat key/value JSON
18+
[source, json]
19+
----
20+
{
21+
"prop1": "value1",
22+
"prop2": "value2"
23+
}
24+
----
25+
26+
[NOTE]
27+
====
28+
This feature can be used to read properties from external sources (e.g. AWS Secrets Manager or Hashicorp Vault).
29+
30+
[source, shell]
31+
----
32+
# Get properties from Vault key/value engine
33+
vault kv get -format=json kv/apim \| jq .data.data \| buildfed --prop - -p src/gw.pol -e src/gw.env -c config/gw.config.json
34+
----
35+
====
36+
1037
|===
1138

1239
== Version 1.1.0

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def main():
8888
if not os.path.isfile(prop_file):
8989
raise ValueError("File for command line property '%s' doesn't exist: %s" % (name, prop_file))
9090

91-
logging.debug("Reading command line property '%s' from file '%s'" % (name, prop_file))
91+
logging.info("Reading command line property '%s' from file '%s'" % (name, prop_file))
9292
with codecs.open(prop_file, encoding='utf-8', mode='r') as pf:
9393
cli_properties[name] = pf.read()
9494

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

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
import logging
44
import base64
5+
import sys
56

67
from com.vordel.es.xes import PortableESPKFactory
78
from java.text import SimpleDateFormat
@@ -24,23 +25,40 @@ def __init__(self, field_key, value):
2425

2526
class PropConfig:
2627
def __init__(self, property_file_path_list):
28+
stdin_loaded = False
2729
self.__properties = {}
2830
if not property_file_path_list:
2931
return
3032

3133
for property_file_path in property_file_path_list:
32-
if not os.path.exists(property_file_path):
33-
raise ValueError("Property file '%s' doesn't exist!" % (property_file_path))
34+
properties = None
35+
if "-" == property_file_path:
36+
if stdin_loaded:
37+
# properties already loaded from stdin
38+
continue
39+
stdin_loaded = True
40+
logging.info("Reading properties from stdin")
41+
properties = json.load(sys.stdin)
42+
else:
43+
if not os.path.exists(property_file_path):
44+
raise ValueError("Property file '%s' doesn't exist!" % (property_file_path))
3445

35-
logging.info("Reading property file '%s'" % (property_file_path))
36-
with open(property_file_path) as property_file:
37-
properties_json = json.load(property_file)
46+
logging.info("Reading property file '%s'" % (property_file_path))
47+
with open(property_file_path) as property_file:
48+
properties_json = json.load(property_file)
3849

39-
if "properties" not in properties_json:
40-
raise ValueError("File '%s' is not a valid property file; missing 'properties' attribute!" % (property_file_path))
41-
42-
for p in properties_json["properties"]:
43-
self.__properties[p] = properties_json["properties"][p]
50+
if "properties" not in properties_json:
51+
raise ValueError("File '%s' is not a valid property file; missing 'properties' attribute!" % (property_file_path))
52+
53+
properties = properties_json["properties"]
54+
55+
# Add or overwrite properties
56+
for p in properties:
57+
if p not in self.__properties:
58+
logging.debug("set property '%s' from '%s'" % (p, property_file_path))
59+
else:
60+
logging.debug("override property '%s' from '%s'" % (p, property_file_path))
61+
self.__properties[p] = properties[p]
4462
return
4563

4664
def get_property(self, key):

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def __configure_entities(self):
123123
def __resolve_file_path(self, file):
124124
"""Searches the certificate file within all specified base directories.
125125
126-
The first existing file wil be returned.
126+
The first existing file will be returned.
127127
"""
128128
if file and self.__base_dirs:
129129
for base_dir in self.__base_dirs:

0 commit comments

Comments
 (0)