Skip to content

Commit

Permalink
New command: configbackup -> Config backup via new api endpoint (OPNs…
Browse files Browse the repository at this point in the history
…ense version >=24.1)
  • Loading branch information
jan-win1993 committed Mar 4, 2024
1 parent 1c99d2c commit 467ebcd
Show file tree
Hide file tree
Showing 12 changed files with 563 additions and 28 deletions.
78 changes: 52 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,31 @@
opn-cli - the OPNsense CLI written in python.

- [opn-cli](#opn-cli)
* [Install](#install)
* [Configure](#configure)
* [Usage](#usage)
* [docker usage](#docker-usage)
* [Features](#features)
+ [Shell Completion](#shell-completion)
+ [Output formats](#output-formats)
- [Install](#install)
- [Configure](#configure)
- [Usage](#usage)
- [docker usage](#docker-usage)
- [Features](#features)
- [Shell Completion](#shell-completion)
- [Output formats](#output-formats)
- [cols](#cols)
- [table](#table)
- [json](#json)
- [json_filter](#json-filter)
- [json\_filter](#json_filter)
- [plain](#plain)
- [yaml](#yaml)
+ [Code Generator](#code-generator)
- [Code Generator](#code-generator)
- [API code core](#api-code-core)
- [API code plugin](#api-code-plugin)
- [Core command code](#core-command-code)
- [Plugin command code](#plugin-command-code)
- [Puppet custom resource type](#puppet-custom-resource-type)
+ [Resolving of names to uuids](#resolving-of-names-to-uuids)
* [Commands](#commands)
+ [Firewall](#firewall)
- [Resolving of names to uuids](#resolving-of-names-to-uuids)
- [Commands](#commands)
- [Firewall](#firewall)
- [Aliases](#aliases)
- [Rules](#rules)
+ [Haproxy](#haproxy)
- [Haproxy](#haproxy)
- [Acl](#acl)
- [Action](#action)
- [Backend](#backend)
Expand All @@ -48,30 +48,32 @@ opn-cli - the OPNsense CLI written in python.
- [Resolver](#resolver)
- [Server](#server)
- [User](#user)
+ [Ipsec](#ipsec)
- [Ipsec](#ipsec)
- [Tunnel phase1](#tunnel-phase1)
- [Tunnel phase2](#tunnel-phase2)
+ [Routes](#routes)
- [Routes](#routes)
- [Static routes](#static-routes)
- [Gateway](#gateway)
+ [Nodeexporter](#nodeexporter)
- [Nodeexporter](#nodeexporter)
- [Config](#config-1)
+ [Syslog](#syslog)
- [Syslog](#syslog)
- [Syslog destination](#syslog-destination)
- [Syslog stats](#syslog-stats)
+ [OpenVPN](#openvpn)
+ [Plugins](#plugins)
+ [Unbound](#unbound)
- [OpenVPN](#openvpn)
- [Plugins](#plugins)
- [Unbound](#unbound)
- [host overrides](#host-overrides)
- [host alias overrides](#host-alias-overrides)
- [domain overrides](#domain-overrides)
- [Examples](#examples)
+ [Apibackup](#apibackup)
- [Examples](#examples)
* [Development](#development)
+ [Setup development environment](#setup-development-environment)
+ [Testing](#testing)
+ [Contributing](#contributing)
- [Apibackup](#apibackup)
- [Examples](#examples-1)
- [Configbackup](#configbackup)
- [Examples](#examples-2)
- [Development](#development)
- [Setup development environment](#setup-development-environment)
- [Testing](#testing)
- [Contributing](#contributing)

## Install
```
Expand Down Expand Up @@ -1038,6 +1040,8 @@ opn-cli unbound domain create --domain rockin.com --server 192.168.56.3
### Apibackup
This feature needs the opnsense plugin os-api-backup.
This plugin was deprecated with the OPNsense 24.1 release.
So if you are running OPNsense 24.1 or higher use the [Configbackup](###configbackup) command.
```
$ opn-cli plugin install os-api-backup
Expand All @@ -1059,6 +1063,28 @@ $ opn-cli apibackup backup download -p /tmp/config_backup.xml
successfully saved to: /tmp/config_backup.xml
```
### Configbackup
The plugin "os-api-backup" was discontinued in OPNsense Version 24.1, because the core API provides the same functionality.
This command provides the exact same functionality than [Apibackup](###apibackup) but uses the OPNsense Core API-Endpoint.
So if you are running a OPNsense Instance version 24.1 or higher use this command for configuration backups instead of "apibackup".
#### Examples
Download a backup of the OPNsense system configuration to the current directory:
```
$ opn-cli configbackup backup download
successfully saved to: ./config.xml
```
Or specify a path and filename:
```
$ opn-cli configbackup backup download -p /tmp/config_backup.xml
successfully saved to: /tmp/config_backup.xml
```
## Development
### Setup development environment
Expand Down
6 changes: 5 additions & 1 deletion opnsense_cli/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ def ssl_verify_cert(self):
return self._ssl_verify_cert

def _process_response(self, response):
content_type = response.headers["Content-Type"]
if response.status_code in HTTP_SUCCESS:
return json.loads(response.text)
if "application/json" in content_type:
return json.loads(response.text)
else:
return response.text
else:
raise APIException(response=response.status_code, resp_body=response.text, url=response.url)

Expand Down
14 changes: 14 additions & 0 deletions opnsense_cli/api/core/configbackup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from opnsense_cli.api.base import ApiBase


class Backup(ApiBase):
MODULE = "core"
CONTROLLER = "backup"
"""
api-backup BackupController
"""

@ApiBase._api_call
def download(self, *args):
self.method = "get"
self.command = "download"
8 changes: 8 additions & 0 deletions opnsense_cli/commands/core/configbackup/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import click


@click.group()
def configbackup(**kwargs):
"""
Manage api-backup operations (OPNsense version >= 24.1)
"""
59 changes: 59 additions & 0 deletions opnsense_cli/commands/core/configbackup/backup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import click
from opnsense_cli.formatters.cli_output import CliOutputFormatter
from opnsense_cli.callbacks.click import formatter_from_formatter_name, expand_path, available_formats
from opnsense_cli.commands.core.configbackup import configbackup
from opnsense_cli.api.client import ApiClient
from opnsense_cli.api.core.configbackup import Backup
from opnsense_cli.facades.commands.core.configbackup.backup import ApibackupBackupFacade

pass_api_client = click.make_pass_decorator(ApiClient)
pass_apibackup_backup_svc = click.make_pass_decorator(ApibackupBackupFacade)


@configbackup.group()
@pass_api_client
@click.pass_context
def backup(ctx, api_client: ApiClient, **kwargs):
"""
Manage api-backup
"""
backup_api = Backup(api_client)
ctx.obj = ApibackupBackupFacade(backup_api)


@backup.command()
@click.option(
"-p",
"--path",
help="The target path.",
type=click.Path(dir_okay=False),
default="./config.xml",
is_eager=True,
show_default=True,
callback=expand_path,
show_envvar=True,
required=True,
)
@click.option(
"--output",
"-o",
help="Specifies the Output format.",
default="plain",
type=click.Choice(available_formats()),
callback=formatter_from_formatter_name,
show_default=True,
)
@click.option(
"--cols",
"-c",
help="Which columns should be printed? Pass empty string (-c " ") to show all columns",
default="status",
show_default=True,
)
@pass_apibackup_backup_svc
def download(apibackup_backup_svc: ApibackupBackupFacade, **kwargs):
"""
Download config.xml from OPNsense.
"""
result = apibackup_backup_svc.download_backup(kwargs["path"])
CliOutputFormatter(result, kwargs["output"], kwargs["cols"].split(",")).echo()
2 changes: 1 addition & 1 deletion opnsense_cli/commands/plugin/apibackup/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
@click.group()
def apibackup(**kwargs):
"""
Manage api-backup operations
Manage api-backup operations (OPNsense version <= 23.7)
"""
4 changes: 4 additions & 0 deletions opnsense_cli/facades/commands/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ def _write_base64_string_to_zipfile(self, path, base64_data):
with open(path, "wb") as zipFile:
zipFile.write(content)

def _write_xml_string_to_file(self, path, xml_content):
with open(path, "w") as xmlFile:
xmlFile.write(xml_content)

def resolve_linked_uuids(self, resolve_map, resolve_items):
uuids = [item for item in resolve_items.split(",") if self.is_uuid(item)]
names = [item for item in resolve_items.split(",") if not self.is_uuid(item)]
Expand Down
Empty file.
12 changes: 12 additions & 0 deletions opnsense_cli/facades/commands/core/configbackup/backup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from opnsense_cli.api.core.configbackup import Backup
from opnsense_cli.facades.commands.base import CommandFacade


class ApibackupBackupFacade(CommandFacade):
def __init__(self, backup_api: Backup):
self._backup_api = backup_api

def download_backup(self, path):
config = self._backup_api.download("this")
self._write_xml_string_to_file(path, config)
return {"status": f"successfully saved to: {path}"}
Empty file.
Loading

0 comments on commit 467ebcd

Please sign in to comment.