Skip to content

Commit 4959ac2

Browse files
committed
Add optional "group" parameter to FromKeePass
1 parent 37fb491 commit 4959ac2

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,13 @@ Via the `KeePass` class.
6767
use security\credentials\{Credentials, FromKeePass};
6868
use util\Secret;
6969

70-
$credentials= new Credentials(new FromKeePass('database.kdbx', new Secret('key')));
70+
$secret= new Secret('key');
71+
72+
$credentials= new Credentials(new FromKeePass('database.kdbx', $secret));
7173
$secret= $credentials->named('ldap_password'); // Reads top-level entry ldap_password
72-
$secret= $credentials->named('vendor/name/mysql'); // Reads mysql entry in vendor/name subfolder
74+
75+
$credentials= new Credentials(new FromKeePass('database.kdbx', $secret, 'vendor/name'));
76+
$secret= $credentials->named('mysql'); // Reads mysql entry in vendor/name subfolder
7377
```
7478

7579
### Docker secrets

src/main/php/security/credentials/FromKeePass.class.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ class FromKeePass implements Secrets {
1414
*
1515
* @param io.File|string $file
1616
* @param util.Secret $key
17+
* @param string $group The secret group, e.g. "/vendor/name"
1718
*/
18-
public function __construct($file, Secret $key) {
19+
public function __construct($file, Secret $key, $group= '/') {
1920
$this->file= $file instanceof File ? $file : new File($file);
2021
$this->key= $key;
22+
$this->group= trim($group, '/');
2123
}
2224

2325
/** @return self */
@@ -43,7 +45,7 @@ public function named($name) {
4345
$match= substr($name, $p + 1);
4446
}
4547

46-
foreach ($this->db->group($group)->passwords() as $path => $value) {
48+
foreach ($this->db->group($this->group.$group)->passwords() as $path => $value) {
4749
if (basename($path) === $match) return new Secret((string)$value);
4850
}
4951
return null;
@@ -64,7 +66,7 @@ public function all($pattern) {
6466
$match= substr($pattern, $p + 1, strrpos($pattern, '*') - $p - 1);
6567
}
6668

67-
foreach ($this->db->group($group)->passwords() as $path => $value) {
69+
foreach ($this->db->group($this->group.$group)->passwords() as $path => $value) {
6870
if (0 === strncmp(basename($path), $match, strlen($match))) yield substr($path, 1) => new Secret((string)$value);
6971
}
7072
}
Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
11
<?php namespace security\credentials\unittest;
22

3+
use lang\ClassLoader;
34
use security\credentials\FromKeePass;
45
use util\Secret;
5-
use lang\ClassLoader;
66

77
class FromKeePassTest extends AbstractSecretsTest {
88

99
/** @return security.vault.Secrets */
10-
protected function newFixture() {
10+
protected function newFixture($group= '/') {
1111
return new FromKeePass(
1212
ClassLoader::getDefault()->getResourceAsStream('keepass/unittest.kdbx'),
13-
new Secret('test')
13+
new Secret('test'),
14+
$group
1415
);
1516
}
17+
18+
#[@test, @values(['xp/app', '/xp/app', '/xp/app/'])]
19+
public function using_group($group) {
20+
$fixture= $this->newFixture($group);
21+
$fixture->open();
22+
try {
23+
$this->assertEquals('test', $fixture->named('mysql')->reveal());
24+
} finally {
25+
$fixture->close();
26+
}
27+
}
1628
}

0 commit comments

Comments
 (0)