mirror of
https://github.com/NginxProxyManager/nginx-proxy-manager.git
synced 2025-06-18 10:06:26 +00:00
Updated deps, go.19 migration, nginx template work
This commit is contained in:
@ -16,9 +16,8 @@ func ConfigureHost(h host.Model) error {
|
||||
data := TemplateData{
|
||||
ConfDir: fmt.Sprintf("%s/nginx/hosts", config.Configuration.DataFolder),
|
||||
DataDir: config.Configuration.DataFolder,
|
||||
CertsDir: config.Configuration.Acmesh.CertHome,
|
||||
Host: &h,
|
||||
Certificate: h.Certificate,
|
||||
Host: h.GetTemplate(),
|
||||
Certificate: h.Certificate.GetTemplate(),
|
||||
}
|
||||
|
||||
filename := fmt.Sprintf("%s/host_%d.conf", data.ConfDir, h.ID)
|
||||
|
@ -15,22 +15,16 @@ func TestWriteTemplate(t *testing.T) {
|
||||
# Host is disabled
|
||||
{{else}}
|
||||
server {
|
||||
{{#if Certificate}}
|
||||
{{#if Certificate.CertificateAuthorityID}}
|
||||
# Acme SSL
|
||||
include {{ConfDir}}/npm/conf.d/acme-challenge.conf;
|
||||
include {{ConfDir}}/npm/conf.d/include/ssl-ciphers.conf;
|
||||
ssl_certificate {{CertsDir}}/npm-{{Certificate.ID}}/fullchain.pem;
|
||||
ssl_certificate_key {{CertsDir}}/npm-{{Certificate.ID}}/privkey.pem;
|
||||
{{else}}
|
||||
# Custom SSL
|
||||
ssl_certificate {{DataDir}}/custom_ssl/npm-{{Certificate.ID}}/fullchain.pem;
|
||||
ssl_certificate_key {{DataDir}}/custom_ssl/npm-{{Certificate.ID}}/privkey.pem;
|
||||
{{#if Certificate.IsProvided}}
|
||||
{{#if Certificate.IsAcme}}
|
||||
include {{ConfDir}}/npm/conf.d/acme-challenge.conf;
|
||||
include {{ConfDir}}/npm/conf.d/include/ssl-ciphers.conf;
|
||||
{{/if}}
|
||||
ssl_certificate {{Certificate.Folder}}/fullchain.pem;
|
||||
ssl_certificate_key {{Certificate.Folder}}/privkey.pem;
|
||||
{{/if}}
|
||||
}
|
||||
{{/if}}
|
||||
|
||||
`
|
||||
|
||||
type want struct {
|
||||
@ -41,36 +35,49 @@ server {
|
||||
tests := []struct {
|
||||
name string
|
||||
data TemplateData
|
||||
host host.Model
|
||||
cert certificate.Model
|
||||
want want
|
||||
}{
|
||||
{
|
||||
name: "Basic Template enabled",
|
||||
data: TemplateData{
|
||||
ConfDir: "/etc/nginx/conf.d",
|
||||
Host: &host.Model{
|
||||
IsDisabled: false,
|
||||
},
|
||||
Certificate: &certificate.Model{
|
||||
CertificateAuthorityID: 0,
|
||||
},
|
||||
host: host.Model{
|
||||
IsDisabled: false,
|
||||
},
|
||||
cert: certificate.Model{
|
||||
ID: 77,
|
||||
Status: certificate.StatusProvided,
|
||||
Type: certificate.TypeHTTP,
|
||||
CertificateAuthorityID: 99,
|
||||
},
|
||||
want: want{
|
||||
output: "\nserver {\n # Custom SSL\n ssl_certificate /custom_ssl/npm-0/fullchain.pem;\n ssl_certificate_key /custom_ssl/npm-0/privkey.pem;\n \n}\n\n",
|
||||
output: "\nserver {\n include /etc/nginx/conf.d/npm/conf.d/acme-challenge.conf;\n include /etc/nginx/conf.d/npm/conf.d/include/ssl-ciphers.conf;\n ssl_certificate /npm-77/fullchain.pem;\n ssl_certificate_key /npm-77/privkey.pem;\n}\n",
|
||||
err: nil,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Basic Template custom ssl",
|
||||
host: host.Model{
|
||||
IsDisabled: false,
|
||||
},
|
||||
cert: certificate.Model{
|
||||
ID: 66,
|
||||
Status: certificate.StatusProvided,
|
||||
Type: certificate.TypeCustom,
|
||||
},
|
||||
want: want{
|
||||
output: "\nserver {\n ssl_certificate /custom_ssl/npm-66/fullchain.pem;\n ssl_certificate_key /custom_ssl/npm-66/privkey.pem;\n}\n",
|
||||
err: nil,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Basic Template disabled",
|
||||
data: TemplateData{
|
||||
ConfDir: "/etc/nginx/conf.d",
|
||||
DataDir: "/data",
|
||||
CertsDir: "/acme.sh/certs",
|
||||
Host: &host.Model{
|
||||
IsDisabled: true,
|
||||
},
|
||||
host: host.Model{
|
||||
IsDisabled: true,
|
||||
},
|
||||
cert: certificate.Model{},
|
||||
want: want{
|
||||
output: "\n # Host is disabled\n\n",
|
||||
output: "\n # Host is disabled\n",
|
||||
err: nil,
|
||||
},
|
||||
},
|
||||
@ -78,7 +85,14 @@ server {
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(st *testing.T) {
|
||||
output, err := generateHostConfig(template, test.data)
|
||||
templateData := TemplateData{
|
||||
ConfDir: "/etc/nginx/conf.d",
|
||||
DataDir: "/data",
|
||||
Host: test.host.GetTemplate(),
|
||||
Certificate: test.cert.GetTemplate(),
|
||||
}
|
||||
|
||||
output, err := generateHostConfig(template, templateData)
|
||||
assert.Equal(t, test.want.err, err)
|
||||
assert.Equal(t, test.want.output, output)
|
||||
})
|
||||
|
@ -2,7 +2,7 @@ package nginx
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
"npm/internal/entity/certificate"
|
||||
"npm/internal/entity/host"
|
||||
@ -11,13 +11,12 @@ import (
|
||||
"github.com/aymerick/raymond"
|
||||
)
|
||||
|
||||
// TemplateData ...
|
||||
// TemplateData is a struct
|
||||
type TemplateData struct {
|
||||
ConfDir string
|
||||
DataDir string
|
||||
CertsDir string
|
||||
Host *host.Model
|
||||
Certificate *certificate.Model
|
||||
Host host.Template
|
||||
Certificate certificate.Template
|
||||
}
|
||||
|
||||
func generateHostConfig(template string, data TemplateData) (string, error) {
|
||||
@ -42,5 +41,5 @@ func writeTemplate(filename, template string, data TemplateData) error {
|
||||
func writeConfigFile(filename, content string) error {
|
||||
logger.Debug("Writing %s with:\n%s", filename, content)
|
||||
// nolint: gosec
|
||||
return ioutil.WriteFile(filename, []byte(content), 0644)
|
||||
return os.WriteFile(filename, []byte(content), 0644)
|
||||
}
|
||||
|
Reference in New Issue
Block a user