|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SimpleSAML\Test\Module\oidc\unit; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Attributes\CoversNothing; |
| 8 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | +use SimpleSAML\Module\oidc\ModuleConfig; |
| 11 | +use SimpleSAML\OpenID\Algorithms\SignatureAlgorithmEnum; |
| 12 | + |
| 13 | +/** |
| 14 | + * Keeps the configuration file that ships with the module in step with the installation guide. |
| 15 | + * |
| 16 | + * Nothing else in the suite reads `config/module_oidc.php.dist`: both `tests/config/module_oidc.php` |
| 17 | + * and `docker/ssp/module_oidc.php` declare their own key pairs. The distributed file is therefore |
| 18 | + * exercised for the first time by whoever installs the module, which is how it came to activate an |
| 19 | + * ES256 entry pointing at EC key files that the guide creates only in an optional section. |
| 20 | + */ |
| 21 | +#[CoversNothing] |
| 22 | +class DistributedConfigTest extends TestCase |
| 23 | +{ |
| 24 | + /** @var array<string,mixed> */ |
| 25 | + protected array $distributedConfig; |
| 26 | + |
| 27 | + protected string $installationGuide; |
| 28 | + |
| 29 | + protected function setUp(): void |
| 30 | + { |
| 31 | + $repositoryRoot = dirname(__DIR__, 3); |
| 32 | + |
| 33 | + // The distributed configuration follows the SimpleSAMLphp convention of assigning $config |
| 34 | + // instead of returning it, so include it for that side effect and pick the variable up. |
| 35 | + require $repositoryRoot . '/config/module_oidc.php.dist'; |
| 36 | + /** @var array<string,mixed> $config */ |
| 37 | + $this->distributedConfig = $config; |
| 38 | + |
| 39 | + $this->installationGuide = (string)file_get_contents( |
| 40 | + $repositoryRoot . '/docs/2-oidc-installation.md', |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * The protocol key pairs are resolved before any JWS can be signed, and all of them are, so an |
| 46 | + * entry naming a key file that a fresh installation does not have stops the module from signing |
| 47 | + * altogether rather than just dropping its own algorithm. Only the RSA pair that the guide |
| 48 | + * creates in its baseline instructions may therefore ship active; anything else belongs in the |
| 49 | + * file commented out, next to the key generation it needs. |
| 50 | + */ |
| 51 | + public function testDistributedProtocolKeyPairsMatchTheBaselineInstallation(): void |
| 52 | + { |
| 53 | + /** @var array<array<string,mixed>> $keyPairs */ |
| 54 | + $keyPairs = $this->distributedConfig[ModuleConfig::OPTION_PROTOCOL_SIGNATURE_KEY_PAIRS]; |
| 55 | + |
| 56 | + $this->assertCount( |
| 57 | + 1, |
| 58 | + $keyPairs, |
| 59 | + 'The distributed configuration must activate exactly the one protocol key pair that the ' . |
| 60 | + 'baseline installation instructions create. Ship additional algorithms commented out.', |
| 61 | + ); |
| 62 | + |
| 63 | + $keyPair = $keyPairs[0]; |
| 64 | + |
| 65 | + $this->assertSame(SignatureAlgorithmEnum::RS256, $keyPair[ModuleConfig::KEY_ALGORITHM]); |
| 66 | + $this->assertSame( |
| 67 | + 'oidc_module_connect_rsa_01.key', |
| 68 | + $keyPair[ModuleConfig::KEY_PRIVATE_KEY_FILENAME], |
| 69 | + ); |
| 70 | + $this->assertSame( |
| 71 | + 'oidc_module_connect_rsa_01.pub', |
| 72 | + $keyPair[ModuleConfig::KEY_PUBLIC_KEY_FILENAME], |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Catches the milder version of the same drift: an active entry naming a key file that the guide |
| 78 | + * never tells anyone to create, in either direction, such as a renamed or mistyped filename. |
| 79 | + */ |
| 80 | + #[DataProvider('keyPairOptionProvider')] |
| 81 | + public function testActiveKeyFilenamesAreCreatedByTheInstallationGuide(string $option): void |
| 82 | + { |
| 83 | + /** @var array<array<string,mixed>> $keyPairs */ |
| 84 | + $keyPairs = $this->distributedConfig[$option]; |
| 85 | + |
| 86 | + foreach ($keyPairs as $keyPair) { |
| 87 | + foreach ( |
| 88 | + [ModuleConfig::KEY_PRIVATE_KEY_FILENAME, ModuleConfig::KEY_PUBLIC_KEY_FILENAME] as $filenameKey |
| 89 | + ) { |
| 90 | + /** @var string $filename */ |
| 91 | + $filename = $keyPair[$filenameKey]; |
| 92 | + |
| 93 | + $this->assertStringContainsString( |
| 94 | + $filename, |
| 95 | + $this->installationGuide, |
| 96 | + sprintf( |
| 97 | + 'Option %s activates key file %s, which docs/2-oidc-installation.md does not ' . |
| 98 | + 'tell anyone to create.', |
| 99 | + $option, |
| 100 | + $filename, |
| 101 | + ), |
| 102 | + ); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @return array<string,array{string}> |
| 109 | + */ |
| 110 | + public static function keyPairOptionProvider(): array |
| 111 | + { |
| 112 | + return [ |
| 113 | + 'protocol' => [ModuleConfig::OPTION_PROTOCOL_SIGNATURE_KEY_PAIRS], |
| 114 | + 'federation' => [ModuleConfig::OPTION_FEDERATION_SIGNATURE_KEY_PAIRS], |
| 115 | + 'vci' => [ModuleConfig::OPTION_VCI_SIGNATURE_KEY_PAIRS], |
| 116 | + ]; |
| 117 | + } |
| 118 | +} |
0 commit comments