From aae53dad4a02ca5b7591ff07059c0b99190aee7e Mon Sep 17 00:00:00 2001 From: John Estabrook Date: Mon, 29 Nov 2021 16:02:12 -0600 Subject: graphql: T3993: add config session show_config Note that one can also use the mutation Show, with path ["configuration", "json", "pretty"]; that command will obscure passwords and keys, and we may want to disallow this version. (cherry picked from commit a05866e5301934f61a3c83550f91926e03bfc7b0) --- src/services/api/graphql/graphql/directives.py | 9 +++++++++ src/services/api/graphql/graphql/mutations.py | 4 ++++ .../api/graphql/graphql/schema/schema.graphql | 2 ++ .../api/graphql/graphql/schema/show_config.graphql | 21 +++++++++++++++++++++ src/services/api/graphql/recipes/session.py | 19 +++++++++++++++++++ 5 files changed, 55 insertions(+) create mode 100644 src/services/api/graphql/graphql/schema/show_config.graphql (limited to 'src') diff --git a/src/services/api/graphql/graphql/directives.py b/src/services/api/graphql/graphql/directives.py index 55aceca1b..4bc31c6b5 100644 --- a/src/services/api/graphql/graphql/directives.py +++ b/src/services/api/graphql/graphql/directives.py @@ -24,6 +24,14 @@ class ConfigureDirective(VyosDirective): super().visit_field_definition(field, object_type, make_resolver=make_configure_resolver) +class ShowConfigDirective(VyosDirective): + """ + Class providing implementation of 'show' directive in schema. + """ + def visit_field_definition(self, field, object_type): + super().visit_field_definition(field, object_type, + make_resolver=make_show_config_resolver) + class ConfigFileDirective(VyosDirective): """ Class providing implementation of 'configfile' directive in schema. @@ -41,5 +49,6 @@ class ShowDirective(VyosDirective): make_resolver=make_show_resolver) directives_dict = {"configure": ConfigureDirective, + "showconfig": ShowConfigDirective, "configfile": ConfigFileDirective, "show": ShowDirective} diff --git a/src/services/api/graphql/graphql/mutations.py b/src/services/api/graphql/graphql/mutations.py index 5913ee8b1..0ba2cd4bb 100644 --- a/src/services/api/graphql/graphql/mutations.py +++ b/src/services/api/graphql/graphql/mutations.py @@ -70,6 +70,10 @@ def make_configure_resolver(mutation_name): class_name = mutation_name return make_resolver(mutation_name, class_name, 'configure') +def make_show_config_resolver(mutation_name): + class_name = mutation_name + return make_resolver(mutation_name, class_name, 'show_config') + def make_config_file_resolver(mutation_name): if 'Save' in mutation_name: class_name = mutation_name.replace('Save', '', 1) diff --git a/src/services/api/graphql/graphql/schema/schema.graphql b/src/services/api/graphql/graphql/schema/schema.graphql index 764a50130..375b88cc5 100644 --- a/src/services/api/graphql/graphql/schema/schema.graphql +++ b/src/services/api/graphql/graphql/schema/schema.graphql @@ -10,6 +10,7 @@ type Query { directive @configure on FIELD_DEFINITION directive @configfile on FIELD_DEFINITION directive @show on FIELD_DEFINITION +directive @showconfig on FIELD_DEFINITION type Mutation { CreateDhcpServer(data: DhcpServerConfigInput) : CreateDhcpServerResult @configure @@ -20,4 +21,5 @@ type Mutation { SaveConfigFile(data: SaveConfigFileInput) : SaveConfigFileResult @configfile LoadConfigFile(data: LoadConfigFileInput) : LoadConfigFileResult @configfile Show(data: ShowInput) : ShowResult @show + ShowConfig(data: ShowConfigInput) : ShowConfigResult @showconfig } diff --git a/src/services/api/graphql/graphql/schema/show_config.graphql b/src/services/api/graphql/graphql/schema/show_config.graphql new file mode 100644 index 000000000..34afd2aa9 --- /dev/null +++ b/src/services/api/graphql/graphql/schema/show_config.graphql @@ -0,0 +1,21 @@ +""" +Use 'scalar Generic' for show config output, to avoid attempts to +JSON-serialize in case of JSON output. +""" +scalar Generic + +input ShowConfigInput { + path: [String!]! + configFormat: String +} + +type ShowConfig { + path: [String] + result: Generic +} + +type ShowConfigResult { + data: ShowConfig + success: Boolean! + errors: [String] +} diff --git a/src/services/api/graphql/recipes/session.py b/src/services/api/graphql/recipes/session.py index c6c3209c0..f8c072b39 100644 --- a/src/services/api/graphql/recipes/session.py +++ b/src/services/api/graphql/recipes/session.py @@ -1,6 +1,10 @@ +import json + from ariadne import convert_camel_case_to_snake + import vyos.defaults from vyos.config import Config +from vyos.configtree import ConfigTree from vyos.template import render class Session(object): @@ -43,6 +47,21 @@ class Session(object): session.delete(path) session.commit() + def show_config(self): + session = self._session + data = self._data + out = '' + + try: + out = session.show_config(data['path']) + if data.get('config_format', '') == 'json': + config_tree = vyos.configtree.ConfigTree(out) + out = json.loads(config_tree.to_json()) + except Exception as error: + raise error + + return out + def save(self): session = self._session data = self._data -- cgit v1.2.3