This commit is contained in:
Ad Schellevis 2020-10-05 18:37:48 +02:00
parent 2671d7276d
commit 1d765d50bc
5 changed files with 224 additions and 0 deletions

View file

@ -0,0 +1,6 @@
PLUGIN_NAME= git-backup
PLUGIN_VERSION= 0.1
PLUGIN_COMMENT= Track config changes using git
PLUGIN_MAINTAINER= ad@opnsense.org
.include "../../Mk/plugins.mk"

View file

@ -0,0 +1,3 @@
This package adds a backup option using git version control.
Due to the sensitive nature of the data being send to the backup, we strongly advise to not use a public service to send backups to.

View file

@ -0,0 +1,127 @@
<?php
/**
* Copyright (C) 2020 Deciso B.V.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
namespace OPNsense\Backup;
use OPNsense\Core\Config;
use OPNsense\Backup\GitSettings;
/**
* Class Git backup
* @package OPNsense\Backup
*/
class Git extends Base implements IBackupProvider
{
/**
* @inheritdoc
*/
public function getConfigurationFields()
{
$fields = [
[
"name" => "enabled",
"type" => "checkbox",
"label" => gettext("Enable"),
"value" => null
],
[
"name" => "url",
"type" => "text",
"label" => gettext("URL"),
"help" => gettext("Target location, which defined transport protocol, such as ssh://server/project.git or https://server/project.git."),
"value" => null
],
[
"name" => "privkey",
"type" => "textarea",
"label" => gettext("SSH private key"),
"help" => gettext("When provided, ssh based authentication will be used."),
"value" => null
],
[
"name" => "user",
"type" => "text",
"label" => gettext("User Name"),
"value" => null
],
[
"name" => "password",
"type" => "password",
"label" => gettext("Password"),
"value" => null
]
];
$mdl = new GitSettings();
foreach ($fields as &$field) {
$field['value'] = (string)$mdl->getNodeByReference($field['name']);
}
return $fields;
}
/**
* @inheritdoc
*/
public function getName()
{
return gettext("Git");
}
/**
* @inheritdoc
*/
public function setConfiguration($conf)
{
$mdl = new GitSettings();
$this->setModelProperties($mdl, $conf);
$validation_messages = $this->validateModel($mdl);
if (empty($validation_messages)) {
$mdl->serializeToConfig();
Config::getInstance()->save();
}
return $validation_messages;
}
/**
* @inheritdoc
*/
public function backup()
{
return ['config.xml'];
}
/**
* @inheritdoc
*/
public function isEnabled()
{
return (string)(new GitSettings())->enabled === "1";
}
}

View file

@ -0,0 +1,39 @@
<?php
/*
* Copyright (C) 2020 Deciso B.V.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
namespace OPNsense\Backup;
use OPNsense\Base\BaseModel;
/**
* Class GitSettings
* @package Backup
*/
class GitSettings extends BaseModel
{
}

View file

@ -0,0 +1,49 @@
<model>
<mount>//system/backup/git</mount>
<version>1.0.0</version>
<description>OPNsense Git Backup Settings</description>
<items>
<enabled type="BooleanField">
<default>0</default>
<Required>Y</Required>
<Constraints>
<check001>
<reference>user.check001</reference>
</check001>
<check002>
<reference>url.check001</reference>
</check002>
</Constraints>
</enabled>
<url type="TextField">
<Required>N</Required>
<mask>/^((https)|(ssh))?:\/\/.*[^\/]$/</mask>
<ValidationMessage>A valid git location must be provided. e.g. ssh://server/project.git, https://server/project.git</ValidationMessage>
<Constraints>
<check001>
<ValidationMessage>A backup location (url) is required.</ValidationMessage>
<type>DependConstraint</type>
<addFields>
<field1>enabled</field1>
</addFields>
</check001>
</Constraints>
</url>
<privkey type="TextField">
<Required>N</Required>
</privkey>
<user type="TextField">
<Constraints>
<check001>
<ValidationMessage>A username is required.</ValidationMessage>
<type>DependConstraint</type>
<addFields>
<field1>enabled</field1>
</addFields>
</check001>
</Constraints>
</user>
<password type="TextField">
</password>
</items>
</model>