Skip to content

Commit a1efcdf

Browse files
masterbenderForceu
andauthored
Added env variable to set a minium password length (#312)
Co-authored-by: Marc Ole Bulling <[email protected]>
1 parent 8c375e9 commit a1efcdf

File tree

13 files changed

+73
-51
lines changed

13 files changed

+73
-51
lines changed

docs/advanced.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ Available environment variables
8080
+-------------------------------+-------------------------------------------------------------------------------------+-----------------+--------------------------------------+
8181
| GOKAPI_MAX_PARALLEL_UPLOADS | Set the amount of chunks that are uploaded in parallel for a single file | Yes | 4 |
8282
+-------------------------------+-------------------------------------------------------------------------------------+-----------------+--------------------------------------+
83+
| GOKAPI_MIN_LENGTH_PASSWORD | Sets the minium password length - must be at least 6 characters | No | 8 |
84+
+-------------------------------+-------------------------------------------------------------------------------------+-----------------+--------------------------------------+
8385
| GOKAPI_PORT | Sets the webserver port | Yes | 53842 |
8486
+-------------------------------+-------------------------------------------------------------------------------------+-----------------+--------------------------------------+
8587
| GOKAPI_DISABLE_CORS_CHECK | Disables the CORS check on startup and during setup, if set to "true" | No | false |

internal/configuration/Configuration.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ import (
2525
"github.com/forceu/gokapi/internal/storage/filesystem"
2626
)
2727

28-
// MinLengthPassword is the required length of admin password in characters
29-
const MinLengthPassword = 8
30-
3128
// Environment is an object containing the environment variables
3229
var Environment environment.Environment
3330

@@ -94,6 +91,7 @@ func Load() {
9491
if serverSettings.ChunkSize == 0 {
9592
serverSettings.ChunkSize = 45
9693
}
94+
serverSettings.MinLengthPassword = Environment.MinLengthPassword
9795
serverSettings.LengthId = Environment.LengthId
9896
serverSettings.LengthHotlinkId = Environment.LengthHotlinkId
9997
helper.CreateDir(serverSettings.DataDir)
@@ -187,8 +185,8 @@ func deleteAllEncryptedStorage() {
187185

188186
// SetDeploymentPassword sets a new password. This should only be used for non-interactive deployment, but is not enforced
189187
func SetDeploymentPassword(newPassword string) {
190-
if len(newPassword) < MinLengthPassword {
191-
fmt.Printf("Password needs to be at least %d characters long\n", MinLengthPassword)
188+
if len(newPassword) < serverSettings.MinLengthPassword {
189+
fmt.Printf("Password needs to be at least %d characters long\n", serverSettings.MinLengthPassword)
192190
os.Exit(1)
193191
}
194192
serverSettings.Authentication.SaltAdmin = helper.GenerateRandomString(30)

internal/configuration/Configuration_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package configuration
22

33
import (
4+
"os"
5+
"testing"
6+
47
"github.com/forceu/gokapi/internal/configuration/cloudconfig"
58
"github.com/forceu/gokapi/internal/configuration/configupgrade"
69
"github.com/forceu/gokapi/internal/models"
710
"github.com/forceu/gokapi/internal/test"
811
"github.com/forceu/gokapi/internal/test/testconfiguration"
9-
"os"
10-
"testing"
1112
)
1213

1314
func TestMain(m *testing.M) {

internal/configuration/setup/Setup.go

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,6 @@ import (
77
"encoding/json"
88
"errors"
99
"fmt"
10-
"github.com/aws/aws-sdk-go/aws/awserr"
11-
"github.com/aws/aws-sdk-go/service/s3"
12-
"github.com/forceu/gokapi/internal/configuration"
13-
"github.com/forceu/gokapi/internal/configuration/cloudconfig"
14-
"github.com/forceu/gokapi/internal/configuration/configupgrade"
15-
"github.com/forceu/gokapi/internal/configuration/database"
16-
"github.com/forceu/gokapi/internal/configuration/database/dbabstraction"
17-
"github.com/forceu/gokapi/internal/encryption"
18-
"github.com/forceu/gokapi/internal/environment"
19-
"github.com/forceu/gokapi/internal/helper"
20-
"github.com/forceu/gokapi/internal/models"
21-
"github.com/forceu/gokapi/internal/storage/filesystem/s3filesystem/aws"
22-
"github.com/forceu/gokapi/internal/webserver/authentication"
2310
"html/template"
2411
"io"
2512
"io/fs"
@@ -33,6 +20,20 @@ import (
3320
"strings"
3421
"syscall"
3522
"time"
23+
24+
"github.com/aws/aws-sdk-go/aws/awserr"
25+
"github.com/aws/aws-sdk-go/service/s3"
26+
"github.com/forceu/gokapi/internal/configuration"
27+
"github.com/forceu/gokapi/internal/configuration/cloudconfig"
28+
"github.com/forceu/gokapi/internal/configuration/configupgrade"
29+
"github.com/forceu/gokapi/internal/configuration/database"
30+
"github.com/forceu/gokapi/internal/configuration/database/dbabstraction"
31+
"github.com/forceu/gokapi/internal/encryption"
32+
"github.com/forceu/gokapi/internal/environment"
33+
"github.com/forceu/gokapi/internal/helper"
34+
"github.com/forceu/gokapi/internal/models"
35+
"github.com/forceu/gokapi/internal/storage/filesystem/s3filesystem/aws"
36+
"github.com/forceu/gokapi/internal/webserver/authentication"
3637
)
3738

3839
// webserverDir is the embedded version of the "static" folder
@@ -244,6 +245,7 @@ func toConfiguration(formObjects *[]jsonFormObject) (models.Configuration, *clou
244245
MaxMemory: parsedEnv.MaxMemory,
245246
DataDir: parsedEnv.DataDir,
246247
MaxParallelUploads: parsedEnv.MaxParallelUploads,
248+
MinLengthPassword: parsedEnv.MinLengthPassword,
247249
ChunkSize: parsedEnv.ChunkSizeMB,
248250
ConfigVersion: configupgrade.CurrentConfigVersion,
249251
Authentication: models.AuthenticationConfig{},
@@ -644,8 +646,9 @@ func parseEncryptionAndDelete(result *models.Configuration, formObjects *[]jsonF
644646
if encLevel == encryption.LocalEncryptionInput || encLevel == encryption.FullEncryptionInput {
645647
result.Encryption.Salt = helper.GenerateRandomString(30)
646648
result.Encryption.ChecksumSalt = helper.GenerateRandomString(30)
647-
if len(masterPw) < configuration.MinLengthPassword {
648-
return configuration.End2EndReconfigParameters{}, errors.New("password is less than " + strconv.Itoa(configuration.MinLengthPassword) + " characters long")
649+
minLength := environment.New().MinLengthPassword
650+
if len(masterPw) < minLength {
651+
return configuration.End2EndReconfigParameters{}, errors.New("password is less than " + strconv.Itoa(minLength) + " characters long")
649652
}
650653
result.Encryption.Checksum = encryption.PasswordChecksum(masterPw, result.Encryption.ChecksumSalt)
651654
}
@@ -705,6 +708,7 @@ type setupView struct {
705708
CloudSettings cloudconfig.CloudConfig
706709
DatabaseSettings models.DbConnection
707710
ProtectedUrls []string
711+
MinPasswordLength int
708712
}
709713

710714
func (v *setupView) loadFromConfig() {
@@ -717,6 +721,7 @@ func (v *setupView) loadFromConfig() {
717721
v.HasAwsFeature = aws.IsIncludedInBuild
718722
v.ProtectedUrls = protectedUrls
719723
if isInitialSetup {
724+
v.MinPasswordLength = environment.New().MinLengthPassword
720725
return
721726
}
722727
configuration.Load()

internal/configuration/setup/Setup_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,6 @@ import (
66
"encoding/json"
77
"errors"
88
"fmt"
9-
"github.com/forceu/gokapi/internal/configuration"
10-
"github.com/forceu/gokapi/internal/configuration/cloudconfig"
11-
"github.com/forceu/gokapi/internal/configuration/database"
12-
"github.com/forceu/gokapi/internal/environment"
13-
"github.com/forceu/gokapi/internal/models"
14-
"github.com/forceu/gokapi/internal/test"
15-
"github.com/forceu/gokapi/internal/test/testconfiguration"
169
"log"
1710
"net"
1811
"net/http"
@@ -22,6 +15,14 @@ import (
2215
"strings"
2316
"testing"
2417
"time"
18+
19+
"github.com/forceu/gokapi/internal/configuration"
20+
"github.com/forceu/gokapi/internal/configuration/cloudconfig"
21+
"github.com/forceu/gokapi/internal/configuration/database"
22+
"github.com/forceu/gokapi/internal/environment"
23+
"github.com/forceu/gokapi/internal/models"
24+
"github.com/forceu/gokapi/internal/test"
25+
"github.com/forceu/gokapi/internal/test/testconfiguration"
2526
)
2627

2728
var jsonForms []jsonFormObject

internal/configuration/setup/templates/setup.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,10 @@
261261
<input type="text" class="form-control" id="auth_username" name="auth_username" placeholder="Username" data-min="3" required data-validate="validateMinLength">
262262
</div><br><br>
263263
<div class="col-sm-8">
264-
<input type="password" autocomplete="new-password" class="form-control" id="auth_pw" name="auth_pw" placeholder="Password" data-min="8" required data-validate="validatePassword">
264+
<input type="password" autocomplete="new-password" class="form-control" id="auth_pw" name="auth_pw" placeholder="Password" data-min={{ .MinPasswordLength }} required data-validate="validatePassword">
265265
</div><br><br>
266266
<div class="col-sm-8">
267-
<input type="password" autocomplete="new-password" class="form-control" id="auth_pw2" name="auth_pw2" placeholder="Password (repeat)" data-min="8" required>
267+
<input type="password" autocomplete="new-password" class="form-control" id="auth_pw2" name="auth_pw2" placeholder="Password (repeat)" data-min={{ .MinPasswordLength }} required>
268268
</div>
269269
</div>
270270

internal/environment/Environment.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,31 @@ package environment
22

33
import (
44
"fmt"
5+
"os"
6+
"path"
7+
58
envParser "github.com/caarlos0/env/v6"
69
"github.com/forceu/gokapi/internal/environment/flagparser"
710
"github.com/forceu/gokapi/internal/helper"
8-
"os"
9-
"path"
1011
)
1112

1213
// DefaultPort for the webserver
1314
const DefaultPort = 53842
1415

1516
// Environment is a struct containing available env variables
1617
type Environment struct {
17-
ChunkSizeMB int `env:"CHUNK_SIZE_MB" envDefault:"45"`
1818
ConfigDir string `env:"CONFIG_DIR" envDefault:"config"`
1919
ConfigFile string `env:"CONFIG_FILE" envDefault:"config.json"`
2020
ConfigPath string
2121
DataDir string `env:"DATA_DIR" envDefault:"data"`
22+
ChunkSizeMB int `env:"CHUNK_SIZE_MB" envDefault:"45"`
2223
LengthId int `env:"LENGTH_ID" envDefault:"15"`
2324
LengthHotlinkId int `env:"LENGTH_HOTLINK_ID" envDefault:"40"`
2425
MaxFileSize int `env:"MAX_FILESIZE" envDefault:"102400"` // 102400==100GB
2526
MaxMemory int `env:"MAX_MEMORY_UPLOAD" envDefault:"50"`
2627
MaxParallelUploads int `env:"MAX_PARALLEL_UPLOADS" envDefault:"3"`
2728
WebserverPort int `env:"PORT" envDefault:"53842"`
29+
MinLengthPassword int `env:"MIN_LENGTH_PASSWORD" envDefault:"8"`
2830
DisableCorsCheck bool `env:"DISABLE_CORS_CHECK" envDefault:"false"`
2931
LogToStdout bool `env:"LOG_STDOUT" envDefault:"false"`
3032
HotlinkVideos bool `env:"ENABLE_HOTLINK_VIDEOS" envDefault:"false"`
@@ -81,6 +83,9 @@ func New() Environment {
8183
if result.MaxFileSize < 1 {
8284
result.MaxFileSize = 5
8385
}
86+
if result.MinLengthPassword < 6 {
87+
result.MinLengthPassword = 6
88+
}
8489
return result
8590
}
8691

internal/environment/Environment_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ func TestEnvLoad(t *testing.T) {
4545
env = New()
4646
test.IsEqualInt(t, env.LengthId, 86)
4747
os.Unsetenv("GOKAPI_LENGTH_ID")
48+
os.Setenv("GOKAPI_MIN_LENGTH_PASSWORD", "12")
49+
env = New()
50+
test.IsEqualInt(t, env.MinLengthPassword, 12)
51+
os.Unsetenv("GOKAPI_MIN_LENGTH_PASSWORD")
4852
env = New()
4953
os.Setenv("GOKAPI_LENGTH_ID", "15")
5054
os.Setenv("GOKAPI_MAX_MEMORY_UPLOAD", "0")

internal/models/Configuration.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type Configuration struct {
2121
MaxParallelUploads int `json:"MaxParallelUploads"`
2222
LengthId int `json:"-"`
2323
LengthHotlinkId int `json:"-"`
24+
MinLengthPassword int `json:"-"`
2425
Encryption Encryption `json:"Encryption"`
2526
UseSsl bool `json:"UseSsl"`
2627
PicturesAlwaysLocal bool `json:"PicturesAlwaysLocal"`

internal/models/Configuration_test.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package models
22

33
import (
4-
"github.com/forceu/gokapi/internal/test"
54
"strings"
65
"testing"
6+
7+
"github.com/forceu/gokapi/internal/test"
78
)
89

910
var testConfig = Configuration{
@@ -17,18 +18,19 @@ var testConfig = Configuration{
1718
OAuthClientId: "",
1819
OAuthClientSecret: "",
1920
},
20-
Port: ":12345",
21-
ServerUrl: "https://testserver.com/",
22-
RedirectUrl: "https://test.com",
23-
DatabaseUrl: "sqlite://./test/gokapitest.sqlite",
24-
ConfigVersion: 14,
25-
LengthId: 5,
26-
LengthHotlinkId: 10,
27-
DataDir: "test",
28-
MaxMemory: 50,
29-
UseSsl: true,
30-
MaxFileSizeMB: 20,
31-
PublicName: "public-name",
21+
Port: ":12345",
22+
ServerUrl: "https://testserver.com/",
23+
RedirectUrl: "https://test.com",
24+
DatabaseUrl: "sqlite://./test/gokapitest.sqlite",
25+
ConfigVersion: 14,
26+
LengthId: 5,
27+
LengthHotlinkId: 10,
28+
DataDir: "test",
29+
MaxMemory: 50,
30+
UseSsl: true,
31+
MaxFileSizeMB: 20,
32+
MinLengthPassword: 8,
33+
PublicName: "public-name",
3234
Encryption: Encryption{
3335
Level: 1,
3436
Cipher: []byte{0x00},

0 commit comments

Comments
 (0)