Skip to content
This repository was archived by the owner on May 21, 2025. It is now read-only.

Commit 0c4f8e9

Browse files
committed
separate key files for module
1 parent 4d3a870 commit 0c4f8e9

File tree

4 files changed

+62
-20
lines changed

4 files changed

+62
-20
lines changed

nix/integration-tests.nix

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@
8585
"pm.max_spare_servers" = "15";
8686
"catch_workers_output" = "yes";
8787
"listen.owner" = "nginx";
88-
"listen.group" = "nginx";
8988
};
9089
phpEnv = {
9190
BASE_HOST = "demos.tf";
@@ -97,12 +96,20 @@
9796
DB_DATABASE = "demostf";
9897
DB_USERNAME = "demostf";
9998
APP_ROOT = "http://localhost";
100-
EDIT_SECRET = "edit";
99+
EDIT_KEY = "/$CREDENTIALS_DIRECTORY/edit_key";
101100
PARSER_PATH = lib.getExe pkgs.demostf-parser;
102101
};
103102
user = "demostf";
104103
group = "demostf";
105104
};
105+
systemd.services.phpfpm-demostf-api.serviceConfig = {
106+
User = "demostf";
107+
AmbientCapabilities = "CAP_CHOWN";
108+
NoNewPrivileges = true;
109+
LoadCredential = [
110+
"edit_key:${pkgs.writeText "edit-key.conf" "edit"}"
111+
];
112+
};
106113
};
107114
};
108115

nix/module.nix

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
lib,
55
...
66
}: let
7+
inherit (lib) optionals optionalAttrs;
78
cfg = config.services.demostf.api;
89
fpmCfg = config.services.phpfpm.pools.demostf-api;
910
exporterCfg = config.services.prometheus.exporters.php-fpm;
@@ -45,9 +46,20 @@ in {
4546
type = types.str;
4647
description = "path the demos are stored";
4748
};
48-
keyFile = mkOption {
49-
type = types.str;
50-
description = "path containing key environment variables";
49+
editKeyFile = mkOption {
50+
type = types.nullOr types.str;
51+
default = null;
52+
description = "path containing edit key environment variables";
53+
};
54+
uploadKeyFile = mkOption {
55+
type = types.nullOr types.str;
56+
default = null;
57+
description = "path containing upload key environment variables";
58+
};
59+
accessKeyFile = mkOption {
60+
type = types.nullOr types.str;
61+
default = null;
62+
description = "path containing access key environment variables";
5163
};
5264
};
5365
};
@@ -99,24 +111,46 @@ in {
99111
"listen.owner" = "nginx";
100112
"listen.group" = "nginx";
101113
};
102-
phpEnv = {
103-
BASE_HOST = cfg.baseDomain;
104-
DEMO_ROOT = cfg.demoRoot;
105-
DEMO_HOST = cfg.hostDomain;
106-
DB_TYPE = "pgsql";
107-
DB_HOST = "/run/postgresql";
108-
DB_PORT = "5432";
109-
DB_DATABASE = "demostf";
110-
DB_USERNAME = "demostf";
111-
APP_ROOT = "https://${cfg.apiDomain}";
112-
PARSER_PATH = "${pkgs.demostf-parser}/bin/parse_demo";
113-
};
114+
phpEnv =
115+
{
116+
BASE_HOST = cfg.baseDomain;
117+
DEMO_ROOT = cfg.demoRoot;
118+
DEMO_HOST = cfg.hostDomain;
119+
DB_TYPE = "pgsql";
120+
DB_HOST = "/run/postgresql";
121+
DB_PORT = "5432";
122+
DB_DATABASE = "demostf";
123+
DB_USERNAME = "demostf";
124+
APP_ROOT = "https://${cfg.apiDomain}";
125+
PARSER_PATH = "${pkgs.demostf-parser}/bin/parse_demo";
126+
}
127+
// (optionalAttrs (cfg.editKeyFile != null) {
128+
EDIT_KEY = "/$CREDENTIALS_DIRECTORY/edit_key";
129+
})
130+
// (optionalAttrs (cfg.uploadKeyFile != null) {
131+
UPLOAD_KEY = "/$CREDENTIALS_DIRECTORY/upload_key";
132+
})
133+
// (optionalAttrs (cfg.accessKeyFile != null) {
134+
ACCESS_KEY = "/$CREDENTIALS_DIRECTORY/access_key";
135+
});
114136
user = "demostf";
115137
group = "demostf";
116138
};
117139

118140
systemd.services.phpfpm-demostf-api.serviceConfig = {
119-
EnvironmentFile = cfg.keyFile;
141+
User = "demostf";
142+
AmbientCapabilities = "CAP_CHOWN";
143+
NoNewPrivileges = true;
144+
LoadCredential =
145+
(optionals (cfg.editKeyFile != null) [
146+
"edit_key:${cfg.editKeyFile}"
147+
])
148+
++ (optionals (cfg.uploadKeyFile != null) [
149+
"upload_key:${cfg.uploadKeyFile}"
150+
])
151+
++ (optionals (cfg.accessKeyFile != null) [
152+
"access_key:${cfg.accessKeyFile}"
153+
]);
120154
};
121155

122156
services.prometheus.exporters.php-fpm = {

src/init.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ function get_magic_quotes_gpc(): bool {
1515

1616
function getEnvVar(string $name): string {
1717
$var = getenv($name) ?: '';
18+
error_log("$name='$var'");
1819
if (str_contains($var, '$CREDENTIALS_DIRECTORY')) {
1920
$credentialsDirectory = getenv('CREDENTIALS_DIRECTORY') ?: '';
2021
$path = str_replace('$CREDENTIALS_DIRECTORY', $credentialsDirectory, $var);
@@ -58,7 +59,7 @@ function getEnvVar(string $name): string {
5859
$storeHost = getEnvVar('DEMO_HOST');
5960
$parserPath = getEnvVar('PARSER_PATH');
6061
$appRoot = getEnvVar('APP_ROOT');
61-
$editKey = getEnvVar('EDIT_SECRET');
62+
$editKey = getEnvVar('EDIT_KEY');
6263
$uploadKey = getEnvVar('UPLOAD_KEY');
6364
$accessKey = getEnvVar('ACCESS_KEY');
6465

test/Integration/Tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ chakram.setRequestDefaults({baseUrl: root});
1515

1616
before((done) => {
1717
console.log('spawn server');
18-
const server = require('child_process').spawn('php', ['-S', '0.0.0.0:8000', 'router.php'], {
18+
const server = require('child_process').spawn('php', ['-d', 'post_max_size=100M', '-S', '0.0.0.0:8000', 'router.php'], {
1919
cwd: __dirname + '/../',
2020
env: process.env
2121
});

0 commit comments

Comments
 (0)