Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
65.45% |
36 / 55 |
|
58.33% |
7 / 12 |
CRAP | |
0.00% |
0 / 1 |
| ConfigurationMarkers | |
65.45% |
36 / 55 |
|
58.33% |
7 / 12 |
47.75 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| replaceMarkers | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| removeIndexesWithoutMarkers | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| getReport | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| walkReplaceMarkers | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
4.01 | |||
| unsetRecursive | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
42 | |||
| findMatches | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| printMatchNotification | |
91.67% |
11 / 12 |
|
0.00% |
0 / 1 |
3.01 | |||
| reportError | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| reportSuccess | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| notice | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| error | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2023 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | declare(strict_types=1); |
| 23 | |
| 24 | namespace oat\tao\model\configurationMarkers; |
| 25 | |
| 26 | use InvalidArgumentException; |
| 27 | use oat\oatbox\reporting\Report; |
| 28 | use oat\tao\model\configurationMarkers\Secrets\EnvironmentValueStorage; |
| 29 | use Psr\Log\LoggerInterface; |
| 30 | |
| 31 | class ConfigurationMarkers |
| 32 | { |
| 33 | /** @var string */ |
| 34 | private const MARKER_PATTERN = '/\$ENV{([a-zA-Z0-9\-\_]+)}/'; |
| 35 | private LoggerInterface $logger; |
| 36 | private SerializableSecretDtoFactory $serializableFactory; |
| 37 | private Report $report; |
| 38 | private array $envVars; |
| 39 | private array $matchedVars = []; |
| 40 | |
| 41 | public function __construct( |
| 42 | SerializableSecretDtoFactory $serializableFactory, |
| 43 | LoggerInterface $logger, |
| 44 | ?array $envVars = null |
| 45 | ) { |
| 46 | $this->serializableFactory = $serializableFactory; |
| 47 | $this->logger = $logger; |
| 48 | $this->envVars = $envVars ?? $_ENV; |
| 49 | } |
| 50 | |
| 51 | public function replaceMarkers(array $configurationWithMarkers): array |
| 52 | { |
| 53 | $this->report = Report::createInfo('Starting ConfigurationMarkers.'); |
| 54 | if (empty($configurationWithMarkers)) { |
| 55 | throw new InvalidArgumentException('Empty configuration.'); |
| 56 | } |
| 57 | |
| 58 | array_walk_recursive($configurationWithMarkers, 'self::walkReplaceMarkers'); |
| 59 | |
| 60 | return $configurationWithMarkers; |
| 61 | } |
| 62 | |
| 63 | public function removeIndexesWithoutMarkers(array $configurationWithMarkers): array |
| 64 | { |
| 65 | if (empty($configurationWithMarkers)) { |
| 66 | throw new InvalidArgumentException('Empty configuration.'); |
| 67 | } |
| 68 | |
| 69 | $this->unsetRecursive($configurationWithMarkers); |
| 70 | |
| 71 | return $configurationWithMarkers; |
| 72 | } |
| 73 | |
| 74 | public function getReport(): Report |
| 75 | { |
| 76 | return $this->report; |
| 77 | } |
| 78 | |
| 79 | private function walkReplaceMarkers(&$item): void |
| 80 | { |
| 81 | if (is_string($item) === false) { |
| 82 | return; |
| 83 | } |
| 84 | $matches = $this->findMatches($item); |
| 85 | if (empty($matches)) { |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | $isSecretDefined = $this->envVars[$matches[1]] ?? false; |
| 90 | $this->printMatchNotification((bool) $isSecretDefined, $matches[1]); |
| 91 | if (!$isSecretDefined) { |
| 92 | //remove not found markers from config array as reference |
| 93 | $item = ''; |
| 94 | return; |
| 95 | } |
| 96 | $item = $this->serializableFactory->create($matches[1]); |
| 97 | } |
| 98 | |
| 99 | private function unsetRecursive(&$item): bool |
| 100 | { |
| 101 | foreach ($item as $key => &$value) { |
| 102 | if (!is_array($value)) { |
| 103 | if (is_string($item) === false) { |
| 104 | continue; |
| 105 | } |
| 106 | $matches = $this->findMatches($value); |
| 107 | if (empty($matches)) { |
| 108 | unset($item[$key]); |
| 109 | } |
| 110 | continue; |
| 111 | } |
| 112 | $size = $this->unsetRecursive($value); |
| 113 | if (!$size) { |
| 114 | unset($item[$key]); |
| 115 | } |
| 116 | } |
| 117 | return count($item) > 0; |
| 118 | } |
| 119 | |
| 120 | private function findMatches(string $item): array |
| 121 | { |
| 122 | $matches = []; |
| 123 | preg_match(self::MARKER_PATTERN, $item, $matches); |
| 124 | |
| 125 | return $matches; |
| 126 | } |
| 127 | |
| 128 | private function printMatchNotification(bool $isSecretDefined, string $secretName): void |
| 129 | { |
| 130 | if (in_array($secretName, $this->matchedVars)) { |
| 131 | return; |
| 132 | } |
| 133 | array_push($this->matchedVars, $secretName); |
| 134 | $message = sprintf('Found seed file marker: %s', $secretName); |
| 135 | if ($isSecretDefined) { |
| 136 | $message .= ' and its Secrets Storage value.'; |
| 137 | $this->notice($message); |
| 138 | $this->reportSuccess($message); |
| 139 | return; |
| 140 | } |
| 141 | $message .= ' but no corresponding value in Secrets Storage!'; |
| 142 | $this->error($message); |
| 143 | $this->reportError($message); |
| 144 | } |
| 145 | |
| 146 | private function reportError(string $message): void |
| 147 | { |
| 148 | $this->report->add(Report::createError($message)); |
| 149 | } |
| 150 | private function reportSuccess(string $message): void |
| 151 | { |
| 152 | $this->report->add(Report::createSuccess($message)); |
| 153 | } |
| 154 | |
| 155 | private function notice(string $message): void |
| 156 | { |
| 157 | $this->logger->notice($message); |
| 158 | } |
| 159 | |
| 160 | private function error(string $message): void |
| 161 | { |
| 162 | $this->logger->error($message); |
| 163 | } |
| 164 | } |