Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tools/shoestring]: add support for advanced customization of rest.json #1091

Merged
merged 2 commits into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ _data/
.next/

# python
.python-version
__pycache__/
*.egg-info/
build/
Expand Down
33 changes: 16 additions & 17 deletions tools/shoestring/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,17 @@ setup \
[--package PACKAGE] \
[--directory DIRECTORY] \
[--overrides OVERRIDES] \
[--metadata METADATA] \
[--rest-overrides REST_OVERRIDES] \
[--security {default,paranoid,insecure}] \
--ca-key-path CA_KEY_PATH

--config CONFIG path to shoestring configuration file
--package PACKAGE Network configuration package. Possible values: (name | file:///filename | http(s)://uri) (default: mainnet)
--directory DIRECTORY installation directory (default: $HOME)
--overrides OVERRIDES path to custom user settings
--metadata METADATA custom node metadata (this is only valid for API roles)
--security security mode (default: default)
--ca-key-path CA_KEY_PATH path to main private key PEM file
--config CONFIG path to shoestring configuration file
--package PACKAGE Network configuration package. Possible values: (name | file:///filename | http(s)://uri) (default: mainnet)
--directory DIRECTORY installation directory (default: $HOME)
--overrides OVERRIDES path to custom user settings
--rest-overrides REST_OVERRIDES path to custom user REST settings (this is only valid for API roles)
--security security mode (default: default)
--ca-key-path CA_KEY_PATH path to main private key PEM file
```

Please note that only security mode "default" is supported at this time.
Expand Down Expand Up @@ -201,13 +201,13 @@ upgrade \
[--package PACKAGE] \
[--directory DIRECTORY] \
[--overrides OVERRIDES] \
[--metadata METADATA]
[--rest-overrides REST_OVERRIDES]

--config CONFIG path to shoestring configuration file
--package PACKAGE Network configuration package. Possible values: (name | file:///filename | http(s)://uri) (default: mainnet)
--directory DIRECTORY installation directory (default: $HOME)
--overrides OVERRIDES path to custom user settings
--metadata METADATA custom node metadata (this is only valid for API roles)
--config CONFIG path to shoestring configuration file
--package PACKAGE Network configuration package. Possible values: (name | file:///filename | http(s)://uri) (default: mainnet)
--directory DIRECTORY installation directory (default: $HOME)
--overrides OVERRIDES path to custom user settings
--rest-overrides REST_OVERRIDES path to custom user REST settings (this is only valid for API roles)
```

### renew-certificates
Expand Down Expand Up @@ -371,10 +371,9 @@ maxUnlockedAccounts = 2
Notice that these custom settings are applied *BEFORE* shoestring updates the Symbol configuration files.
In cases of conflicts, the shoestring changes will take precedence.

### Node Metadata
### REST Overrides

JSON file that is ingested and used to replace the contents of `nodeMetadata` in rest.json.
This data is then accessible via the `node/metadata` REST endpoint.
JSON file that is ingested and used to update the contents of rest.json.
This file is optional and only used for deployments including API role.

# Running
Expand Down
4 changes: 2 additions & 2 deletions tools/shoestring/shoestring/commands/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ async def run_main(args):
user_patches = load_patches_from_file(args.overrides)

preparer.configure_resources(user_patches)
preparer.configure_rest(args.metadata)
preparer.configure_rest(args.rest_overrides)

hostname = _resolve_hostname_and_configure_https(config, preparer)

Expand All @@ -137,7 +137,7 @@ def add_arguments(parser, is_initial_setup=True):
parser.add_argument('--package', help=_('argument-help-setup-package'), default='mainnet')
parser.add_argument('--directory', help=_('argument-help-directory').format(default_path=Path.home()), default=str(Path.home()))
parser.add_argument('--overrides', help=_('argument-help-setup-overrides'))
parser.add_argument('--metadata', help=_('argument-help-setup-metadata'))
parser.add_argument('--rest-overrides', help=_('argument-help-setup-rest-overrides'))

if is_initial_setup:
parser.add_argument('--security', help=_('argument-help-setup-security'), choices=SECURITY_MODES, default='default')
Expand Down
10 changes: 10 additions & 0 deletions tools/shoestring/shoestring/internal/ConfigurationManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,13 @@ def parse_time_span(str_value):
raise ValueError(f'time span specified in unknown units \'{unit_indicator}\'')

return value.timestamp


def merge_json_configuration(lhs, rhs):
"""Merges two json configuration objects."""

for key in rhs.keys():
if isinstance(rhs[key], dict) and key in lhs and isinstance(lhs[key], dict):
merge_json_configuration(lhs[key], rhs[key])
else:
lhs[key] = rhs[key]
12 changes: 6 additions & 6 deletions tools/shoestring/shoestring/internal/Preparer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from symbolchain.symbol.Network import NetworkTimestamp

from .CertificateFactory import CertificateFactory
from .ConfigurationManager import ConfigurationManager
from .ConfigurationManager import ConfigurationManager, merge_json_configuration
from .FileTemplater import apply_template
from .HarvesterConfigurator import HarvesterConfigurator
from .LinkTransactionBuilder import LinkTransactionBuilder
Expand Down Expand Up @@ -266,7 +266,7 @@ def configure_resources(self, user_patches=None):
# make all resources read only
self._make_files_readonly(self.directories.resources)

def configure_rest(self, node_metadata_filename=None):
def configure_rest(self, rest_overrides_filename=None):
"""Copies mongo and rest files."""

if NodeFeatures.API not in self.config.node.features:
Expand All @@ -277,16 +277,16 @@ def configure_rest(self, node_metadata_filename=None):

self._copy_file(self.directories.temp / 'rest' / 'rest.json', self.directories.userconfig)

if node_metadata_filename:
if rest_overrides_filename:
rest_json_filepath = self.directories.userconfig / 'rest.json'

# load the rest config
with open(rest_json_filepath, 'rt', encoding='utf8') as rest_config_infile:
rest_config = json.load(rest_config_infile)

# replace user metadata
with open(node_metadata_filename, 'rt', encoding='utf8') as node_metadata_infile:
rest_config['nodeMetadata'] = json.load(node_metadata_infile)
# customize user rest config
with open(rest_overrides_filename, 'rt', encoding='utf8') as rest_overrides_infile:
merge_json_configuration(rest_config, json.load(rest_overrides_infile))

# rewrite the rest config
with open(rest_json_filepath, 'wt', encoding='utf8') as rest_config_outfile:
Expand Down
23 changes: 11 additions & 12 deletions tools/shoestring/shoestring/lang/en/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ msgstr "path to main private key PEM file"

#: shoestring/commands/announce_transaction.py:35
#: shoestring/commands/health.py:74 shoestring/commands/import_bootstrap.py:33
#: shoestring/commands/import_harvesters.py:94 shoestring/commands/init.py:29
#: shoestring/commands/import_harvesters.py:97 shoestring/commands/init.py:29
#: shoestring/commands/min_cosignatures_count.py:34
#: shoestring/commands/renew_certificates.py:29
#: shoestring/commands/renew_voting_keys.py:112
Expand All @@ -47,19 +47,19 @@ msgstr "installation directory (default: {default_path})"
msgid "argument-help-import-bootstrap-bootstrap"
msgstr "path to bootstrap target directory"

#: shoestring/commands/import_harvesters.py:95
#: shoestring/commands/import_harvesters.py:98
msgid "argument-help-import-harvesters-in-harvesters"
msgstr "input harvesters.dat file that is encrypted with in-pem"

#: shoestring/commands/import_harvesters.py:96
#: shoestring/commands/import_harvesters.py:99
msgid "argument-help-import-harvesters-in-pem"
msgstr "PEM file that can be used to decrypt in-harvesters"

#: shoestring/commands/import_harvesters.py:98
#: shoestring/commands/import_harvesters.py:101
msgid "argument-help-import-harvesters-out-harvesters"
msgstr "output harvesters.dat file that will be encrypted with out-pem"

#: shoestring/commands/import_harvesters.py:99
#: shoestring/commands/import_harvesters.py:102
msgid "argument-help-import-harvesters-out-pem"
msgstr "PEM file that can be used to encrypt out-harvesters"

Expand Down Expand Up @@ -91,10 +91,6 @@ msgstr "renews CA certificate too"
msgid "argument-help-reset-data-purge-harvesters"
msgstr "purge harvesters.dat file"

#: shoestring/commands/setup.py:140
msgid "argument-help-setup-metadata"
msgstr "Custom node metadata (this is only valid for API roles)"

#: shoestring/commands/setup.py:139
msgid "argument-help-setup-overrides"
msgstr "path to custom user settings"
Expand All @@ -105,6 +101,10 @@ msgstr ""
"Network configuration package. Possible values: (name | file:///filename "
"| http(s)://uri) (default: mainnet)"

#: shoestring/commands/setup.py:140
msgid "argument-help-setup-rest-overrides"
msgstr "path to custom user REST settings (this is only valid for API roles)"

#: shoestring/commands/setup.py:143
msgid "argument-help-setup-security"
msgstr "security mode (default: default)"
Expand Down Expand Up @@ -260,11 +260,11 @@ msgstr ""
"bootstrap directory provided ({directory}) does not look like bootstrap's"
" target directory, nothing to import"

#: shoestring/commands/import_harvesters.py:74
#: shoestring/commands/import_harvesters.py:75
msgid "import-harvesters-error-in-harvesters-is-equal-to-out-harvesters"
msgstr "in-harvesters and out-harvesters must be different"

#: shoestring/commands/import_harvesters.py:51
#: shoestring/commands/import_harvesters.py:52
msgid "import-harvesters-list-header"
msgstr "listing harvesters in {filepath} using public key {public_key}"

Expand Down Expand Up @@ -826,4 +826,3 @@ msgstr "Welcome"
#: shoestring/wizard/screens/welcome.py:36
msgid "wizard-welcome-upgrade"
msgstr "upgrade"

23 changes: 11 additions & 12 deletions tools/shoestring/shoestring/lang/ja/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ msgstr ""

#: shoestring/commands/announce_transaction.py:35
#: shoestring/commands/health.py:74 shoestring/commands/import_bootstrap.py:33
#: shoestring/commands/import_harvesters.py:94 shoestring/commands/init.py:29
#: shoestring/commands/import_harvesters.py:97 shoestring/commands/init.py:29
#: shoestring/commands/min_cosignatures_count.py:34
#: shoestring/commands/renew_certificates.py:29
#: shoestring/commands/renew_voting_keys.py:112
Expand All @@ -45,19 +45,19 @@ msgstr ""
msgid "argument-help-import-bootstrap-bootstrap"
msgstr ""

#: shoestring/commands/import_harvesters.py:95
#: shoestring/commands/import_harvesters.py:98
msgid "argument-help-import-harvesters-in-harvesters"
msgstr ""

#: shoestring/commands/import_harvesters.py:96
#: shoestring/commands/import_harvesters.py:99
msgid "argument-help-import-harvesters-in-pem"
msgstr ""

#: shoestring/commands/import_harvesters.py:98
#: shoestring/commands/import_harvesters.py:101
msgid "argument-help-import-harvesters-out-harvesters"
msgstr ""

#: shoestring/commands/import_harvesters.py:99
#: shoestring/commands/import_harvesters.py:102
msgid "argument-help-import-harvesters-out-pem"
msgstr ""

Expand Down Expand Up @@ -89,10 +89,6 @@ msgstr ""
msgid "argument-help-reset-data-purge-harvesters"
msgstr ""

#: shoestring/commands/setup.py:140
msgid "argument-help-setup-metadata"
msgstr ""

#: shoestring/commands/setup.py:139
msgid "argument-help-setup-overrides"
msgstr ""
Expand All @@ -101,6 +97,10 @@ msgstr ""
msgid "argument-help-setup-package"
msgstr ""

#: shoestring/commands/setup.py:140
msgid "argument-help-setup-rest-overrides"
msgstr ""

#: shoestring/commands/setup.py:143
msgid "argument-help-setup-security"
msgstr ""
Expand Down Expand Up @@ -250,11 +250,11 @@ msgstr ""
msgid "import-bootstrap-invalid-directory"
msgstr ""

#: shoestring/commands/import_harvesters.py:74
#: shoestring/commands/import_harvesters.py:75
msgid "import-harvesters-error-in-harvesters-is-equal-to-out-harvesters"
msgstr ""

#: shoestring/commands/import_harvesters.py:51
#: shoestring/commands/import_harvesters.py:52
msgid "import-harvesters-list-header"
msgstr ""

Expand Down Expand Up @@ -803,4 +803,3 @@ msgstr ""
#: shoestring/wizard/screens/welcome.py:36
msgid "wizard-welcome-upgrade"
msgstr ""

22 changes: 11 additions & 11 deletions tools/shoestring/shoestring/lang/messages.pot
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ msgstr ""

#: shoestring/commands/announce_transaction.py:35
#: shoestring/commands/health.py:74 shoestring/commands/import_bootstrap.py:33
#: shoestring/commands/import_harvesters.py:94 shoestring/commands/init.py:29
#: shoestring/commands/import_harvesters.py:97 shoestring/commands/init.py:29
#: shoestring/commands/min_cosignatures_count.py:34
#: shoestring/commands/renew_certificates.py:29
#: shoestring/commands/renew_voting_keys.py:112
Expand All @@ -38,19 +38,19 @@ msgstr ""
msgid "argument-help-import-bootstrap-bootstrap"
msgstr ""

#: shoestring/commands/import_harvesters.py:95
#: shoestring/commands/import_harvesters.py:98
msgid "argument-help-import-harvesters-in-harvesters"
msgstr ""

#: shoestring/commands/import_harvesters.py:96
#: shoestring/commands/import_harvesters.py:99
msgid "argument-help-import-harvesters-in-pem"
msgstr ""

#: shoestring/commands/import_harvesters.py:98
#: shoestring/commands/import_harvesters.py:101
msgid "argument-help-import-harvesters-out-harvesters"
msgstr ""

#: shoestring/commands/import_harvesters.py:99
#: shoestring/commands/import_harvesters.py:102
msgid "argument-help-import-harvesters-out-pem"
msgstr ""

Expand Down Expand Up @@ -82,10 +82,6 @@ msgstr ""
msgid "argument-help-reset-data-purge-harvesters"
msgstr ""

#: shoestring/commands/setup.py:140
msgid "argument-help-setup-metadata"
msgstr ""

#: shoestring/commands/setup.py:139
msgid "argument-help-setup-overrides"
msgstr ""
Expand All @@ -94,6 +90,10 @@ msgstr ""
msgid "argument-help-setup-package"
msgstr ""

#: shoestring/commands/setup.py:140
msgid "argument-help-setup-rest-overrides"
msgstr ""

#: shoestring/commands/setup.py:143
msgid "argument-help-setup-security"
msgstr ""
Expand Down Expand Up @@ -243,11 +243,11 @@ msgstr ""
msgid "import-bootstrap-invalid-directory"
msgstr ""

#: shoestring/commands/import_harvesters.py:74
#: shoestring/commands/import_harvesters.py:75
msgid "import-harvesters-error-in-harvesters-is-equal-to-out-harvesters"
msgstr ""

#: shoestring/commands/import_harvesters.py:51
#: shoestring/commands/import_harvesters.py:52
msgid "import-harvesters-list-header"
msgstr ""

Expand Down
6 changes: 3 additions & 3 deletions tools/shoestring/shoestring/wizard/ShoestringOperation.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def build_shoestring_command(
shoestring_directory,
ca_pem_path,
package,
has_custom_node_metadata=False
has_custom_rest_overrides=False
): # pylint: disable=too-many-arguments
"""Builds shoestring command arguments."""

Expand Down Expand Up @@ -58,7 +58,7 @@ def build_shoestring_command(
if ShoestringOperation.SETUP == operation:
shoestring_args.extend(['--security', 'insecure'])

if has_custom_node_metadata:
shoestring_args.extend(['--metadata', str(Path(shoestring_directory) / 'node_metadata.json')])
if has_custom_rest_overrides:
shoestring_args.extend(['--rest-overrides', str(Path(shoestring_directory) / 'rest_overrides.json')])

return shoestring_args
6 changes: 3 additions & 3 deletions tools/shoestring/shoestring/wizard/setup_file_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ def _to_bool_string(flag):
return 'true' if flag else 'false'


def try_prepare_node_metadata_file(screens, output_filename):
"""Prepares a node metadata file based on screens, if specified."""
def try_prepare_rest_overrides_file(screens, output_filename):
"""Prepares a REST overrides file based on screens, if specified."""

node_type = screens.get('node-type').current_value
node_settings = screens.get('node-settings')
Expand All @@ -30,7 +30,7 @@ def try_prepare_node_metadata_file(screens, output_filename):
return False

with open(output_filename, 'wt', encoding='utf8') as outfile:
outfile.write(node_settings.metadata_info)
outfile.write(f'{{"nodeMetadata":{node_settings.metadata_info}}}')

return True

Expand Down
Loading