Require access to Still via method

This commit adds a safeguard to accessing the still from a specific
component by requiring access via the component.GetStill method.
This commit is contained in:
Tom Wiesing 2024-04-08 22:39:32 +02:00
parent 81fa84c244
commit 8235ea9105
No known key found for this signature in database
63 changed files with 288 additions and 197 deletions

View file

@ -48,7 +48,7 @@ func (sql *SQL) QueryTable(ctx context.Context, table component.Table) (*gorm.DB
// queryTable returns a gorm.DB to connect to the provided distillery database table
func (sql *SQL) queryTable(ctx context.Context, silent bool, table string) (*gorm.DB, error) {
conn, err := sql.connect(sql.Config.SQL.Database)
conn, err := sql.connect(component.GetStill(sql).Config.SQL.Database)
if err != nil {
return nil, err
}
@ -113,8 +113,9 @@ func (ssql *SQL) connect(database string) (*sql.DB, error) {
// dsn returns a dsn fof connecting to the database
func (sql *SQL) dsn(database string) string {
user := sql.Config.SQL.AdminUsername
pass := sql.Config.SQL.AdminPassword
config := component.GetStill(sql).Config.SQL
user := config.AdminUsername
pass := config.AdminPassword
network := "tcp"
server := sql.ServerURL

View file

@ -5,7 +5,7 @@ import (
"path/filepath"
"time"
"github.com/FAU-CDI/wisski-distillery/internal/config"
config_package "github.com/FAU-CDI/wisski-distillery/internal/config"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/tkw1536/pkglib/fsx/umaskfree"
"github.com/tkw1536/pkglib/yamlx"
@ -32,7 +32,7 @@ var (
)
func (sql *SQL) Path() string {
return filepath.Join(sql.Still.Config.Paths.Root, "core", "sql")
return filepath.Join(component.GetStill(sql).Config.Paths.Root, "core", "sql")
}
func (*SQL) Context(parent component.InstallationContext) component.InstallationContext {
@ -43,19 +43,20 @@ func (*SQL) Context(parent component.InstallationContext) component.Installation
var resources embed.FS
func (sql *SQL) Stack() component.StackWithResources {
config := component.GetStill(sql).Config
return component.MakeStack(sql, component.StackWithResources{
Resources: resources,
ContextPath: "sql",
EnvContext: map[string]string{
"DOCKER_NETWORK_NAME": sql.Config.Docker.Network(),
"HTTPS_ENABLED": sql.Config.HTTP.HTTPSEnabledEnv(),
"HOST_RULE": sql.Config.HTTP.HostRule(config.PHPMyAdminDomain.Domain()),
"DOCKER_NETWORK_NAME": config.Docker.Network(),
"HTTPS_ENABLED": config.HTTP.HTTPSEnabledEnv(),
"HOST_RULE": config.HTTP.HostRule(config_package.PHPMyAdminDomain.Domain()),
},
ComposerYML: func(root *yaml.Node) (*yaml.Node, error) {
// phpmyadmin is exposed => everything is fine
if sql.Config.HTTP.PhpMyAdmin.Set && sql.Config.HTTP.PhpMyAdmin.Value {
if config.HTTP.PhpMyAdmin.Set && config.HTTP.PhpMyAdmin.Value {
return root, nil
}

View file

@ -8,6 +8,7 @@ import (
"reflect"
"time"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/FAU-CDI/wisski-distillery/pkg/logging"
"github.com/tkw1536/goprogram/exit"
"github.com/tkw1536/pkglib/sqlx"
@ -68,6 +69,7 @@ var errSQLUnableToMigrate = exit.Error{
// Update initializes or updates the SQL database.
func (sql *SQL) Update(ctx context.Context, progress io.Writer) error {
config := component.GetStill(sql).Config.SQL
// unsafely create the admin user!
{
@ -76,8 +78,8 @@ func (sql *SQL) Update(ctx context.Context, progress io.Writer) error {
}
logging.LogMessage(progress, "Creating administrative user")
{
username := sql.Config.SQL.AdminUsername
password := sql.Config.SQL.AdminPassword
username := config.AdminUsername
password := config.AdminPassword
if err := sql.CreateSuperuser(ctx, username, password, true); err != nil {
return errSQLUnableToCreateUser
}
@ -87,10 +89,10 @@ func (sql *SQL) Update(ctx context.Context, progress io.Writer) error {
// create the admin user
logging.LogMessage(progress, "Creating sql database")
{
if !sqlx.IsSafeDatabaseLiteral(sql.Config.SQL.Database) {
if !sqlx.IsSafeDatabaseLiteral(config.Database) {
return errSQLUnsafeDatabaseName
}
createDBSQL := fmt.Sprintf("CREATE DATABASE IF NOT EXISTS `%s`;", sql.Config.SQL.Database)
createDBSQL := fmt.Sprintf("CREATE DATABASE IF NOT EXISTS `%s`;", config.Database)
if err := sql.Exec(createDBSQL); err != nil {
return err
}