-
Notifications
You must be signed in to change notification settings - Fork 1
/
MultiEncryptor.php
165 lines (142 loc) · 4.23 KB
/
MultiEncryptor.php
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
namespace Detail\VarCrypt;
use Keboola\Encryption;
/**
* MultiEncryptor.
*
* Use this class to write key/value pairs as encrypted and base64 encoded environment variables.
*
* @package Detail\VarCrypt
*/
class MultiEncryptor extends BaseEncryptor
{
/**
* Apply group's values as individual, plain environment variables.
*
* The group name prefixes the variable name (joined by the separator).
*
* @param array|string $groups
* @param string $separator
*/
public function applyVariables($groups, $separator = '_')
{
if (!is_array($groups)) {
$groups = array($groups);
}
foreach ($groups as $group) {
$values = $this->getVariables($group);
$this->setEnvironmentVariables($values, $group, $separator);
}
}
/**
* Set values as an encrypted and base64 encoded environment variable.
*
* @param string $group
* @param array $values
*/
public function setVariables($group, array $values)
{
$this->setEnvironmentVariable($group, $this->encode($values));
}
/**
* Decode and decrypt values from a previously set environment variable.
*
* @param string $group
* @return array
*/
public function getVariables($group)
{
$encodedValues = $this->getEnvironmentVariable($group);
if ($encodedValues === null) {
return null;
}
return $this->decode($encodedValues);
}
/**
* Decode and decrypt a single value from a previously set environment variable.
*
* @param string $group
* @param string $name
* @return mixed|null
*/
public function getVariable($group, $name)
{
$variables = $this->getVariables($group);
return array_key_exists($name, $variables) ? $variables[$name] : null;
}
/**
* Encrypt and encode group values.
*
* @param array $values
* @return string
*/
public function encode($values)
{
$json = json_encode($values);
$this->handleJsonError('Failed to encode group values to JSON');
return parent::encode($json);
}
/**
* Decode and decrypt group values.
*
* @param string $value
* @return array
*/
public function decode($value)
{
$json = parent::decode($value);
$values = json_decode($json, true);
$this->handleJsonError('Failed to decode group values from JSON');
return $values;
}
/**
* Recursively set environment variables (for each leaf).
*
* @param array $values
* @param string $prefix
* @param string $separator
*/
protected function setEnvironmentVariables(array $values, $prefix, $separator = '_')
{
foreach ($values as $variable => $value) {
$name = $prefix . $separator . $variable;
if (is_array($value)) {
$this->setEnvironmentVariables($value, $name, $separator);
} else {
$this->setEnvironmentVariable($name, $value);
}
}
}
/**
* @param string $errorMessage
*/
private function handleJsonError($errorMessage)
{
$error = json_last_error();
if ($error !== JSON_ERROR_NONE) {
switch (json_last_error()) {
case JSON_ERROR_DEPTH:
$message = 'Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
$message = 'Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
$message = 'Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
$message = 'Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
$message = 'Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
$message = 'Unknown error';
break;
}
throw new Exception\RuntimeException(
$errorMessage . '; ' . $message
);
}
}
}