diff --git a/app/config/config.go b/app/config/config.go index 8d4b46f..18cee00 100644 --- a/app/config/config.go +++ b/app/config/config.go @@ -18,7 +18,7 @@ import ( "mstore/pkg/auxx509" - "sigs.k8s.io/yaml" + yaml "go.yaml.in/yaml/v4" ) type Service struct { @@ -35,18 +35,18 @@ type Storage struct { } type Config struct { - Service Service `json:"service"` - Database Database `json:"database" yaml:"database"` - Storage Storage `json:"storage" yaml:"storage"` - AsDaemon bool `json:"asDaemon" yaml:"asDaemon"` - Logpath string `json:"logpath" yaml:"logpath"` - Runpath string `json:"runpath" yaml:"runpath"` - Version string `json:"version" yaml:"version"` + Service Service `json:"service" yaml:"service"` + Database Database `json:"database" yaml:"database"` + Storage Storage `json:"storage" yaml:"storage"` + AsDaemon bool `json:"asDaemon" yaml:"asDaemon"` + Logpath string `json:"logpath" yaml:"logpath"` + Runpath string `json:"runpath" yaml:"runpath"` + Version string `json:"version" yaml:"version"` Certpath string `json:"certpath,omitempty" yaml:"certath"` Keypath string `json:"keypath,omitempty" yaml:"keypath"` - X509Cert string `json:"-" yaml:"-"` - X509Key string `json:"-" yaml:"-"` - Datadir string `json:"datadir"` + X509Cert string `json:"-" yaml:"-"` + X509Key string `json:"-" yaml:"-"` + Datadir string `json:"datadir" yaml:datadir` } func NewConfig() *Config { diff --git a/app/descr/server.go b/app/descr/server.go index 64af171..8009567 100644 --- a/app/descr/server.go +++ b/app/descr/server.go @@ -10,8 +10,11 @@ package descr type Server struct { - SchemeCreated bool `json:"SchemeCreated"` - AnonymousCreated bool `json:"AnonymousCreated"` - InituserCreated bool `json:"inituserCreated"` - SchemeCreatedAt string `json:"inituserCreatedAt"` + SchemeCreated bool `yaml:"schemeCreated"` + AnonymousCreated bool `yaml:"anonymousCreated"` + InituserCreated bool `yaml:"inituserCreated"` + + SchemeCreatedAt string `yaml:"schemeCreatedAt"` + AnonymousCreatedAt string `yaml:"anonymousCreatedAt"` + InituserCreatedAt string `yaml:"inituserCreatedAt"` } diff --git a/app/maindb/init.go b/app/maindb/init.go index 0fcb3b3..926823e 100644 --- a/app/maindb/init.go +++ b/app/maindb/init.go @@ -26,7 +26,7 @@ func (db *Database) WriteAnonymous(ctx context.Context) error { password := auxtool.RandomString(64) passhash := auxpwd.MakeSHA256Hash([]byte(password)) accountDescr := &descr.Account{ - ID: auxuuid.NewUUID(), + ID: descr.AnonymousID, Username: descr.AnonimousUsername, Passhash: passhash, Disabled: false, @@ -56,7 +56,7 @@ func (db *Database) WriteAnonymous(ctx context.Context) error { grantDescr = &descr.Grant{ ID: auxuuid.NewUUID(), AccountID: accountDescr.ID, - Right: descr.RightReadFiles, + Right: descr.RightReadImages, Pattern: "*", CreatedAt: now, UpdatedAt: now, @@ -78,8 +78,8 @@ func (db *Database) WriteInituser(ctx context.Context) error { now := auxtool.TimeNow() passhash := auxpwd.MakeSHA256Hash([]byte(descr.InitUsername)) accountDescr := &descr.Account{ - ID: auxuuid.NewUUID(), - Username: descr.AnonimousUsername, + ID: descr.InitID, + Username: descr.InitUsername, Passhash: passhash, Disabled: false, CreatedAt: now, diff --git a/app/maindb/schema.go b/app/maindb/schema.go index 858ad0e..1dbadd8 100644 --- a/app/maindb/schema.go +++ b/app/maindb/schema.go @@ -76,7 +76,7 @@ const schema = ` CREATE TABLE IF NOT EXISTS grants ( id TEXT NOT NULL, account_id INT NOT NULL, - operation TEXT NOT NULL, + right TEXT NOT NULL, pattern TEXT NOT NULL, created_at TEXT NOT NULL, updated_at TEXT NOT NULL, @@ -88,8 +88,8 @@ const schema = ` CREATE INDEX IF NOT EXISTS grants_index01 ON grants(account_id); 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 - ON grants(account_id, operation, pattern); + ON grants(account_id, right, pattern); ` diff --git a/app/server/server.go b/app/server/server.go index 5174c9a..99a711e 100644 --- a/app/server/server.go +++ b/app/server/server.go @@ -29,8 +29,9 @@ import ( "mstore/app/operator" "mstore/app/service" "mstore/app/storage" + "mstore/pkg/auxtool" - "sigs.k8s.io/yaml" + yaml "go.yaml.in/yaml/v4" ) type Server struct { @@ -100,6 +101,37 @@ func (srv *Server) Configure() error { 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 { var err error srv.logg.Infof("Server building") @@ -131,23 +163,20 @@ func (srv *Server) Build() error { } // Read state file - statefilePath := filepath.Join(srv.conf.Datadir, "server.yaml") - stateData, err := ioutil.ReadFile(statefilePath) + srv.logg.Infof("Reading server status") + err = srv.ReadStat() if err != nil { - err = yaml.Unmarshal(stateData, &srv.stat) - if err != nil { - return err - } + return err } - // Creating database + // Creating database dir dbdir := srv.conf.Database.Basepath srv.logg.Infof("Creating database directory %s ", dbdir) err = os.MkdirAll(dbdir, 0750) if err != nil { return err } - + // Creating database mdb := maindb.NewDatabase(dbdir) srv.logg.Infof("Opening main database") err = mdb.OpenDatabase() @@ -165,34 +194,42 @@ func (srv *Server) Build() error { return err } 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 if !srv.stat.AnonymousCreated { - srv.logg.Infof("Creating anonimous user") + srv.logg.Infof("Creating anonymous user") err = mdb.WriteAnonymous(ctx) if err != nil { return err } srv.stat.AnonymousCreated = true - } - if !srv.stat.InituserCreated { - srv.logg.Infof("Creating init user") - err = srv.mdb.WriteAnonymous(ctx) + srv.stat.AnonymousCreatedAt = auxtool.TimeNow() + srv.logg.Infof("Writing server status") + err = srv.WriteStat() if err != nil { 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 - } - // Write status file - stateData, err = yaml.Marshal(srv.stat) - if err != nil { - return err - } - - err = ioutil.WriteFile(statefilePath, stateData, 0640) - if err != nil { - return err + srv.logg.Infof("Writing server status") + err = srv.WriteStat() + if err != nil { + return err + } } // Creating storage diff --git a/go.mod b/go.mod index 3bd5b37..887dc4d 100644 --- a/go.mod +++ b/go.mod @@ -28,6 +28,7 @@ require ( github.com/spf13/pflag v1.0.9 // indirect github.com/vbatts/tar-split v0.12.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/sys v0.38.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index f6de17f..0b74cb6 100644 --- a/go.sum +++ b/go.sum @@ -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/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc= 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/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=