Skip to content

Commit

Permalink
Fix exports settings save error
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmacdonald committed Aug 6, 2024
1 parent 579225e commit 415b80e
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 14 deletions.
2 changes: 1 addition & 1 deletion frontend/src/api/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,5 @@ type SSH = {
type Exports = {
bd_enabled: boolean;
valve_enabled: boolean;
// authorized_keys: string[];
authorized_keys: string;
};
21 changes: 19 additions & 2 deletions frontend/src/routes/_admin.admin.settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1910,7 +1910,8 @@ const ExportsSection = ({ tab, settings, mutate }: { tab: tabs; settings: Config
validatorAdapter: zodValidator,
defaultValues: {
bd_enabled: settings.exports.bd_enabled,
valve_enabled: settings.exports.valve_enabled
valve_enabled: settings.exports.valve_enabled,
authorized_keys: settings.exports.authorized_keys
}
});

Expand All @@ -1929,6 +1930,22 @@ const ExportsSection = ({ tab, settings, mutate }: { tab: tabs; settings: Config
}}
>
<Grid container spacing={2}>
<Grid xs={12}>
<Field
name={'authorized_keys'}
validators={{
onChange: z.string()
}}
children={(props) => {
return <TextFieldSimple {...props} label={'Authorized Keys (comma separated).'} />;
}}
/>
<SubHeading>
Comma separated list of authorized keys which can access these resources. If no keys are
specified, access will be granted to everyone. Append key to query with{' '}
<kbd>&key=value</kbd>
</SubHeading>
</Grid>
<Grid xs={12}>
<Field
name={'bd_enabled'}
Expand All @@ -1950,7 +1967,7 @@ const ExportsSection = ({ tab, settings, mutate }: { tab: tabs; settings: Config
<Field
name={'valve_enabled'}
validators={{
onChange: z.string()
onChange: z.boolean()
}}
children={(props) => {
return <CheckboxSimple {...props} label={'Enable srcds formatted ban list'} />;
Expand Down
8 changes: 4 additions & 4 deletions internal/ban/ban_steam_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,9 @@ func (h banHandler) onAPIGetStats() gin.HandlerFunc {
}

func (h banHandler) onAPIExportBansValveSteamID() gin.HandlerFunc {
authorizedKeys := h.config.Config().Exports.AuthorizedKeys

return func(ctx *gin.Context) {
authorizedKeys := strings.Split(h.config.Config().Exports.AuthorizedKeys, ",")

if len(authorizedKeys) > 0 {
key, ok := ctx.GetQuery("key")
if !ok || !slices.Contains(authorizedKeys, key) {
Expand Down Expand Up @@ -257,9 +257,9 @@ func (h banHandler) onAPIExportBansValveSteamID() gin.HandlerFunc {
}

func (h banHandler) onAPIExportBansTF2BD() gin.HandlerFunc {
authorizedKeys := h.config.Config().Exports.AuthorizedKeys

return func(ctx *gin.Context) {
authorizedKeys := strings.Split(h.config.Config().Exports.AuthorizedKeys, ",")

if len(authorizedKeys) > 0 {
key, ok := ctx.GetQuery("key")
if !ok || !slices.Contains(authorizedKeys, key) {
Expand Down
12 changes: 9 additions & 3 deletions internal/config/config_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"context"
"errors"
"strings"
"sync"

"github.com/leighmacdonald/gbans/internal/database"
Expand Down Expand Up @@ -58,7 +59,10 @@ func (c *configRepository) Read(ctx context.Context) (domain.Config, error) {
exports_bd_enabled, exports_valve_enabled, exports_authorized_keys
FROM config`

var cfg domain.Config
var (
cfg domain.Config
authorizedKeys []string
)

err := c.db.QueryRow(ctx, query).
Scan(&cfg.General.SiteName, &cfg.General.Mode, &cfg.General.FileServeMode, &cfg.General.SrcdsLogAddr, &cfg.General.AssetURL,
Expand All @@ -78,11 +82,13 @@ func (c *configRepository) Read(ctx context.Context) (domain.Config, error) {
&cfg.LocalStore.PathRoot,
&cfg.SSH.Enabled, &cfg.SSH.Username, &cfg.SSH.Password, &cfg.SSH.Port, &cfg.SSH.PrivateKeyPath, &cfg.SSH.UpdateInterval,
&cfg.SSH.Timeout, &cfg.SSH.DemoPathFmt,
&cfg.Exports.BDEnabled, &cfg.Exports.ValveEnabled, &cfg.Exports.AuthorizedKeys)
&cfg.Exports.BDEnabled, &cfg.Exports.ValveEnabled, &authorizedKeys)
if err != nil {
return cfg, c.db.DBErr(err)
}

cfg.Exports.AuthorizedKeys = strings.Join(authorizedKeys, ",")

return cfg, nil
}

Expand Down Expand Up @@ -189,6 +195,6 @@ func (c *configRepository) Write(ctx context.Context, config domain.Config) erro
"ssh_demo_path_fmt": config.SSH.DemoPathFmt,
"exports_bd_enabled": config.Exports.BDEnabled,
"exports_valve_enabled": config.Exports.ValveEnabled,
"exports_authorized_keys": config.Exports.AuthorizedKeys,
"exports_authorized_keys": strings.Split(config.Exports.AuthorizedKeys, ","),
})))
}
6 changes: 3 additions & 3 deletions internal/domain/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ type ConfigSSH struct {
}

type ConfigExports struct {
BDEnabled bool `json:"bd_enabled"`
ValveEnabled bool `json:"valve_enabled"`
AuthorizedKeys []string `json:"authorized_keys"`
BDEnabled bool `json:"bd_enabled"`
ValveEnabled bool `json:"valve_enabled"`
AuthorizedKeys string `json:"authorized_keys"`
}

type ConfigFilter struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/test/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func TestConfig(t *testing.T) {

config.LocalStore.PathRoot += "/x"

config.Exports.AuthorizedKeys = append(config.Exports.AuthorizedKeys, "test-key")
config.Exports.AuthorizedKeys += ",test-key"
config.Exports.BDEnabled = !config.Exports.BDEnabled
config.Exports.ValveEnabled = !config.Exports.ValveEnabled

Expand Down

0 comments on commit 415b80e

Please sign in to comment.