Convert to gormigrate

This commit is contained in:
Jamie Curnow committed 2026-09-24 15:23:05 +10:00
1 parent 2b3bef186c
commit 6c72c63532
15 files changed
+639 -1803

No files matched your search

+2 -1
View File
@@ -15,6 +15,7 @@ import (
"npm/internal/jobqueue"
"npm/internal/jwt"
"npm/internal/logger"
"npm/internal/migrations"
// properly respect available cpu cores
_ "go.uber.org/automaxprocs"
@@ -29,7 +30,7 @@ func main() {
config.CreateDataFolders()
logger.Info("Build Version: %s (%s)", version, commit)
database.Migrate(func() {
migrations.Migrate(func() {
if err := jwt.LoadKeys(); err != nil {
logger.Error("KeysError", err)
os.Exit(1)
-5
View File
@@ -12,11 +12,6 @@ var APIDocFiles embed.FS
//go:embed assets
var Assets embed.FS
// MigrationFiles are database migrations
//
//go:embed migrations
var MigrationFiles embed.FS
// NginxFiles hold nginx config templates
//
//go:embed nginx
@@ -1,246 +0,0 @@
-- migrate:up
CREATE TABLE IF NOT EXISTS `jwt_keys`
(
`id` INT AUTO_INCREMENT PRIMARY KEY,
`created_at` BIGINT NOT NULL DEFAULT 0,
`updated_at` BIGINT NOT NULL DEFAULT 0,
`is_deleted` INT NOT NULL DEFAULT 0, -- int on purpose, gormism
`public_key` TEXT NOT NULL,
`private_key` TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS `user`
(
`id` INT AUTO_INCREMENT PRIMARY KEY,
`created_at` BIGINT NOT NULL DEFAULT 0,
`updated_at` BIGINT NOT NULL DEFAULT 0,
`is_deleted` INT NOT NULL DEFAULT 0, -- int on purpose, gormism
`name` VARCHAR(50) NOT NULL,
`email` VARCHAR(255) NOT NULL,
`is_system` BOOLEAN NOT NULL DEFAULT FALSE,
`is_disabled` BOOLEAN NOT NULL DEFAULT FALSE
);
CREATE TABLE IF NOT EXISTS `capability`
(
`name` VARCHAR(50) PRIMARY KEY,
UNIQUE (`name`)
);
CREATE TABLE IF NOT EXISTS `user_has_capability`
(
`user_id` INT NOT NULL,
`capability_name` VARCHAR(50) NOT NULL,
UNIQUE (`user_id`, `capability_name`),
FOREIGN KEY (`capability_name`) REFERENCES `capability`(`name`) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS `auth`
(
`id` INT AUTO_INCREMENT PRIMARY KEY,
`created_at` BIGINT NOT NULL DEFAULT 0,
`updated_at` BIGINT NOT NULL DEFAULT 0,
`is_deleted` INT NOT NULL DEFAULT 0, -- int on purpose, gormism
`user_id` INT NOT NULL,
`type` VARCHAR(50) NOT NULL,
`identity` VARCHAR(255) NOT NULL,
`secret` VARCHAR(255) NOT NULL,
FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON DELETE CASCADE,
UNIQUE (`user_id`, `type`)
);
CREATE TABLE IF NOT EXISTS `setting`
(
`id` INT AUTO_INCREMENT PRIMARY KEY,
`created_at` BIGINT NOT NULL DEFAULT 0,
`updated_at` BIGINT NOT NULL DEFAULT 0,
`is_deleted` INT NOT NULL DEFAULT 0, -- int on purpose, gormism
`name` VARCHAR(50) NOT NULL,
`description` VARCHAR(255) NOT NULL DEFAULT '',
`value` TEXT NOT NULL,
UNIQUE (`name`)
);
CREATE TABLE IF NOT EXISTS `audit_log`
(
`id` INT AUTO_INCREMENT PRIMARY KEY,
`created_at` BIGINT NOT NULL DEFAULT 0,
`updated_at` BIGINT NOT NULL DEFAULT 0,
`is_deleted` INT NOT NULL DEFAULT 0, -- int on purpose, gormism
`user_id` INT NOT NULL,
`object_type` VARCHAR(50) NOT NULL,
`object_id` INT NOT NULL,
`action` VARCHAR(50) NOT NULL,
`meta` TEXT NOT NULL,
FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS `certificate_authority`
(
`id` INT AUTO_INCREMENT PRIMARY KEY,
`created_at` BIGINT NOT NULL DEFAULT 0,
`updated_at` BIGINT NOT NULL DEFAULT 0,
`is_deleted` INT NOT NULL DEFAULT 0, -- int on purpose, gormism
`name` VARCHAR(50) NOT NULL,
`acmesh_server` VARCHAR(255) NOT NULL DEFAULT '',
`ca_bundle` VARCHAR(255) NOT NULL DEFAULT '',
`is_wildcard_supported` BOOLEAN NOT NULL DEFAULT FALSE, -- specific to each CA, acme v1 doesn't usually have wildcards
`max_domains` INT NOT NULL DEFAULT 5, -- per request
`is_readonly` BOOLEAN NOT NULL DEFAULT FALSE
);
CREATE TABLE IF NOT EXISTS `dns_provider`
(
`id` INT AUTO_INCREMENT PRIMARY KEY,
`created_at` BIGINT NOT NULL DEFAULT 0,
`updated_at` BIGINT NOT NULL DEFAULT 0,
`is_deleted` INT NOT NULL DEFAULT 0, -- int on purpose, gormism
`user_id` INT NOT NULL,
`name` VARCHAR(50) NOT NULL,
`acmesh_name` VARCHAR(50) NOT NULL,
`dns_sleep` INT NOT NULL DEFAULT 0,
`meta` TEXT NOT NULL,
FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS `certificate`
(
`id` INT AUTO_INCREMENT PRIMARY KEY,
`created_at` BIGINT NOT NULL DEFAULT 0,
`updated_at` BIGINT NOT NULL DEFAULT 0,
`is_deleted` INT NOT NULL DEFAULT 0, -- int on purpose, gormism
`user_id` INT NOT NULL,
`type` VARCHAR(50) NOT NULL, -- custom,dns,http
`certificate_authority_id` INT, -- null for a custom cert
`dns_provider_id` INT, -- 0, for a http or custom cert
`name` VARCHAR(50) NOT NULL,
`domain_names` TEXT NOT NULL,
`expires_on` BIGINT NOT NULL DEFAULT 0,
`status` VARCHAR(50) NOT NULL, -- ready,requesting,failed,provided
`error_message` TEXT NOT NULL,
`meta` TEXT NOT NULL,
`is_ecc` BOOLEAN NOT NULL DEFAULT FALSE,
FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON DELETE CASCADE,
FOREIGN KEY (`certificate_authority_id`) REFERENCES `certificate_authority`(`id`) ON DELETE CASCADE,
FOREIGN KEY (`dns_provider_id`) REFERENCES `dns_provider`(`id`) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS `stream`
(
`id` INT AUTO_INCREMENT PRIMARY KEY,
`created_at` BIGINT NOT NULL DEFAULT 0,
`updated_at` BIGINT NOT NULL DEFAULT 0,
`is_deleted` INT NOT NULL DEFAULT 0, -- int on purpose, gormism
`user_id` INT NOT NULL,
`listen_interface` VARCHAR(50) NOT NULL,
`incoming_port` INT NOT NULL,
`tcp_forwarding` INT NOT NULL DEFAULT 0,
`udp_forwarding` INT NOT NULL DEFAULT 0,
`advanced_config` TEXT NOT NULL,
`is_disabled` BOOLEAN NOT NULL DEFAULT FALSE,
FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS `nginx_template`
(
`id` INT AUTO_INCREMENT PRIMARY KEY,
`created_at` BIGINT NOT NULL DEFAULT 0,
`updated_at` BIGINT NOT NULL DEFAULT 0,
`is_deleted` INT NOT NULL DEFAULT 0, -- int on purpose, gormism
`user_id` INT NOT NULL,
`name` VARCHAR(50) NOT NULL,
`type` VARCHAR(50) NOT NULL,
`template` TEXT NOT NULL,
FOREIGN KEY (`user_id`) REFERENCES `user`(`id`)
);
CREATE TABLE IF NOT EXISTS `upstream`
(
`id` INT AUTO_INCREMENT PRIMARY KEY,
`created_at` BIGINT NOT NULL DEFAULT 0,
`updated_at` BIGINT NOT NULL DEFAULT 0,
`is_deleted` INT NOT NULL DEFAULT 0, -- int on purpose, gormism
`user_id` INT NOT NULL,
`name` VARCHAR(50) NOT NULL,
`nginx_template_id` INT NOT NULL,
`ip_hash` BOOLEAN NOT NULL DEFAULT FALSE,
`ntlm` BOOLEAN NOT NULL DEFAULT FALSE,
`keepalive` INT NOT NULL DEFAULT 0,
`keepalive_requests` INT NOT NULL DEFAULT 0,
`keepalive_time` VARCHAR(50) NOT NULL DEFAULT '',
`keepalive_timeout` VARCHAR(50) NOT NULL DEFAULT '',
`advanced_config` TEXT NOT NULL,
`status` VARCHAR(50) NOT NULL DEFAULT '',
`error_message` TEXT NOT NULL,
FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON DELETE CASCADE,
FOREIGN KEY (`nginx_template_id`) REFERENCES `nginx_template`(`id`) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS `upstream_server`
(
`id` INT AUTO_INCREMENT PRIMARY KEY,
`created_at` BIGINT NOT NULL DEFAULT 0,
`updated_at` BIGINT NOT NULL DEFAULT 0,
`is_deleted` INT NOT NULL DEFAULT 0, -- int on purpose, gormism
`upstream_id` INT NOT NULL,
`server` VARCHAR(50) NOT NULL,
`weight` INT NOT NULL DEFAULT 0,
`max_conns` INT NOT NULL DEFAULT 0,
`max_fails` INT NOT NULL DEFAULT 0,
`fail_timeout` INT NOT NULL DEFAULT 0,
`is_backup` BOOLEAN NOT NULL DEFAULT FALSE,
FOREIGN KEY (`upstream_id`) REFERENCES `upstream`(`id`) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS `access_list`
(
`id` INT AUTO_INCREMENT PRIMARY KEY,
`created_at` BIGINT NOT NULL DEFAULT 0,
`updated_at` BIGINT NOT NULL DEFAULT 0,
`is_deleted` INT NOT NULL DEFAULT 0, -- int on purpose, gormism
`user_id` INT NOT NULL,
`name` VARCHAR(50) NOT NULL,
`meta` TEXT NOT NULL,
FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS `host`
(
`id` INT AUTO_INCREMENT PRIMARY KEY,
`created_at` BIGINT NOT NULL DEFAULT 0,
`updated_at` BIGINT NOT NULL DEFAULT 0,
`is_deleted` INT NOT NULL DEFAULT 0, -- int on purpose, gormism
`user_id` INT NOT NULL,
`type` VARCHAR(50) NOT NULL,
`nginx_template_id` INT NOT NULL,
`listen_interface` VARCHAR(50) NOT NULL DEFAULT '',
`domain_names` TEXT NOT NULL,
`upstream_id` INT,
`proxy_scheme` VARCHAR(50) NOT NULL DEFAULT '',
`proxy_host` VARCHAR(50) NOT NULL DEFAULT '',
`proxy_port` INT NOT NULL DEFAULT 0,
`certificate_id` INT,
`access_list_id` INT,
`ssl_forced` BOOLEAN NOT NULL DEFAULT FALSE,
`caching_enabled` BOOLEAN NOT NULL DEFAULT FALSE,
`block_exploits` BOOLEAN NOT NULL DEFAULT FALSE,
`allow_websocket_upgrade` BOOLEAN NOT NULL DEFAULT FALSE,
`http2_support` BOOLEAN NOT NULL DEFAULT FALSE,
`hsts_enabled` BOOLEAN NOT NULL DEFAULT FALSE,
`hsts_subdomains` BOOLEAN NOT NULL DEFAULT FALSE,
`paths` TEXT NOT NULL,
`advanced_config` TEXT NOT NULL,
`status` VARCHAR(50) NOT NULL DEFAULT '',
`error_message` TEXT NOT NULL,
`is_disabled` BOOLEAN NOT NULL DEFAULT FALSE,
FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON DELETE CASCADE,
FOREIGN KEY (`nginx_template_id`) REFERENCES `nginx_template`(`id`) ON DELETE CASCADE,
FOREIGN KEY (`upstream_id`) REFERENCES `upstream`(`id`) ON DELETE CASCADE,
FOREIGN KEY (`certificate_id`) REFERENCES `certificate`(`id`) ON DELETE CASCADE,
FOREIGN KEY (`access_list_id`) REFERENCES `access_list`(`id`) ON DELETE CASCADE
);
-- migrate:down
-- Not allowed to go down from initial
@@ -1,337 +0,0 @@
-- migrate:up
-- User permissions
INSERT INTO `capability` (
`name`
) VALUES
("full-admin"),
("access-lists.view"),
("access-lists.manage"),
("audit-log.view"),
("certificates.view"),
("certificates.manage"),
("certificate-authorities.view"),
("certificate-authorities.manage"),
("dns-providers.view"),
("dns-providers.manage"),
("hosts.view"),
("hosts.manage"),
("nginx-templates.view"),
("nginx-templates.manage"),
("settings.manage"),
("streams.view"),
("streams.manage"),
("users.manage");
INSERT INTO `setting` (
`created_at`,
`updated_at`,
`name`,
`description`,
`value`
) VALUES
-- Default site
(
ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000),
ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000),
"default-site",
"What to show users who hit your Nginx server by default",
'"welcome"' -- remember this is json
),
(
ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000),
ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000),
"auth-methods",
"Which methods are enabled for authentication",
'["local"]' -- remember this is json
),
(
ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000),
ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000),
"oauth-auth",
"Configuration for OAuth authentication",
'{}' -- remember this is json
),
(
ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000),
ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000),
"ldap-auth",
"Configuration for LDAP authentication",
'{"host": "", "dn": "", "sync_by": "uid"}' -- remember this is json
);
-- Default Certificate Authorities
INSERT INTO `certificate_authority` (
`created_at`,
`updated_at`,
`name`,
`acmesh_server`,
`is_wildcard_supported`,
`max_domains`,
`is_readonly`
) VALUES (
ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000),
ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000),
"ZeroSSL",
"zerossl",
TRUE,
10,
TRUE
), (
ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000),
ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000),
"Let's Encrypt",
"https://acme-v02.api.letsencrypt.org/directory",
TRUE,
10,
TRUE
), (
ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000),
ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000),
"Buypass Go SSL",
"https://api.buypass.com/acme/directory",
FALSE,
5,
TRUE
), (
ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000),
ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000),
"SSL.com",
"ssl.com",
FALSE,
10,
TRUE
), (
ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000),
ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000),
"Let's Encrypt (Testing)",
"https://acme-staging-v02.api.letsencrypt.org/directory",
TRUE,
10,
TRUE
), (
ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000),
ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000),
"Buypass Go SSL (Testing)",
"https://api.test4.buypass.no/acme/directory",
FALSE,
5,
TRUE
);
-- System User
INSERT INTO `user` (
`created_at`,
`updated_at`,
`name`,
`email`,
`is_system`
) VALUES (
ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000),
ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000),
"System",
"system@localhost",
TRUE
);
-- Host Templates
INSERT INTO `nginx_template` (
`created_at`,
`updated_at`,
`user_id`,
`name`,
`type`,
`template`
) VALUES (
ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000),
ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000),
(SELECT `id` FROM `user` WHERE `is_system` IS TRUE LIMIT 1),
"Default Proxy Template",
"proxy",
"# ------------------------------------------------------------
{{#each Host.DomainNames}}
# {{this}}
{{/each}}
# ------------------------------------------------------------
server {
{{#if Config.Ipv4}}
listen 80;
{{/if}}
{{#if Config.Ipv6}}
listen [::]:80;
{{/if}}
{{#if Certificate.ID}}
{{#if Config.Ipv4}}
listen 443 ssl {{#if Host.HTTP2Support}}http2{{/if}};
{{/if}}
{{#if Config.Ipv6}}
listen [::]:443 ssl {{#if Host.HTTP2Support}}http2{{/if}};
{{/if}}
{{/if}}
server_name {{#each Host.DomainNames}}{{this}} {{/each}};
{{#if Certificate.ID}}
include conf.d/include/ssl-ciphers.conf;
{{#if Certificate.IsAcme}}
ssl_certificate {{Certificate.Folder}}/fullchain.pem;
ssl_certificate_key {{Certificate.Folder}}/privkey.pem;
{{else}}
# Custom SSL
ssl_certificate /data/custom_ssl/npm-{{Certicicate.ID}}/fullchain.pem;
ssl_certificate_key /data/custom_ssl/npm-{{Certificate.ID}}/privkey.pem;
{{/if}}
{{/if}}
{{#if Host.CachingEnabled}}
include conf.d/include/assets.conf;
{{/if}}
{{#if Host.BlockExploits}}
include conf.d/include/block-exploits.conf;
{{/if}}
{{#if Certificate.ID}}
{{#if Host.SSLForced}}
{{#if Host.HSTSEnabled}}
# HSTS (ngx_http_headers_module is required) (63072000 seconds = 2 years)
add_header Strict-Transport-Security ""max-age=63072000;{{#if Host.HSTSSubdomains}} includeSubDomains;{{/if}} preload"" always;
{{/if}}
# Force SSL
include conf.d/include/force-ssl.conf;
{{/if}}
{{/if}}
{{#if Host.AllowWebsocketUpgrade}}
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $http_connection;
proxy_http_version 1.1;
{{/if}}
access_log /data/logs/host-{{Host.ID}}_access.log proxy;
error_log /data/logs/host-{{Host.ID}}_error.log warn;
{{Host.AdvancedConfig}}
# locations ?
# default location:
location / {
{{#if Host.AccessListID}}
# Authorization
auth_basic ""Authorization required"";
auth_basic_user_file /data/access/{{Host.AccessListID}};
# access_list.passauth ? todo
{{/if}}
# Access Rules ? todo
# Access checks must...? todo
{{#if Certificate.ID}}
{{#if Host.SSLForced}}
{{#if Host.HSTSEnabled}}
# HSTS (ngx_http_headers_module is required) (63072000 seconds = 2 years)
add_header Strict-Transport-Security ""max-age=63072000;{{#if Host.HSTSSubdomains}} includeSubDomains;{{/if}} preload"" always;
{{/if}}
{{/if}}
{{/if}}
{{#if Host.AllowWebsocketUpgrade}}
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $http_connection;
{{/if}}
# Proxy!
add_header X-Served-By $host;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Scheme $scheme;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_http_version 1.1;
{{#if Upstream.ID}}
# upstream
proxy_pass {{Host.ProxyScheme}}://npm_upstream_{{Upstream.ID}};
{{else}}
# proxy a single host
proxy_pass {{Host.ProxyScheme}}://{{Host.ProxyHost}}:{{Host.ProxyPort}};
{{/if}}
}
# Legacy Custom Configuration
include /data/nginx/custom/server_proxy[.]conf;
}
"
), (
ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000),
ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000),
(SELECT `id` FROM `user` WHERE `is_system` IS TRUE LIMIT 1),
"Default Redirect Template",
"redirect",
"# this is a redirect template"
), (
ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000),
ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000),
(SELECT `id` FROM `user` WHERE `is_system` IS TRUE LIMIT 1),
"Default Dead Template",
"dead",
"# this is a dead template"
), (
ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000),
ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000),
(SELECT `id` FROM `user` WHERE `is_system` IS TRUE LIMIT 1),
"Default Stream Template",
"stream",
"# this is a stream template"
), (
ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000),
ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000),
(SELECT `id` FROM `user` WHERE `is_system` IS TRUE LIMIT 1),
"Default Upstream Template",
"upstream",
"# ------------------------------------------------------------
# Upstream {{Upstream.ID}}: {{Upstream.Name}}
# ------------------------------------------------------------
upstream npm_upstream_{{Upstream.ID}} {
{{#if Upstream.IPHash~}}
ip_hash;
{{~/if}}
{{#if Upstream.NTLM~}}
ntlm;
{{~/if}}
{{#if Upstream.Keepalive~}}
keepalive {{Upstream.Keepalive}};
{{~/if}}
{{#if Upstream.KeepaliveRequests~}}
keepalive_requests {{Upstream.KeepaliveRequests}};
{{~/if}}
{{#if Upstream.KeepaliveTime~}}
keepalive_time {{Upstream.KeepaliveTime}};
{{~/if}}
{{#if Upstream.KeepaliveTimeout~}}
keepalive_timeout {{Upstream.KeepaliveTimeout}};
{{~/if}}
{{Upstream.AdvancedConfig}}
{{#each Upstream.Servers~}}
{{#unless IsDeleted~}}
server {{Server}} {{#if Weight}}weight={{Weight}} {{/if}}{{#if MaxConns}}max_conns={{MaxConns}} {{/if}}{{#if MaxFails}}max_fails={{MaxFails}} {{/if}}{{#if FailTimeout}}fail_timeout={{FailTimeout}} {{/if}}{{#if Backup}}backup{{/if}};
{{/unless}}
{{/each}}
}
"
);
-- migrate:down
@@ -1,212 +0,0 @@
-- migrate:up
CREATE TABLE "jwt_keys" (
"id" SERIAL PRIMARY KEY,
"created_at" BIGINT NOT NULL DEFAULT 0,
"updated_at" BIGINT NOT NULL DEFAULT 0,
"is_deleted" INTEGER NOT NULL DEFAULT 0, -- int on purpose, gormism
"public_key" TEXT NOT NULL,
"private_key" TEXT NOT NULL
);
CREATE TABLE "user" (
"id" SERIAL PRIMARY KEY,
"created_at" BIGINT NOT NULL DEFAULT 0,
"updated_at" BIGINT NOT NULL DEFAULT 0,
"is_deleted" INTEGER NOT NULL DEFAULT 0, -- int on purpose, gormism
"name" VARCHAR(50) NOT NULL,
"email" VARCHAR(255) NOT NULL,
"is_system" BOOLEAN NOT NULL DEFAULT FALSE,
"is_disabled" BOOLEAN NOT NULL DEFAULT FALSE
);
CREATE TABLE "capability" (
"name" TEXT NOT NULL PRIMARY KEY,
UNIQUE ("name")
);
CREATE TABLE "user_has_capability" (
"user_id" INTEGER NOT NULL,
"capability_name" TEXT NOT NULL REFERENCES "capability"("name") ON DELETE CASCADE,
UNIQUE ("user_id", "capability_name")
);
CREATE TABLE "auth" (
"id" SERIAL PRIMARY KEY,
"created_at" BIGINT NOT NULL DEFAULT 0,
"updated_at" BIGINT NOT NULL DEFAULT 0,
"is_deleted" INTEGER NOT NULL DEFAULT 0, -- int on purpose, gormism
"user_id" INTEGER NOT NULL REFERENCES "user"("id") ON DELETE CASCADE,
"type" VARCHAR(50) NOT NULL,
"identity" VARCHAR(255) NOT NULL,
"secret" VARCHAR(255) NOT NULL,
UNIQUE ("user_id", "type")
);
CREATE TABLE "setting" (
"id" SERIAL PRIMARY KEY,
"created_at" BIGINT NOT NULL DEFAULT 0,
"updated_at" BIGINT NOT NULL DEFAULT 0,
"is_deleted" INTEGER NOT NULL DEFAULT 0, -- int on purpose, gormism
"name" VARCHAR(50) NOT NULL,
"description" VARCHAR(255) NOT NULL DEFAULT '',
"value" TEXT NOT NULL,
UNIQUE ("name")
);
CREATE TABLE "audit_log" (
"id" SERIAL PRIMARY KEY,
"created_at" BIGINT NOT NULL DEFAULT 0,
"updated_at" BIGINT NOT NULL DEFAULT 0,
"is_deleted" INTEGER NOT NULL DEFAULT 0, -- int on purpose, gormism
"user_id" INTEGER NOT NULL REFERENCES "user"("id") ON DELETE CASCADE,
"object_type" VARCHAR(50) NOT NULL,
"object_id" INTEGER NOT NULL,
"action" VARCHAR(50) NOT NULL,
"meta" TEXT NOT NULL
);
CREATE TABLE "certificate_authority" (
"id" SERIAL PRIMARY KEY,
"created_at" BIGINT NOT NULL DEFAULT 0,
"updated_at" BIGINT NOT NULL DEFAULT 0,
"is_deleted" INTEGER NOT NULL DEFAULT 0, -- int on purpose, gormism
"name" VARCHAR(50) NOT NULL,
"acmesh_server" VARCHAR(255) NOT NULL DEFAULT '',
"ca_bundle" VARCHAR(255) NOT NULL DEFAULT '',
"is_wildcard_supported" BOOLEAN NOT NULL DEFAULT FALSE, -- specific to each CA, acme v1 doesn't usually have wildcards
"max_domains" INTEGER NOT NULL DEFAULT 5, -- per request
"is_readonly" BOOLEAN NOT NULL DEFAULT FALSE
);
CREATE TABLE "dns_provider" (
"id" SERIAL PRIMARY KEY,
"created_at" BIGINT NOT NULL DEFAULT 0,
"updated_at" BIGINT NOT NULL DEFAULT 0,
"is_deleted" INTEGER NOT NULL DEFAULT 0, -- int on purpose, gormism
"user_id" INTEGER NOT NULL REFERENCES "user"("id") ON DELETE CASCADE,
"name" VARCHAR(50) NOT NULL,
"acmesh_name" VARCHAR(50) NOT NULL,
"dns_sleep" INTEGER NOT NULL DEFAULT 0,
"meta" TEXT NOT NULL
);
CREATE TABLE "certificate" (
"id" SERIAL PRIMARY KEY,
"created_at" BIGINT NOT NULL DEFAULT 0,
"updated_at" BIGINT NOT NULL DEFAULT 0,
"is_deleted" INTEGER NOT NULL DEFAULT 0, -- int on purpose, gormism
"user_id" INTEGER NOT NULL REFERENCES "user"("id") ON DELETE CASCADE,
"type" VARCHAR(50) NOT NULL, -- custom,dns,http
"certificate_authority_id" INTEGER REFERENCES "certificate_authority"("id") ON DELETE CASCADE, -- 0 for a custom cert
"dns_provider_id" INTEGER REFERENCES "dns_provider"("id") ON DELETE CASCADE, -- 0, for a http or custom cert
"name" VARCHAR(50) NOT NULL,
"domain_names" TEXT NOT NULL,
"expires_on" BIGINT NOT NULL DEFAULT 0,
"status" VARCHAR(50) NOT NULL, -- ready,requesting,failed,provided
"error_message" TEXT NOT NULL DEFAULT '',
"meta" TEXT NOT NULL,
"is_ecc" BOOLEAN NOT NULL DEFAULT FALSE
);
CREATE TABLE "stream" (
"id" SERIAL PRIMARY KEY,
"created_at" BIGINT NOT NULL DEFAULT 0,
"updated_at" BIGINT NOT NULL DEFAULT 0,
"is_deleted" INTEGER NOT NULL DEFAULT 0, -- int on purpose, gormism
"user_id" INTEGER NOT NULL REFERENCES "user"("id") ON DELETE CASCADE,
"listen_interface" VARCHAR(50) NOT NULL,
"incoming_port" INTEGER NOT NULL,
"tcp_forwarding" INTEGER NOT NULL DEFAULT 0,
"udp_forwarding" INTEGER NOT NULL DEFAULT 0,
"advanced_config" TEXT NOT NULL,
"is_disabled" BOOLEAN NOT NULL DEFAULT FALSE
);
CREATE TABLE "nginx_template" (
"id" SERIAL PRIMARY KEY,
"created_at" BIGINT NOT NULL DEFAULT 0,
"updated_at" BIGINT NOT NULL DEFAULT 0,
"is_deleted" INTEGER NOT NULL DEFAULT 0, -- int on purpose, gormism
"user_id" INTEGER NOT NULL REFERENCES "user"("id") ON DELETE CASCADE,
"name" VARCHAR(50) NOT NULL,
"type" VARCHAR(50) NOT NULL,
"template" TEXT NOT NULL
);
CREATE TABLE "upstream" (
"id" SERIAL PRIMARY KEY,
"created_at" BIGINT NOT NULL DEFAULT 0,
"updated_at" BIGINT NOT NULL DEFAULT 0,
"is_deleted" INTEGER NOT NULL DEFAULT 0, -- int on purpose, gormism
"user_id" INTEGER NOT NULL REFERENCES "user"("id") ON DELETE CASCADE,
"name" VARCHAR(50) NOT NULL,
"nginx_template_id" INTEGER NOT NULL REFERENCES "nginx_template"("id") ON DELETE CASCADE,
"ip_hash" BOOLEAN NOT NULL DEFAULT FALSE,
"ntlm" BOOLEAN NOT NULL DEFAULT FALSE,
"keepalive" INTEGER NOT NULL DEFAULT 0,
"keepalive_requests" INTEGER NOT NULL DEFAULT 0,
"keepalive_time" VARCHAR(50) NOT NULL DEFAULT '',
"keepalive_timeout" VARCHAR(50) NOT NULL DEFAULT '',
"advanced_config" TEXT NOT NULL,
"status" VARCHAR(50) NOT NULL DEFAULT '',
"error_message" TEXT NOT NULL DEFAULT ''
);
CREATE TABLE "upstream_server" (
"id" SERIAL PRIMARY KEY,
"created_at" BIGINT NOT NULL DEFAULT 0,
"updated_at" BIGINT NOT NULL DEFAULT 0,
"is_deleted" INTEGER NOT NULL DEFAULT 0, -- int on purpose, gormism
"upstream_id" INTEGER NOT NULL REFERENCES "upstream"("id") ON DELETE CASCADE,
"server" VARCHAR(50) NOT NULL,
"weight" INTEGER NOT NULL DEFAULT 0,
"max_conns" INTEGER NOT NULL DEFAULT 0,
"max_fails" INTEGER NOT NULL DEFAULT 0,
"fail_timeout" INTEGER NOT NULL DEFAULT 0,
"is_backup" BOOLEAN NOT NULL DEFAULT FALSE
);
CREATE TABLE "access_list" (
"id" SERIAL PRIMARY KEY,
"created_at" BIGINT NOT NULL DEFAULT 0,
"updated_at" BIGINT NOT NULL DEFAULT 0,
"is_deleted" INTEGER NOT NULL DEFAULT 0, -- int on purpose, gormism
"user_id" INTEGER NOT NULL REFERENCES "user"("id") ON DELETE CASCADE,
"name" VARCHAR(50) NOT NULL,
"meta" TEXT NOT NULL
);
CREATE TABLE "host" (
"id" SERIAL PRIMARY KEY,
"created_at" BIGINT NOT NULL DEFAULT 0,
"updated_at" BIGINT NOT NULL DEFAULT 0,
"is_deleted" INTEGER NOT NULL DEFAULT 0, -- int on purpose, gormism
"user_id" INTEGER NOT NULL REFERENCES "user"("id") ON DELETE CASCADE,
"type" TEXT NOT NULL,
"nginx_template_id" INTEGER NOT NULL REFERENCES "nginx_template"("id") ON DELETE CASCADE,
"listen_interface" TEXT NOT NULL DEFAULT '',
"domain_names" TEXT NOT NULL,
"upstream_id" INTEGER REFERENCES "upstream"("id") ON DELETE CASCADE,
"proxy_scheme" TEXT NOT NULL DEFAULT '',
"proxy_host" TEXT NOT NULL DEFAULT '',
"proxy_port" INTEGER NOT NULL DEFAULT 0,
"certificate_id" INTEGER REFERENCES "certificate"("id") ON DELETE CASCADE,
"access_list_id" INTEGER REFERENCES "access_list"("id") ON DELETE CASCADE,
"ssl_forced" BOOLEAN NOT NULL DEFAULT FALSE,
"caching_enabled" BOOLEAN NOT NULL DEFAULT FALSE,
"block_exploits" BOOLEAN NOT NULL DEFAULT FALSE,
"allow_websocket_upgrade" BOOLEAN NOT NULL DEFAULT FALSE,
"http2_support" BOOLEAN NOT NULL DEFAULT FALSE,
"hsts_enabled" BOOLEAN NOT NULL DEFAULT FALSE,
"hsts_subdomains" BOOLEAN NOT NULL DEFAULT FALSE,
"paths" TEXT NOT NULL DEFAULT '',
"advanced_config" TEXT NOT NULL DEFAULT '',
"status" TEXT NOT NULL DEFAULT '',
"error_message" TEXT NOT NULL DEFAULT '',
"is_disabled" BOOLEAN NOT NULL DEFAULT FALSE
);
-- migrate:down
-- Not allowed to go down from initial
@@ -1,337 +0,0 @@
-- migrate:up
-- User permissions
INSERT INTO "capability" (
"name"
) VALUES
('full-admin'),
('access-lists.view'),
('access-lists.manage'),
('audit-log.view'),
('certificates.view'),
('certificates.manage'),
('certificate-authorities.view'),
('certificate-authorities.manage'),
('dns-providers.view'),
('dns-providers.manage'),
('hosts.view'),
('hosts.manage'),
('nginx-templates.view'),
('nginx-templates.manage'),
('settings.manage'),
('streams.view'),
('streams.manage'),
('users.manage');
INSERT INTO "setting" (
"created_at",
"updated_at",
"name",
"description",
"value"
) VALUES
-- Default site
(
EXTRACT(EPOCH FROM TIMESTAMP '2011-05-17 10:40:28.876944') * 1000,
EXTRACT(EPOCH FROM TIMESTAMP '2011-05-17 10:40:28.876944') * 1000,
'default-site',
'What to show users who hit your Nginx server by default',
'"welcome"' -- remember this is json
),
(
EXTRACT(EPOCH FROM TIMESTAMP '2011-05-17 10:40:28.876944') * 1000,
EXTRACT(EPOCH FROM TIMESTAMP '2011-05-17 10:40:28.876944') * 1000,
'auth-methods',
'Which methods are enabled for authentication',
'["local"]' -- remember this is json
),
(
EXTRACT(EPOCH FROM TIMESTAMP '2011-05-17 10:40:28.876944') * 1000,
EXTRACT(EPOCH FROM TIMESTAMP '2011-05-17 10:40:28.876944') * 1000,
'oauth-auth',
'Configuration for OAuth authentication',
'{}' -- remember this is json
),
(
EXTRACT(EPOCH FROM TIMESTAMP '2011-05-17 10:40:28.876944') * 1000,
EXTRACT(EPOCH FROM TIMESTAMP '2011-05-17 10:40:28.876944') * 1000,
'ldap-auth',
'Configuration for LDAP authentication',
'{"host": "", "dn": "", "sync_by": "uid"}' -- remember this is json
);
-- Default Certificate Authorities
INSERT INTO "certificate_authority" (
"created_at",
"updated_at",
"name",
"acmesh_server",
"is_wildcard_supported",
"max_domains",
"is_readonly"
) VALUES (
EXTRACT(EPOCH FROM TIMESTAMP '2011-05-17 10:40:28.876944') * 1000,
EXTRACT(EPOCH FROM TIMESTAMP '2011-05-17 10:40:28.876944') * 1000,
'ZeroSSL',
'zerossl',
TRUE,
10,
TRUE
), (
EXTRACT(EPOCH FROM TIMESTAMP '2011-05-17 10:40:28.876944') * 1000,
EXTRACT(EPOCH FROM TIMESTAMP '2011-05-17 10:40:28.876944') * 1000,
'Let''s Encrypt',
'https://acme-v02.api.letsencrypt.org/directory',
TRUE,
10,
TRUE
), (
EXTRACT(EPOCH FROM TIMESTAMP '2011-05-17 10:40:28.876944') * 1000,
EXTRACT(EPOCH FROM TIMESTAMP '2011-05-17 10:40:28.876944') * 1000,
'Buypass Go SSL',
'https://api.buypass.com/acme/directory',
FALSE,
5,
TRUE
), (
EXTRACT(EPOCH FROM TIMESTAMP '2011-05-17 10:40:28.876944') * 1000,
EXTRACT(EPOCH FROM TIMESTAMP '2011-05-17 10:40:28.876944') * 1000,
'SSL.com',
'ssl.com',
FALSE,
10,
TRUE
), (
EXTRACT(EPOCH FROM TIMESTAMP '2011-05-17 10:40:28.876944') * 1000,
EXTRACT(EPOCH FROM TIMESTAMP '2011-05-17 10:40:28.876944') * 1000,
'Let''s Encrypt (Testing)',
'https://acme-staging-v02.api.letsencrypt.org/directory',
TRUE,
10,
TRUE
), (
EXTRACT(EPOCH FROM TIMESTAMP '2011-05-17 10:40:28.876944') * 1000,
EXTRACT(EPOCH FROM TIMESTAMP '2011-05-17 10:40:28.876944') * 1000,
'Buypass Go SSL (Testing)',
'https://api.test4.buypass.no/acme/directory',
FALSE,
5,
TRUE
);
-- System User
INSERT INTO "user" (
"created_at",
"updated_at",
"name",
"email",
"is_system"
) VALUES (
EXTRACT(EPOCH FROM TIMESTAMP '2011-05-17 10:40:28.876944') * 1000,
EXTRACT(EPOCH FROM TIMESTAMP '2011-05-17 10:40:28.876944') * 1000,
'System',
'system@localhost',
TRUE
);
-- Host Templates
INSERT INTO "nginx_template" (
"created_at",
"updated_at",
"user_id",
"name",
"type",
"template"
) VALUES (
EXTRACT(EPOCH FROM TIMESTAMP '2011-05-17 10:40:28.876944') * 1000,
EXTRACT(EPOCH FROM TIMESTAMP '2011-05-17 10:40:28.876944') * 1000,
(SELECT "id" FROM "user" WHERE "is_system" IS TRUE LIMIT 1),
'Default Proxy Template',
'proxy',
'# ------------------------------------------------------------
{{#each Host.DomainNames}}
# {{this}}
{{/each}}
# ------------------------------------------------------------
server {
{{#if Config.Ipv4}}
listen 80;
{{/if}}
{{#if Config.Ipv6}}
listen [::]:80;
{{/if}}
{{#if Certificate.ID}}
{{#if Config.Ipv4}}
listen 443 ssl {{#if Host.HTTP2Support}}http2{{/if}};
{{/if}}
{{#if Config.Ipv6}}
listen [::]:443 ssl {{#if Host.HTTP2Support}}http2{{/if}};
{{/if}}
{{/if}}
server_name {{#each Host.DomainNames}}{{this}} {{/each}};
{{#if Certificate.ID}}
include conf.d/include/ssl-ciphers.conf;
{{#if Certificate.IsAcme}}
ssl_certificate {{Certificate.Folder}}/fullchain.pem;
ssl_certificate_key {{Certificate.Folder}}/privkey.pem;
{{else}}
# Custom SSL
ssl_certificate /data/custom_ssl/npm-{{Certicicate.ID}}/fullchain.pem;
ssl_certificate_key /data/custom_ssl/npm-{{Certificate.ID}}/privkey.pem;
{{/if}}
{{/if}}
{{#if Host.CachingEnabled}}
include conf.d/include/assets.conf;
{{/if}}
{{#if Host.BlockExploits}}
include conf.d/include/block-exploits.conf;
{{/if}}
{{#if Certificate.ID}}
{{#if Host.SSLForced}}
{{#if Host.HSTSEnabled}}
# HSTS (ngx_http_headers_module is required) (63072000 seconds = 2 years)
add_header Strict-Transport-Security "max-age=63072000;{{#if Host.HSTSSubdomains}} includeSubDomains;{{/if}} preload" always;
{{/if}}
# Force SSL
include conf.d/include/force-ssl.conf;
{{/if}}
{{/if}}
{{#if Host.AllowWebsocketUpgrade}}
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $http_connection;
proxy_http_version 1.1;
{{/if}}
access_log /data/logs/host-{{Host.ID}}_access.log proxy;
error_log /data/logs/host-{{Host.ID}}_error.log warn;
{{Host.AdvancedConfig}}
# locations ?
# default location:
location / {
{{#if Host.AccessListID}}
# Authorization
auth_basic "Authorization required";
auth_basic_user_file /data/access/{{Host.AccessListID}};
# access_list.passauth ? todo
{{/if}}
# Access Rules ? todo
# Access checks must...? todo
{{#if Certificate.ID}}
{{#if Host.SSLForced}}
{{#if Host.HSTSEnabled}}
# HSTS (ngx_http_headers_module is required) (63072000 seconds = 2 years)
add_header Strict-Transport-Security "max-age=63072000;{{#if Host.HSTSSubdomains}} includeSubDomains;{{/if}} preload" always;
{{/if}}
{{/if}}
{{/if}}
{{#if Host.AllowWebsocketUpgrade}}
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $http_connection;
{{/if}}
# Proxy!
add_header X-Served-By $host;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Scheme $scheme;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_http_version 1.1;
{{#if Upstream.ID}}
# upstream
proxy_pass {{Host.ProxyScheme}}://npm_upstream_{{Upstream.ID}};
{{else}}
# proxy a single host
proxy_pass {{Host.ProxyScheme}}://{{Host.ProxyHost}}:{{Host.ProxyPort}};
{{/if}}
}
# Legacy Custom Configuration
include /data/nginx/custom/server_proxy[.]conf;
}
'
), (
EXTRACT(EPOCH FROM TIMESTAMP '2011-05-17 10:40:28.876944') * 1000,
EXTRACT(EPOCH FROM TIMESTAMP '2011-05-17 10:40:28.876944') * 1000,
(SELECT "id" FROM "user" WHERE "is_system" IS TRUE LIMIT 1),
'Default Redirect Template',
'redirect',
'# this is a redirect template'
), (
EXTRACT(EPOCH FROM TIMESTAMP '2011-05-17 10:40:28.876944') * 1000,
EXTRACT(EPOCH FROM TIMESTAMP '2011-05-17 10:40:28.876944') * 1000,
(SELECT "id" FROM "user" WHERE "is_system" IS TRUE LIMIT 1),
'Default Dead Template',
'dead',
'# this is a dead template'
), (
EXTRACT(EPOCH FROM TIMESTAMP '2011-05-17 10:40:28.876944') * 1000,
EXTRACT(EPOCH FROM TIMESTAMP '2011-05-17 10:40:28.876944') * 1000,
(SELECT "id" FROM "user" WHERE "is_system" IS TRUE LIMIT 1),
'Default Stream Template',
'stream',
'# this is a stream template'
), (
EXTRACT(EPOCH FROM TIMESTAMP '2011-05-17 10:40:28.876944') * 1000,
EXTRACT(EPOCH FROM TIMESTAMP '2011-05-17 10:40:28.876944') * 1000,
(SELECT "id" FROM "user" WHERE "is_system" IS TRUE LIMIT 1),
'Default Upstream Template',
'upstream',
'# ------------------------------------------------------------
# Upstream {{Upstream.ID}}: {{Upstream.Name}}
# ------------------------------------------------------------
upstream npm_upstream_{{Upstream.ID}} {
{{#if Upstream.IPHash~}}
ip_hash;
{{~/if}}
{{#if Upstream.NTLM~}}
ntlm;
{{~/if}}
{{#if Upstream.Keepalive~}}
keepalive {{Upstream.Keepalive}};
{{~/if}}
{{#if Upstream.KeepaliveRequests~}}
keepalive_requests {{Upstream.KeepaliveRequests}};
{{~/if}}
{{#if Upstream.KeepaliveTime~}}
keepalive_time {{Upstream.KeepaliveTime}};
{{~/if}}
{{#if Upstream.KeepaliveTimeout~}}
keepalive_timeout {{Upstream.KeepaliveTimeout}};
{{~/if}}
{{Upstream.AdvancedConfig}}
{{#each Upstream.Servers~}}
{{#unless IsDeleted~}}
server {{Server}} {{#if Weight}}weight={{Weight}} {{/if}}{{#if MaxConns}}max_conns={{MaxConns}} {{/if}}{{#if MaxFails}}max_fails={{MaxFails}} {{/if}}{{#if FailTimeout}}fail_timeout={{FailTimeout}} {{/if}}{{#if Backup}}backup{{/if}};
{{/unless}}
{{/each}}
}
'
);
-- migrate:down
@@ -1,246 +0,0 @@
-- migrate:up
CREATE TABLE IF NOT EXISTS `jwt_keys`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`created_at` INTEGER NOT NULL DEFAULT 0,
`updated_at` INTEGER NOT NULL DEFAULT 0,
`is_deleted` INTEGER NOT NULL DEFAULT 0,
`public_key` TEXT NOT NULL,
`private_key` TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS `user`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`created_at` INTEGER NOT NULL DEFAULT 0,
`updated_at` INTEGER NOT NULL DEFAULT 0,
`is_deleted` INTEGER NOT NULL DEFAULT 0,
`name` TEXT NOT NULL,
`email` TEXT NOT NULL,
`is_system` INTEGER NOT NULL DEFAULT 0,
`is_disabled` INTEGER NOT NULL DEFAULT 0
);
CREATE TABLE IF NOT EXISTS `capability`
(
`name` TEXT PRIMARY KEY,
UNIQUE (`name`)
);
CREATE TABLE IF NOT EXISTS `user_has_capability`
(
`user_id` INTEGER NOT NULL,
`capability_name` TEXT NOT NULL,
UNIQUE (`user_id`, `capability_name`),
FOREIGN KEY (`capability_name`) REFERENCES `capability` (`name`) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS `auth`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`created_at` INTEGER NOT NULL DEFAULT 0,
`updated_at` INTEGER NOT NULL DEFAULT 0,
`is_deleted` INTEGER NOT NULL DEFAULT 0,
`user_id` INTEGER NOT NULL,
`type` TEXT NOT NULL,
`identity` TEXT NOT NULL,
`secret` TEXT NOT NULL,
FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE,
UNIQUE (`user_id`, `type`)
);
CREATE TABLE IF NOT EXISTS `setting`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`created_at` INTEGER NOT NULL DEFAULT 0,
`updated_at` INTEGER NOT NULL DEFAULT 0,
`is_deleted` INTEGER NOT NULL DEFAULT 0,
`name` TEXT NOT NULL,
`description` TEXT NOT NULL DEFAULT "",
`value` TEXT NOT NULL,
UNIQUE (`name`)
);
CREATE TABLE IF NOT EXISTS `audit_log`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`created_at` INTEGER NOT NULL DEFAULT 0,
`updated_at` INTEGER NOT NULL DEFAULT 0,
`is_deleted` INTEGER NOT NULL DEFAULT 0,
`user_id` INTEGER NOT NULL,
`object_type` TEXT NOT NULL,
`object_id` INTEGER NOT NULL,
`action` TEXT NOT NULL,
`meta` TEXT NOT NULL,
FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS `certificate_authority`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`created_at` INTEGER NOT NULL DEFAULT 0,
`updated_at` INTEGER NOT NULL DEFAULT 0,
`is_deleted` INTEGER NOT NULL DEFAULT 0,
`name` TEXT NOT NULL,
`acmesh_server` TEXT NOT NULL DEFAULT "",
`ca_bundle` TEXT NOT NULL DEFAULT "",
`is_wildcard_supported` INTEGER NOT NULL DEFAULT 0, -- specific to each CA, acme v1 doesn't usually have wildcards
`max_domains` INTEGER NOT NULL DEFAULT 5, -- per request
`is_readonly` INTEGER NOT NULL DEFAULT 0
);
CREATE TABLE IF NOT EXISTS `dns_provider`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`created_at` INTEGER NOT NULL DEFAULT 0,
`updated_at` INTEGER NOT NULL DEFAULT 0,
`is_deleted` INTEGER NOT NULL DEFAULT 0,
`user_id` INTEGER NOT NULL,
`name` TEXT NOT NULL,
`acmesh_name` TEXT NOT NULL,
`dns_sleep` INTEGER NOT NULL DEFAULT 0,
`meta` TEXT NOT NULL,
FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS `certificate`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`created_at` INTEGER NOT NULL DEFAULT 0,
`updated_at` INTEGER NOT NULL DEFAULT 0,
`is_deleted` INTEGER NOT NULL DEFAULT 0,
`user_id` INTEGER NOT NULL,
`type` TEXT NOT NULL, -- custom,dns,http
`certificate_authority_id` INTEGER, -- 0 for a custom cert
`dns_provider_id` INTEGER, -- 0, for a http or custom cert
`name` TEXT NOT NULL,
`domain_names` TEXT NOT NULL,
`expires_on` INTEGER NOT NULL DEFAULT 0,
`status` TEXT NOT NULL, -- ready,requesting,failed,provided
`error_message` TEXT NOT NULL DEFAULT "",
`meta` TEXT NOT NULL,
`is_ecc` INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE,
FOREIGN KEY (`certificate_authority_id`) REFERENCES `certificate_authority` (`id`) ON DELETE CASCADE,
FOREIGN KEY (`dns_provider_id`) REFERENCES `dns_provider` (`id`) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS `stream`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`created_at` INTEGER NOT NULL DEFAULT 0,
`updated_at` INTEGER NOT NULL DEFAULT 0,
`is_deleted` INTEGER NOT NULL DEFAULT 0,
`user_id` INTEGER NOT NULL,
`listen_interface` TEXT NOT NULL,
`incoming_port` INTEGER NOT NULL,
`tcp_forwarding` INTEGER NOT NULL DEFAULT 0,
`udp_forwarding` INTEGER NOT NULL DEFAULT 0,
`advanced_config` TEXT NOT NULL,
`is_disabled` INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS `nginx_template`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`created_at` INTEGER NOT NULL DEFAULT 0,
`updated_at` INTEGER NOT NULL DEFAULT 0,
`is_deleted` INTEGER NOT NULL DEFAULT 0,
`user_id` INTEGER NOT NULL,
`name` TEXT NOT NULL,
`type` TEXT NOT NULL,
`template` TEXT NOT NULL,
FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS `upstream`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`created_at` INTEGER NOT NULL DEFAULT 0,
`updated_at` INTEGER NOT NULL DEFAULT 0,
`is_deleted` INTEGER NOT NULL DEFAULT 0,
`user_id` INTEGER NOT NULL,
`name` TEXT NOT NULL,
`nginx_template_id` INTEGER NOT NULL,
`ip_hash` INTEGER NOT NULL DEFAULT 0,
`ntlm` INTEGER NOT NULL DEFAULT 0,
`keepalive` INTEGER NOT NULL DEFAULT 0,
`keepalive_requests` INTEGER NOT NULL DEFAULT 0,
`keepalive_time` TEXT NOT NULL DEFAULT "",
`keepalive_timeout` TEXT NOT NULL DEFAULT "",
`advanced_config` TEXT NOT NULL,
`status` TEXT NOT NULL DEFAULT "",
`error_message` TEXT NOT NULL DEFAULT "",
FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE,
FOREIGN KEY (`nginx_template_id`) REFERENCES `nginx_template` (`id`) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS `upstream_server`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`created_at` INTEGER NOT NULL DEFAULT 0,
`updated_at` INTEGER NOT NULL DEFAULT 0,
`is_deleted` INTEGER NOT NULL DEFAULT 0,
`upstream_id` INTEGER NOT NULL,
`server` TEXT NOT NULL,
`weight` INTEGER NOT NULL DEFAULT 0,
`max_conns` INTEGER NOT NULL DEFAULT 0,
`max_fails` INTEGER NOT NULL DEFAULT 0,
`fail_timeout` INTEGER NOT NULL DEFAULT 0,
`is_backup` INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY (`upstream_id`) REFERENCES `upstream` (`id`) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS `access_list`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`created_at` INTEGER NOT NULL DEFAULT 0,
`updated_at` INTEGER NOT NULL DEFAULT 0,
`is_deleted` INTEGER NOT NULL DEFAULT 0,
`user_id` INTEGER NOT NULL,
`name` TEXT NOT NULL,
`meta` TEXT NOT NULL,
FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS `host`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`created_at` INTEGER NOT NULL DEFAULT 0,
`updated_at` INTEGER NOT NULL DEFAULT 0,
`is_deleted` INTEGER NOT NULL DEFAULT 0,
`user_id` INTEGER NOT NULL,
`type` TEXT NOT NULL,
`nginx_template_id` INTEGER NOT NULL,
`listen_interface` TEXT NOT NULL DEFAULT "",
`domain_names` TEXT NOT NULL,
`upstream_id` INTEGER,
`proxy_scheme` TEXT NOT NULL DEFAULT "",
`proxy_host` TEXT NOT NULL DEFAULT "",
`proxy_port` INTEGER NOT NULL DEFAULT 0,
`certificate_id` INTEGER,
`access_list_id` INTEGER,
`ssl_forced` INTEGER NOT NULL DEFAULT 0,
`caching_enabled` INTEGER NOT NULL DEFAULT 0,
`block_exploits` INTEGER NOT NULL DEFAULT 0,
`allow_websocket_upgrade` INTEGER NOT NULL DEFAULT 0,
`http2_support` INTEGER NOT NULL DEFAULT 0,
`hsts_enabled` INTEGER NOT NULL DEFAULT 0,
`hsts_subdomains` INTEGER NOT NULL DEFAULT 0,
`paths` TEXT NOT NULL DEFAULT "",
`advanced_config` TEXT NOT NULL DEFAULT "",
`status` TEXT NOT NULL DEFAULT "",
`error_message` TEXT NOT NULL DEFAULT "",
`is_disabled` INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE,
FOREIGN KEY (`nginx_template_id`) REFERENCES `nginx_template` (`id`) ON DELETE CASCADE,
FOREIGN KEY (`upstream_id`) REFERENCES `upstream` (`id`) ON DELETE CASCADE,
FOREIGN KEY (`certificate_id`) REFERENCES `certificate` (`id`) ON DELETE CASCADE,
FOREIGN KEY (`access_list_id`) REFERENCES `access_list` (`id`) ON DELETE CASCADE
);
-- migrate:down
-- Not allowed to go down from initial
@@ -1,336 +0,0 @@
-- migrate:up
-- User permissions
INSERT INTO `capability` (
name
) VALUES
("full-admin"),
("access-lists.view"),
("access-lists.manage"),
("audit-log.view"),
("certificates.view"),
("certificates.manage"),
("certificate-authorities.view"),
("certificate-authorities.manage"),
("dns-providers.view"),
("dns-providers.manage"),
("hosts.view"),
("hosts.manage"),
("nginx-templates.view"),
("nginx-templates.manage"),
("settings.manage"),
("streams.view"),
("streams.manage"),
("users.manage");
-- Default site
INSERT INTO `setting` (
created_at,
updated_at,
name,
description,
value
) VALUES (
unixepoch() * 1000,
unixepoch() * 1000,
"default-site",
"What to show users who hit your Nginx server by default",
'"welcome"' -- remember this is json
),
(
unixepoch() * 1000,
unixepoch() * 1000,
"auth-methods",
"Which methods are enabled for authentication",
'["local"]' -- remember this is json
),
(
unixepoch() * 1000,
unixepoch() * 1000,
"oauth-auth",
"Configuration for OAuth authentication",
'{}' -- remember this is json
),
(
unixepoch() * 1000,
unixepoch() * 1000,
"ldap-auth",
"Configuration for LDAP authentication",
'{"host": "", "dn": "", "sync_by": "uid"}' -- remember this is json
);
-- Default Certificate Authorities
INSERT INTO `certificate_authority` (
created_at,
updated_at,
name,
acmesh_server,
is_wildcard_supported,
max_domains,
is_readonly
) VALUES (
unixepoch() * 1000,
unixepoch() * 1000,
"ZeroSSL",
"zerossl",
1,
10,
1
), (
unixepoch() * 1000,
unixepoch() * 1000,
"Let's Encrypt",
"https://acme-v02.api.letsencrypt.org/directory",
1,
10,
1
), (
unixepoch() * 1000,
unixepoch() * 1000,
"Buypass Go SSL",
"https://api.buypass.com/acme/directory",
0,
5,
1
), (
unixepoch() * 1000,
unixepoch() * 1000,
"SSL.com",
"ssl.com",
0,
10,
1
), (
unixepoch() * 1000,
unixepoch() * 1000,
"Let's Encrypt (Testing)",
"https://acme-staging-v02.api.letsencrypt.org/directory",
1,
10,
1
), (
unixepoch() * 1000,
unixepoch() * 1000,
"Buypass Go SSL (Testing)",
"https://api.test4.buypass.no/acme/directory",
0,
5,
1
);
-- System User
INSERT INTO `user` (
created_at,
updated_at,
name,
email,
is_system
) VALUES (
unixepoch() * 1000,
unixepoch() * 1000,
"System",
"system@localhost",
1
);
-- Host Templates
INSERT INTO `nginx_template` (
created_at,
updated_at,
user_id,
name,
type,
template
) VALUES (
unixepoch() * 1000,
unixepoch() * 1000,
(SELECT id FROM user WHERE is_system = 1 LIMIT 1),
"Default Proxy Template",
"proxy",
"# ------------------------------------------------------------
{{#each Host.DomainNames}}
# {{this}}
{{/each}}
# ------------------------------------------------------------
server {
{{#if Config.Ipv4}}
listen 80;
{{/if}}
{{#if Config.Ipv6}}
listen [::]:80;
{{/if}}
{{#if Certificate.ID}}
{{#if Config.Ipv4}}
listen 443 ssl {{#if Host.HTTP2Support}}http2{{/if}};
{{/if}}
{{#if Config.Ipv6}}
listen [::]:443 ssl {{#if Host.HTTP2Support}}http2{{/if}};
{{/if}}
{{/if}}
server_name {{#each Host.DomainNames}}{{this}} {{/each}};
{{#if Certificate.ID}}
include conf.d/include/ssl-ciphers.conf;
{{#if Certificate.IsAcme}}
ssl_certificate {{Certificate.Folder}}/fullchain.pem;
ssl_certificate_key {{Certificate.Folder}}/privkey.pem;
{{else}}
# Custom SSL
ssl_certificate /data/custom_ssl/npm-{{Certicicate.ID}}/fullchain.pem;
ssl_certificate_key /data/custom_ssl/npm-{{Certificate.ID}}/privkey.pem;
{{/if}}
{{/if}}
{{#if Host.CachingEnabled}}
include conf.d/include/assets.conf;
{{/if}}
{{#if Host.BlockExploits}}
include conf.d/include/block-exploits.conf;
{{/if}}
{{#if Certificate.ID}}
{{#if Host.SSLForced}}
{{#if Host.HSTSEnabled}}
# HSTS (ngx_http_headers_module is required) (63072000 seconds = 2 years)
add_header Strict-Transport-Security ""max-age=63072000;{{#if Host.HSTSSubdomains}} includeSubDomains;{{/if}} preload"" always;
{{/if}}
# Force SSL
include conf.d/include/force-ssl.conf;
{{/if}}
{{/if}}
{{#if Host.AllowWebsocketUpgrade}}
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $http_connection;
proxy_http_version 1.1;
{{/if}}
access_log /data/logs/host-{{Host.ID}}_access.log proxy;
error_log /data/logs/host-{{Host.ID}}_error.log warn;
{{Host.AdvancedConfig}}
# locations ?
# default location:
location / {
{{#if Host.AccessListID}}
# Authorization
auth_basic ""Authorization required"";
auth_basic_user_file /data/access/{{Host.AccessListID}};
# access_list.passauth ? todo
{{/if}}
# Access Rules ? todo
# Access checks must...? todo
{{#if Certificate.ID}}
{{#if Host.SSLForced}}
{{#if Host.HSTSEnabled}}
# HSTS (ngx_http_headers_module is required) (63072000 seconds = 2 years)
add_header Strict-Transport-Security ""max-age=63072000;{{#if Host.HSTSSubdomains}} includeSubDomains;{{/if}} preload"" always;
{{/if}}
{{/if}}
{{/if}}
{{#if Host.AllowWebsocketUpgrade}}
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $http_connection;
{{/if}}
# Proxy!
add_header X-Served-By $host;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Scheme $scheme;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_http_version 1.1;
{{#if Upstream.ID}}
# upstream
proxy_pass {{Host.ProxyScheme}}://npm_upstream_{{Upstream.ID}};
{{else}}
# proxy a single host
proxy_pass {{Host.ProxyScheme}}://{{Host.ProxyHost}}:{{Host.ProxyPort}};
{{/if}}
}
# Legacy Custom Configuration
include /data/nginx/custom/server_proxy[.]conf;
}
"
), (
unixepoch() * 1000,
unixepoch() * 1000,
(SELECT id FROM user WHERE is_system = 1 LIMIT 1),
"Default Redirect Template",
"redirect",
"# this is a redirect template"
), (
unixepoch() * 1000,
unixepoch() * 1000,
(SELECT id FROM user WHERE is_system = 1 LIMIT 1),
"Default Dead Template",
"dead",
"# this is a dead template"
), (
unixepoch() * 1000,
unixepoch() * 1000,
(SELECT id FROM user WHERE is_system = 1 LIMIT 1),
"Default Stream Template",
"stream",
"# this is a stream template"
), (
unixepoch() * 1000,
unixepoch() * 1000,
(SELECT id FROM user WHERE is_system = 1 LIMIT 1),
"Default Upstream Template",
"upstream",
"# ------------------------------------------------------------
# Upstream {{Upstream.ID}}: {{Upstream.Name}}
# ------------------------------------------------------------
upstream npm_upstream_{{Upstream.ID}} {
{{#if Upstream.IPHash~}}
ip_hash;
{{~/if}}
{{#if Upstream.NTLM~}}
ntlm;
{{~/if}}
{{#if Upstream.Keepalive~}}
keepalive {{Upstream.Keepalive}};
{{~/if}}
{{#if Upstream.KeepaliveRequests~}}
keepalive_requests {{Upstream.KeepaliveRequests}};
{{~/if}}
{{#if Upstream.KeepaliveTime~}}
keepalive_time {{Upstream.KeepaliveTime}};
{{~/if}}
{{#if Upstream.KeepaliveTimeout~}}
keepalive_timeout {{Upstream.KeepaliveTimeout}};
{{~/if}}
{{Upstream.AdvancedConfig}}
{{#each Upstream.Servers~}}
{{#unless IsDeleted~}}
server {{Server}} {{#if Weight}}weight={{Weight}} {{/if}}{{#if MaxConns}}max_conns={{MaxConns}} {{/if}}{{#if MaxFails}}max_fails={{MaxFails}} {{/if}}{{#if FailTimeout}}fail_timeout={{FailTimeout}} {{/if}}{{#if Backup}}backup{{/if}};
{{/unless}}
{{/each}}
}
"
);
-- migrate:down
+4 -9
View File
@@ -53,8 +53,7 @@ func TestConnectURLs(t *testing.T) {
defer goleak.VerifyNone(t, goleak.IgnoreAnyFunction("database/sql.(*DB).connectionOpener"))
type want struct {
gorm string
dbmate string
gorm string
}
tests := []struct {
@@ -69,8 +68,7 @@ func TestConnectURLs(t *testing.T) {
"NPM_DATA_FOLDER=/path/to/data",
},
want: want{
gorm: "/path/to/data/nginxproxymanager.db",
dbmate: "sqlite:/path/to/data/nginxproxymanager.db",
gorm: "/path/to/data/nginxproxymanager.db",
},
},
{
@@ -85,8 +83,7 @@ func TestConnectURLs(t *testing.T) {
"NPM_DB_NAME=npm",
},
want: want{
gorm: "host=2.2.2.2 user=postgresuser password=pgpass dbname=npm port=9824 sslmode=strict TimeZone=UTC",
dbmate: "postgres://postgresuser:pgpass@2.2.2.2:9824/npm?sslmode=strict",
gorm: "host=2.2.2.2 user=postgresuser password=pgpass dbname=npm port=9824 sslmode=strict TimeZone=UTC",
},
},
{
@@ -100,8 +97,7 @@ func TestConnectURLs(t *testing.T) {
"NPM_DB_NAME=npm",
},
want: want{
gorm: "mysqluser:mypass@tcp(3.3.3.3:3307)/npm?charset=utf8mb4&parseTime=True&loc=Local",
dbmate: "mysql://mysqluser:mypass@3.3.3.3:3307/npm",
gorm: "mysqluser:mypass@tcp(3.3.3.3:3307)/npm?charset=utf8mb4&parseTime=True&loc=Local",
},
},
}
@@ -119,7 +115,6 @@ func TestConnectURLs(t *testing.T) {
}
Init(&version, &commit)
assert.Equal(t, tt.want.gorm, Configuration.DB.GetGormConnectURL())
assert.Equal(t, tt.want.dbmate, Configuration.DB.GetDBMateConnectURL())
})
}
}
-26
View File
@@ -83,29 +83,3 @@ func (d *db) GetGormConnectURL() string {
}
return ""
}
// GetDBMateConnectURL is used by Dbmate
func (d *db) GetDBMateConnectURL() string {
switch d.GetDriver() {
case DatabaseSqlite:
return fmt.Sprintf("sqlite:%s/nginxproxymanager.db", Configuration.DataFolder)
case DatabasePostgres:
return fmt.Sprintf("postgres://%s:%s@%s:%d/%s?sslmode=%s",
d.Username,
d.Password,
d.Host,
d.Port,
d.Name,
d.SSLMode,
)
case DatabaseMysql:
return fmt.Sprintf("mysql://%s:%s@%s:%d/%s",
d.Username,
d.Password,
d.Host,
d.Port,
d.Name,
)
}
return ""
}
-48
View File
@@ -1,48 +0,0 @@
package database
import (
"fmt"
"net/url"
"npm/embed"
"npm/internal/config"
"npm/internal/logger"
"github.com/amacneil/dbmate/v2/pkg/dbmate"
// Drivers:
_ "github.com/amacneil/dbmate/v2/pkg/driver/mysql"
_ "github.com/amacneil/dbmate/v2/pkg/driver/postgres"
_ "github.com/amacneil/dbmate/v2/pkg/driver/sqlite"
)
type afterMigrationComplete func()
// Migrate will bring the db up to date
func Migrate(followup afterMigrationComplete) bool {
dbURL := config.Configuration.DB.GetDBMateConnectURL()
u, _ := url.Parse(dbURL)
db := dbmate.New(u)
db.AutoDumpSchema = false
db.FS = embed.MigrationFiles
db.MigrationsDir = []string{fmt.Sprintf("./migrations/%s", config.Configuration.DB.GetDriver())}
migrations, err := db.FindMigrations()
if err != nil {
logger.Error("MigrationError", err)
return false
}
for _, m := range migrations {
logger.Debug("%s: %s", m.Version, m.FilePath)
}
err = db.CreateAndMigrate()
if err != nil {
logger.Error("MigrationError", err)
return false
}
followup()
return true
}
+347
View File
@@ -0,0 +1,347 @@
package migrations
import (
"time"
"npm/internal/entity"
"npm/internal/entity/certificateauthority"
"npm/internal/entity/nginxtemplate"
"npm/internal/entity/setting"
"npm/internal/entity/user"
"npm/internal/model"
"github.com/go-gormigrate/gormigrate/v2"
"gorm.io/datatypes"
"gorm.io/gorm"
)
const proxyTemplate = `# ------------------------------------------------------------
{{#each Host.DomainNames}}
# {{this}}
{{/each}}
# ------------------------------------------------------------
server {
{{#if Config.Ipv4}}
listen 80;
{{/if}}
{{#if Config.Ipv6}}
listen [::]:80;
{{/if}}
{{#if Certificate.ID}}
{{#if Config.Ipv4}}
listen 443 ssl {{#if Host.HTTP2Support}}http2{{/if}};
{{/if}}
{{#if Config.Ipv6}}
listen [::]:443 ssl {{#if Host.HTTP2Support}}http2{{/if}};
{{/if}}
{{/if}}
server_name {{#each Host.DomainNames}}{{this}} {{/each}};
{{#if Certificate.ID}}
include conf.d/include/ssl-ciphers.conf;
{{#if Certificate.IsAcme}}
ssl_certificate {{Certificate.Folder}}/fullchain.pem;
ssl_certificate_key {{Certificate.Folder}}/privkey.pem;
{{else}}
# Custom SSL
ssl_certificate /data/custom_ssl/npm-{{Certicicate.ID}}/fullchain.pem;
ssl_certificate_key /data/custom_ssl/npm-{{Certificate.ID}}/privkey.pem;
{{/if}}
{{/if}}
{{#if Host.CachingEnabled}}
include conf.d/include/assets.conf;
{{/if}}
{{#if Host.BlockExploits}}
include conf.d/include/block-exploits.conf;
{{/if}}
{{#if Certificate.ID}}
{{#if Host.SSLForced}}
{{#if Host.HSTSEnabled}}
# HSTS (ngx_http_headers_module is required) (63072000 seconds = 2 years)
add_header Strict-Transport-Security "max-age=63072000;{{#if Host.HSTSSubdomains}} includeSubDomains;{{/if}} preload" always;
{{/if}}
# Force SSL
include conf.d/include/force-ssl.conf;
{{/if}}
{{/if}}
{{#if Host.AllowWebsocketUpgrade}}
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $http_connection;
proxy_http_version 1.1;
{{/if}}
access_log /data/logs/host-{{Host.ID}}_access.log proxy;
error_log /data/logs/host-{{Host.ID}}_error.log warn;
{{Host.AdvancedConfig}}
# locations ?
# default location:
location / {
{{#if Host.AccessListID}}
# Authorization
auth_basic "Authorization required";
auth_basic_user_file /data/access/{{Host.AccessListID}};
# access_list.passauth ? todo
{{/if}}
# Access Rules ? todo
# Access checks must...? todo
{{#if Certificate.ID}}
{{#if Host.SSLForced}}
{{#if Host.HSTSEnabled}}
# HSTS (ngx_http_headers_module is required) (63072000 seconds = 2 years)
add_header Strict-Transport-Security "max-age=63072000;{{#if Host.HSTSSubdomains}} includeSubDomains;{{/if}} preload" always;
{{/if}}
{{/if}}
{{/if}}
{{#if Host.AllowWebsocketUpgrade}}
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $http_connection;
{{/if}}
# Proxy!
add_header X-Served-By $host;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Scheme $scheme;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_http_version 1.1;
{{#if Upstream.ID}}
# upstream
proxy_pass {{Host.ProxyScheme}}://npm_upstream_{{Upstream.ID}};
{{else}}
# proxy a single host
proxy_pass {{Host.ProxyScheme}}://{{Host.ProxyHost}}:{{Host.ProxyPort}};
{{/if}}
}
# Legacy Custom Configuration
include /data/nginx/custom/server_proxy[.]conf;
}
`
const upstreamTemplate = `# ------------------------------------------------------------
# Upstream {{Upstream.ID}}: {{Upstream.Name}}
# ------------------------------------------------------------
upstream npm_upstream_{{Upstream.ID}} {
{{#if Upstream.IPHash~}}
ip_hash;
{{~/if}}
{{#if Upstream.NTLM~}}
ntlm;
{{~/if}}
{{#if Upstream.Keepalive~}}
keepalive {{Upstream.Keepalive}};
{{~/if}}
{{#if Upstream.KeepaliveRequests~}}
keepalive_requests {{Upstream.KeepaliveRequests}};
{{~/if}}
{{#if Upstream.KeepaliveTime~}}
keepalive_time {{Upstream.KeepaliveTime}};
{{~/if}}
{{#if Upstream.KeepaliveTimeout~}}
keepalive_timeout {{Upstream.KeepaliveTimeout}};
{{~/if}}
{{Upstream.AdvancedConfig}}
{{#each Upstream.Servers~}}
{{#unless IsDeleted~}}
server {{Server}} {{#if Weight}}weight={{Weight}} {{/if}}{{#if MaxConns}}max_conns={{MaxConns}} {{/if}}{{#if MaxFails}}max_fails={{MaxFails}} {{/if}}{{#if FailTimeout}}fail_timeout={{FailTimeout}} {{/if}}{{#if Backup}}backup{{/if}};
{{/unless}}
{{/each}}
}
`
// initialData seeds the tables created by initialSchema with the same
// default rows that the old per-engine dbmate migrations inserted:
// capabilities, default settings, built-in certificate authorities, the
// system user, and the built-in nginx templates.
func initialData() *gormigrate.Migration {
return &gormigrate.Migration{
ID: "20201013035839",
Migrate: func(tx *gorm.DB) error {
now := time.Now().UnixMilli()
base := func() model.Base {
return model.Base{CreatedAt: now, UpdatedAt: now}
}
capabilities := []entity.Capability{
{Name: user.CapabilityFullAdmin},
{Name: user.CapabilityAccessListsView},
{Name: user.CapabilityAccessListsManage},
{Name: user.CapabilityAuditLogView},
{Name: user.CapabilityCertificatesView},
{Name: user.CapabilityCertificatesManage},
{Name: user.CapabilityCertificateAuthoritiesView},
{Name: user.CapabilityCertificateAuthoritiesManage},
{Name: user.CapabilityDNSProvidersView},
{Name: user.CapabilityDNSProvidersManage},
{Name: user.CapabilityHostsView},
{Name: user.CapabilityHostsManage},
{Name: user.CapabilityNginxTemplatesView},
{Name: user.CapabilityNginxTemplatesManage},
{Name: user.CapabilitySettingsManage},
{Name: user.CapabilityStreamsView},
{Name: user.CapabilityStreamsManage},
{Name: user.CapabilityUsersManage},
}
if err := tx.Create(&capabilities).Error; err != nil {
return err
}
settings := []setting.Model{
{
Base: base(),
Name: "default-site",
Description: "What to show users who hit your Nginx server by default",
Value: datatypes.JSON(`"welcome"`),
},
{
Base: base(),
Name: "auth-methods",
Description: "Which methods are enabled for authentication",
Value: datatypes.JSON(`["local"]`),
},
{
Base: base(),
Name: "oauth-auth",
Description: "Configuration for OAuth authentication",
Value: datatypes.JSON(`{}`),
},
{
Base: base(),
Name: "ldap-auth",
Description: "Configuration for LDAP authentication",
Value: datatypes.JSON(`{"host": "", "dn": "", "sync_by": "uid"}`),
},
}
if err := tx.Create(&settings).Error; err != nil {
return err
}
certificateAuthorities := []certificateauthority.Model{
{
Base: base(),
Name: "ZeroSSL",
AcmeshServer: "zerossl",
IsWildcardSupported: true,
MaxDomains: 10,
IsReadonly: true,
},
{
Base: base(),
Name: "Let's Encrypt",
AcmeshServer: "https://acme-v02.api.letsencrypt.org/directory",
IsWildcardSupported: true,
MaxDomains: 10,
IsReadonly: true,
},
{
Base: base(),
Name: "Buypass Go SSL",
AcmeshServer: "https://api.buypass.com/acme/directory",
IsWildcardSupported: false,
MaxDomains: 5,
IsReadonly: true,
},
{
Base: base(),
Name: "SSL.com",
AcmeshServer: "ssl.com",
IsWildcardSupported: false,
MaxDomains: 10,
IsReadonly: true,
},
{
Base: base(),
Name: "Let's Encrypt (Testing)",
AcmeshServer: "https://acme-staging-v02.api.letsencrypt.org/directory",
IsWildcardSupported: true,
MaxDomains: 10,
IsReadonly: true,
},
{
Base: base(),
Name: "Buypass Go SSL (Testing)",
AcmeshServer: "https://api.test4.buypass.no/acme/directory",
IsWildcardSupported: false,
MaxDomains: 5,
IsReadonly: true,
},
}
if err := tx.Create(&certificateAuthorities).Error; err != nil {
return err
}
systemUser := user.Model{
Base: base(),
Name: "System",
Email: "system@localhost",
IsSystem: true,
}
if err := tx.Create(&systemUser).Error; err != nil {
return err
}
templates := []nginxtemplate.Model{
{
Base: base(),
UserID: systemUser.ID,
Name: "Default Proxy Template",
Type: "proxy",
Template: proxyTemplate,
},
{
Base: base(),
UserID: systemUser.ID,
Name: "Default Redirect Template",
Type: "redirect",
Template: "# this is a redirect template",
},
{
Base: base(),
UserID: systemUser.ID,
Name: "Default Dead Template",
Type: "dead",
Template: "# this is a dead template",
},
{
Base: base(),
UserID: systemUser.ID,
Name: "Default Stream Template",
Type: "stream",
Template: "# this is a stream template",
},
{
Base: base(),
UserID: systemUser.ID,
Name: "Default Upstream Template",
Type: "upstream",
Template: upstreamTemplate,
},
}
return tx.Create(&templates).Error
},
}
}
+107
View File
@@ -0,0 +1,107 @@
// Package migrations owns the application's database schema. It replaces
// the previous dbmate-based setup: instead of hand-written, per-engine SQL
// files, each migration is a small Go function driven by gorm, so a single
// migration works against Postgres, MySQL and SQLite alike.
package migrations
import (
"npm/internal/database"
"npm/internal/logger"
"github.com/go-gormigrate/gormigrate/v2"
"gorm.io/gorm"
)
// legacyMigrationsTable is the table dbmate used to track applied
// migrations. Its presence indicates a database that was created before
// the switch to gormigrate.
const legacyMigrationsTable = "schema_migrations"
// legacyMigrationIDs maps the dbmate migration versions this application
// used to ship to the gormigrate migrations that replace them, in order.
// Existing installs already have this schema and data applied via dbmate,
// so on first boot after upgrading we mark them as already run rather
// than re-running them.
var legacyMigrationIDs = []string{
"20201013035318", // initial_schema
"20201013035839", // initial_data
}
// all is the ordered list of every migration this application has ever
// shipped. Entries must never be reordered or edited once released;
// schema changes are made by appending a new migration.
func all() []*gormigrate.Migration {
return []*gormigrate.Migration{
initialSchema(),
initialData(),
}
}
type afterMigrationComplete func()
// Migrate will bring the db up to date
func Migrate(followup afterMigrationComplete) bool {
db := database.GetDB()
if db == nil {
return false
}
if err := bootstrapFromLegacyDBMate(db); err != nil {
logger.Error("MigrationError", err)
return false
}
m := gormigrate.New(db, gormigrate.DefaultOptions, all())
if err := m.Migrate(); err != nil {
logger.Error("MigrationError", err)
return false
}
logger.Debug("Database is up to date")
followup()
return true
}
// bootstrapFromLegacyDBMate detects a database that was migrated by the
// old dbmate setup and pre-marks the gormigrate migrations that replace
// dbmate's initial_schema/initial_data as already applied, so gormigrate
// doesn't try to recreate tables or re-insert seed data that already
// exist. It's a no-op for a brand new database and for one that has
// already been bootstrapped.
func bootstrapFromLegacyDBMate(db *gorm.DB) error {
if !db.Migrator().HasTable(legacyMigrationsTable) {
// Fresh install, nothing to bootstrap.
return nil
}
var legacyCount int64
if err := db.Table(legacyMigrationsTable).Count(&legacyCount).Error; err != nil {
return err
}
if legacyCount == 0 {
return nil
}
if db.Migrator().HasTable(gormigrate.DefaultOptions.TableName) {
// Already bootstrapped (or running fresh under gormigrate already).
return nil
}
logger.Info("Existing dbmate-managed database detected, marking bootstrap migrations as applied")
type migrationRecord struct {
ID string `gorm:"primaryKey;column:id;size:255"`
}
if err := db.Table(gormigrate.DefaultOptions.TableName).AutoMigrate(&migrationRecord{}); err != nil {
return err
}
for _, id := range legacyMigrationIDs {
if err := db.Table(gormigrate.DefaultOptions.TableName).Create(&migrationRecord{ID: id}).Error; err != nil {
return err
}
}
return nil
}
@@ -0,0 +1,111 @@
package migrations
import (
"path/filepath"
"testing"
"npm/internal/database"
"npm/internal/entity"
"npm/internal/entity/certificateauthority"
"npm/internal/entity/nginxtemplate"
"npm/internal/entity/setting"
"npm/internal/entity/user"
"github.com/glebarez/sqlite"
"github.com/go-gormigrate/gormigrate/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gorm.io/gorm"
)
func openTestDB(t *testing.T) *gorm.DB {
t.Helper()
dsn := filepath.Join(t.TempDir(), "test.db")
db, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{})
require.NoError(t, err)
return db
}
func TestMigrateFreshDatabase(t *testing.T) {
db := openTestDB(t)
database.SetDB(db)
followupCalled := false
ok := Migrate(func() { followupCalled = true })
require.True(t, ok)
assert.True(t, followupCalled)
var capabilityCount int64
require.NoError(t, db.Model(&entity.Capability{}).Count(&capabilityCount).Error)
assert.EqualValues(t, 18, capabilityCount)
var settingCount int64
require.NoError(t, db.Model(&setting.Model{}).Count(&settingCount).Error)
assert.EqualValues(t, 4, settingCount)
var caCount int64
require.NoError(t, db.Model(&certificateauthority.Model{}).Count(&caCount).Error)
assert.EqualValues(t, 6, caCount)
var userCount int64
require.NoError(t, db.Model(&user.Model{}).Where("is_system = ?", true).Count(&userCount).Error)
assert.EqualValues(t, 1, userCount)
var templateCount int64
require.NoError(t, db.Model(&nginxtemplate.Model{}).Count(&templateCount).Error)
assert.EqualValues(t, 5, templateCount)
var migrationCount int64
require.NoError(t, db.Table(gormigrate.DefaultOptions.TableName).Count(&migrationCount).Error)
assert.EqualValues(t, len(all()), migrationCount)
}
func TestMigrateIsIdempotent(t *testing.T) {
db := openTestDB(t)
database.SetDB(db)
require.True(t, Migrate(func() {}))
require.True(t, Migrate(func() {}))
var capabilityCount int64
require.NoError(t, db.Model(&entity.Capability{}).Count(&capabilityCount).Error)
assert.EqualValues(t, 18, capabilityCount)
var userCount int64
require.NoError(t, db.Model(&user.Model{}).Count(&userCount).Error)
assert.EqualValues(t, 1, userCount)
}
// TestBootstrapFromLegacyDBMate simulates a database that was previously
// migrated by dbmate: it has dbmate's schema_migrations table with the two
// legacy versions recorded, and already contains seeded data. Migrate must
// detect this and mark the equivalent gormigrate migrations as already
// applied instead of re-running them and duplicating the seed data.
func TestBootstrapFromLegacyDBMate(t *testing.T) {
db := openTestDB(t)
database.SetDB(db)
require.NoError(t, db.Exec(`CREATE TABLE schema_migrations (version TEXT PRIMARY KEY)`).Error)
for _, v := range legacyMigrationIDs {
require.NoError(t, db.Exec(`INSERT INTO schema_migrations (version) VALUES (?)`, v).Error)
}
require.NoError(t, db.Exec(`CREATE TABLE capability (name TEXT PRIMARY KEY)`).Error)
require.NoError(t, db.Exec(`INSERT INTO capability (name) VALUES (?)`, "full-admin").Error)
require.True(t, Migrate(func() {}))
var capabilityCount int64
require.NoError(t, db.Table("capability").Count(&capabilityCount).Error)
assert.EqualValues(t, 1, capabilityCount, "legacy data must not be re-seeded")
var migrationIDs []string
require.NoError(t, db.Table(gormigrate.DefaultOptions.TableName).Pluck("id", &migrationIDs).Error)
assert.ElementsMatch(t, legacyMigrationIDs, migrationIDs)
}
func TestMigrateNoDB(t *testing.T) {
database.SetDB(nil)
assert.False(t, Migrate(func() {
t.Error("followup should not be called")
}))
}
+68
View File
@@ -0,0 +1,68 @@
package migrations
import (
"npm/internal/entity"
"npm/internal/entity/accesslist"
"npm/internal/entity/auth"
"npm/internal/entity/certificate"
"npm/internal/entity/certificateauthority"
"npm/internal/entity/dnsprovider"
"npm/internal/entity/host"
"npm/internal/entity/nginxtemplate"
"npm/internal/entity/setting"
"npm/internal/entity/stream"
"npm/internal/entity/upstream"
"npm/internal/entity/upstreamserver"
"npm/internal/entity/user"
"npm/internal/jwt"
"npm/internal/model"
"github.com/go-gormigrate/gormigrate/v2"
"gorm.io/gorm"
)
// auditLogSchema mirrors the audit_log table. There is no application
// entity for it yet, so its schema is only declared here for AutoMigrate.
type auditLogSchema struct {
model.Base
UserID uint `gorm:"column:user_id"`
ObjectType string `gorm:"column:object_type"`
ObjectID uint `gorm:"column:object_id"`
Action string `gorm:"column:action"`
Meta string `gorm:"column:meta"`
}
// TableName overrides the table name used by gorm
func (auditLogSchema) TableName() string {
return "audit_log"
}
// initialSchema creates every table used by the application. It replaces
// the hand-written, per-engine SQL that dbmate used to run: gorm's dialect
// drivers already know how to translate these structs for Postgres, MySQL
// and SQLite, so there's no need to maintain three copies of the same DDL.
func initialSchema() *gormigrate.Migration {
return &gormigrate.Migration{
ID: "20201013035318",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(
&jwt.KeysModel{},
&user.Model{},
&entity.Capability{},
&user.HasCapabilityModel{},
&auth.Model{},
&setting.Model{},
&auditLogSchema{},
&certificateauthority.Model{},
&dnsprovider.Model{},
&certificate.Model{},
&stream.Model{},
&nginxtemplate.Model{},
&upstream.Model{},
&upstreamserver.Model{},
&accesslist.Model{},
&host.Model{},
)
},
}
}