summaryrefslogtreecommitdiffstats
path: root/application/forms/Config/Resource/SshResourceForm.php
blob: a15dc8c37fc84b7bb948876a02daaf619eb46651 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/* Icinga Web 2 | (c) 2015 Icinga Development Team | GPLv2+ */

namespace Icinga\Forms\Config\Resource;

use Icinga\Application\Icinga;
use Icinga\Data\ConfigObject;
use Icinga\Forms\Config\ResourceConfigForm;
use Icinga\Web\Form;
use Icinga\Util\File;
use Zend_Validate_Callback;

/**
 * Form class for adding/modifying ssh identity resources
 */
class SshResourceForm extends Form
{
    /**
     * Initialize this form
     */
    public function init()
    {
        $this->setName('form_config_resource_ssh');
    }

    /**
     * @see Form::createElements()
     */
    public function createElements(array $formData)
    {
        $this->addElement(
            'text',
            'name',
            array(
                'required'      => true,
                'label'         => $this->translate('Resource Name'),
                'description'   => $this->translate('The unique name of this resource')
            )
        );
        $this->addElement(
            'text',
            'user',
            array(
                'required'      => true,
                'label'         => $this->translate('User'),
                'description'   => $this->translate(
                    'User to log in as on the remote Icinga instance. Please note that key-based SSH login must be'
                    . ' possible for this user'
                )
            )
        );

        if ($this->getRequest()->getActionName() != 'editresource') {
            $callbackValidator = new Zend_Validate_Callback(function ($value) {
                if (substr(ltrim($value), 0, 7) === 'file://'
                    || openssl_pkey_get_private($value) === false
                ) {
                    return false;
                }

                return true;
            });
            $callbackValidator->setMessage(
                $this->translate('The given SSH key is invalid'),
                Zend_Validate_Callback::INVALID_VALUE
            );

            $this->addElement(
                'textarea',
                'private_key',
                array(
                    'required'      => true,
                    'label'         => $this->translate('Private Key'),
                    'description'   => $this->translate('The private key which will be used for the SSH connections'),
                    'class'         => 'resource ssh-identity',
                    'validators'    => array($callbackValidator)
                )
            );
        } else {
            $resourceName = $formData['name'];
            $this->addElement(
                'note',
                'private_key_note',
                array(
                    'escape'        => false,
                    'label'         => $this->translate('Private Key'),
                    'value'         => sprintf(
                        '<a href="%1$s" data-base-target="_next" title="%2$s" aria-label="%2$s">%3$s</a>',
                        $this->getView()->url('config/removeresource', array('resource' => $resourceName)),
                        $this->getView()->escape(sprintf($this->translate(
                            'Remove the %s resource'
                        ), $resourceName)),
                        $this->translate('To modify the private key you must recreate this resource.')
                    )
                )
            );
        }

        return $this;
    }

    /**
     * Remove the assigned key to the resource
     *
     * @param ConfigObject $config
     *
     * @return bool
     */
    public static function beforeRemove(ConfigObject $config)
    {
        $file = $config->private_key;

        if (file_exists($file)) {
            unlink($file);
            return true;
        }
        return false;
    }

    /**
     * Creates the assigned key to the resource
     *
     * @param ResourceConfigForm $form
     *
     * @return bool
     */
    public static function beforeAdd(ResourceConfigForm $form)
    {
        $configDir = Icinga::app()->getConfigDir();
        $user = $form->getElement('user')->getValue();

        $filePath = join(DIRECTORY_SEPARATOR, [$configDir, 'ssh', sha1($user)]);
        if (! file_exists($filePath)) {
            $file = File::create($filePath, 0600);
        } else {
            $form->error(
                sprintf($form->translate('The private key for the user "%s" already exists.'), $user)
            );
            return false;
        }

        $file->fwrite($form->getElement('private_key')->getValue());

        $form->getElement('private_key')->setValue($filePath);

        return true;
    }
}