Restructure go project (#2356)

* Move main to cmd
* Move api to internal
* Move logger and manager to internal
* Move shell hiding code to separate package
* Decouple job from desktop and utils
* Decouple session from config
* Move static into internal
* Decouple config from dlna
* Move desktop to internal
* Move dlna to internal
* Decouple remaining packages from config
* Move config into internal
* Move jsonschema and paths to models
* Make ffmpeg functions private
* Move file utility methods into fsutil package
* Move symwalk into fsutil
* Move single-use util functions into client package
* Move slice functions to separate packages
* Add env var to suppress windowsgui arg
* Move hash functions into separate package
* Move identify to internal
* Move autotag to internal
* Touch UI when generating backend
This commit is contained in:
WithoutPants
2022-03-17 11:33:59 +11:00
committed by GitHub
parent dcee874f59
commit f69bd8a94f
334 changed files with 1845 additions and 1525 deletions

View File

@@ -7,7 +7,6 @@ import (
"strings"
"github.com/stashapp/stash/pkg/logger"
"github.com/stashapp/stash/pkg/manager/config"
)
type ExternalAccessError net.IP
@@ -16,7 +15,7 @@ func (e ExternalAccessError) Error() string {
return fmt.Sprintf("stash accessed from external IP %s", net.IP(e).String())
}
func CheckAllowPublicWithoutAuth(c *config.Instance, r *http.Request) error {
func CheckAllowPublicWithoutAuth(c ExternalAccessConfig, r *http.Request) error {
if !c.HasCredentials() && !c.GetDangerousAllowPublicWithoutAuth() && !c.IsNewSystem() {
requestIPString, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
@@ -60,7 +59,7 @@ func CheckAllowPublicWithoutAuth(c *config.Instance, r *http.Request) error {
return nil
}
func CheckExternalAccessTripwire(c *config.Instance) *ExternalAccessError {
func CheckExternalAccessTripwire(c ExternalAccessConfig) *ExternalAccessError {
if !c.HasCredentials() && !c.GetDangerousAllowPublicWithoutAuth() {
if remoteIP := c.GetSecurityTripwireAccessedFromPublicInternet(); remoteIP != "" {
err := ExternalAccessError(net.ParseIP(remoteIP))

View File

@@ -4,13 +4,33 @@ import (
"errors"
"net/http"
"testing"
"github.com/stashapp/stash/pkg/manager/config"
)
type config struct {
username string
password string
dangerousAllowPublicWithoutAuth bool
securityTripwireAccessedFromPublicInternet string
}
func (c *config) HasCredentials() bool {
return c.username != "" && c.password != ""
}
func (c *config) GetDangerousAllowPublicWithoutAuth() bool {
return c.dangerousAllowPublicWithoutAuth
}
func (c *config) GetSecurityTripwireAccessedFromPublicInternet() string {
return c.securityTripwireAccessedFromPublicInternet
}
func (c *config) IsNewSystem() bool {
return false
}
func TestCheckAllowPublicWithoutAuth(t *testing.T) {
c := config.GetInstance()
_ = c.SetInitialMemoryConfig()
c := &config{}
doTest := func(caseIndex int, r *http.Request, expectedErr interface{}) {
t.Helper()
@@ -114,18 +134,17 @@ func TestCheckAllowPublicWithoutAuth(t *testing.T) {
RemoteAddr: "193.168.1.1:8080",
}
c.Set(config.Username, "admin")
c.Set(config.Password, "admin")
c.username = "admin"
c.password = "admin"
if err := CheckAllowPublicWithoutAuth(c, r); err != nil {
t.Errorf("unexpected error: %v", err)
}
c.Set(config.Username, "")
c.Set(config.Password, "")
c.username = ""
c.password = ""
// HACK - this key isn't publically exposed
c.Set("dangerous_allow_public_without_auth", true)
c.dangerousAllowPublicWithoutAuth = true
if err := CheckAllowPublicWithoutAuth(c, r); err != nil {
t.Errorf("unexpected error: %v", err)
@@ -134,36 +153,34 @@ func TestCheckAllowPublicWithoutAuth(t *testing.T) {
}
func TestCheckExternalAccessTripwire(t *testing.T) {
c := config.GetInstance()
_ = c.SetInitialMemoryConfig()
c.Set(config.SecurityTripwireAccessedFromPublicInternet, "4.4.4.4")
c := &config{}
c.securityTripwireAccessedFromPublicInternet = "4.4.4.4"
// always return nil if authentication configured or dangerous key set
c.Set(config.Username, "admin")
c.Set(config.Password, "admin")
c.username = "admin"
c.password = "admin"
if err := CheckExternalAccessTripwire(c); err != nil {
t.Errorf("unexpected error %v", err)
}
c.Set(config.Username, "")
c.Set(config.Password, "")
c.username = ""
c.password = ""
// HACK - this key isn't publically exposed
c.Set("dangerous_allow_public_without_auth", true)
c.dangerousAllowPublicWithoutAuth = true
if err := CheckExternalAccessTripwire(c); err != nil {
t.Errorf("unexpected error %v", err)
}
c.Set("dangerous_allow_public_without_auth", false)
c.dangerousAllowPublicWithoutAuth = false
if err := CheckExternalAccessTripwire(c); err == nil {
t.Errorf("expected error %v", ExternalAccessError("4.4.4.4"))
}
c.Set(config.SecurityTripwireAccessedFromPublicInternet, "")
c.securityTripwireAccessedFromPublicInternet = ""
if err := CheckExternalAccessTripwire(c); err != nil {
t.Errorf("unexpected error %v", err)

17
pkg/session/config.go Normal file
View File

@@ -0,0 +1,17 @@
package session
type ExternalAccessConfig interface {
HasCredentials() bool
GetDangerousAllowPublicWithoutAuth() bool
GetSecurityTripwireAccessedFromPublicInternet() string
IsNewSystem() bool
}
type SessionConfig interface {
GetUsername() string
GetAPIKey() string
GetSessionStoreKey() []byte
GetMaxSessionAge() int
ValidateCredentials(username string, password string) bool
}

View File

@@ -8,8 +8,7 @@ import (
"github.com/gorilla/securecookie"
"github.com/gorilla/sessions"
"github.com/stashapp/stash/pkg/logger"
"github.com/stashapp/stash/pkg/manager/config"
"github.com/stashapp/stash/pkg/utils"
"github.com/stashapp/stash/pkg/sliceutil/stringslice"
)
type key int
@@ -40,16 +39,16 @@ var ErrUnauthorized = errors.New("unauthorized")
type Store struct {
sessionStore *sessions.CookieStore
config *config.Instance
config SessionConfig
}
func NewStore(c *config.Instance) *Store {
func NewStore(c SessionConfig) *Store {
ret := &Store{
sessionStore: sessions.NewCookieStore(config.GetInstance().GetSessionStoreKey()),
sessionStore: sessions.NewCookieStore(c.GetSessionStoreKey()),
config: c,
}
ret.sessionStore.MaxAge(config.GetInstance().GetMaxSessionAge())
ret.sessionStore.MaxAge(c.GetMaxSessionAge())
return ret
}
@@ -62,7 +61,7 @@ func (s *Store) Login(w http.ResponseWriter, r *http.Request) error {
password := r.FormValue(passwordFormKey)
// authenticate the user
if !config.GetInstance().ValidateCredentials(username, password) {
if !s.config.ValidateCredentials(username, password) {
return ErrInvalidCredentials
}
@@ -165,7 +164,7 @@ func GetVisitedPlugins(ctx context.Context) []string {
func AddVisitedPlugin(ctx context.Context, pluginID string) context.Context {
curVal := GetVisitedPlugins(ctx)
curVal = utils.StrAppendUnique(curVal, pluginID)
curVal = stringslice.StrAppendUnique(curVal, pluginID)
return setVisitedPlugins(ctx, curVal)
}