Skip to content

Commit 769426c

Browse files
committed
Add CredentialsProvider for vault, database and email access using ENV or Kubernetes secrets
1 parent 7537888 commit 769426c

4 files changed

Lines changed: 99 additions & 7 deletions

File tree

src/WebApp/Application.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,13 @@ public function init() {
5353

5454
protected function initVault() {
5555
if ($this->config->has('vault')) {
56-
$this->vault = \TgVault\VaultFactory::create($this->config->get('vault'));
56+
$credentials = $this->config->getCredentialsProvider('vault', NULL);
57+
$vaultConfig = $this->config->get('vault');
58+
if ($credentials != NULL) {
59+
$vaultConfig->config->roleId = $credentials->getUsername();
60+
$vaultConfig->config->secretId = $credentials->getPassword();
61+
}
62+
$this->vault = \TgVault\VaultFactory::create($vaultConfig);
5763
// TODO $this->vault->setLogger(Log::instance());
5864
}
5965
}

src/WebApp/Configuration.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,32 @@ public function has($feature) {
3434

3535
/** Return credentials provider when credentials are defined */
3636
public function getCredentialsProvider($feature, $vault) {
37-
if (($vault != NULL) && $this->has($feature)) {
37+
if ($this->has($feature)) {
3838
if (!isset($this->credentialProviders->$feature)) {
3939
if (!isset($this->credentialProviders)) $this->credentialProviders = new \stdClass;
4040
$this->credentialProviders->$feature = NULL;
41-
if (isset($this->data->$feature->security) && ($this->data->$feature->security->type == 'vault')) {
42-
$path = $this->data->$feature->security->path;
43-
$userKey = isset($this->data->$feature->security->userKey) ? $this->data->$feature->security->userKey : 'username';
44-
$passKey = isset($this->data->$feature->security->passKey) ? $this->data->$feature->security->passKey : 'password';
45-
$this->credentialProviders->$feature = new \TgVault\CredentialsProvider($vault, $path, $userKey, $passKey);
41+
42+
// A security object is defined
43+
if (isset($this->data->$feature->security)) {
44+
if ($this->data->$feature->security->type == 'vault') {
45+
// CredentialsProvider is of type vault
46+
if ($vault != NULL) {
47+
$path = $this->data->$feature->security->path;
48+
$userKey = isset($this->data->$feature->security->userKey) ? $this->data->$feature->security->userKey : 'username';
49+
$passKey = isset($this->data->$feature->security->passKey) ? $this->data->$feature->security->passKey : 'password';
50+
$this->credentialProviders->$feature = new \TgVault\CredentialsProvider($vault, $path, $userKey, $passKey);
51+
}
52+
} else if ($this->data->$feature->security->type == 'env') {
53+
// CredentialsProvider is fed from environment variables
54+
$userKey = isset($this->data->$feature->security->userKey) ? $this->data->$feature->security->userKey : strtoupper($feature).'_USERNAME';
55+
$passKey = isset($this->data->$feature->security->passKey) ? $this->data->$feature->security->passKey : strtoupper($feature).'_PASSWORD';
56+
$this->credentialProviders->$feature = new \WebApp\Security\EnvCredentialsProvider($userKey, $passKey);
57+
} else if ($this->data->$feature->security->type == 'k8secret') {
58+
// CredentialsProvider is fed from Kubernetes Secret mounted
59+
$userKey = isset($this->data->$feature->security->userKey) ? $this->data->$feature->security->userKey : 'username';
60+
$passKey = isset($this->data->$feature->security->passKey) ? $this->data->$feature->security->passKey : 'password';
61+
$this->credentialProviders->$feature = new \WebApp\Security\K8SecretCredentialsProvider($this->data->$feature->security->path, $userKey, $passKey);
62+
}
4663
}
4764
}
4865
return $this->credentialProviders->$feature;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace WebApp\Security;
4+
5+
/**
6+
* A Helper class that gets its credentials from environment variables.
7+
*/
8+
class EnvCredentialsProvider extends \TgUtils\Auth\DefaultCredentialsProvider {
9+
10+
/**
11+
* Construct the provider.
12+
* @param string $usernameKey - the name of the environment variable holding the username (default is 'USERNAME')
13+
* @param string $passwordKey - the name of the environment variable holding the password (default is 'PASSWORD')
14+
*/
15+
public function __construct($usernameKey = NULL, $passwordKey = NULL) {
16+
if (($usernameKey == NULL) || (trim($usernameKey) == '')) $usernameKey = 'USERNAME';
17+
if (($passwordKey == NULL) || (trim($passwordKey) == '')) $passwordKey = 'PASSWORD';
18+
parent::__construct($_ENV[$usernameKey], $_ENV[$passwordKey]);
19+
}
20+
21+
}
22+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace WebApp\Security;
4+
5+
/**
6+
* A Helper class that returns the credentials from a Secret mounted in a Kubernetes pod.
7+
*/
8+
class K8SecretCredentialsProvider extends \TgUtils\Auth\DefaultCredentialsProvider {
9+
10+
/** The path where the secret is stored */
11+
private $path;
12+
/** The key in the secret holding the username (default is 'username') */
13+
private $usernameKey;
14+
/** The key in the secret holding the password (default is 'password') */
15+
private $passwordKey;
16+
17+
/**
18+
* Construct the provider.
19+
* @param string $path - the path to the mounted Kubernets secret
20+
* @param string $usernameKey - the key in the secret holding the username (default is 'username')
21+
* @param string $passwordKey - the key in the secret holding the password (default is 'password')
22+
*/
23+
public function __construct($path, $usernameKey = NULL, $passwordKey = NULL) {
24+
if (($usernameKey == NULL) || (trim($usernameKey) == '')) $usernameKey = 'username';
25+
if (($passwordKey == NULL) || (trim($passwordKey) == '')) $passwordKey = 'password';
26+
$this->path = $path;
27+
$this->usernameKey = $usernameKey;
28+
$this->passwordKey = $passwordKey;
29+
parent::__construct('', '');
30+
}
31+
32+
public function getUsername() {
33+
return $this->get($this->usernameKey);
34+
}
35+
36+
public function getPassword() {
37+
return $this->get($this->passwordKey);
38+
}
39+
40+
public function get($key) {
41+
if (file_exists($this->path.'/'.$key)) {
42+
return trim(file_get_contents($this->path.'/'.$key));
43+
}
44+
return NULL;
45+
}
46+
}
47+

0 commit comments

Comments
 (0)