working commit
This commit is contained in:
+11
-11
@@ -18,7 +18,7 @@ import (
|
|||||||
|
|
||||||
"mstore/pkg/auxx509"
|
"mstore/pkg/auxx509"
|
||||||
|
|
||||||
"sigs.k8s.io/yaml"
|
yaml "go.yaml.in/yaml/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Service struct {
|
type Service struct {
|
||||||
@@ -35,18 +35,18 @@ type Storage struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Service Service `json:"service"`
|
Service Service `json:"service" yaml:"service"`
|
||||||
Database Database `json:"database" yaml:"database"`
|
Database Database `json:"database" yaml:"database"`
|
||||||
Storage Storage `json:"storage" yaml:"storage"`
|
Storage Storage `json:"storage" yaml:"storage"`
|
||||||
AsDaemon bool `json:"asDaemon" yaml:"asDaemon"`
|
AsDaemon bool `json:"asDaemon" yaml:"asDaemon"`
|
||||||
Logpath string `json:"logpath" yaml:"logpath"`
|
Logpath string `json:"logpath" yaml:"logpath"`
|
||||||
Runpath string `json:"runpath" yaml:"runpath"`
|
Runpath string `json:"runpath" yaml:"runpath"`
|
||||||
Version string `json:"version" yaml:"version"`
|
Version string `json:"version" yaml:"version"`
|
||||||
Certpath string `json:"certpath,omitempty" yaml:"certath"`
|
Certpath string `json:"certpath,omitempty" yaml:"certath"`
|
||||||
Keypath string `json:"keypath,omitempty" yaml:"keypath"`
|
Keypath string `json:"keypath,omitempty" yaml:"keypath"`
|
||||||
X509Cert string `json:"-" yaml:"-"`
|
X509Cert string `json:"-" yaml:"-"`
|
||||||
X509Key string `json:"-" yaml:"-"`
|
X509Key string `json:"-" yaml:"-"`
|
||||||
Datadir string `json:"datadir"`
|
Datadir string `json:"datadir" yaml:datadir`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConfig() *Config {
|
func NewConfig() *Config {
|
||||||
|
|||||||
+7
-4
@@ -10,8 +10,11 @@
|
|||||||
package descr
|
package descr
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
SchemeCreated bool `json:"SchemeCreated"`
|
SchemeCreated bool `yaml:"schemeCreated"`
|
||||||
AnonymousCreated bool `json:"AnonymousCreated"`
|
AnonymousCreated bool `yaml:"anonymousCreated"`
|
||||||
InituserCreated bool `json:"inituserCreated"`
|
InituserCreated bool `yaml:"inituserCreated"`
|
||||||
SchemeCreatedAt string `json:"inituserCreatedAt"`
|
|
||||||
|
SchemeCreatedAt string `yaml:"schemeCreatedAt"`
|
||||||
|
AnonymousCreatedAt string `yaml:"anonymousCreatedAt"`
|
||||||
|
InituserCreatedAt string `yaml:"inituserCreatedAt"`
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-4
@@ -26,7 +26,7 @@ func (db *Database) WriteAnonymous(ctx context.Context) error {
|
|||||||
password := auxtool.RandomString(64)
|
password := auxtool.RandomString(64)
|
||||||
passhash := auxpwd.MakeSHA256Hash([]byte(password))
|
passhash := auxpwd.MakeSHA256Hash([]byte(password))
|
||||||
accountDescr := &descr.Account{
|
accountDescr := &descr.Account{
|
||||||
ID: auxuuid.NewUUID(),
|
ID: descr.AnonymousID,
|
||||||
Username: descr.AnonimousUsername,
|
Username: descr.AnonimousUsername,
|
||||||
Passhash: passhash,
|
Passhash: passhash,
|
||||||
Disabled: false,
|
Disabled: false,
|
||||||
@@ -56,7 +56,7 @@ func (db *Database) WriteAnonymous(ctx context.Context) error {
|
|||||||
grantDescr = &descr.Grant{
|
grantDescr = &descr.Grant{
|
||||||
ID: auxuuid.NewUUID(),
|
ID: auxuuid.NewUUID(),
|
||||||
AccountID: accountDescr.ID,
|
AccountID: accountDescr.ID,
|
||||||
Right: descr.RightReadFiles,
|
Right: descr.RightReadImages,
|
||||||
Pattern: "*",
|
Pattern: "*",
|
||||||
CreatedAt: now,
|
CreatedAt: now,
|
||||||
UpdatedAt: now,
|
UpdatedAt: now,
|
||||||
@@ -78,8 +78,8 @@ func (db *Database) WriteInituser(ctx context.Context) error {
|
|||||||
now := auxtool.TimeNow()
|
now := auxtool.TimeNow()
|
||||||
passhash := auxpwd.MakeSHA256Hash([]byte(descr.InitUsername))
|
passhash := auxpwd.MakeSHA256Hash([]byte(descr.InitUsername))
|
||||||
accountDescr := &descr.Account{
|
accountDescr := &descr.Account{
|
||||||
ID: auxuuid.NewUUID(),
|
ID: descr.InitID,
|
||||||
Username: descr.AnonimousUsername,
|
Username: descr.InitUsername,
|
||||||
Passhash: passhash,
|
Passhash: passhash,
|
||||||
Disabled: false,
|
Disabled: false,
|
||||||
CreatedAt: now,
|
CreatedAt: now,
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ const schema = `
|
|||||||
CREATE TABLE IF NOT EXISTS grants (
|
CREATE TABLE IF NOT EXISTS grants (
|
||||||
id TEXT NOT NULL,
|
id TEXT NOT NULL,
|
||||||
account_id INT NOT NULL,
|
account_id INT NOT NULL,
|
||||||
operation TEXT NOT NULL,
|
right TEXT NOT NULL,
|
||||||
pattern TEXT NOT NULL,
|
pattern TEXT NOT NULL,
|
||||||
created_at TEXT NOT NULL,
|
created_at TEXT NOT NULL,
|
||||||
updated_at TEXT NOT NULL,
|
updated_at TEXT NOT NULL,
|
||||||
@@ -88,8 +88,8 @@ const schema = `
|
|||||||
CREATE INDEX IF NOT EXISTS grants_index01
|
CREATE INDEX IF NOT EXISTS grants_index01
|
||||||
ON grants(account_id);
|
ON grants(account_id);
|
||||||
CREATE INDEX IF NOT EXISTS grants_index02
|
CREATE INDEX IF NOT EXISTS grants_index02
|
||||||
ON grants(account_id, operation);
|
ON grants(account_id, right);
|
||||||
CREATE UNIQUE INDEX IF NOT EXISTS grants_index03
|
CREATE UNIQUE INDEX IF NOT EXISTS grants_index03
|
||||||
ON grants(account_id, operation, pattern);
|
ON grants(account_id, right, pattern);
|
||||||
|
|
||||||
`
|
`
|
||||||
|
|||||||
+61
-24
@@ -29,8 +29,9 @@ import (
|
|||||||
"mstore/app/operator"
|
"mstore/app/operator"
|
||||||
"mstore/app/service"
|
"mstore/app/service"
|
||||||
"mstore/app/storage"
|
"mstore/app/storage"
|
||||||
|
"mstore/pkg/auxtool"
|
||||||
|
|
||||||
"sigs.k8s.io/yaml"
|
yaml "go.yaml.in/yaml/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
@@ -100,6 +101,37 @@ func (srv *Server) Configure() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (srv *Server) WriteStat() error {
|
||||||
|
// Write status file
|
||||||
|
var err error
|
||||||
|
statefilePath := filepath.Join(srv.conf.Datadir, "server.yaml")
|
||||||
|
stateData, err := yaml.Marshal(srv.stat)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = ioutil.WriteFile(statefilePath, stateData, 0640)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (srv *Server) ReadStat() error {
|
||||||
|
var err error
|
||||||
|
// Read state file
|
||||||
|
statefilePath := filepath.Join(srv.conf.Datadir, "server.yaml")
|
||||||
|
stateData, err := ioutil.ReadFile(statefilePath)
|
||||||
|
if err == nil {
|
||||||
|
stat := descr.Server{}
|
||||||
|
err = yaml.Unmarshal(stateData, &stat)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
srv.stat = stat
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
func (srv *Server) Build() error {
|
func (srv *Server) Build() error {
|
||||||
var err error
|
var err error
|
||||||
srv.logg.Infof("Server building")
|
srv.logg.Infof("Server building")
|
||||||
@@ -131,23 +163,20 @@ func (srv *Server) Build() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Read state file
|
// Read state file
|
||||||
statefilePath := filepath.Join(srv.conf.Datadir, "server.yaml")
|
srv.logg.Infof("Reading server status")
|
||||||
stateData, err := ioutil.ReadFile(statefilePath)
|
err = srv.ReadStat()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = yaml.Unmarshal(stateData, &srv.stat)
|
return err
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Creating database
|
// Creating database dir
|
||||||
dbdir := srv.conf.Database.Basepath
|
dbdir := srv.conf.Database.Basepath
|
||||||
srv.logg.Infof("Creating database directory %s ", dbdir)
|
srv.logg.Infof("Creating database directory %s ", dbdir)
|
||||||
err = os.MkdirAll(dbdir, 0750)
|
err = os.MkdirAll(dbdir, 0750)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
// Creating database
|
||||||
mdb := maindb.NewDatabase(dbdir)
|
mdb := maindb.NewDatabase(dbdir)
|
||||||
srv.logg.Infof("Opening main database")
|
srv.logg.Infof("Opening main database")
|
||||||
err = mdb.OpenDatabase()
|
err = mdb.OpenDatabase()
|
||||||
@@ -165,34 +194,42 @@ func (srv *Server) Build() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
srv.stat.SchemeCreated = true
|
srv.stat.SchemeCreated = true
|
||||||
|
srv.stat.SchemeCreatedAt = auxtool.TimeNow()
|
||||||
|
srv.logg.Infof("Writing server status")
|
||||||
|
err = srv.WriteStat()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// Created anonymous user
|
// Created anonymous user
|
||||||
if !srv.stat.AnonymousCreated {
|
if !srv.stat.AnonymousCreated {
|
||||||
srv.logg.Infof("Creating anonimous user")
|
srv.logg.Infof("Creating anonymous user")
|
||||||
err = mdb.WriteAnonymous(ctx)
|
err = mdb.WriteAnonymous(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
srv.stat.AnonymousCreated = true
|
srv.stat.AnonymousCreated = true
|
||||||
}
|
srv.stat.AnonymousCreatedAt = auxtool.TimeNow()
|
||||||
if !srv.stat.InituserCreated {
|
srv.logg.Infof("Writing server status")
|
||||||
srv.logg.Infof("Creating init user")
|
err = srv.WriteStat()
|
||||||
err = srv.mdb.WriteAnonymous(ctx)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
if !srv.stat.InituserCreated {
|
||||||
|
srv.logg.Infof("Creating init user")
|
||||||
|
err = srv.mdb.WriteInituser(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
srv.stat.InituserCreatedAt = auxtool.TimeNow()
|
||||||
srv.stat.InituserCreated = true
|
srv.stat.InituserCreated = true
|
||||||
}
|
|
||||||
|
|
||||||
// Write status file
|
srv.logg.Infof("Writing server status")
|
||||||
stateData, err = yaml.Marshal(srv.stat)
|
err = srv.WriteStat()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = ioutil.WriteFile(statefilePath, stateData, 0640)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Creating storage
|
// Creating storage
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ require (
|
|||||||
github.com/spf13/pflag v1.0.9 // indirect
|
github.com/spf13/pflag v1.0.9 // indirect
|
||||||
github.com/vbatts/tar-split v0.12.2 // indirect
|
github.com/vbatts/tar-split v0.12.2 // indirect
|
||||||
go.yaml.in/yaml/v2 v2.4.2 // indirect
|
go.yaml.in/yaml/v2 v2.4.2 // indirect
|
||||||
|
go.yaml.in/yaml/v4 v4.0.0-rc.4 // indirect
|
||||||
golang.org/x/sync v0.18.0 // indirect
|
golang.org/x/sync v0.18.0 // indirect
|
||||||
golang.org/x/sys v0.38.0 // indirect
|
golang.org/x/sys v0.38.0 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
|
|||||||
@@ -58,6 +58,8 @@ go.yaml.in/yaml/v2 v2.4.2 h1:DzmwEr2rDGHl7lsFgAHxmNz/1NlQ7xLIrlN2h5d1eGI=
|
|||||||
go.yaml.in/yaml/v2 v2.4.2/go.mod h1:081UH+NErpNdqlCXm3TtEran0rJZGxAYx9hb/ELlsPU=
|
go.yaml.in/yaml/v2 v2.4.2/go.mod h1:081UH+NErpNdqlCXm3TtEran0rJZGxAYx9hb/ELlsPU=
|
||||||
go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc=
|
go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc=
|
||||||
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
|
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
|
||||||
|
go.yaml.in/yaml/v4 v4.0.0-rc.4 h1:UP4+v6fFrBIb1l934bDl//mmnoIZEDK0idg1+AIvX5U=
|
||||||
|
go.yaml.in/yaml/v4 v4.0.0-rc.4/go.mod h1:aZqd9kCMsGL7AuUv/m/PvWLdg5sjJsZ4oHDEnfPPfY0=
|
||||||
golang.org/x/sync v0.18.0 h1:kr88TuHDroi+UVf+0hZnirlk8o8T+4MrK6mr60WkH/I=
|
golang.org/x/sync v0.18.0 h1:kr88TuHDroi+UVf+0hZnirlk8o8T+4MrK6mr60WkH/I=
|
||||||
golang.org/x/sync v0.18.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
|
golang.org/x/sync v0.18.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
|
||||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
|||||||
Reference in New Issue
Block a user