More work to support nullable foreign keys

Adds nullable int/uint types
This commit is contained in:
Jamie Curnow
2023-05-30 22:26:44 +10:00
parent 88f3953f92
commit 4a57956093
14 changed files with 387 additions and 256 deletions

View File

@ -148,7 +148,7 @@ func getCertificateFromRequest(w http.ResponseWriter, r *http.Request) *certific
return nil
}
// getCertificateFromRequest has some reusable code for all endpoints that
// fillObjectFromBody has some reusable code for all endpoints that
// have a certificate id in the url. it will write errors to the output.
func fillObjectFromBody(w http.ResponseWriter, r *http.Request, validationSchema string, o interface{}) bool {
bodyBytes, _ := r.Context().Value(c.BodyCtxKey).([]byte)
@ -167,6 +167,7 @@ func fillObjectFromBody(w http.ResponseWriter, r *http.Request, validationSchema
err := json.Unmarshal(bodyBytes, o)
if err != nil {
logger.Debug("Unmarshal Error: %+v", err)
h.ResultErrorJSON(w, r, http.StatusBadRequest, h.ErrInvalidPayload.Error(), nil)
return false
}

View File

@ -87,7 +87,7 @@ func CreateHost() func(http.ResponseWriter, *http.Request) {
return
}
if newHost.UpstreamID > 0 {
if newHost.UpstreamID.Uint > 0 {
// nolint: errcheck, gosec
newHost.Expand([]string{"upstream"})
}

View File

@ -56,27 +56,19 @@ func connect() (*gorm.DB, error) {
return nil, eris.New(fmt.Sprintf("Database driver %s is not supported. Valid options are: %s, %s or %s", config.Configuration.DB.Driver, config.DatabaseSqlite, config.DatabasePostgres, config.DatabaseMysql))
}
return gorm.Open(d, &gorm.Config{
// see: https://gorm.io/docs/gorm_config.html
// see: https://gorm.io/docs/gorm_config.html
cfg := gorm.Config{
NamingStrategy: schema.NamingStrategy{
SingularTable: true,
NoLowerCase: true,
},
PrepareStmt: false,
Logger: gormlogger.Default.LogMode(gormlogger.Silent),
})
}
/*
func autocreate(dbFile string) {
if _, err := os.Stat(dbFile); os.IsNotExist(err) {
// Create it
logger.Info("Creating Sqlite DB: %s", dbFile)
// nolint: gosec
_, err = os.Create(dbFile)
if err != nil {
logger.Error("FileCreateError", err)
}
}
// Silence gorm query errors unless when not in debug mode
if config.GetLogLevel() != logger.DebugLevel {
cfg.Logger = gormlogger.Default.LogMode(gormlogger.Silent)
}
return gorm.Open(d, &cfg)
}
*/

View File

@ -45,13 +45,13 @@ const (
// Model is the model
type Model struct {
entity.ModelBase
ExpiresOn types.NullableDBDate `json:"expires_on" gorm:"column:expires_on" filter:"expires_on,integer"`
Type string `json:"type" gorm:"column:type" filter:"type,string"`
UserID uint `json:"user_id" gorm:"column:user_id" filter:"user_id,integer"`
CertificateAuthorityID uint `json:"certificate_authority_id" gorm:"column:certificate_authority_id" filter:"certificate_authority_id,integer"`
DNSProviderID uint `json:"dns_provider_id" gorm:"column:dns_provider_id" filter:"dns_provider_id,integer"`
Type string `json:"type" gorm:"column:type" filter:"type,string"`
CertificateAuthorityID types.NullableDBUint `json:"certificate_authority_id" gorm:"column:certificate_authority_id" filter:"certificate_authority_id,integer"`
DNSProviderID types.NullableDBUint `json:"dns_provider_id" gorm:"column:dns_provider_id" filter:"dns_provider_id,integer"`
Name string `json:"name" gorm:"column:name" filter:"name,string"`
DomainNames types.JSONB `json:"domain_names" gorm:"column:domain_names" filter:"domain_names,string"`
ExpiresOn int64 `json:"expires_on" gorm:"column:expires_on" filter:"expires_on,integer"`
Status string `json:"status" gorm:"column:status" filter:"status,string"`
ErrorMessage string `json:"error_message" gorm:"column:error_message" filter:"error_message,string"`
Meta types.JSONB `json:"-" gorm:"column:meta"`
@ -117,13 +117,13 @@ func (m *Model) Validate() bool {
switch m.Type {
case TypeCustom:
// TODO: make sure meta contains required fields
return m.DNSProviderID == 0 && m.CertificateAuthorityID == 0
return m.DNSProviderID.Uint == 0 && m.CertificateAuthorityID.Uint == 0
case TypeHTTP:
return m.DNSProviderID == 0 && m.CertificateAuthorityID > 0
return m.DNSProviderID.Uint == 0 && m.CertificateAuthorityID.Uint > 0
case TypeDNS:
return m.DNSProviderID > 0 && m.CertificateAuthorityID > 0
return m.DNSProviderID.Uint > 0 && m.CertificateAuthorityID.Uint > 0
case TypeMkcert:
return true
@ -175,15 +175,15 @@ func (m *Model) setDefaultStatus() {
func (m *Model) Expand(items []string) error {
var err error
if util.SliceContainsItem(items, "certificate-authority") && m.CertificateAuthorityID > 0 {
if util.SliceContainsItem(items, "certificate-authority") && m.CertificateAuthorityID.Uint > 0 {
var certificateAuthority certificateauthority.Model
certificateAuthority, err = certificateauthority.GetByID(m.CertificateAuthorityID)
certificateAuthority, err = certificateauthority.GetByID(m.CertificateAuthorityID.Uint)
m.CertificateAuthority = &certificateAuthority
}
if util.SliceContainsItem(items, "dns-provider") && m.DNSProviderID > 0 {
if util.SliceContainsItem(items, "dns-provider") && m.DNSProviderID.Uint > 0 {
var dnsProvider dnsprovider.Model
dnsProvider, err = dnsprovider.GetByID(m.DNSProviderID)
dnsProvider, err = dnsprovider.GetByID(m.DNSProviderID.Uint)
m.DNSProvider = &dnsProvider
}
@ -262,8 +262,7 @@ func (m *Model) Request() error {
// If done
m.Status = StatusProvided
t := time.Now()
m.ExpiresOn.Time = &t // todo
m.ExpiresOn = time.Now().UnixMilli()
if err := m.Save(); err != nil {
logger.Error("CertificateSaveError", err)
return err
@ -287,11 +286,11 @@ func (m *Model) GetTemplate() Template {
ID: m.ID,
CreatedAt: fmt.Sprintf("%d", m.CreatedAt), // todo: nice date string
UpdatedAt: fmt.Sprintf("%d", m.UpdatedAt), // todo: nice date string
ExpiresOn: m.ExpiresOn.AsString(),
ExpiresOn: util.UnixMilliToNiceFormat(m.ExpiresOn),
Type: m.Type,
UserID: m.UserID,
CertificateAuthorityID: m.CertificateAuthorityID,
DNSProviderID: m.DNSProviderID,
CertificateAuthorityID: m.CertificateAuthorityID.Uint,
DNSProviderID: m.DNSProviderID.Uint,
Name: m.Name,
DomainNames: domainNames,
Status: m.Status,

View File

@ -27,29 +27,29 @@ const (
// Model is the model
type Model struct {
entity.ModelBase
UserID uint `json:"user_id" gorm:"column:user_id" filter:"user_id,integer"`
Type string `json:"type" gorm:"column:type" filter:"type,string"`
NginxTemplateID uint `json:"nginx_template_id" gorm:"column:nginx_template_id" filter:"nginx_template_id,integer"`
ListenInterface string `json:"listen_interface" gorm:"column:listen_interface" filter:"listen_interface,string"`
DomainNames types.JSONB `json:"domain_names" gorm:"column:domain_names" filter:"domain_names,string"`
UpstreamID uint `json:"upstream_id" gorm:"column:upstream_id" filter:"upstream_id,integer"`
ProxyScheme string `json:"proxy_scheme" gorm:"column:proxy_scheme" filter:"proxy_scheme,string"`
ProxyHost string `json:"proxy_host" gorm:"column:proxy_host" filter:"proxy_host,string"`
ProxyPort int `json:"proxy_port" gorm:"column:proxy_port" filter:"proxy_port,integer"`
CertificateID uint `json:"certificate_id" gorm:"column:certificate_id" filter:"certificate_id,integer"`
AccessListID uint `json:"access_list_id" gorm:"column:access_list_id" filter:"access_list_id,integer"`
SSLForced bool `json:"ssl_forced" gorm:"column:ssl_forced" filter:"ssl_forced,boolean"`
CachingEnabled bool `json:"caching_enabled" gorm:"column:caching_enabled" filter:"caching_enabled,boolean"`
BlockExploits bool `json:"block_exploits" gorm:"column:block_exploits" filter:"block_exploits,boolean"`
AllowWebsocketUpgrade bool `json:"allow_websocket_upgrade" gorm:"column:allow_websocket_upgrade" filter:"allow_websocket_upgrade,boolean"`
HTTP2Support bool `json:"http2_support" gorm:"column:http2_support" filter:"http2_support,boolean"`
HSTSEnabled bool `json:"hsts_enabled" gorm:"column:hsts_enabled" filter:"hsts_enabled,boolean"`
HSTSSubdomains bool `json:"hsts_subdomains" gorm:"column:hsts_subdomains" filter:"hsts_subdomains,boolean"`
Paths string `json:"paths" gorm:"column:paths" filter:"paths,string"`
AdvancedConfig string `json:"advanced_config" gorm:"column:advanced_config" filter:"advanced_config,string"`
Status string `json:"status" gorm:"column:status" filter:"status,string"`
ErrorMessage string `json:"error_message" gorm:"column:error_message" filter:"error_message,string"`
IsDisabled bool `json:"is_disabled" gorm:"column:is_disabled" filter:"is_disabled,boolean"`
UserID uint `json:"user_id" gorm:"column:user_id" filter:"user_id,integer"`
Type string `json:"type" gorm:"column:type" filter:"type,string"`
NginxTemplateID uint `json:"nginx_template_id" gorm:"column:nginx_template_id" filter:"nginx_template_id,integer"`
ListenInterface string `json:"listen_interface" gorm:"column:listen_interface" filter:"listen_interface,string"`
DomainNames types.JSONB `json:"domain_names" gorm:"column:domain_names" filter:"domain_names,string"`
UpstreamID types.NullableDBUint `json:"upstream_id" gorm:"column:upstream_id" filter:"upstream_id,integer"`
ProxyScheme string `json:"proxy_scheme" gorm:"column:proxy_scheme" filter:"proxy_scheme,string"`
ProxyHost string `json:"proxy_host" gorm:"column:proxy_host" filter:"proxy_host,string"`
ProxyPort int `json:"proxy_port" gorm:"column:proxy_port" filter:"proxy_port,integer"`
CertificateID types.NullableDBUint `json:"certificate_id" gorm:"column:certificate_id" filter:"certificate_id,integer"`
AccessListID types.NullableDBUint `json:"access_list_id" gorm:"column:access_list_id" filter:"access_list_id,integer"`
SSLForced bool `json:"ssl_forced" gorm:"column:ssl_forced" filter:"ssl_forced,boolean"`
CachingEnabled bool `json:"caching_enabled" gorm:"column:caching_enabled" filter:"caching_enabled,boolean"`
BlockExploits bool `json:"block_exploits" gorm:"column:block_exploits" filter:"block_exploits,boolean"`
AllowWebsocketUpgrade bool `json:"allow_websocket_upgrade" gorm:"column:allow_websocket_upgrade" filter:"allow_websocket_upgrade,boolean"`
HTTP2Support bool `json:"http2_support" gorm:"column:http2_support" filter:"http2_support,boolean"`
HSTSEnabled bool `json:"hsts_enabled" gorm:"column:hsts_enabled" filter:"hsts_enabled,boolean"`
HSTSSubdomains bool `json:"hsts_subdomains" gorm:"column:hsts_subdomains" filter:"hsts_subdomains,boolean"`
Paths string `json:"paths" gorm:"column:paths" filter:"paths,string"`
AdvancedConfig string `json:"advanced_config" gorm:"column:advanced_config" filter:"advanced_config,string"`
Status string `json:"status" gorm:"column:status" filter:"status,string"`
ErrorMessage string `json:"error_message" gorm:"column:error_message" filter:"error_message,string"`
IsDisabled bool `json:"is_disabled" gorm:"column:is_disabled" filter:"is_disabled,boolean"`
// Expansions
Certificate *certificate.Model `json:"certificate,omitempty" gorm:"-"`
NginxTemplate *nginxtemplate.Model `json:"nginx_template,omitempty" gorm:"-"`
@ -101,9 +101,9 @@ func (m *Model) Expand(items []string) error {
var err error
// Always expand the upstream
if m.UpstreamID > 0 {
if m.UpstreamID.Uint > 0 {
var u upstream.Model
u, err = upstream.GetByID(m.UpstreamID)
u, err = upstream.GetByID(m.UpstreamID.Uint)
m.Upstream = &u
}
@ -113,9 +113,9 @@ func (m *Model) Expand(items []string) error {
m.User = &usr
}
if util.SliceContainsItem(items, "certificate") && m.CertificateID > 0 {
if util.SliceContainsItem(items, "certificate") && m.CertificateID.Uint > 0 {
var cert certificate.Model
cert, err = certificate.GetByID(m.CertificateID)
cert, err = certificate.GetByID(m.CertificateID.Uint)
m.Certificate = &cert
}
@ -125,9 +125,9 @@ func (m *Model) Expand(items []string) error {
m.NginxTemplate = &templ
}
if util.SliceContainsItem(items, "upstream") && m.UpstreamID > 0 {
if util.SliceContainsItem(items, "upstream") && m.UpstreamID.Uint > 0 {
var ups upstream.Model
ups, err = upstream.GetByID(m.UpstreamID)
ups, err = upstream.GetByID(m.UpstreamID.Uint)
m.Upstream = &ups
}
@ -150,9 +150,9 @@ func (m *Model) GetTemplate() Template {
ProxyPort: m.ProxyPort,
ListenInterface: m.ListenInterface,
DomainNames: domainNames,
UpstreamID: m.UpstreamID,
CertificateID: m.CertificateID,
AccessListID: m.AccessListID,
UpstreamID: m.UpstreamID.Uint,
CertificateID: m.CertificateID.Uint,
AccessListID: m.AccessListID.Uint,
SSLForced: m.SSLForced,
CachingEnabled: m.CachingEnabled,
BlockExploits: m.BlockExploits,

View File

@ -6,6 +6,7 @@ import (
"npm/internal/entity"
"npm/internal/entity/certificate"
"npm/internal/entity/host"
"npm/internal/types"
"github.com/stretchr/testify/assert"
)
@ -51,7 +52,7 @@ server {
},
Status: certificate.StatusProvided,
Type: certificate.TypeHTTP,
CertificateAuthorityID: 99,
CertificateAuthorityID: types.NullableDBUint{Uint: 99},
},
want: want{
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",

View File

@ -0,0 +1,63 @@
package types
import (
"database/sql/driver"
"encoding/json"
"strconv"
)
// NullableDBInt works with database values that can be null or an integer value
type NullableDBInt struct {
Int int
}
// Value encodes the type ready for the database
func (d NullableDBInt) Value() (driver.Value, error) {
if d.Int == 0 {
return nil, nil
}
// According to current database/sql docs, the sql has four builtin functions that
// returns driver.Value, and the underlying types are `int64`, `float64`, `string` and `bool`
return driver.Value(int64(d.Int)), nil
}
// Scan takes data from the database and modifies it for Go Types
func (d *NullableDBInt) Scan(src interface{}) error {
var i int
switch v := src.(type) {
case int:
i = v
case int64:
i = int(v)
case float32:
i = int(v)
case float64:
i = int(v)
case string:
i, _ = strconv.Atoi(v)
}
d.Int = i
return nil
}
// UnmarshalJSON will unmarshal both database and post given values
func (d *NullableDBInt) UnmarshalJSON(data []byte) error {
// total_deploy_time: 10,
// total_deploy_time: null,
var i int
if err := json.Unmarshal(data, &i); err != nil {
i = 0
return nil
}
d.Int = i
return nil
}
// MarshalJSON will marshal for output in api responses
func (d NullableDBInt) MarshalJSON() ([]byte, error) {
if d.Int == 0 {
return json.Marshal(nil)
}
return json.Marshal(d.Int)
}

View File

@ -0,0 +1,64 @@
package types
import (
"database/sql/driver"
"encoding/json"
"strconv"
)
// NullableDBUint works with database values that can be null or an integer value
type NullableDBUint struct {
Uint uint
}
// Value encodes the type ready for the database
func (d NullableDBUint) Value() (driver.Value, error) {
if d.Uint == 0 {
return nil, nil
}
// According to current database/sql docs, the sql has four builtin functions that
// returns driver.Value, and the underlying types are `int64`, `float64`, `string` and `bool`
return driver.Value(int64(d.Uint)), nil
}
// Scan takes data from the database and modifies it for Go Types
func (d *NullableDBUint) Scan(src interface{}) error {
var i uint
switch v := src.(type) {
case int:
i = uint(v)
case int64:
i = uint(v)
case float32:
i = uint(v)
case float64:
i = uint(v)
case string:
a, _ := strconv.Atoi(v)
i = uint(a)
}
d.Uint = i
return nil
}
// UnmarshalJSON will unmarshal both database and post given values
func (d *NullableDBUint) UnmarshalJSON(data []byte) error {
// total_deploy_time: 10,
// total_deploy_time: null,
var i uint
if err := json.Unmarshal(data, &i); err != nil {
i = 0
return nil
}
d.Uint = i
return nil
}
// MarshalJSON will marshal for output in api responses
func (d NullableDBUint) MarshalJSON() ([]byte, error) {
if d.Uint == 0 {
return json.Marshal(nil)
}
return json.Marshal(d.Uint)
}

View File

@ -17,6 +17,8 @@ func (d NullableDBDate) Value() (driver.Value, error) {
if d.Time == nil {
return nil, nil
}
// According to current database/sql docs, the sql has four builtin functions that
// returns driver.Value, and the underlying types are `int64`, `float64`, `string` and `bool`
return driver.Value(d.Time.Unix()), nil
}

View File

@ -0,0 +1,9 @@
package util
import "time"
// UnixMilliToNiceFormat converts a millisecond to nice string
func UnixMilliToNiceFormat(milli int64) string {
t := time.Unix(0, milli*int64(time.Millisecond))
return t.Format("2006-01-02 15:04:05")
}

View File

@ -12,27 +12,27 @@ import (
// ValidateHost will check if associated objects exist and other checks
// will return a nil error if things are OK
func ValidateHost(h host.Model) error {
if h.CertificateID > 0 {
if h.CertificateID.Uint > 0 {
// Check certificate exists and is valid
// This will not determine if the certificate is Ready to use,
// as this validation only cares that the row exists.
if _, cErr := certificate.GetByID(h.CertificateID); cErr != nil {
return eris.Wrapf(cErr, "Certificate #%d does not exist", h.CertificateID)
if _, cErr := certificate.GetByID(h.CertificateID.Uint); cErr != nil {
return eris.Wrapf(cErr, "Certificate #%d does not exist", h.CertificateID.Uint)
}
}
if h.UpstreamID > 0 {
if h.UpstreamID.Uint > 0 {
// Check upstream exists
if _, uErr := upstream.GetByID(h.UpstreamID); uErr != nil {
return eris.Wrapf(uErr, "Upstream #%d does not exist", h.UpstreamID)
if _, uErr := upstream.GetByID(h.UpstreamID.Uint); uErr != nil {
return eris.Wrapf(uErr, "Upstream #%d does not exist", h.UpstreamID.Uint)
}
}
// Ensure either UpstreamID is set or appropriate proxy host params are set
if h.UpstreamID > 0 && (h.ProxyHost != "" || h.ProxyPort > 0) {
if h.UpstreamID.Uint > 0 && (h.ProxyHost != "" || h.ProxyPort > 0) {
return eris.Errorf("Proxy Host or Port cannot be set when using an Upstream")
}
if h.UpstreamID == 0 && (h.ProxyHost == "" || h.ProxyPort < 1) {
if h.UpstreamID.Uint == 0 && (h.ProxyHost == "" || h.ProxyPort < 1) {
return eris.Errorf("Proxy Host and Port must be specified, unless using an Upstream")
}