Skip to content
This repository was archived by the owner on Mar 16, 2023. It is now read-only.

Commit cc28a74

Browse files
committed
introduce the support for configuration via environment variables
1 parent 51b29bd commit cc28a74

8 files changed

+198
-8
lines changed

.env.default

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
; this is an string containing the supported RSA privated keys for encryption and decryption, the LAST RSA private key
2+
; within the string is used to encrypt new secrets while all RSA private keys are used to decrypt secrets, this allows
3+
; for smooth key rollovers; for share-only instances it is sufficient to set the RSA public key of the corresponding
4+
; read-only instance here
5+
RSA_PRIVATE_KEYS="-----BEGIN RSA PRIVATE KEY-----
6+
...
7+
...
8+
...
9+
-----END RSA PRIVATE KEY-----
10+
-----BEGIN PUBLIC KEY-----
11+
...
12+
...
13+
...
14+
-----END PUBLIC KEY-----"
15+
16+
; this is the title of the service, it is shown in header of all pages
17+
SERVICE_TITLE="Shared-Secrets"
18+
19+
; this is the full path to the secret sharing service, the encrypted secret will be appended to this string
20+
SECRET_SHARING_URL="https://localhost.local/"
21+
22+
; this is the text of the imprint link
23+
IMPRINT_TEXT=
24+
25+
; this is the URL the imprint link shall forward to
26+
IMPRINT_URL="https://localhost.local/"
27+
28+
; this is the MySQL configuration, do not forget to create the corresponding database and the following table:
29+
; > CREATE TABLE secrets ( keyid VARCHAR(64), fingerprint VARCHAR(64), time TIMESTAMP, PRIMARY KEY (keyid, fingerprint) );
30+
MYSQL_HOST="localhost"
31+
MYSQL_PORT="3306"
32+
MYSQL_USER="<SET THE MYSQL USER!!!>"
33+
MYSQL_PASS="<SET THE MYSQL PASSWORD!!!>"
34+
MYSQL_DB="<SET THE MYSQL DATABASE!!!>"
35+
36+
; this enables or disables the debug mode of the instance
37+
DEBUG_MODE="false"
38+
39+
; this is the default timezone for the execution of the script
40+
DEFAULT_TIMEZONE="Europe/Berlin"
41+
42+
; this enables or disables the read-only mode of the instance,
43+
; by using the read-only mode you need another instance to create secret sharing links,
44+
; this separation can be useful if you only want to be internally able to create links
45+
READ_ONLY="false"
46+
47+
; this enables or disables the share-only mode of the instance,
48+
; by using the share-only mode you need another instance to read secret sharing links,
49+
; this separation can be useful if you only want to be internally able to create links
50+
SHARE_ONLY="false"
51+
52+
; this enables or disables the jumbo secret support,
53+
; jumbo secrets can be up to 16384 bytes (16kb) in size,
54+
; jumbo secret sharing links that exceed 2048 bytes (2k) in size will most likely be incompatible with older Internet Explorer versions
55+
JUMBO_SECRETS="false"
56+

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# do not publish live config file
2+
.env
23
config/*
34
!config/config.php.default
45

.htaccess

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
RewriteBase /
44

55
# prevent access to certain locations
6+
RewriteRule ^\.env$ - [R=404,L]
7+
RewriteRule ^\.env\.default$ - [R=404,L]
68
RewriteRule ^\.git(\/.*)?$ - [R=404,L]
79
RewriteRule ^\.gitattributes$ - [R=404,L]
810
RewriteRule ^\.gitignore$ - [R=404,L]

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 0.29b0 (2021-12-14)
2+
3+
* introduce support for configuration via environment variables
4+
* introduce support for configuration via .env file
5+
* updated README to document environment variables
6+
17
# 0.28b0 (2021-06-07)
28

39
* updated jQuery to version 3.6.0

README.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ server {
118118
add_header X-XSS-Protection "1; mode=block";
119119
120120
# prevent access to certain locations
121+
location ~ ^\/\.env$ { return 404; }
122+
location ~ ^\/\.env\.default$ { return 404; }
121123
location ~ ^\/\.git(\/.*)?$ { return 404; }
122124
location ~ ^\/\.gitattributes$ { return 404; }
123125
location ~ ^\/\.gitignore$ { return 404; }
@@ -187,7 +189,17 @@ openssl genrsa -out ./rsa.key 2048
187189

188190
### Service Setup
189191

190-
Copy the `config/config.php.default` file to `config/config.php` and set the necessary configuration items.
192+
#### Configuration via config.php
193+
194+
Copy the `config/config.php.default` file to `config/config.php` and set the necessary configuration values. When a `config/config.php` file exists then it is used as the **only** configuration source for the entire Shared-Secrets instance.
195+
196+
#### Configuration via .env
197+
198+
Copy the `.env.default` file to `.env` and set the necessary configuration values. When a `config/config.php` file exists then the configuration values in the `.env` file will **not** be used. Configuration values in the `.env` file can be overwritten by setting environment variables.
199+
200+
#### Configuration via environment variables
201+
202+
Configuration values can also be set by defining corresponding environment variables. When a `config/config.php` file exists then the configuration values set via environment variables will **not** be used. Configuration values in the `.env` file can be overwritten by setting environment variables.
191203

192204
### Read-Only and Share-Only Instances
193205

config/config.php.default

+5-5
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727

2828
# this is the MySQL configuration, do not forget to create the corresponding database and the following table:
2929
# > CREATE TABLE secrets ( keyid VARCHAR(64), fingerprint VARCHAR(64), time TIMESTAMP, PRIMARY KEY (keyid, fingerprint) );
30-
define("MYSQL_HOST", "localhost");
31-
define("MYSQL_PORT", 3306);
32-
define("MYSQL_USER", "<SET THE MYSQL USER!!!>");
33-
define("MYSQL_PASS", "<SET THE MYSQL PASSWORD!!!>");
34-
define("MYSQL_DB", "<SET THE MYSQL DATABASE!!!>");
30+
define("MYSQL_HOST", "localhost");
31+
define("MYSQL_PORT", 3306);
32+
define("MYSQL_USER", "<SET THE MYSQL USER!!!>");
33+
define("MYSQL_PASS", "<SET THE MYSQL PASSWORD!!!>");
34+
define("MYSQL_DB", "<SET THE MYSQL DATABASE!!!>");
3535

3636
# this enables or disables the debug mode of the instance
3737
define("DEBUG_MODE", false);

index.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
# Shared-Secrets v0.28b0
3+
# Shared-Secrets v0.29b0
44
#
55
# Copyright (c) 2016-2021, SysEleven GmbH
66
# All rights reserved.
@@ -25,7 +25,13 @@
2525
define("ROOT_DIR", __DIR__);
2626

2727
# include required configuration
28-
require_once(ROOT_DIR."/config/config.php");
28+
if (is_file(ROOT_DIR."/config/config.php")) {
29+
# if there is a config file then we use that
30+
require_once(ROOT_DIR."/config/config.php");
31+
} else {
32+
# otherwise we define the config through environment variables
33+
require_once(ROOT_DIR."/lib/shared-secrets.env.php");
34+
}
2935

3036
# include required defines
3137
require_once(ROOT_DIR."/lib/shared-secrets.def.php");

lib/shared-secrets.env.php

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
# prevent direct access
4+
if (!defined("SYS11_SECRETS")) { die(""); }
5+
6+
function env($name, $default = null) {
7+
$result = getenv($name);
8+
9+
# set the default if the environment variable isn't set
10+
if (false === $result) {
11+
$result = $default;
12+
}
13+
14+
return $result;
15+
}
16+
17+
function checkbool($string) {
18+
return filter_var($string, FILTER_VALIDATE_BOOLEAN);
19+
}
20+
21+
function load_dot_env($filename) {
22+
# read the .env file
23+
$dotenv = parse_ini_file($filename);
24+
if (false !== $dotenv) {
25+
foreach ($dotenv as $key => $value) {
26+
# only set environment variables that are not already set
27+
if (false === getenv($key)) {
28+
putenv($key."=".$value);
29+
}
30+
}
31+
}
32+
}
33+
34+
function split_rsa_keys($string) {
35+
$result = [];
36+
37+
if (false !== preg_match_all("@(?<rsakeys>-----BEGIN (?:RSA )?(?:PRIVATE|PUBLIC) KEY-----(?:.+?)-----END (?:RSA )?(?:PRIVATE|PUBLIC) KEY-----)@is",
38+
$string, $matches)) {
39+
if (array_key_exists("rsakeys", $matches)) {
40+
# cleanup strings
41+
foreach ($matches["rsakeys"] as $match_key => $match_value) {
42+
$lines = explode("\n", $match_value);
43+
foreach ($lines as $line_key => $line_value) {
44+
$lines[$line_key] = trim($line_value);
45+
}
46+
$matches["rsakeys"][$match_key] = implode("\n", $lines);
47+
}
48+
49+
$result = $matches["rsakeys"];
50+
}
51+
}
52+
53+
return $result;
54+
}
55+
56+
# load a .env file if it exists
57+
if (is_file(ROOT_DIR."/.env")) {
58+
load_dot_env(ROOT_DIR."/.env");
59+
}
60+
61+
# this is an array containing the supported RSA privated keys for encryption and decryption, the LAST RSA private key
62+
# within the array is used to encrypt new secrets while all RSA private keys are used to decrypt secrets, this allows
63+
# for smooth key rollovers; for share-only instances it is sufficient to set the RSA public key of the corresponding
64+
# read-only instance here
65+
define("RSA_PRIVATE_KEYS", split_rsa_keys(env("RSA_PRIVATE_KEYS", null)));
66+
67+
# this is the title of the service, it is shown in header of all pages
68+
define("SERVICE_TITLE", env("SERVICE_TITLE", "Shared-Secrets"));
69+
70+
# this is the full path to the secret sharing service, the encrypted secret will be appended to this string
71+
define("SECRET_SHARING_URL", env("SECRET_SHARING_URL", "https://localhost.local/"));
72+
73+
# this is the text of the imprint link
74+
define("IMPRINT_TEXT", env("IMPRINT_TEXT", null));
75+
76+
# this is the URL the imprint link shall forward to
77+
define("IMPRINT_URL", env("IMPRINT_URL", "https://localhost.local/"));
78+
79+
# this is the MySQL configuration, do not forget to create the corresponding database and the following table:
80+
# > CREATE TABLE secrets ( keyid VARCHAR(64), fingerprint VARCHAR(64), time TIMESTAMP, PRIMARY KEY (keyid, fingerprint) );
81+
define("MYSQL_HOST", env("MYSQL_HOST", "localhost"));
82+
define("MYSQL_PORT", intval(env("MYSQL_PORT", 3306)));
83+
define("MYSQL_USER", env("MYSQL_USER", null));
84+
define("MYSQL_PASS", env("MYSQL_PASS", null));
85+
define("MYSQL_DB", env("MYSQL_DB", null));
86+
87+
# this enables or disables the debug mode of the instance
88+
define("DEBUG_MODE", checkbool(env("DEBUG_MODE", false)));
89+
90+
# this is the default timezone for the execution of the script
91+
define("DEFAULT_TIMEZONE", env("DEFAULT_TIMEZONE", "Europe/Berlin"));
92+
93+
# this enables or disables the read-only mode of the instance,
94+
# by using the read-only mode you need another instance to create secret sharing links,
95+
# this separation can be useful if you only want to be internally able to create links
96+
define("READ_ONLY", checkbool(env("READ_ONLY", false)));
97+
98+
# this enables or disables the share-only mode of the instance,
99+
# by using the share-only mode you need another instance to read secret sharing links,
100+
# this separation can be useful if you only want to be internally able to create links
101+
define("SHARE_ONLY", checkbool(env("SHARE_ONLY", false)));
102+
103+
# this enables or disables the jumbo secret support,
104+
# jumbo secrets can be up to 16384 bytes (16kb) in size,
105+
# jumbo secret sharing links that exceed 2048 bytes (2k) in size will most likely be incompatible with older Internet Explorer versions
106+
define("JUMBO_SECRETS", checkbool(env("JUMBO_SECRETS", false)));
107+

0 commit comments

Comments
 (0)