From dc1674ec551c9ba24a16886d7076a3a91eefdd64 Mon Sep 17 00:00:00 2001 From: lash Date: Sat, 4 Jan 2025 08:40:43 +0000 Subject: [PATCH 01/38] WIP add connection string parser --- internal/storage/parse.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 internal/storage/parse.go diff --git a/internal/storage/parse.go b/internal/storage/parse.go new file mode 100644 index 0000000..0745a72 --- /dev/null +++ b/internal/storage/parse.go @@ -0,0 +1,21 @@ +package storage + +const ( + DBTYPE_MEM = iota + DBTYPE_GDBM + DBTYPE_POSTGRES +) + +type connData struct { + typ int + str string +} + +func toConnData(s string) connData { + var o connData + + if s == "" { + return o + } + return o +} From 2e30739ec9223d2028c558c4b56e1bf03394bec3 Mon Sep 17 00:00:00 2001 From: lash Date: Sat, 4 Jan 2025 09:37:12 +0000 Subject: [PATCH 02/38] Implement connstr --- cmd/africastalking/main.go | 3 +- cmd/async/main.go | 31 +++++++++++----- cmd/http/main.go | 23 ++++++++---- cmd/main.go | 22 ++++++++--- common/storage.go | 14 ++++--- devtools/gen/main.go | 24 ++++++++++-- devtools/store/main.go | 22 +++++++++-- internal/storage/parse.go | 59 ++++++++++++++++++++++++++++-- internal/storage/parse_test.go | 37 +++++++++++++++++++ internal/storage/storageservice.go | 51 ++++++++++++++++---------- internal/testutil/TestEngine.go | 6 ++- 11 files changed, 227 insertions(+), 65 deletions(-) create mode 100644 internal/storage/parse_test.go diff --git a/cmd/africastalking/main.go b/cmd/africastalking/main.go index 2696b51..11c6512 100644 --- a/cmd/africastalking/main.go +++ b/cmd/africastalking/main.go @@ -15,11 +15,10 @@ import ( "git.defalsify.org/vise.git/logging" "git.defalsify.org/vise.git/resource" - "git.grassecon.net/urdt/ussd/common" "git.grassecon.net/urdt/ussd/config" "git.grassecon.net/urdt/ussd/initializers" "git.grassecon.net/urdt/ussd/internal/handlers" - httpserver "git.grassecon.net/urdt/ussd/internal/http/at" + at "git.grassecon.net/urdt/ussd/internal/http/at" "git.grassecon.net/urdt/ussd/internal/storage" "git.grassecon.net/urdt/ussd/remote" ) diff --git a/cmd/async/main.go b/cmd/async/main.go index 9cd04b3..a9a0b1e 100644 --- a/cmd/async/main.go +++ b/cmd/async/main.go @@ -7,6 +7,7 @@ import ( "os" "os/signal" "path" + "path/filepath" "syscall" "git.defalsify.org/vise.git/engine" @@ -47,24 +48,33 @@ func main() { config.LoadConfig() var sessionId string - var dbDir string + var connStr string var resourceDir string var size uint var database string var engineDebug bool var host string var port uint + var err error + flag.StringVar(&sessionId, "session-id", "075xx2123", "session id") - flag.StringVar(&dbDir, "dbdir", ".state", "database dir to read from") flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir") - flag.StringVar(&database, "db", "gdbm", "database to be used") + flag.StringVar(&connStr, "c", ".", "connection string") flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.UintVar(&size, "s", 160, "max size of output") flag.StringVar(&host, "h", initializers.GetEnv("HOST", "127.0.0.1"), "http host") flag.UintVar(&port, "p", initializers.GetEnvUint("PORT", 7123), "http port") flag.Parse() - logg.Infof("start command", "dbdir", dbDir, "resourcedir", resourceDir, "outputsize", size, "sessionId", sessionId) + if connStr == "." { + connStr, err = filepath.Abs(".state/state.gdbm") + if err != nil { + fmt.Fprintf(os.Stderr, "auto connstr generate error: %v", err) + os.Exit(1) + } + } + + logg.Infof("start command", "connstr", connStr, "resourcedir", resourceDir, "outputsize", size, "sessionId", sessionId) ctx := context.Background() ctx = context.WithValue(ctx, "Database", database) @@ -81,18 +91,19 @@ func main() { cfg.EngineDebug = true } - menuStorageService := storage.NewMenuStorageService(dbDir, resourceDir) + menuStorageService := storage.NewMenuStorageService(resourceDir) + err = menuStorageService.SetConn(connStr) + if err != nil { + fmt.Fprintf(os.Stderr, err.Error()) + os.Exit(1) + } + rs, err := menuStorageService.GetResource(ctx) if err != nil { fmt.Fprintf(os.Stderr, err.Error()) os.Exit(1) } - err = menuStorageService.EnsureDbDir() - if err != nil { - fmt.Fprintf(os.Stderr, err.Error()) - os.Exit(1) - } userdataStore, err := menuStorageService.GetUserdataDb(ctx) if err != nil { diff --git a/cmd/http/main.go b/cmd/http/main.go index 6ddfded..dec4e64 100644 --- a/cmd/http/main.go +++ b/cmd/http/main.go @@ -8,6 +8,7 @@ import ( "os" "os/signal" "path" + "path/filepath" "strconv" "syscall" @@ -36,23 +37,31 @@ func init() { func main() { config.LoadConfig() - var dbDir string + var connStr string var resourceDir string var size uint var database string var engineDebug bool var host string var port uint - flag.StringVar(&dbDir, "dbdir", ".state", "database dir to read from") + var err error + flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir") - flag.StringVar(&database, "db", "gdbm", "database to be used") flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.UintVar(&size, "s", 160, "max size of output") flag.StringVar(&host, "h", initializers.GetEnv("HOST", "127.0.0.1"), "http host") flag.UintVar(&port, "p", initializers.GetEnvUint("PORT", 7123), "http port") flag.Parse() - logg.Infof("start command", "dbdir", dbDir, "resourcedir", resourceDir, "outputsize", size) + if connStr == "." { + connStr, err = filepath.Abs(".state/state.gdbm") + if err != nil { + fmt.Fprintf(os.Stderr, "auto connstr generate error: %v", err) + os.Exit(1) + } + } + + logg.Infof("start command", "connstr", connStr, "resourcedir", resourceDir, "outputsize", size) ctx := context.Background() ctx = context.WithValue(ctx, "Database", database) @@ -69,14 +78,14 @@ func main() { cfg.EngineDebug = true } - menuStorageService := storage.NewMenuStorageService(dbDir, resourceDir) - rs, err := menuStorageService.GetResource(ctx) + menuStorageService := storage.NewMenuStorageService(resourceDir) + err = menuStorageService.SetConn(connStr) if err != nil { fmt.Fprintf(os.Stderr, err.Error()) os.Exit(1) } - err = menuStorageService.EnsureDbDir() + rs, err := menuStorageService.GetResource(ctx) if err != nil { fmt.Fprintf(os.Stderr, err.Error()) os.Exit(1) diff --git a/cmd/main.go b/cmd/main.go index 4fd084f..c45615b 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -6,6 +6,7 @@ import ( "fmt" "os" "path" + "path/filepath" "git.defalsify.org/vise.git/engine" "git.defalsify.org/vise.git/logging" @@ -30,19 +31,28 @@ func init() { func main() { config.LoadConfig() - var dbDir string + var connStr string var size uint var sessionId string var database string var engineDebug bool + var err error + flag.StringVar(&sessionId, "session-id", "075xx2123", "session id") - flag.StringVar(&database, "db", "gdbm", "database to be used") - flag.StringVar(&dbDir, "dbdir", ".state", "database dir to read from") + flag.StringVar(&connStr, "c", ".", "connection string") flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.UintVar(&size, "s", 160, "max size of output") flag.Parse() - logg.Infof("start command", "dbdir", dbDir, "outputsize", size) + if connStr == "." { + connStr, err = filepath.Abs(".state/state.gdbm") + if err != nil { + fmt.Fprintf(os.Stderr, "auto connstr generate error: %v", err) + os.Exit(1) + } + } + + logg.Infof("start command", "connstr", connStr, "outputsize", size) ctx := context.Background() ctx = context.WithValue(ctx, "SessionId", sessionId) @@ -58,9 +68,9 @@ func main() { } resourceDir := scriptDir - menuStorageService := storage.NewMenuStorageService(dbDir, resourceDir) + menuStorageService := storage.NewMenuStorageService(resourceDir) - err := menuStorageService.EnsureDbDir() + err = menuStorageService.SetConn(connStr) if err != nil { fmt.Fprintf(os.Stderr, err.Error()) os.Exit(1) diff --git a/common/storage.go b/common/storage.go index d37bce3..c8a4ff2 100644 --- a/common/storage.go +++ b/common/storage.go @@ -23,17 +23,19 @@ type StorageServices interface { GetPersister(ctx context.Context) (*persist.Persister, error) GetUserdataDb(ctx context.Context) (db.Db, error) GetResource(ctx context.Context) (resource.Resource, error) - EnsureDbDir() error + SetConn(connStr string) error } type StorageService struct { svc *storage.MenuStorageService } -func NewStorageService(dbDir string) *StorageService { - return &StorageService{ - svc: storage.NewMenuStorageService(dbDir, ""), +func NewStorageService(connStr string) (*StorageService, error) { + svc := &StorageService{ + svc: storage.NewMenuStorageService(""), } + err := svc.SetConn(connStr) + return svc, err } func(ss *StorageService) GetPersister(ctx context.Context) (*persist.Persister, error) { @@ -48,6 +50,6 @@ func(ss *StorageService) GetResource(ctx context.Context) (resource.Resource, er return nil, errors.New("not implemented") } -func(ss *StorageService) EnsureDbDir() error { - return ss.svc.EnsureDbDir() +func(ss *StorageService) SetConn(connStr string) error { + return ss.svc.SetConn(connStr) } diff --git a/devtools/gen/main.go b/devtools/gen/main.go index b9e2aed..448cc16 100644 --- a/devtools/gen/main.go +++ b/devtools/gen/main.go @@ -7,6 +7,7 @@ import ( "fmt" "os" "path" + "path/filepath" "git.defalsify.org/vise.git/logging" "git.grassecon.net/urdt/ussd/config" @@ -28,23 +29,38 @@ func init() { func main() { config.LoadConfig() - var dbDir string + var connStr string var sessionId string var database string var engineDebug bool + var err error flag.StringVar(&sessionId, "session-id", "075xx2123", "session id") - flag.StringVar(&database, "db", "gdbm", "database to be used") - flag.StringVar(&dbDir, "dbdir", ".state", "database dir to read from") + flag.StringVar(&connStr, "c", ".", "connection string") flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.Parse() + if connStr == "." { + connStr, err = filepath.Abs(".state/state.gdbm") + if err != nil { + fmt.Fprintf(os.Stderr, "auto connstr generate error: %v", err) + os.Exit(1) + } + } + + logg.Infof("start command", "connstr", connStr) + ctx := context.Background() ctx = context.WithValue(ctx, "SessionId", sessionId) ctx = context.WithValue(ctx, "Database", database) resourceDir := scriptDir - menuStorageService := storage.NewMenuStorageService(dbDir, resourceDir) + menuStorageService := storage.NewMenuStorageService(resourceDir) + err = menuStorageService.SetConn(connStr) + if err != nil { + fmt.Fprintf(os.Stderr, "connection string error: %v", err) + os.Exit(1) + } store, err := menuStorageService.GetUserdataDb(ctx) if err != nil { diff --git a/devtools/store/main.go b/devtools/store/main.go index 8bd4d16..ed54ba3 100644 --- a/devtools/store/main.go +++ b/devtools/store/main.go @@ -6,6 +6,7 @@ import ( "fmt" "os" "path" + "path/filepath" "git.grassecon.net/urdt/ussd/config" "git.grassecon.net/urdt/ussd/initializers" @@ -28,23 +29,36 @@ func init() { func main() { config.LoadConfig() - var dbDir string + var connStr string var sessionId string var database string var engineDebug bool + var err error flag.StringVar(&sessionId, "session-id", "075xx2123", "session id") - flag.StringVar(&database, "db", "gdbm", "database to be used") - flag.StringVar(&dbDir, "dbdir", ".state", "database dir to read from") + flag.StringVar(&connStr, "c", ".state", "connection string") flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.Parse() + if connStr == "." { + connStr, err = filepath.Abs(".state/state.gdbm") + if err != nil { + fmt.Fprintf(os.Stderr, "auto connstr generate error: %v", err) + os.Exit(1) + } + } + ctx := context.Background() ctx = context.WithValue(ctx, "SessionId", sessionId) ctx = context.WithValue(ctx, "Database", database) resourceDir := scriptDir - menuStorageService := storage.NewMenuStorageService(dbDir, resourceDir) + menuStorageService := storage.NewMenuStorageService(resourceDir) + err = menuStorageService.SetConn(connStr) + if err != nil { + fmt.Fprintf(os.Stderr, "connection string error: %v", err) + os.Exit(1) + } store, err := menuStorageService.GetUserdataDb(ctx) if err != nil { diff --git a/internal/storage/parse.go b/internal/storage/parse.go index 0745a72..d4bc564 100644 --- a/internal/storage/parse.go +++ b/internal/storage/parse.go @@ -1,5 +1,11 @@ package storage +import ( + "fmt" + "net/url" + "path" +) + const ( DBTYPE_MEM = iota DBTYPE_GDBM @@ -11,11 +17,56 @@ type connData struct { str string } -func toConnData(s string) connData { +func (cd *connData) DbType() int { + return cd.typ +} + +func (cd *connData) String() string { + return cd.str +} + +func probePostgres(s string) (string, bool) { + v, err := url.Parse(s) + if err != nil { + return "", false + } + if v.Scheme != "postgres" { + return "", false + } + return s, true +} + +func probeGdbm(s string) (string, bool) { + if !path.IsAbs(s) { + return "", false + } + if path.Ext(s) != ".gdbm" { + return "", false + } + s = path.Clean(s) + return s, true +} + +func toConnData(connStr string) (connData, error) { var o connData - if s == "" { - return o + if connStr == "" { + return o, nil } - return o + + v, ok := probePostgres(connStr) + if ok { + o.typ = DBTYPE_POSTGRES + o.str = v + return o, nil + } + + v, ok = probeGdbm(connStr) + if ok { + o.typ = DBTYPE_GDBM + o.str = v + return o, nil + } + + return o, fmt.Errorf("invalid connection string: %s", connStr) } diff --git a/internal/storage/parse_test.go b/internal/storage/parse_test.go new file mode 100644 index 0000000..d4b8ecb --- /dev/null +++ b/internal/storage/parse_test.go @@ -0,0 +1,37 @@ +package storage + +import ( + "testing" +) + +func TestParseConnStr(t *testing.T) { + svc := NewMenuStorageService("") + err := svc.SetConn("postgres://foo:bar@localhost:5432/baz") + if err != nil { + t.Fatal(err) + } + err = svc.SetConn("/foo/bar/baz.gdbm") + if err != nil { + t.Fatal(err) + } + err = svc.SetConn("foo/bar/baz.gdbm") + if err == nil { + t.Fatalf("expected error") + } + err = svc.SetConn("http://foo/bar") + if err == nil { + t.Fatalf("expected error") + } + err = svc.SetConn("foo/bar/baz.txt") + if err == nil { + t.Fatalf("expected error") + } + err = svc.SetConn("/foo/bar") + if err == nil { + t.Fatalf("expected error") + } + err = svc.SetConn("foo/bar") + if err == nil { + t.Fatalf("expected error") + } +} diff --git a/internal/storage/storageservice.go b/internal/storage/storageservice.go index 04e75ce..6a42bac 100644 --- a/internal/storage/storageservice.go +++ b/internal/storage/storageservice.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "os" - "path" "git.defalsify.org/vise.git/db" fsdb "git.defalsify.org/vise.git/db/fs" @@ -24,11 +23,12 @@ type StorageService interface { GetPersister(ctx context.Context) (*persist.Persister, error) GetUserdataDb(ctx context.Context) db.Db GetResource(ctx context.Context) (resource.Resource, error) - EnsureDbDir() error + SetConn(connStr string) error } type MenuStorageService struct { - dbDir string + //dbDir string + conn connData resourceDir string resourceStore db.Db stateStore db.Db @@ -51,36 +51,47 @@ func buildConnStr() string { return connString } -func NewMenuStorageService(dbDir string, resourceDir string) *MenuStorageService { +func NewMenuStorageService(resourceDir string) *MenuStorageService { return &MenuStorageService{ - dbDir: dbDir, resourceDir: resourceDir, } } -func (ms *MenuStorageService) getOrCreateDb(ctx context.Context, existingDb db.Db, fileName string) (db.Db, error) { - database, ok := ctx.Value("Database").(string) - if !ok { - return nil, fmt.Errorf("failed to select the database") +func (ms *MenuStorageService) SetConn(connStr string) error { + o, err := toConnData(connStr) + if err != nil { + return err } + ms.conn = o + return nil +} + +func (ms *MenuStorageService) getOrCreateDb(ctx context.Context, existingDb db.Db, fileName string) (db.Db, error) { + var newDb db.Db + var err error +// database, ok := ctx.Value("Database").(string) +// if !ok { +// return nil, fmt.Errorf("failed to select the database") +// } if existingDb != nil { return existingDb, nil } - var newDb db.Db - var err error - if database == "postgres" { + dbTyp := ms.conn.DbType() + if dbTyp == DBTYPE_POSTGRES { newDb = postgres.NewPgDb() - connStr := buildConnStr() - err = newDb.Connect(ctx, connStr) - } else { + } else if dbTyp == DBTYPE_GDBM { + err = ms.ensureDbDir() + if err != nil { + return nil, err + } newDb = gdbmstorage.NewThreadGdbmDb() - storeFile := path.Join(ms.dbDir, fileName) - err = newDb.Connect(ctx, storeFile) + } else { + return nil, fmt.Errorf("unsupported connection string: %s", ms.conn.String()) } - + err = newDb.Connect(ctx, ms.conn.String()) if err != nil { return nil, err } @@ -137,8 +148,8 @@ func (ms *MenuStorageService) GetStateStore(ctx context.Context) (db.Db, error) return ms.stateStore, nil } -func (ms *MenuStorageService) EnsureDbDir() error { - err := os.MkdirAll(ms.dbDir, 0700) +func (ms *MenuStorageService) ensureDbDir() error { + err := os.MkdirAll(ms.conn.String(), 0700) if err != nil { return fmt.Errorf("state dir create exited with error: %v\n", err) } diff --git a/internal/testutil/TestEngine.go b/internal/testutil/TestEngine.go index 3fcb307..63e9e84 100644 --- a/internal/testutil/TestEngine.go +++ b/internal/testutil/TestEngine.go @@ -41,9 +41,11 @@ func TestEngine(sessionId string) (engine.Engine, func(), chan bool) { dbDir := ".test_state" resourceDir := scriptDir - menuStorageService := storage.NewMenuStorageService(dbDir, resourceDir) + //menuStorageService := storage.NewMenuStorageService(dbDir, resourceDir) + menuStorageService := storage.NewMenuStorageService(resourceDir) - err := menuStorageService.EnsureDbDir() + //err := menuStorageService.EnsureDbDir() + err := menuStorageService.SetConn(dbDir) if err != nil { fmt.Fprintf(os.Stderr, err.Error()) os.Exit(1) From c3cbe1cd927df9c94faa73d8a6417a71d18986bb Mon Sep 17 00:00:00 2001 From: lash Date: Sat, 4 Jan 2025 09:41:17 +0000 Subject: [PATCH 03/38] Add connstr to last executable --- cmd/africastalking/main.go | 24 +++++++++++++++++------- cmd/http/main.go | 1 + 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/cmd/africastalking/main.go b/cmd/africastalking/main.go index cb83a3a..e1035e9 100644 --- a/cmd/africastalking/main.go +++ b/cmd/africastalking/main.go @@ -8,6 +8,7 @@ import ( "os" "os/signal" "path" + "path/filepath" "strconv" "syscall" @@ -36,23 +37,32 @@ func init() { func main() { config.LoadConfig() - var dbDir string + var connStr string var resourceDir string var size uint var database string var engineDebug bool var host string var port uint - flag.StringVar(&dbDir, "dbdir", ".state", "database dir to read from") + var err error + flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir") - flag.StringVar(&database, "db", "gdbm", "database to be used") + flag.StringVar(&connStr, "c", ".", "connection string") flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.UintVar(&size, "s", 160, "max size of output") flag.StringVar(&host, "h", initializers.GetEnv("HOST", "127.0.0.1"), "http host") flag.UintVar(&port, "p", initializers.GetEnvUint("PORT", 7123), "http port") flag.Parse() - logg.Infof("start command", "build", build, "dbdir", dbDir, "resourcedir", resourceDir, "outputsize", size) + if connStr == "." { + connStr, err = filepath.Abs(".state/state.gdbm") + if err != nil { + fmt.Fprintf(os.Stderr, "auto connstr generate error: %v", err) + os.Exit(1) + } + } + + logg.Infof("start command", "build", build, "dbdir", connStr, "resourcedir", resourceDir, "outputsize", size) ctx := context.Background() ctx = context.WithValue(ctx, "Database", database) @@ -69,14 +79,14 @@ func main() { cfg.EngineDebug = true } - menuStorageService := storage.NewMenuStorageService(dbDir, resourceDir) - rs, err := menuStorageService.GetResource(ctx) + menuStorageService := storage.NewMenuStorageService(resourceDir) + err = menuStorageService.SetConn(connStr) if err != nil { fmt.Fprintf(os.Stderr, err.Error()) os.Exit(1) } - err = menuStorageService.EnsureDbDir() + rs, err := menuStorageService.GetResource(ctx) if err != nil { fmt.Fprintf(os.Stderr, err.Error()) os.Exit(1) diff --git a/cmd/http/main.go b/cmd/http/main.go index dec4e64..070cf79 100644 --- a/cmd/http/main.go +++ b/cmd/http/main.go @@ -47,6 +47,7 @@ func main() { var err error flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir") + flag.StringVar(&connStr, "c", ".", "connection string") flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.UintVar(&size, "s", 160, "max size of output") flag.StringVar(&host, "h", initializers.GetEnv("HOST", "127.0.0.1"), "http host") From dc61d055844fffa71cab6c96893436f14360fa68 Mon Sep 17 00:00:00 2001 From: lash Date: Sat, 4 Jan 2025 12:16:28 +0000 Subject: [PATCH 04/38] WIP revert connstr gdbm to dir only, add file as table spec --- internal/storage/storageservice.go | 8 +++++-- .../testutil/{TestEngine.go => engine.go} | 22 +++++++++++-------- internal/testutil/engine_test.go | 15 +++++++++++++ menutraversal_test/menu_traversal_test.go | 11 +++++++--- 4 files changed, 42 insertions(+), 14 deletions(-) rename internal/testutil/{TestEngine.go => engine.go} (82%) create mode 100644 internal/testutil/engine_test.go diff --git a/internal/storage/storageservice.go b/internal/storage/storageservice.go index 6a42bac..e14b7a4 100644 --- a/internal/storage/storageservice.go +++ b/internal/storage/storageservice.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "os" + "path" "git.defalsify.org/vise.git/db" fsdb "git.defalsify.org/vise.git/db/fs" @@ -66,7 +67,7 @@ func (ms *MenuStorageService) SetConn(connStr string) error { return nil } -func (ms *MenuStorageService) getOrCreateDb(ctx context.Context, existingDb db.Db, fileName string) (db.Db, error) { +func (ms *MenuStorageService) getOrCreateDb(ctx context.Context, existingDb db.Db, section string) (db.Db, error) { var newDb db.Db var err error // database, ok := ctx.Value("Database").(string) @@ -79,6 +80,7 @@ func (ms *MenuStorageService) getOrCreateDb(ctx context.Context, existingDb db.D } + connStr := ms.conn.String() dbTyp := ms.conn.DbType() if dbTyp == DBTYPE_POSTGRES { newDb = postgres.NewPgDb() @@ -87,11 +89,13 @@ func (ms *MenuStorageService) getOrCreateDb(ctx context.Context, existingDb db.D if err != nil { return nil, err } + connStr = path.Join(connStr, section) newDb = gdbmstorage.NewThreadGdbmDb() } else { return nil, fmt.Errorf("unsupported connection string: %s", ms.conn.String()) } - err = newDb.Connect(ctx, ms.conn.String()) + logg.DebugCtxf(ctx, "connecting to db", "conn", connStr) + err = newDb.Connect(ctx, connStr) if err != nil { return nil, err } diff --git a/internal/testutil/TestEngine.go b/internal/testutil/engine.go similarity index 82% rename from internal/testutil/TestEngine.go rename to internal/testutil/engine.go index 63e9e84..4b02e32 100644 --- a/internal/testutil/TestEngine.go +++ b/internal/testutil/engine.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "path" + "path/filepath" "time" "git.defalsify.org/vise.git/engine" @@ -27,7 +28,6 @@ var ( func TestEngine(sessionId string) (engine.Engine, func(), chan bool) { ctx := context.Background() ctx = context.WithValue(ctx, "SessionId", sessionId) - ctx = context.WithValue(ctx, "Database", "gdbm") pfp := path.Join(scriptDir, "pp.csv") var eventChannel = make(chan bool) @@ -39,39 +39,43 @@ func TestEngine(sessionId string) (engine.Engine, func(), chan bool) { FlagCount: uint32(128), } - dbDir := ".test_state" + connStr, err := filepath.Abs(".test_state/state.gdbm") + if err != nil { + fmt.Fprintf(os.Stderr, "connstr err: %v", err) + os.Exit(1) + } resourceDir := scriptDir - //menuStorageService := storage.NewMenuStorageService(dbDir, resourceDir) + //menuStorageService := storage.NewMenuStorageService(connStr, resourceDir) menuStorageService := storage.NewMenuStorageService(resourceDir) //err := menuStorageService.EnsureDbDir() - err := menuStorageService.SetConn(dbDir) + err = menuStorageService.SetConn(connStr) if err != nil { - fmt.Fprintf(os.Stderr, err.Error()) + fmt.Fprintf(os.Stderr, "conn error: %v", err) os.Exit(1) } rs, err := menuStorageService.GetResource(ctx) if err != nil { - fmt.Fprintf(os.Stderr, err.Error()) + fmt.Fprintf(os.Stderr, "resource error: %v", err) os.Exit(1) } pe, err := menuStorageService.GetPersister(ctx) if err != nil { - fmt.Fprintf(os.Stderr, err.Error()) + fmt.Fprintf(os.Stderr, "persister error: %v", err) os.Exit(1) } userDataStore, err := menuStorageService.GetUserdataDb(ctx) if err != nil { - fmt.Fprintf(os.Stderr, err.Error()) + fmt.Fprintf(os.Stderr, "userdb error: %v", err) os.Exit(1) } dbResource, ok := rs.(*resource.DbResource) if !ok { - fmt.Fprintf(os.Stderr, err.Error()) + fmt.Fprintf(os.Stderr, "dbresource cast error") os.Exit(1) } diff --git a/internal/testutil/engine_test.go b/internal/testutil/engine_test.go new file mode 100644 index 0000000..f747468 --- /dev/null +++ b/internal/testutil/engine_test.go @@ -0,0 +1,15 @@ +package testutil + +import ( + "testing" +) + +func TestCreateEngine(t *testing.T) { + o, clean, eventC := TestEngine("foo") + defer clean() + defer func() { + <-eventC + close(eventC) + }() + _ = o +} diff --git a/menutraversal_test/menu_traversal_test.go b/menutraversal_test/menu_traversal_test.go index 6b6b3da..52e2273 100644 --- a/menutraversal_test/menu_traversal_test.go +++ b/menutraversal_test/menu_traversal_test.go @@ -7,6 +7,7 @@ import ( "log" "math/rand" "os" + "path/filepath" "regexp" "testing" @@ -17,7 +18,6 @@ import ( var ( testData = driver.ReadData() - testStore = ".test_state" sessionID string src = rand.NewSource(42) g = rand.New(src) @@ -25,6 +25,11 @@ var ( var groupTestFile = flag.String("test-file", "group_test.json", "The test file to use for running the group tests") +func testStore() string { + v, _ := filepath.Abs(".test_state/state.gdbm") + return v +} + func GenerateSessionId() string { uu := uuid.NewGenWithOptions(uuid.WithRandomReader(g)) v, err := uu.NewV4() @@ -81,8 +86,8 @@ func extractSendAmount(response []byte) string { func TestMain(m *testing.M) { sessionID = GenerateSessionId() defer func() { - if err := os.RemoveAll(testStore); err != nil { - log.Fatalf("Failed to delete state store %s: %v", testStore, err) + if err := os.RemoveAll(testStore()); err != nil { + log.Fatalf("Failed to delete state store %s: %v", testStore(), err) } }() m.Run() From 2992f7ae8e9983c51aeb1328197270db395b5b1a Mon Sep 17 00:00:00 2001 From: lash Date: Sat, 4 Jan 2025 13:19:30 +0000 Subject: [PATCH 05/38] Update executables with new conn str --- cmd/africastalking/main.go | 7 ++++--- cmd/async/main.go | 6 +++--- cmd/http/main.go | 6 +++--- cmd/main.go | 6 +++--- internal/storage/parse.go | 3 --- internal/storage/storageservice.go | 1 - 6 files changed, 13 insertions(+), 16 deletions(-) diff --git a/cmd/africastalking/main.go b/cmd/africastalking/main.go index e1035e9..8820215 100644 --- a/cmd/africastalking/main.go +++ b/cmd/africastalking/main.go @@ -34,6 +34,7 @@ var ( func init() { initializers.LoadEnvVariables() } + func main() { config.LoadConfig() @@ -47,15 +48,15 @@ func main() { var err error flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir") - flag.StringVar(&connStr, "c", ".", "connection string") + flag.StringVar(&connStr, "c", ".state", "connection string") flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.UintVar(&size, "s", 160, "max size of output") flag.StringVar(&host, "h", initializers.GetEnv("HOST", "127.0.0.1"), "http host") flag.UintVar(&port, "p", initializers.GetEnvUint("PORT", 7123), "http port") flag.Parse() - if connStr == "." { - connStr, err = filepath.Abs(".state/state.gdbm") + if connStr == ".state" { + connStr, err = filepath.Abs(connStr) if err != nil { fmt.Fprintf(os.Stderr, "auto connstr generate error: %v", err) os.Exit(1) diff --git a/cmd/async/main.go b/cmd/async/main.go index a9a0b1e..8b15031 100644 --- a/cmd/async/main.go +++ b/cmd/async/main.go @@ -59,15 +59,15 @@ func main() { flag.StringVar(&sessionId, "session-id", "075xx2123", "session id") flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir") - flag.StringVar(&connStr, "c", ".", "connection string") + flag.StringVar(&connStr, "c", ".state", "connection string") flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.UintVar(&size, "s", 160, "max size of output") flag.StringVar(&host, "h", initializers.GetEnv("HOST", "127.0.0.1"), "http host") flag.UintVar(&port, "p", initializers.GetEnvUint("PORT", 7123), "http port") flag.Parse() - if connStr == "." { - connStr, err = filepath.Abs(".state/state.gdbm") + if connStr == ".state" { + connStr, err = filepath.Abs(connStr) if err != nil { fmt.Fprintf(os.Stderr, "auto connstr generate error: %v", err) os.Exit(1) diff --git a/cmd/http/main.go b/cmd/http/main.go index 070cf79..f42d4d9 100644 --- a/cmd/http/main.go +++ b/cmd/http/main.go @@ -47,15 +47,15 @@ func main() { var err error flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir") - flag.StringVar(&connStr, "c", ".", "connection string") + flag.StringVar(&connStr, "c", ".state", "connection string") flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.UintVar(&size, "s", 160, "max size of output") flag.StringVar(&host, "h", initializers.GetEnv("HOST", "127.0.0.1"), "http host") flag.UintVar(&port, "p", initializers.GetEnvUint("PORT", 7123), "http port") flag.Parse() - if connStr == "." { - connStr, err = filepath.Abs(".state/state.gdbm") + if connStr == ".state" { + connStr, err = filepath.Abs(connStr) if err != nil { fmt.Fprintf(os.Stderr, "auto connstr generate error: %v", err) os.Exit(1) diff --git a/cmd/main.go b/cmd/main.go index c45615b..2506012 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -39,13 +39,13 @@ func main() { var err error flag.StringVar(&sessionId, "session-id", "075xx2123", "session id") - flag.StringVar(&connStr, "c", ".", "connection string") + flag.StringVar(&connStr, "c", ".state", "connection string") flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.UintVar(&size, "s", 160, "max size of output") flag.Parse() - if connStr == "." { - connStr, err = filepath.Abs(".state/state.gdbm") + if connStr == ".state" { + connStr, err = filepath.Abs(connStr) if err != nil { fmt.Fprintf(os.Stderr, "auto connstr generate error: %v", err) os.Exit(1) diff --git a/internal/storage/parse.go b/internal/storage/parse.go index d4bc564..a97b938 100644 --- a/internal/storage/parse.go +++ b/internal/storage/parse.go @@ -40,9 +40,6 @@ func probeGdbm(s string) (string, bool) { if !path.IsAbs(s) { return "", false } - if path.Ext(s) != ".gdbm" { - return "", false - } s = path.Clean(s) return s, true } diff --git a/internal/storage/storageservice.go b/internal/storage/storageservice.go index e14b7a4..68e6916 100644 --- a/internal/storage/storageservice.go +++ b/internal/storage/storageservice.go @@ -28,7 +28,6 @@ type StorageService interface { } type MenuStorageService struct { - //dbDir string conn connData resourceDir string resourceStore db.Db From a4d6cef9c0fa3e077b6691d4ced7085a7e7f52c0 Mon Sep 17 00:00:00 2001 From: lash Date: Sat, 4 Jan 2025 13:21:31 +0000 Subject: [PATCH 06/38] Remove commented code --- internal/storage/storageservice.go | 4 ---- internal/testutil/engine.go | 2 -- 2 files changed, 6 deletions(-) diff --git a/internal/storage/storageservice.go b/internal/storage/storageservice.go index 68e6916..c9c138f 100644 --- a/internal/storage/storageservice.go +++ b/internal/storage/storageservice.go @@ -69,10 +69,6 @@ func (ms *MenuStorageService) SetConn(connStr string) error { func (ms *MenuStorageService) getOrCreateDb(ctx context.Context, existingDb db.Db, section string) (db.Db, error) { var newDb db.Db var err error -// database, ok := ctx.Value("Database").(string) -// if !ok { -// return nil, fmt.Errorf("failed to select the database") -// } if existingDb != nil { return existingDb, nil diff --git a/internal/testutil/engine.go b/internal/testutil/engine.go index 4b02e32..4234773 100644 --- a/internal/testutil/engine.go +++ b/internal/testutil/engine.go @@ -45,10 +45,8 @@ func TestEngine(sessionId string) (engine.Engine, func(), chan bool) { os.Exit(1) } resourceDir := scriptDir - //menuStorageService := storage.NewMenuStorageService(connStr, resourceDir) menuStorageService := storage.NewMenuStorageService(resourceDir) - //err := menuStorageService.EnsureDbDir() err = menuStorageService.SetConn(connStr) if err != nil { fmt.Fprintf(os.Stderr, "conn error: %v", err) From f61e65f4fe38380978fc7961a86ece26c50c2eac Mon Sep 17 00:00:00 2001 From: lash Date: Sat, 4 Jan 2025 13:24:14 +0000 Subject: [PATCH 07/38] Rename accountservice testservice source file --- .../testservice/{TestAccountService.go => accountservice.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename internal/testutil/testservice/{TestAccountService.go => accountservice.go} (100%) diff --git a/internal/testutil/testservice/TestAccountService.go b/internal/testutil/testservice/accountservice.go similarity index 100% rename from internal/testutil/testservice/TestAccountService.go rename to internal/testutil/testservice/accountservice.go From 450dfa02cc016b525d743a864530581e0372acda Mon Sep 17 00:00:00 2001 From: lash Date: Sat, 4 Jan 2025 20:36:18 +0000 Subject: [PATCH 08/38] Refactor to use conndata as menustorageservice conn arg --- cmd/africastalking/main.go | 16 ++++++++-------- cmd/async/main.go | 20 ++++++++++---------- cmd/http/main.go | 24 ++++++++++-------------- cmd/main.go | 23 +++++++++-------------- common/storage.go | 12 ++++++------ config/config.go | 15 +++++++++++++-- devtools/gen/main.go | 26 +++++++++++--------------- devtools/store/main.go | 16 ++++++++-------- internal/storage/parse.go | 10 +++++----- internal/storage/parse_test.go | 25 ++++++++----------------- internal/storage/storageservice.go | 29 ++++------------------------- internal/testutil/engine.go | 7 ++++++- 12 files changed, 98 insertions(+), 125 deletions(-) diff --git a/cmd/africastalking/main.go b/cmd/africastalking/main.go index 8820215..6035fc6 100644 --- a/cmd/africastalking/main.go +++ b/cmd/africastalking/main.go @@ -8,7 +8,6 @@ import ( "os" "os/signal" "path" - "path/filepath" "strconv" "syscall" @@ -55,12 +54,13 @@ func main() { flag.UintVar(&port, "p", initializers.GetEnvUint("PORT", 7123), "http port") flag.Parse() - if connStr == ".state" { - connStr, err = filepath.Abs(connStr) - if err != nil { - fmt.Fprintf(os.Stderr, "auto connstr generate error: %v", err) - os.Exit(1) - } + if connStr != "" { + connStr = config.DbConn + } + connData, err := storage.ToConnData(config.DbConn) + if err != nil { + fmt.Fprintf(os.Stderr, "connstr err: %v", err) + os.Exit(1) } logg.Infof("start command", "build", build, "dbdir", connStr, "resourcedir", resourceDir, "outputsize", size) @@ -81,7 +81,7 @@ func main() { } menuStorageService := storage.NewMenuStorageService(resourceDir) - err = menuStorageService.SetConn(connStr) + menuStorageService = menuStorageService.WithConn(connData) if err != nil { fmt.Fprintf(os.Stderr, err.Error()) os.Exit(1) diff --git a/cmd/async/main.go b/cmd/async/main.go index 8b15031..17ea2a1 100644 --- a/cmd/async/main.go +++ b/cmd/async/main.go @@ -7,7 +7,6 @@ import ( "os" "os/signal" "path" - "path/filepath" "syscall" "git.defalsify.org/vise.git/engine" @@ -47,8 +46,8 @@ func (p *asyncRequestParser) GetInput(r any) ([]byte, error) { func main() { config.LoadConfig() - var sessionId string var connStr string + var sessionId string var resourceDir string var size uint var database string @@ -59,19 +58,20 @@ func main() { flag.StringVar(&sessionId, "session-id", "075xx2123", "session id") flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir") - flag.StringVar(&connStr, "c", ".state", "connection string") + flag.StringVar(&connStr, "c", "", "connection string") flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.UintVar(&size, "s", 160, "max size of output") flag.StringVar(&host, "h", initializers.GetEnv("HOST", "127.0.0.1"), "http host") flag.UintVar(&port, "p", initializers.GetEnvUint("PORT", 7123), "http port") flag.Parse() - if connStr == ".state" { - connStr, err = filepath.Abs(connStr) - if err != nil { - fmt.Fprintf(os.Stderr, "auto connstr generate error: %v", err) - os.Exit(1) - } + if connStr != "" { + connStr = config.DbConn + } + connData, err := storage.ToConnData(config.DbConn) + if err != nil { + fmt.Fprintf(os.Stderr, "connstr err: %v", err) + os.Exit(1) } logg.Infof("start command", "connstr", connStr, "resourcedir", resourceDir, "outputsize", size, "sessionId", sessionId) @@ -92,7 +92,7 @@ func main() { } menuStorageService := storage.NewMenuStorageService(resourceDir) - err = menuStorageService.SetConn(connStr) + menuStorageService = menuStorageService.WithConn(connData) if err != nil { fmt.Fprintf(os.Stderr, err.Error()) os.Exit(1) diff --git a/cmd/http/main.go b/cmd/http/main.go index f42d4d9..69a9c90 100644 --- a/cmd/http/main.go +++ b/cmd/http/main.go @@ -8,7 +8,6 @@ import ( "os" "os/signal" "path" - "path/filepath" "strconv" "syscall" @@ -47,19 +46,20 @@ func main() { var err error flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir") - flag.StringVar(&connStr, "c", ".state", "connection string") + flag.StringVar(&connStr, "c", "", "connection string") flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.UintVar(&size, "s", 160, "max size of output") flag.StringVar(&host, "h", initializers.GetEnv("HOST", "127.0.0.1"), "http host") flag.UintVar(&port, "p", initializers.GetEnvUint("PORT", 7123), "http port") flag.Parse() - if connStr == ".state" { - connStr, err = filepath.Abs(connStr) - if err != nil { - fmt.Fprintf(os.Stderr, "auto connstr generate error: %v", err) - os.Exit(1) - } + if connStr != "" { + connStr = config.DbConn + } + connData, err := storage.ToConnData(config.DbConn) + if err != nil { + fmt.Fprintf(os.Stderr, "connstr err: %v", err) + os.Exit(1) } logg.Infof("start command", "connstr", connStr, "resourcedir", resourceDir, "outputsize", size) @@ -80,12 +80,8 @@ func main() { } menuStorageService := storage.NewMenuStorageService(resourceDir) - err = menuStorageService.SetConn(connStr) - if err != nil { - fmt.Fprintf(os.Stderr, err.Error()) - os.Exit(1) - } - + menuStorageService = menuStorageService.WithConn(connData) + rs, err := menuStorageService.GetResource(ctx) if err != nil { fmt.Fprintf(os.Stderr, err.Error()) diff --git a/cmd/main.go b/cmd/main.go index 2506012..a475dbe 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -6,7 +6,6 @@ import ( "fmt" "os" "path" - "path/filepath" "git.defalsify.org/vise.git/engine" "git.defalsify.org/vise.git/logging" @@ -39,17 +38,18 @@ func main() { var err error flag.StringVar(&sessionId, "session-id", "075xx2123", "session id") - flag.StringVar(&connStr, "c", ".state", "connection string") + flag.StringVar(&connStr, "c", "", "connection string") flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.UintVar(&size, "s", 160, "max size of output") flag.Parse() - if connStr == ".state" { - connStr, err = filepath.Abs(connStr) - if err != nil { - fmt.Fprintf(os.Stderr, "auto connstr generate error: %v", err) - os.Exit(1) - } + if connStr != "" { + connStr = config.DbConn + } + connData, err := storage.ToConnData(config.DbConn) + if err != nil { + fmt.Fprintf(os.Stderr, "connstr err: %v", err) + os.Exit(1) } logg.Infof("start command", "connstr", connStr, "outputsize", size) @@ -69,12 +69,7 @@ func main() { resourceDir := scriptDir menuStorageService := storage.NewMenuStorageService(resourceDir) - - err = menuStorageService.SetConn(connStr) - if err != nil { - fmt.Fprintf(os.Stderr, err.Error()) - os.Exit(1) - } + menuStorageService = menuStorageService.WithConn(connData) rs, err := menuStorageService.GetResource(ctx) if err != nil { diff --git a/common/storage.go b/common/storage.go index c8a4ff2..6ab5211 100644 --- a/common/storage.go +++ b/common/storage.go @@ -23,19 +23,19 @@ type StorageServices interface { GetPersister(ctx context.Context) (*persist.Persister, error) GetUserdataDb(ctx context.Context) (db.Db, error) GetResource(ctx context.Context) (resource.Resource, error) - SetConn(connStr string) error + SetConn(conn storage.ConnData) } type StorageService struct { svc *storage.MenuStorageService } -func NewStorageService(connStr string) (*StorageService, error) { +func NewStorageService(conn storage.ConnData) (*StorageService, error) { svc := &StorageService{ svc: storage.NewMenuStorageService(""), } - err := svc.SetConn(connStr) - return svc, err + svc.SetConn(conn) + return svc, nil } func(ss *StorageService) GetPersister(ctx context.Context) (*persist.Persister, error) { @@ -50,6 +50,6 @@ func(ss *StorageService) GetResource(ctx context.Context) (resource.Resource, er return nil, errors.New("not implemented") } -func(ss *StorageService) SetConn(connStr string) error { - return ss.svc.SetConn(connStr) +func(ss *StorageService) SetConn(conn storage.ConnData) { + ss.svc = ss.svc.WithConn(conn) } diff --git a/config/config.go b/config/config.go index 3a8e8ed..6f2b225 100644 --- a/config/config.go +++ b/config/config.go @@ -34,6 +34,7 @@ var ( VoucherTransfersURL string VoucherDataURL string CheckAliasURL string + DbConn string ) func setBase() error { @@ -43,14 +44,20 @@ func setBase() error { dataURLBase = initializers.GetEnv("DATA_URL_BASE", "http://localhost:5006") BearerToken = initializers.GetEnv("BEARER_TOKEN", "") - _, err = url.JoinPath(custodialURLBase, "/foo") + _, err = url.Parse(custodialURLBase) if err != nil { return err } - _, err = url.JoinPath(dataURLBase, "/bar") + _, err = url.Parse(dataURLBase) if err != nil { return err } + + return nil +} + +func setConn() error { + DbConn = initializers.GetEnv("DB_CONN", "") return nil } @@ -60,6 +67,10 @@ func LoadConfig() error { if err != nil { return err } + err = setConn() + if err != nil { + return err + } CreateAccountURL, _ = url.JoinPath(custodialURLBase, createAccountPath) TrackStatusURL, _ = url.JoinPath(custodialURLBase, trackStatusPath) BalanceURL, _ = url.JoinPath(custodialURLBase, balancePathPrefix) diff --git a/devtools/gen/main.go b/devtools/gen/main.go index 448cc16..272fd9c 100644 --- a/devtools/gen/main.go +++ b/devtools/gen/main.go @@ -7,7 +7,6 @@ import ( "fmt" "os" "path" - "path/filepath" "git.defalsify.org/vise.git/logging" "git.grassecon.net/urdt/ussd/config" @@ -36,19 +35,20 @@ func main() { var err error flag.StringVar(&sessionId, "session-id", "075xx2123", "session id") - flag.StringVar(&connStr, "c", ".", "connection string") + flag.StringVar(&connStr, "c", "", "connection string") flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.Parse() - if connStr == "." { - connStr, err = filepath.Abs(".state/state.gdbm") - if err != nil { - fmt.Fprintf(os.Stderr, "auto connstr generate error: %v", err) - os.Exit(1) - } + if connStr != "" { + connStr = config.DbConn + } + connData, err := storage.ToConnData(config.DbConn) + if err != nil { + fmt.Fprintf(os.Stderr, "connstr err: %v", err) + os.Exit(1) } - logg.Infof("start command", "connstr", connStr) + logg.Infof("start command", "conn", connData) ctx := context.Background() ctx = context.WithValue(ctx, "SessionId", sessionId) @@ -56,12 +56,8 @@ func main() { resourceDir := scriptDir menuStorageService := storage.NewMenuStorageService(resourceDir) - err = menuStorageService.SetConn(connStr) - if err != nil { - fmt.Fprintf(os.Stderr, "connection string error: %v", err) - os.Exit(1) - } - + menuStorageService = menuStorageService.WithConn(connData) + store, err := menuStorageService.GetUserdataDb(ctx) if err != nil { fmt.Fprintf(os.Stderr, err.Error()) diff --git a/devtools/store/main.go b/devtools/store/main.go index ed54ba3..da988ad 100644 --- a/devtools/store/main.go +++ b/devtools/store/main.go @@ -6,7 +6,6 @@ import ( "fmt" "os" "path" - "path/filepath" "git.grassecon.net/urdt/ussd/config" "git.grassecon.net/urdt/ussd/initializers" @@ -40,12 +39,13 @@ func main() { flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.Parse() - if connStr == "." { - connStr, err = filepath.Abs(".state/state.gdbm") - if err != nil { - fmt.Fprintf(os.Stderr, "auto connstr generate error: %v", err) - os.Exit(1) - } + if connStr != "" { + connStr = config.DbConn + } + connData, err := storage.ToConnData(config.DbConn) + if err != nil { + fmt.Fprintf(os.Stderr, "connstr err: %v", err) + os.Exit(1) } ctx := context.Background() @@ -54,7 +54,7 @@ func main() { resourceDir := scriptDir menuStorageService := storage.NewMenuStorageService(resourceDir) - err = menuStorageService.SetConn(connStr) + menuStorageService = menuStorageService.WithConn(connData) if err != nil { fmt.Fprintf(os.Stderr, "connection string error: %v", err) os.Exit(1) diff --git a/internal/storage/parse.go b/internal/storage/parse.go index a97b938..bb25627 100644 --- a/internal/storage/parse.go +++ b/internal/storage/parse.go @@ -12,16 +12,16 @@ const ( DBTYPE_POSTGRES ) -type connData struct { +type ConnData struct { typ int str string } -func (cd *connData) DbType() int { +func (cd *ConnData) DbType() int { return cd.typ } -func (cd *connData) String() string { +func (cd *ConnData) String() string { return cd.str } @@ -44,8 +44,8 @@ func probeGdbm(s string) (string, bool) { return s, true } -func toConnData(connStr string) (connData, error) { - var o connData +func ToConnData(connStr string) (ConnData, error) { + var o ConnData if connStr == "" { return o, nil diff --git a/internal/storage/parse_test.go b/internal/storage/parse_test.go index d4b8ecb..e18e57c 100644 --- a/internal/storage/parse_test.go +++ b/internal/storage/parse_test.go @@ -5,32 +5,23 @@ import ( ) func TestParseConnStr(t *testing.T) { - svc := NewMenuStorageService("") - err := svc.SetConn("postgres://foo:bar@localhost:5432/baz") + _, err := ToConnData("postgres://foo:bar@localhost:5432/baz") if err != nil { t.Fatal(err) } - err = svc.SetConn("/foo/bar/baz.gdbm") + _, err = ToConnData("/foo/bar") if err != nil { t.Fatal(err) } - err = svc.SetConn("foo/bar/baz.gdbm") + _, err = ToConnData("/foo/bar/") + if err != nil { + t.Fatal(err) + } + _, err = ToConnData("foo/bar") if err == nil { t.Fatalf("expected error") } - err = svc.SetConn("http://foo/bar") - if err == nil { - t.Fatalf("expected error") - } - err = svc.SetConn("foo/bar/baz.txt") - if err == nil { - t.Fatalf("expected error") - } - err = svc.SetConn("/foo/bar") - if err == nil { - t.Fatalf("expected error") - } - err = svc.SetConn("foo/bar") + _, err = ToConnData("http://foo/bar") if err == nil { t.Fatalf("expected error") } diff --git a/internal/storage/storageservice.go b/internal/storage/storageservice.go index c9c138f..e3429bc 100644 --- a/internal/storage/storageservice.go +++ b/internal/storage/storageservice.go @@ -12,7 +12,6 @@ import ( "git.defalsify.org/vise.git/logging" "git.defalsify.org/vise.git/persist" "git.defalsify.org/vise.git/resource" - "git.grassecon.net/urdt/ussd/initializers" gdbmstorage "git.grassecon.net/urdt/ussd/internal/storage/db/gdbm" ) @@ -28,42 +27,22 @@ type StorageService interface { } type MenuStorageService struct { - conn connData + conn ConnData resourceDir string resourceStore db.Db stateStore db.Db userDataStore db.Db } -func buildConnStr() string { - host := initializers.GetEnv("DB_HOST", "localhost") - user := initializers.GetEnv("DB_USER", "postgres") - password := initializers.GetEnv("DB_PASSWORD", "") - dbName := initializers.GetEnv("DB_NAME", "") - port := initializers.GetEnv("DB_PORT", "5432") - - connString := fmt.Sprintf( - "postgres://%s:%s@%s:%s/%s", - user, password, host, port, dbName, - ) - logg.Debugf("pg conn string", "conn", connString) - - return connString -} - func NewMenuStorageService(resourceDir string) *MenuStorageService { return &MenuStorageService{ resourceDir: resourceDir, } } -func (ms *MenuStorageService) SetConn(connStr string) error { - o, err := toConnData(connStr) - if err != nil { - return err - } - ms.conn = o - return nil +func (ms *MenuStorageService) WithConn(conn ConnData) *MenuStorageService { + ms.conn = conn + return ms } func (ms *MenuStorageService) getOrCreateDb(ctx context.Context, existingDb db.Db, section string) (db.Db, error) { diff --git a/internal/testutil/engine.go b/internal/testutil/engine.go index 4234773..3d804ae 100644 --- a/internal/testutil/engine.go +++ b/internal/testutil/engine.go @@ -44,10 +44,15 @@ func TestEngine(sessionId string) (engine.Engine, func(), chan bool) { fmt.Fprintf(os.Stderr, "connstr err: %v", err) os.Exit(1) } + conn, err := storage.ToConnData(connStr) + if err != nil { + fmt.Fprintf(os.Stderr, "connstr parse err: %v", err) + os.Exit(1) + } resourceDir := scriptDir menuStorageService := storage.NewMenuStorageService(resourceDir) - err = menuStorageService.SetConn(connStr) + menuStorageService.WithConn(conn) if err != nil { fmt.Fprintf(os.Stderr, "conn error: %v", err) os.Exit(1) From 89c21847b959d304c364f657249dee2c01c4b281 Mon Sep 17 00:00:00 2001 From: lash Date: Sat, 4 Jan 2025 20:52:49 +0000 Subject: [PATCH 09/38] Improve error message --- internal/storage/storageservice.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/storage/storageservice.go b/internal/storage/storageservice.go index e3429bc..24d5eff 100644 --- a/internal/storage/storageservice.go +++ b/internal/storage/storageservice.go @@ -66,7 +66,7 @@ func (ms *MenuStorageService) getOrCreateDb(ctx context.Context, existingDb db.D connStr = path.Join(connStr, section) newDb = gdbmstorage.NewThreadGdbmDb() } else { - return nil, fmt.Errorf("unsupported connection string: %s", ms.conn.String()) + return nil, fmt.Errorf("unsupported connection string: '%s'\n", ms.conn.String()) } logg.DebugCtxf(ctx, "connecting to db", "conn", connStr) err = newDb.Connect(ctx, connStr) From 3a9f3fa373c7feb4295ad3e52896d1ba7389269e Mon Sep 17 00:00:00 2001 From: lash Date: Sat, 4 Jan 2025 20:54:51 +0000 Subject: [PATCH 10/38] Update env example --- .env.example | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/.env.example b/.env.example index c636fa8..0a43945 100644 --- a/.env.example +++ b/.env.example @@ -6,13 +6,8 @@ HOST=127.0.0.1 AT_ENDPOINT=/ussd/africastalking #PostgreSQL -DB_HOST=localhost -DB_USER=postgres -DB_PASSWORD=strongpass -DB_NAME=urdt_ussd -DB_PORT=5432 -DB_SSLMODE=disable -DB_TIMEZONE=Africa/Nairobi +DB_CONN=postgres://postgres:strongpass@localhost:5432/urdt_ussd +#DB_TIMEZONE=Africa/Nairobi #External API Calls CUSTODIAL_URL_BASE=http://localhost:5003 From cc9760125aa9e47f1939fc90a6b50b0a2e8e1376 Mon Sep 17 00:00:00 2001 From: lash Date: Sat, 4 Jan 2025 21:02:08 +0000 Subject: [PATCH 11/38] Remove unnecessary connection chain step --- .env.example | 1 + cmd/africastalking/main.go | 3 +-- cmd/async/main.go | 3 +-- cmd/http/main.go | 3 +-- cmd/main.go | 3 +-- common/storage.go | 7 +------ devtools/gen/main.go | 3 +-- devtools/store/main.go | 7 +------ internal/storage/storageservice.go | 8 ++------ internal/testutil/engine.go | 8 +------- 10 files changed, 11 insertions(+), 35 deletions(-) diff --git a/.env.example b/.env.example index 0a43945..dae37bc 100644 --- a/.env.example +++ b/.env.example @@ -8,6 +8,7 @@ AT_ENDPOINT=/ussd/africastalking #PostgreSQL DB_CONN=postgres://postgres:strongpass@localhost:5432/urdt_ussd #DB_TIMEZONE=Africa/Nairobi +#DB_SCHEMA=vise #External API Calls CUSTODIAL_URL_BASE=http://localhost:5003 diff --git a/cmd/africastalking/main.go b/cmd/africastalking/main.go index 6035fc6..0d2b8ec 100644 --- a/cmd/africastalking/main.go +++ b/cmd/africastalking/main.go @@ -80,8 +80,7 @@ func main() { cfg.EngineDebug = true } - menuStorageService := storage.NewMenuStorageService(resourceDir) - menuStorageService = menuStorageService.WithConn(connData) + menuStorageService := storage.NewMenuStorageService(connData, resourceDir) if err != nil { fmt.Fprintf(os.Stderr, err.Error()) os.Exit(1) diff --git a/cmd/async/main.go b/cmd/async/main.go index 17ea2a1..65e681d 100644 --- a/cmd/async/main.go +++ b/cmd/async/main.go @@ -91,8 +91,7 @@ func main() { cfg.EngineDebug = true } - menuStorageService := storage.NewMenuStorageService(resourceDir) - menuStorageService = menuStorageService.WithConn(connData) + menuStorageService := storage.NewMenuStorageService(connData, resourceDir) if err != nil { fmt.Fprintf(os.Stderr, err.Error()) os.Exit(1) diff --git a/cmd/http/main.go b/cmd/http/main.go index 69a9c90..2f043e6 100644 --- a/cmd/http/main.go +++ b/cmd/http/main.go @@ -79,8 +79,7 @@ func main() { cfg.EngineDebug = true } - menuStorageService := storage.NewMenuStorageService(resourceDir) - menuStorageService = menuStorageService.WithConn(connData) + menuStorageService := storage.NewMenuStorageService(connData, resourceDir) rs, err := menuStorageService.GetResource(ctx) if err != nil { diff --git a/cmd/main.go b/cmd/main.go index a475dbe..ad4535b 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -68,8 +68,7 @@ func main() { } resourceDir := scriptDir - menuStorageService := storage.NewMenuStorageService(resourceDir) - menuStorageService = menuStorageService.WithConn(connData) + menuStorageService := storage.NewMenuStorageService(connData, resourceDir) rs, err := menuStorageService.GetResource(ctx) if err != nil { diff --git a/common/storage.go b/common/storage.go index 6ab5211..c7e478b 100644 --- a/common/storage.go +++ b/common/storage.go @@ -32,9 +32,8 @@ type StorageService struct { func NewStorageService(conn storage.ConnData) (*StorageService, error) { svc := &StorageService{ - svc: storage.NewMenuStorageService(""), + svc: storage.NewMenuStorageService(conn, ""), } - svc.SetConn(conn) return svc, nil } @@ -49,7 +48,3 @@ func(ss *StorageService) GetUserdataDb(ctx context.Context) (db.Db, error) { func(ss *StorageService) GetResource(ctx context.Context) (resource.Resource, error) { return nil, errors.New("not implemented") } - -func(ss *StorageService) SetConn(conn storage.ConnData) { - ss.svc = ss.svc.WithConn(conn) -} diff --git a/devtools/gen/main.go b/devtools/gen/main.go index 272fd9c..749f340 100644 --- a/devtools/gen/main.go +++ b/devtools/gen/main.go @@ -55,8 +55,7 @@ func main() { ctx = context.WithValue(ctx, "Database", database) resourceDir := scriptDir - menuStorageService := storage.NewMenuStorageService(resourceDir) - menuStorageService = menuStorageService.WithConn(connData) + menuStorageService := storage.NewMenuStorageService(connData, resourceDir) store, err := menuStorageService.GetUserdataDb(ctx) if err != nil { diff --git a/devtools/store/main.go b/devtools/store/main.go index da988ad..f7f76c6 100644 --- a/devtools/store/main.go +++ b/devtools/store/main.go @@ -53,12 +53,7 @@ func main() { ctx = context.WithValue(ctx, "Database", database) resourceDir := scriptDir - menuStorageService := storage.NewMenuStorageService(resourceDir) - menuStorageService = menuStorageService.WithConn(connData) - if err != nil { - fmt.Fprintf(os.Stderr, "connection string error: %v", err) - os.Exit(1) - } + menuStorageService := storage.NewMenuStorageService(connData, resourceDir) store, err := menuStorageService.GetUserdataDb(ctx) if err != nil { diff --git a/internal/storage/storageservice.go b/internal/storage/storageservice.go index 24d5eff..9148d7c 100644 --- a/internal/storage/storageservice.go +++ b/internal/storage/storageservice.go @@ -34,17 +34,13 @@ type MenuStorageService struct { userDataStore db.Db } -func NewMenuStorageService(resourceDir string) *MenuStorageService { +func NewMenuStorageService(conn ConnData, resourceDir string) *MenuStorageService { return &MenuStorageService{ + conn: conn, resourceDir: resourceDir, } } -func (ms *MenuStorageService) WithConn(conn ConnData) *MenuStorageService { - ms.conn = conn - return ms -} - func (ms *MenuStorageService) getOrCreateDb(ctx context.Context, existingDb db.Db, section string) (db.Db, error) { var newDb db.Db var err error diff --git a/internal/testutil/engine.go b/internal/testutil/engine.go index 3d804ae..2372ce9 100644 --- a/internal/testutil/engine.go +++ b/internal/testutil/engine.go @@ -50,13 +50,7 @@ func TestEngine(sessionId string) (engine.Engine, func(), chan bool) { os.Exit(1) } resourceDir := scriptDir - menuStorageService := storage.NewMenuStorageService(resourceDir) - - menuStorageService.WithConn(conn) - if err != nil { - fmt.Fprintf(os.Stderr, "conn error: %v", err) - os.Exit(1) - } + menuStorageService := storage.NewMenuStorageService(conn, resourceDir) rs, err := menuStorageService.GetResource(ctx) if err != nil { From 51b6fc0dde431b6489e5ceb26a6e0cc2c66c6b51 Mon Sep 17 00:00:00 2001 From: lash Date: Sat, 4 Jan 2025 21:13:39 +0000 Subject: [PATCH 12/38] Remove unused methods in storage interfaces, improve logs --- cmd/africastalking/main.go | 4 ++-- cmd/async/main.go | 2 +- cmd/http/main.go | 2 +- cmd/main.go | 2 +- common/storage.go | 1 - devtools/store/main.go | 2 ++ internal/storage/storageservice.go | 1 - 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/cmd/africastalking/main.go b/cmd/africastalking/main.go index 0d2b8ec..40c3609 100644 --- a/cmd/africastalking/main.go +++ b/cmd/africastalking/main.go @@ -47,7 +47,7 @@ func main() { var err error flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir") - flag.StringVar(&connStr, "c", ".state", "connection string") + flag.StringVar(&connStr, "c", "", "connection string") flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.UintVar(&size, "s", 160, "max size of output") flag.StringVar(&host, "h", initializers.GetEnv("HOST", "127.0.0.1"), "http host") @@ -63,7 +63,7 @@ func main() { os.Exit(1) } - logg.Infof("start command", "build", build, "dbdir", connStr, "resourcedir", resourceDir, "outputsize", size) + logg.Infof("start command", "build", build, "conn", connData, "resourcedir", resourceDir, "outputsize", size) ctx := context.Background() ctx = context.WithValue(ctx, "Database", database) diff --git a/cmd/async/main.go b/cmd/async/main.go index 65e681d..b0c7caa 100644 --- a/cmd/async/main.go +++ b/cmd/async/main.go @@ -74,7 +74,7 @@ func main() { os.Exit(1) } - logg.Infof("start command", "connstr", connStr, "resourcedir", resourceDir, "outputsize", size, "sessionId", sessionId) + logg.Infof("start command", "conn", connData, "resourcedir", resourceDir, "outputsize", size, "sessionId", sessionId) ctx := context.Background() ctx = context.WithValue(ctx, "Database", database) diff --git a/cmd/http/main.go b/cmd/http/main.go index 2f043e6..d744afc 100644 --- a/cmd/http/main.go +++ b/cmd/http/main.go @@ -62,7 +62,7 @@ func main() { os.Exit(1) } - logg.Infof("start command", "connstr", connStr, "resourcedir", resourceDir, "outputsize", size) + logg.Infof("start command", "conn", connData, "resourcedir", resourceDir, "outputsize", size) ctx := context.Background() ctx = context.WithValue(ctx, "Database", database) diff --git a/cmd/main.go b/cmd/main.go index ad4535b..ac680fc 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -52,7 +52,7 @@ func main() { os.Exit(1) } - logg.Infof("start command", "connstr", connStr, "outputsize", size) + logg.Infof("start command", "conn", connData, "outputsize", size) ctx := context.Background() ctx = context.WithValue(ctx, "SessionId", sessionId) diff --git a/common/storage.go b/common/storage.go index c7e478b..2960578 100644 --- a/common/storage.go +++ b/common/storage.go @@ -23,7 +23,6 @@ type StorageServices interface { GetPersister(ctx context.Context) (*persist.Persister, error) GetUserdataDb(ctx context.Context) (db.Db, error) GetResource(ctx context.Context) (resource.Resource, error) - SetConn(conn storage.ConnData) } type StorageService struct { diff --git a/devtools/store/main.go b/devtools/store/main.go index f7f76c6..6915f74 100644 --- a/devtools/store/main.go +++ b/devtools/store/main.go @@ -48,6 +48,8 @@ func main() { os.Exit(1) } + logg.Infof("start command", "conn", connData) + ctx := context.Background() ctx = context.WithValue(ctx, "SessionId", sessionId) ctx = context.WithValue(ctx, "Database", database) diff --git a/internal/storage/storageservice.go b/internal/storage/storageservice.go index 9148d7c..83ce051 100644 --- a/internal/storage/storageservice.go +++ b/internal/storage/storageservice.go @@ -23,7 +23,6 @@ type StorageService interface { GetPersister(ctx context.Context) (*persist.Persister, error) GetUserdataDb(ctx context.Context) db.Db GetResource(ctx context.Context) (resource.Resource, error) - SetConn(connStr string) error } type MenuStorageService struct { From d39740a09abd7dc8a7ba092d946cfcebcb7e0c9f Mon Sep 17 00:00:00 2001 From: lash Date: Mon, 6 Jan 2025 07:41:24 +0000 Subject: [PATCH 13/38] Edit ssh cli help text --- cmd/ssh/main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/ssh/main.go b/cmd/ssh/main.go index 0227616..0b22216 100644 --- a/cmd/ssh/main.go +++ b/cmd/ssh/main.go @@ -37,8 +37,8 @@ func main() { flag.BoolVar(&engineDebug, "engine-debug", false, "use engine debug output") flag.BoolVar(&stateDebug, "state-debug", false, "use engine debug output") flag.UintVar(&size, "s", 160, "max size of output") - flag.StringVar(&host, "h", "127.0.0.1", "http host") - flag.UintVar(&port, "p", 7122, "http port") + flag.StringVar(&host, "h", "127.0.0.1", "socket host") + flag.UintVar(&port, "p", 7122, "socket port") flag.Parse() sshKeyFile := flag.Arg(0) From 52fd1eced24fc499e5b9bf04151df7a78973f62f Mon Sep 17 00:00:00 2001 From: lash Date: Mon, 6 Jan 2025 08:11:37 +0000 Subject: [PATCH 14/38] Enable env, config, db in ssh --- cmd/main.go | 3 ++- cmd/ssh/main.go | 28 +++++++++++++++++++++++----- internal/ssh/ssh.go | 35 +++++++++++++++-------------------- 3 files changed, 40 insertions(+), 26 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index ac680fc..01a352c 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -35,8 +35,10 @@ func main() { var sessionId string var database string var engineDebug bool + var resourceDir string var err error + flag.StringVar(&resourceDir, "resourcedir", scriptDir, "resource dir") flag.StringVar(&sessionId, "session-id", "075xx2123", "session id") flag.StringVar(&connStr, "c", "", "connection string") flag.BoolVar(&engineDebug, "d", false, "use engine debug output") @@ -67,7 +69,6 @@ func main() { MenuSeparator: menuSeparator, } - resourceDir := scriptDir menuStorageService := storage.NewMenuStorageService(connData, resourceDir) rs, err := menuStorageService.GetResource(ctx) diff --git a/cmd/ssh/main.go b/cmd/ssh/main.go index 0b22216..58f2b0b 100644 --- a/cmd/ssh/main.go +++ b/cmd/ssh/main.go @@ -14,7 +14,10 @@ import ( "git.defalsify.org/vise.git/engine" "git.defalsify.org/vise.git/logging" + "git.grassecon.net/urdt/ussd/config" + "git.grassecon.net/urdt/ussd/initializers" "git.grassecon.net/urdt/ussd/internal/ssh" + "git.grassecon.net/urdt/ussd/internal/storage" ) var ( @@ -24,7 +27,14 @@ var ( scriptDir = path.Join("services", "registration") ) +func init() { + initializers.LoadEnvVariables() +} + func main() { + config.LoadConfig() + + var connStr string var dbDir string var resourceDir string var size uint @@ -32,17 +42,25 @@ func main() { var stateDebug bool var host string var port uint - flag.StringVar(&dbDir, "dbdir", ".state", "database dir to read from") + flag.StringVar(&connStr, "c", "", "connection string") flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir") - flag.BoolVar(&engineDebug, "engine-debug", false, "use engine debug output") - flag.BoolVar(&stateDebug, "state-debug", false, "use engine debug output") + flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.UintVar(&size, "s", 160, "max size of output") flag.StringVar(&host, "h", "127.0.0.1", "socket host") flag.UintVar(&port, "p", 7122, "socket port") flag.Parse() + if connStr != "" { + connStr = config.DbConn + } + connData, err := storage.ToConnData(config.DbConn) + if err != nil { + fmt.Fprintf(os.Stderr, "connstr err: %v", err) + os.Exit(1) + } + sshKeyFile := flag.Arg(0) - _, err := os.Stat(sshKeyFile) + _, err = os.Stat(sshKeyFile) if err != nil { fmt.Fprintf(os.Stderr, "cannot open ssh server private key file: %v\n", err) os.Exit(1) @@ -93,7 +111,7 @@ func main() { Cfg: cfg, Debug: engineDebug, FlagFile: pfp, - DbDir: dbDir, + Conn: connData, ResourceDir: resourceDir, SrvKeyFile: sshKeyFile, Host: host, diff --git a/internal/ssh/ssh.go b/internal/ssh/ssh.go index 1831ebe..c3ec725 100644 --- a/internal/ssh/ssh.go +++ b/internal/ssh/ssh.go @@ -71,6 +71,20 @@ func(a *auther) Get(k []byte) (string, error) { return v, nil } +type SshRunner struct { + Ctx context.Context + Cfg engine.Config + FlagFile string + Conn storage.ConnData + ResourceDir string + Debug bool + SrvKeyFile string + Host string + Port uint + wg sync.WaitGroup + lst net.Listener +} + func(s *SshRunner) serve(ctx context.Context, sessionId string, ch ssh.NewChannel, en engine.Engine) error { if ch == nil { return errors.New("nil channel") @@ -128,32 +142,13 @@ func(s *SshRunner) serve(ctx context.Context, sessionId string, ch ssh.NewChanne return nil } -type SshRunner struct { - Ctx context.Context - Cfg engine.Config - FlagFile string - DbDir string - ResourceDir string - Debug bool - SrvKeyFile string - Host string - Port uint - wg sync.WaitGroup - lst net.Listener -} - func(s *SshRunner) Stop() error { return s.lst.Close() } func(s *SshRunner) GetEngine(sessionId string) (engine.Engine, func(), error) { ctx := s.Ctx - menuStorageService := storage.NewMenuStorageService(s.DbDir, s.ResourceDir) - - err := menuStorageService.EnsureDbDir() - if err != nil { - return nil, nil, err - } + menuStorageService := storage.NewMenuStorageService(s.Conn, s.ResourceDir) rs, err := menuStorageService.GetResource(ctx) if err != nil { From 599815c343e958234b88a2075670e368ad89a597 Mon Sep 17 00:00:00 2001 From: lash Date: Mon, 6 Jan 2025 09:00:41 +0000 Subject: [PATCH 15/38] Fix remaining conflict in cmd cli --- cmd/main.go | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 8547994..4bf134b 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -88,22 +88,12 @@ func main() { MenuSeparator: menuSeparator, } -<<<<<<< HEAD menuStorageService := storage.NewMenuStorageService(connData, resourceDir) -======= - resourceDir := scriptDir - menuStorageService := storage.NewMenuStorageService(dbDir, resourceDir) + if gettextDir != "" { menuStorageService = menuStorageService.WithGettext(gettextDir, langs.Langs()) } - err = menuStorageService.EnsureDbDir() - if err != nil { - fmt.Fprintf(os.Stderr, err.Error()) - os.Exit(1) - } ->>>>>>> master - rs, err := menuStorageService.GetResource(ctx) if err != nil { fmt.Fprintf(os.Stderr, err.Error()) From 68e4c9af036e2c38499f9e7b2c7ec1a77dec1e24 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Mon, 6 Jan 2025 21:00:34 +0300 Subject: [PATCH 16/38] add key for incorrect pin attempts --- common/db.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/common/db.go b/common/db.go index a5cf1c1..5e2fc4c 100644 --- a/common/db.go +++ b/common/db.go @@ -7,7 +7,7 @@ import ( "git.defalsify.org/vise.git/logging" ) -// DataType is a subprefix value used in association with vise/db.DATATYPE_USERDATA. +// DataType is a subprefix value used in association with vise/db.DATATYPE_USERDATA. // // All keys are used only within the context of a single account. Unless otherwise specified, the user context is the session id. // @@ -55,6 +55,8 @@ const ( DATA_ACTIVE_DECIMAL // EVM address of the currently active voucher DATA_ACTIVE_ADDRESS + //Holds count of the number of incorrect PIN attempts + DATA_INCORRECT_PIN_ATTEMPTS ) const ( From b37f2a0a110cc5e2c50f76eeac854144d61363c3 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Mon, 6 Jan 2025 21:06:54 +0300 Subject: [PATCH 17/38] add flag for when an account has been blocked --- services/registration/pp.csv | 2 ++ 1 file changed, 2 insertions(+) diff --git a/services/registration/pp.csv b/services/registration/pp.csv index 26a8833..aa1eb05 100644 --- a/services/registration/pp.csv +++ b/services/registration/pp.csv @@ -28,3 +28,5 @@ flag,flag_gender_set,34,this is set when the gender of the profile is set flag,flag_location_set,35,this is set when the location of the profile is set flag,flag_offerings_set,36,this is set when the offerings of the profile is set flag,flag_back_set,37,this is set when it is a back navigation +flag,flag_account_blocked,38,this is set when an account has been blocked after the allowed incorrect PIN attempts have been exceeded + From 90df0eefc39dc62ee6c32b96e1de16705a4a2442 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Mon, 6 Jan 2025 22:53:59 +0300 Subject: [PATCH 18/38] add value for allowed number of PIN attempts --- common/pin.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/common/pin.go b/common/pin.go index 6db9d15..d42fb58 100644 --- a/common/pin.go +++ b/common/pin.go @@ -8,7 +8,8 @@ import ( // Define the regex pattern as a constant const ( - pinPattern = `^\d{4}$` + pinPattern = `^\d{4}$` + AllowedPINAttempts = uint8(3) ) // checks whether the given input is a 4 digit number From 562bd4fa241b9759cf344a5a2cad85e65bf1935e Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Mon, 6 Jan 2025 22:54:31 +0300 Subject: [PATCH 19/38] check for incorrect pin --- internal/handlers/ussd/menuhandler.go | 44 +++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index dfdbd02..70fd305 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -739,6 +739,7 @@ func (h *Handlers) Authorize(ctx context.Context, sym string, input []byte) (res res.FlagReset = append(res.FlagReset, flag_account_authorized) } } else { + h.countIncorrectPINAttempts(ctx, sessionId) res.FlagSet = append(res.FlagSet, flag_incorrect_pin) res.FlagReset = append(res.FlagReset, flag_account_authorized) return res, nil @@ -752,6 +753,24 @@ func (h *Handlers) Authorize(ctx context.Context, sym string, input []byte) (res // ResetIncorrectPin resets the incorrect pin flag after a new PIN attempt. func (h *Handlers) ResetIncorrectPin(ctx context.Context, sym string, input []byte) (resource.Result, error) { var res resource.Result + store := h.userdataStore + + sessionId, ok := ctx.Value("SessionId").(string) + if !ok { + return res, fmt.Errorf("missing session") + } + currentWrongPinAttempts, err := store.ReadEntry(ctx, sessionId, common.DATA_INCORRECT_PIN_ATTEMPTS) + if err != nil { + if !db.IsNotFound(err) { + return res, err + } + } + pinAttemptsValue, _ := strconv.ParseUint(string(currentWrongPinAttempts), 0, 64) + remainingPINAttempts := common.AllowedPINAttempts - uint8(pinAttemptsValue) + if remainingPINAttempts < common.AllowedPINAttempts { + res.Content = string(remainingPINAttempts) + } + flag_incorrect_pin, _ := h.flagManager.GetFlag("flag_incorrect_pin") res.FlagReset = append(res.FlagReset, flag_incorrect_pin) return res, nil @@ -2075,3 +2094,28 @@ func (h *Handlers) UpdateAllProfileItems(ctx context.Context, sym string, input } return res, nil } + +func (h *Handlers) countIncorrectPINAttempts(ctx context.Context, sessionId string) { + var pinAttemptsCount uint8 + store := h.userdataStore + + currentWrongPinAttempts, err := store.ReadEntry(ctx, sessionId, common.DATA_INCORRECT_PIN_ATTEMPTS) + if err != nil { + if db.IsNotFound(err) { + //First time Wrong PIN attempt: initialize with a count of 1 + pinAttemptsCount = 1 + err = store.WriteEntry(ctx, sessionId, common.DATA_INCORRECT_PIN_ATTEMPTS, []byte(strconv.Itoa(int(pinAttemptsCount)))) + if err != nil { + logg.ErrorCtxf(ctx, "failed to write incorrect PIN attempts ", "key", common.DATA_INCORRECT_PIN_ATTEMPTS, "value", currentWrongPinAttempts, "error", err) + } + return + } + } + pinAttemptsValue, _ := strconv.ParseUint(string(currentWrongPinAttempts), 0, 64) + pinAttemptsCount = uint8(pinAttemptsValue) + 1 + + err = store.WriteEntry(ctx, sessionId, common.DATA_INCORRECT_PIN_ATTEMPTS, []byte(strconv.Itoa(int(pinAttemptsCount)))) + if err != nil { + logg.ErrorCtxf(ctx, "failed to write incorrect PIN attempts ", "key", common.DATA_INCORRECT_PIN_ATTEMPTS, "value", pinAttemptsCount, "error", err) + } +} From f83f53904668883a4f71b2dc78b282c7a287e861 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Tue, 7 Jan 2025 10:48:59 +0300 Subject: [PATCH 20/38] add node to show remaining pin attempts --- services/registration/incorrect_pin | 2 +- services/registration/incorrect_pin_swa | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/services/registration/incorrect_pin b/services/registration/incorrect_pin index 7fcf610..0e34cd1 100644 --- a/services/registration/incorrect_pin +++ b/services/registration/incorrect_pin @@ -1 +1 @@ -Incorrect PIN \ No newline at end of file +Incorrect PIN.You have: {{.reset_incorrect}} remaining attempt(s). \ No newline at end of file diff --git a/services/registration/incorrect_pin_swa b/services/registration/incorrect_pin_swa index 34a0b28..8a8b913 100644 --- a/services/registration/incorrect_pin_swa +++ b/services/registration/incorrect_pin_swa @@ -1 +1 @@ -PIN ulioeka sio sahihi \ No newline at end of file +PIN ulioeka sio sahihi,una majaribio: {{.reset_incorrect}} yaliyobaki \ No newline at end of file From 11d30583a432088503388ba35cc479fc719fe2c9 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Tue, 7 Jan 2025 10:50:30 +0300 Subject: [PATCH 21/38] map content of reset_incorrect and catch blocked account --- services/registration/incorrect_pin.vis | 2 ++ 1 file changed, 2 insertions(+) diff --git a/services/registration/incorrect_pin.vis b/services/registration/incorrect_pin.vis index 844f3d6..167364a 100644 --- a/services/registration/incorrect_pin.vis +++ b/services/registration/incorrect_pin.vis @@ -1,5 +1,7 @@ LOAD reset_incorrect 0 RELOAD reset_incorrect +MAP reset_incorrect +CATCH blocked_account flag_account_blocked 1 MOUT retry 1 MOUT quit 9 HALT From ee442daefa0c04db6b91183d37589cdf74b76795 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Tue, 7 Jan 2025 14:03:53 +0300 Subject: [PATCH 22/38] add blocked account node --- services/registration/blocked_account | 1 + services/registration/blocked_account.vis | 3 +++ services/registration/blocked_account_swa | 1 + 3 files changed, 5 insertions(+) create mode 100644 services/registration/blocked_account create mode 100644 services/registration/blocked_account.vis create mode 100644 services/registration/blocked_account_swa diff --git a/services/registration/blocked_account b/services/registration/blocked_account new file mode 100644 index 0000000..9a34267 --- /dev/null +++ b/services/registration/blocked_account @@ -0,0 +1 @@ +Your account has been locked.For help on how to unblock your account,contact support at: 0757628885 diff --git a/services/registration/blocked_account.vis b/services/registration/blocked_account.vis new file mode 100644 index 0000000..d5e6b2d --- /dev/null +++ b/services/registration/blocked_account.vis @@ -0,0 +1,3 @@ +MOUT quit 9 +HALT +INCMP quit 9 diff --git a/services/registration/blocked_account_swa b/services/registration/blocked_account_swa new file mode 100644 index 0000000..c00243b --- /dev/null +++ b/services/registration/blocked_account_swa @@ -0,0 +1 @@ +Akaunti yako imefungwa.Kwa usaidizi wa jinsi ya kufungua akaunti yako, wasiliana na usaidizi kwa: 0757628885 \ No newline at end of file From c15958a1ad8d98cc684363bc181bd4627b5684a4 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Tue, 7 Jan 2025 14:32:44 +0300 Subject: [PATCH 23/38] reset incorrect pin attempts on correct entry --- internal/handlers/ussd/menuhandler.go | 54 ++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 6 deletions(-) diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index 70fd305..5582265 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -734,12 +734,23 @@ func (h *Handlers) Authorize(ctx context.Context, sym string, input []byte) (res if h.st.MatchFlag(flag_account_authorized, false) { res.FlagReset = append(res.FlagReset, flag_incorrect_pin) res.FlagSet = append(res.FlagSet, flag_allow_update, flag_account_authorized) + err := h.resetIncorrectPINAttempts(ctx, sessionId) + if err != nil { + return res, err + } } else { res.FlagSet = append(res.FlagSet, flag_allow_update) res.FlagReset = append(res.FlagReset, flag_account_authorized) + err := h.resetIncorrectPINAttempts(ctx, sessionId) + if err != nil { + return res, err + } } } else { - h.countIncorrectPINAttempts(ctx, sessionId) + err := h.countIncorrectPINAttempts(ctx, sessionId) + if err != nil { + return res, err + } res.FlagSet = append(res.FlagSet, flag_incorrect_pin) res.FlagReset = append(res.FlagReset, flag_account_authorized) return res, nil @@ -755,10 +766,16 @@ func (h *Handlers) ResetIncorrectPin(ctx context.Context, sym string, input []by var res resource.Result store := h.userdataStore + flag_incorrect_pin, _ := h.flagManager.GetFlag("flag_incorrect_pin") + flag_account_blocked, _ := h.flagManager.GetFlag("flag_account_blocked") + sessionId, ok := ctx.Value("SessionId").(string) if !ok { return res, fmt.Errorf("missing session") } + + res.FlagReset = append(res.FlagReset, flag_incorrect_pin) + currentWrongPinAttempts, err := store.ReadEntry(ctx, sessionId, common.DATA_INCORRECT_PIN_ATTEMPTS) if err != nil { if !db.IsNotFound(err) { @@ -767,12 +784,14 @@ func (h *Handlers) ResetIncorrectPin(ctx context.Context, sym string, input []by } pinAttemptsValue, _ := strconv.ParseUint(string(currentWrongPinAttempts), 0, 64) remainingPINAttempts := common.AllowedPINAttempts - uint8(pinAttemptsValue) + if remainingPINAttempts == 0 { + res.FlagSet = append(res.FlagSet, flag_account_blocked) + return res, nil + } if remainingPINAttempts < common.AllowedPINAttempts { - res.Content = string(remainingPINAttempts) + res.Content = strconv.Itoa(int(remainingPINAttempts)) } - flag_incorrect_pin, _ := h.flagManager.GetFlag("flag_incorrect_pin") - res.FlagReset = append(res.FlagReset, flag_incorrect_pin) return res, nil } @@ -2095,7 +2114,7 @@ func (h *Handlers) UpdateAllProfileItems(ctx context.Context, sym string, input return res, nil } -func (h *Handlers) countIncorrectPINAttempts(ctx context.Context, sessionId string) { +func (h *Handlers) countIncorrectPINAttempts(ctx context.Context, sessionId string) error { var pinAttemptsCount uint8 store := h.userdataStore @@ -2107,8 +2126,9 @@ func (h *Handlers) countIncorrectPINAttempts(ctx context.Context, sessionId stri err = store.WriteEntry(ctx, sessionId, common.DATA_INCORRECT_PIN_ATTEMPTS, []byte(strconv.Itoa(int(pinAttemptsCount)))) if err != nil { logg.ErrorCtxf(ctx, "failed to write incorrect PIN attempts ", "key", common.DATA_INCORRECT_PIN_ATTEMPTS, "value", currentWrongPinAttempts, "error", err) + return err } - return + return nil } } pinAttemptsValue, _ := strconv.ParseUint(string(currentWrongPinAttempts), 0, 64) @@ -2117,5 +2137,27 @@ func (h *Handlers) countIncorrectPINAttempts(ctx context.Context, sessionId stri err = store.WriteEntry(ctx, sessionId, common.DATA_INCORRECT_PIN_ATTEMPTS, []byte(strconv.Itoa(int(pinAttemptsCount)))) if err != nil { logg.ErrorCtxf(ctx, "failed to write incorrect PIN attempts ", "key", common.DATA_INCORRECT_PIN_ATTEMPTS, "value", pinAttemptsCount, "error", err) + return err } + return nil +} + +func (h *Handlers) resetIncorrectPINAttempts(ctx context.Context, sessionId string) error { + store := h.userdataStore + currentWrongPinAttempts, err := store.ReadEntry(ctx, sessionId, common.DATA_INCORRECT_PIN_ATTEMPTS) + if err != nil { + if db.IsNotFound(err) { + return nil + } + return err + } + remainingPINAttempts, _ := strconv.ParseUint(string(currentWrongPinAttempts), 0, 64) + if remainingPINAttempts <= uint64(common.AllowedPINAttempts) { + err = store.WriteEntry(ctx, sessionId, common.DATA_INCORRECT_PIN_ATTEMPTS, []byte(string("0"))) + if err != nil { + logg.ErrorCtxf(ctx, "failed to reset incorrect PIN attempts ", "key", common.DATA_INCORRECT_PIN_ATTEMPTS, "value", common.AllowedPINAttempts, "error", err) + return err + } + } + return nil } From 64fba916700508d8f3ab6c1ba29ca4b8d1c5c618 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Tue, 7 Jan 2025 14:38:44 +0300 Subject: [PATCH 24/38] catch blocked account --- services/registration/root.vis | 1 + 1 file changed, 1 insertion(+) diff --git a/services/registration/root.vis b/services/registration/root.vis index 02ef9e9..102e6e5 100644 --- a/services/registration/root.vis +++ b/services/registration/root.vis @@ -1,3 +1,4 @@ +CATCH blocked_account flag_account_blocked 1 CATCH select_language flag_language_set 0 CATCH terms flag_account_created 0 LOAD check_account_status 0 From 1fa830f2866d9b39b96d4ea593b7aad99653bab4 Mon Sep 17 00:00:00 2001 From: lash Date: Tue, 7 Jan 2025 13:51:26 +0000 Subject: [PATCH 25/38] Add auth conn string to ssh, use connstr for execs --- cmd/africastalking/main.go | 2 +- cmd/async/main.go | 2 +- cmd/http/main.go | 2 +- cmd/main.go | 2 +- cmd/ssh/main.go | 19 ++++++++++++++----- internal/ssh/ssh.go | 1 + 6 files changed, 19 insertions(+), 9 deletions(-) diff --git a/cmd/africastalking/main.go b/cmd/africastalking/main.go index e0d05f6..72d3944 100644 --- a/cmd/africastalking/main.go +++ b/cmd/africastalking/main.go @@ -63,7 +63,7 @@ func main() { if connStr != "" { connStr = config.DbConn } - connData, err := storage.ToConnData(config.DbConn) + connData, err := storage.ToConnData(connStr) if err != nil { fmt.Fprintf(os.Stderr, "connstr err: %v", err) os.Exit(1) diff --git a/cmd/async/main.go b/cmd/async/main.go index e63c469..dc293e6 100644 --- a/cmd/async/main.go +++ b/cmd/async/main.go @@ -74,7 +74,7 @@ func main() { if connStr != "" { connStr = config.DbConn } - connData, err := storage.ToConnData(config.DbConn) + connData, err := storage.ToConnData(connStr) if err != nil { fmt.Fprintf(os.Stderr, "connstr err: %v", err) os.Exit(1) diff --git a/cmd/http/main.go b/cmd/http/main.go index c61d68d..8e65232 100644 --- a/cmd/http/main.go +++ b/cmd/http/main.go @@ -62,7 +62,7 @@ func main() { if connStr != "" { connStr = config.DbConn } - connData, err := storage.ToConnData(config.DbConn) + connData, err := storage.ToConnData(connStr) if err != nil { fmt.Fprintf(os.Stderr, "connstr err: %v", err) os.Exit(1) diff --git a/cmd/main.go b/cmd/main.go index 4bf134b..3939c9d 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -55,7 +55,7 @@ func main() { if connStr != "" { connStr = config.DbConn } - connData, err := storage.ToConnData(config.DbConn) + connData, err := storage.ToConnData(connStr) if err != nil { fmt.Fprintf(os.Stderr, "connstr err: %v", err) os.Exit(1) diff --git a/cmd/ssh/main.go b/cmd/ssh/main.go index e55d793..51023e5 100644 --- a/cmd/ssh/main.go +++ b/cmd/ssh/main.go @@ -37,7 +37,7 @@ func main() { config.LoadConfig() var connStr string - var dbDir string + var authConnStr string var resourceDir string var size uint var engineDebug bool @@ -45,6 +45,7 @@ func main() { var host string var port uint flag.StringVar(&connStr, "c", "", "connection string") + flag.StringVar(&authConnStr, "authdb", "", "auth connection string") flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir") flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.UintVar(&size, "s", 160, "max size of output") @@ -52,14 +53,22 @@ func main() { flag.UintVar(&port, "p", 7122, "socket port") flag.Parse() - if connStr != "" { + if connStr == "" { connStr = config.DbConn } - connData, err := storage.ToConnData(config.DbConn) + if authConnStr == "" { + authConnStr = connStr + } + connData, err := storage.ToConnData(connStr) if err != nil { fmt.Fprintf(os.Stderr, "connstr err: %v", err) os.Exit(1) } + authConnData, err := storage.ToConnData(authConnStr) + if err != nil { + fmt.Fprintf(os.Stderr, "auth connstr err: %v", err) + os.Exit(1) + } sshKeyFile := flag.Arg(0) _, err = os.Stat(sshKeyFile) @@ -75,7 +84,7 @@ func main() { logg.WarnCtxf(ctx, "!!!!! Do not expose to internet and only use with tunnel!") logg.WarnCtxf(ctx, "!!!!! (See ssh -L <...>)") - logg.Infof("start command", "dbdir", dbDir, "resourcedir", resourceDir, "outputsize", size, "keyfile", sshKeyFile, "host", host, "port", port) + logg.Infof("start command", "conn", connData, "authconn", authConnData, "resourcedir", resourceDir, "outputsize", size, "keyfile", sshKeyFile, "host", host, "port", port) pfp := path.Join(scriptDir, "pp.csv") @@ -91,7 +100,7 @@ func main() { cfg.EngineDebug = true } - authKeyStore, err := ssh.NewSshKeyStore(ctx, dbDir) + authKeyStore, err := ssh.NewSshKeyStore(ctx, authConnData.String()) if err != nil { fmt.Fprintf(os.Stderr, "keystore file open error: %v", err) os.Exit(1) diff --git a/internal/ssh/ssh.go b/internal/ssh/ssh.go index c3ec725..4b744a8 100644 --- a/internal/ssh/ssh.go +++ b/internal/ssh/ssh.go @@ -41,6 +41,7 @@ func NewAuther(ctx context.Context, keyStore *SshKeyStore) *auther { } func(a *auther) Check(conn ssh.ConnMetadata, pubKey ssh.PublicKey) (*ssh.Permissions, error) { + logg.TraceCtxf(a.Ctx, "looking for publickey", "pubkey", fmt.Sprintf("%x", pubKey)) va, err := a.keyStore.Get(a.Ctx, pubKey) if err != nil { return nil, err From 07b061a68b4d034a607cc38ab9c217f0c3c2eee5 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Wed, 8 Jan 2025 09:26:53 +0300 Subject: [PATCH 26/38] remove blocked account templates --- services/registration/blocked_account | 1 - services/registration/blocked_account_swa | 1 - 2 files changed, 2 deletions(-) delete mode 100644 services/registration/blocked_account delete mode 100644 services/registration/blocked_account_swa diff --git a/services/registration/blocked_account b/services/registration/blocked_account deleted file mode 100644 index 9a34267..0000000 --- a/services/registration/blocked_account +++ /dev/null @@ -1 +0,0 @@ -Your account has been locked.For help on how to unblock your account,contact support at: 0757628885 diff --git a/services/registration/blocked_account_swa b/services/registration/blocked_account_swa deleted file mode 100644 index c00243b..0000000 --- a/services/registration/blocked_account_swa +++ /dev/null @@ -1 +0,0 @@ -Akaunti yako imefungwa.Kwa usaidizi wa jinsi ya kufungua akaunti yako, wasiliana na usaidizi kwa: 0757628885 \ No newline at end of file From 50c7ff104616994ce532078483f2b66433a27dfe Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Wed, 8 Jan 2025 09:27:45 +0300 Subject: [PATCH 27/38] register handler to show blocked account message --- internal/handlers/handlerservice.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/handlers/handlerservice.go b/internal/handlers/handlerservice.go index 1da28c3..0d49b0c 100644 --- a/internal/handlers/handlerservice.go +++ b/internal/handlers/handlerservice.go @@ -128,6 +128,7 @@ func (ls *LocalHandlerService) GetHandler(accountService remote.AccountServiceIn ls.DbRs.AddLocalFunc("view_statement", ussdHandlers.ViewTransactionStatement) ls.DbRs.AddLocalFunc("update_all_profile_items", ussdHandlers.UpdateAllProfileItems) ls.DbRs.AddLocalFunc("set_back", ussdHandlers.SetBack) + ls.DbRs.AddLocalFunc("show_blocked_account", ussdHandlers.ShowBlockedAccount) return ussdHandlers, nil } From 3004698d5bdeab0cc0def616a97de4f93e407e25 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Wed, 8 Jan 2025 09:28:31 +0300 Subject: [PATCH 28/38] add a message for a blocked account --- services/registration/locale/swa/default.po | 3 +++ 1 file changed, 3 insertions(+) diff --git a/services/registration/locale/swa/default.po b/services/registration/locale/swa/default.po index 4bf876b..ab238e7 100644 --- a/services/registration/locale/swa/default.po +++ b/services/registration/locale/swa/default.po @@ -10,6 +10,9 @@ msgstr "Asante kwa kutumia huduma ya Sarafu. Kwaheri!" msgid "For more help,please call: 0757628885" msgstr "Kwa usaidizi zaidi,piga: 0757628885" +msgid "Your account has been locked.For help on how to unblock your account,contact support at: 0757628885" +msgstr "Akaunti yako imefungwa.Kwa usaidizi wa jinsi ya kufungua akaunti yako, wasiliana na usaidizi kwa: 0757628885" + msgid "Balance: %s\n" msgstr "Salio: %s\n" From 4fe64a774748f2936ea55bc4bf69f6cfb1736a80 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Wed, 8 Jan 2025 09:29:00 +0300 Subject: [PATCH 29/38] show message for a blocked account --- services/registration/blocked_account.vis | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/services/registration/blocked_account.vis b/services/registration/blocked_account.vis index d5e6b2d..d8adab2 100644 --- a/services/registration/blocked_account.vis +++ b/services/registration/blocked_account.vis @@ -1,3 +1,2 @@ -MOUT quit 9 +LOAD show_blocked_account 0 HALT -INCMP quit 9 From 4d72ae0313ad78052979c0ddcfbe25de94185fd3 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Wed, 8 Jan 2025 09:30:51 +0300 Subject: [PATCH 30/38] add handler showing a message for a blocked account --- internal/handlers/ussd/menuhandler.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index 5582265..2fcf92e 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -878,6 +878,16 @@ func (h *Handlers) QuitWithHelp(ctx context.Context, sym string, input []byte) ( return res, nil } +// ShowBlockedAccount displays a message after an account has been blocked and how to reach support. +func (h *Handlers) ShowBlockedAccount(ctx context.Context, sym string, input []byte) (resource.Result, error) { + var res resource.Result + code := codeFromCtx(ctx) + l := gotext.NewLocale(translationDir, code) + l.AddDomain("default") + res.Content = l.Get("Your account has been locked.For help on how to unblock your account,contact support at: 0757628885") + return res, nil +} + // VerifyYob verifies the length of the given input. func (h *Handlers) VerifyYob(ctx context.Context, sym string, input []byte) (resource.Result, error) { var res resource.Result @@ -2114,6 +2124,7 @@ func (h *Handlers) UpdateAllProfileItems(ctx context.Context, sym string, input return res, nil } +// countIncorrectPINAttempts keeps track of the number of incorrect PIN attempts func (h *Handlers) countIncorrectPINAttempts(ctx context.Context, sessionId string) error { var pinAttemptsCount uint8 store := h.userdataStore @@ -2142,6 +2153,7 @@ func (h *Handlers) countIncorrectPINAttempts(ctx context.Context, sessionId stri return nil } +// resetIncorrectPINAttempts resets the number of incorrect PIN attempts after a correct PIN entry func (h *Handlers) resetIncorrectPINAttempts(ctx context.Context, sessionId string) error { store := h.userdataStore currentWrongPinAttempts, err := store.ReadEntry(ctx, sessionId, common.DATA_INCORRECT_PIN_ATTEMPTS) From 721f80d0f268e76ae4942de245804a5743886187 Mon Sep 17 00:00:00 2001 From: lash Date: Wed, 8 Jan 2025 07:34:15 +0000 Subject: [PATCH 31/38] Repalce missing context --- internal/ssh/ssh.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/ssh/ssh.go b/internal/ssh/ssh.go index 4b744a8..8209187 100644 --- a/internal/ssh/ssh.go +++ b/internal/ssh/ssh.go @@ -204,6 +204,7 @@ func(s *SshRunner) GetEngine(sessionId string) (engine.Engine, func(), error) { // adapted example from crypto/ssh package, NewServerConn doc func(s *SshRunner) Run(ctx context.Context, keyStore *SshKeyStore) { + s.Ctx = ctx running := true // TODO: waitgroup should probably not be global From 7f3294a8a2e4ad188801d867eb793b11b1ea2c4f Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Wed, 8 Jan 2025 11:47:41 +0300 Subject: [PATCH 32/38] update tests --- internal/handlers/ussd/menuhandler_test.go | 101 ++++++++++++++++++++- 1 file changed, 98 insertions(+), 3 deletions(-) diff --git a/internal/handlers/ussd/menuhandler_test.go b/internal/handlers/ussd/menuhandler_test.go index 914dffc..5914821 100644 --- a/internal/handlers/ussd/menuhandler_test.go +++ b/internal/handlers/ussd/menuhandler_test.go @@ -5,6 +5,7 @@ import ( "fmt" "log" "path" + "strconv" "strings" "testing" @@ -907,37 +908,79 @@ func TestResetAccountAuthorized(t *testing.T) { } func TestIncorrectPinReset(t *testing.T) { + sessionId := "session123" + ctx, store := InitializeTestStore(t) fm, err := NewFlagManager(flagsPath) + if err != nil { log.Fatal(err) } flag_incorrect_pin, _ := fm.parser.GetFlag("flag_incorrect_pin") + flag_account_blocked, _ := fm.parser.GetFlag("flag_account_blocked") + + ctx = context.WithValue(ctx, "SessionId", sessionId) // Define test cases tests := []struct { name string input []byte + attempts uint8 expectedResult resource.Result }{ { - name: "Test incorrect pin reset", + name: "Test when incorrect PIN attempts is 2", input: []byte(""), expectedResult: resource.Result{ FlagReset: []uint32{flag_incorrect_pin}, + Content: "1", //Expected remaining PIN attempts }, + attempts: 2, + }, + { + name: "Test incorrect pin reset when incorrect PIN attempts is 1", + input: []byte(""), + expectedResult: resource.Result{ + FlagReset: []uint32{flag_incorrect_pin}, + Content: "2", //Expected remaining PIN attempts + }, + attempts: 1, + }, + { + name: "Test incorrect pin reset when incorrect PIN attempts is 1", + input: []byte(""), + expectedResult: resource.Result{ + FlagReset: []uint32{flag_incorrect_pin}, + Content: "2", //Expected remaining PIN attempts + }, + attempts: 1, + }, + { + name: "Test incorrect pin reset when incorrect PIN attempts is 3(account expected to be blocked)", + input: []byte(""), + expectedResult: resource.Result{ + FlagReset: []uint32{flag_incorrect_pin}, + FlagSet: []uint32{flag_account_blocked}, + }, + attempts: 3, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { + + if err := store.WriteEntry(ctx, sessionId, common.DATA_INCORRECT_PIN_ATTEMPTS, []byte(strconv.Itoa(int(tt.attempts)))); err != nil { + t.Fatal(err) + } + // Create the Handlers instance with the mock flag manager h := &Handlers{ - flagManager: fm.parser, + flagManager: fm.parser, + userdataStore: store, } // Call the method - res, err := h.ResetIncorrectPin(context.Background(), "reset_incorrect_pin", tt.input) + res, err := h.ResetIncorrectPin(ctx, "reset_incorrect_pin", tt.input) if err != nil { t.Error(err) } @@ -2190,3 +2233,55 @@ func TestGetVoucherDetails(t *testing.T) { assert.NoError(t, err) assert.Equal(t, expectedResult, res) } + +func TestCountIncorrectPINAttempts(t *testing.T) { + ctx, store := InitializeTestStore(t) + sessionId := "session123" + ctx = context.WithValue(ctx, "SessionId", sessionId) + attempts := uint8(2) + + h := &Handlers{ + userdataStore: store, + } + err := store.WriteEntry(ctx, sessionId, common.DATA_INCORRECT_PIN_ATTEMPTS, []byte(strconv.Itoa(int(attempts)))) + if err != nil { + t.Logf(err.Error()) + } + err = h.countIncorrectPINAttempts(ctx, sessionId) + if err != nil { + t.Logf(err.Error()) + } + + attemptsAfterCount, err := store.ReadEntry(ctx, sessionId, common.DATA_INCORRECT_PIN_ATTEMPTS) + if err != nil { + t.Logf(err.Error()) + } + pinAttemptsValue, _ := strconv.ParseUint(string(attemptsAfterCount), 0, 64) + pinAttemptsCount := uint8(pinAttemptsValue) + expectedAttempts := attempts + 1 + assert.Equal(t, pinAttemptsCount, expectedAttempts) + +} + +func TestResetIncorrectPINAttempts(t *testing.T) { + ctx, store := InitializeTestStore(t) + sessionId := "session123" + ctx = context.WithValue(ctx, "SessionId", sessionId) + + err := store.WriteEntry(ctx, sessionId, common.DATA_INCORRECT_PIN_ATTEMPTS, []byte(string("2"))) + if err != nil { + t.Logf(err.Error()) + } + + h := &Handlers{ + userdataStore: store, + } + h.resetIncorrectPINAttempts(ctx, sessionId) + incorrectAttempts, err := store.ReadEntry(ctx, sessionId, common.DATA_INCORRECT_PIN_ATTEMPTS) + + if err != nil { + t.Logf(err.Error()) + } + assert.Equal(t, "0", string(incorrectAttempts)) + +} From 8d477356f38e26112d89e0e70e3e48caa50a991f Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Wed, 8 Jan 2025 11:47:55 +0300 Subject: [PATCH 33/38] update tests --- menutraversal_test/group_test.json | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/menutraversal_test/group_test.json b/menutraversal_test/group_test.json index f35beb9..0998c11 100644 --- a/menutraversal_test/group_test.json +++ b/menutraversal_test/group_test.json @@ -54,7 +54,7 @@ }, { "input": "1235", - "expectedContent": "Incorrect PIN\n1:Retry\n9:Quit" + "expectedContent": "Incorrect PIN.You have: 2 remaining attempt(s).\n1:Retry\n9:Quit" }, { "input": "1", @@ -95,7 +95,7 @@ }, { "input": "1235", - "expectedContent": "Incorrect PIN\n1:Retry\n9:Quit" + "expectedContent": "Incorrect PIN.You have: 2 remaining attempt(s).\n1:Retry\n9:Quit" }, { "input": "1", @@ -107,8 +107,7 @@ }, { "input": "0", - "expectedContent": "Balances:\n1:My balance\n2:Community balance\n0:Back" - + "expectedContent": "Balances:\n1:My balance\n2:Community balance\n0:Back" }, { "input": "0", @@ -141,7 +140,7 @@ }, { "input": "1235", - "expectedContent": "Incorrect PIN\n1:Retry\n9:Quit" + "expectedContent": "Incorrect PIN.You have: 2 remaining attempt(s).\n1:Retry\n9:Quit" }, { "input": "1", @@ -153,8 +152,7 @@ }, { "input": "0", - "expectedContent": "Balances:\n1:My balance\n2:Community balance\n0:Back" - + "expectedContent": "Balances:\n1:My balance\n2:Community balance\n0:Back" }, { "input": "0", @@ -195,7 +193,7 @@ }, { "input": "1", - "expectedContent": "Enter your year of birth\n0:Back" + "expectedContent": "Enter your year of birth\n0:Back" }, { "input": "1940", @@ -258,7 +256,6 @@ "input": "0", "expectedContent": "{balance}\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" } - ] }, { @@ -443,10 +440,4 @@ ] } ] -} - - - - - - +} \ No newline at end of file From ed6651697aff2b56d012704fb3e0883888283d65 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Wed, 8 Jan 2025 14:13:14 +0300 Subject: [PATCH 34/38] chore : add variable description to AllowedPINAttempts --- common/pin.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/common/pin.go b/common/pin.go index d42fb58..13f21b3 100644 --- a/common/pin.go +++ b/common/pin.go @@ -6,10 +6,13 @@ import ( "golang.org/x/crypto/bcrypt" ) -// Define the regex pattern as a constant const ( - pinPattern = `^\d{4}$` + // Define the regex pattern as a constant + pinPattern = `^\d{4}$` + + //Allowed incorrect PIN attempts AllowedPINAttempts = uint8(3) + ) // checks whether the given input is a 4 digit number From 477b4cf8f6471e331d9e624bb90a53ef4ad2c9d8 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Wed, 8 Jan 2025 14:15:45 +0300 Subject: [PATCH 35/38] chore : rename remainingPINAttempts to currentWrongPinAttemptsCount --- internal/handlers/ussd/menuhandler.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index a06c274..b76cc7f 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -2163,8 +2163,8 @@ func (h *Handlers) resetIncorrectPINAttempts(ctx context.Context, sessionId stri } return err } - remainingPINAttempts, _ := strconv.ParseUint(string(currentWrongPinAttempts), 0, 64) - if remainingPINAttempts <= uint64(common.AllowedPINAttempts) { + currentWrongPinAttemptsCount, _ := strconv.ParseUint(string(currentWrongPinAttempts), 0, 64) + if currentWrongPinAttemptsCount <= uint64(common.AllowedPINAttempts) { err = store.WriteEntry(ctx, sessionId, common.DATA_INCORRECT_PIN_ATTEMPTS, []byte(string("0"))) if err != nil { logg.ErrorCtxf(ctx, "failed to reset incorrect PIN attempts ", "key", common.DATA_INCORRECT_PIN_ATTEMPTS, "value", common.AllowedPINAttempts, "error", err) From 2ff9fed3c5686a70a0dccd9f1f885b47a01f908a Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Wed, 8 Jan 2025 14:51:15 +0300 Subject: [PATCH 36/38] chore: rename countIncorrectPINAttempts to incrementIncorrectPINAttempts --- internal/handlers/ussd/menuhandler.go | 8 ++++---- internal/handlers/ussd/menuhandler_test.go | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index b76cc7f..f831696 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -747,7 +747,7 @@ func (h *Handlers) Authorize(ctx context.Context, sym string, input []byte) (res } } } else { - err := h.countIncorrectPINAttempts(ctx, sessionId) + err := h.incrementIncorrectPINAttempts(ctx, sessionId) if err != nil { return res, err } @@ -884,7 +884,7 @@ func (h *Handlers) ShowBlockedAccount(ctx context.Context, sym string, input []b code := codeFromCtx(ctx) l := gotext.NewLocale(translationDir, code) l.AddDomain("default") - res.Content = l.Get("Your account has been locked.For help on how to unblock your account,contact support at: 0757628885") + res.Content = l.Get("Your account has been locked.For help on how to unblock your account, contact support at: 0757628885") return res, nil } @@ -2124,8 +2124,8 @@ func (h *Handlers) UpdateAllProfileItems(ctx context.Context, sym string, input return res, nil } -// countIncorrectPINAttempts keeps track of the number of incorrect PIN attempts -func (h *Handlers) countIncorrectPINAttempts(ctx context.Context, sessionId string) error { +// incrementIncorrectPINAttempts keeps track of the number of incorrect PIN attempts +func (h *Handlers) incrementIncorrectPINAttempts(ctx context.Context, sessionId string) error { var pinAttemptsCount uint8 store := h.userdataStore diff --git a/internal/handlers/ussd/menuhandler_test.go b/internal/handlers/ussd/menuhandler_test.go index 5914821..af1380d 100644 --- a/internal/handlers/ussd/menuhandler_test.go +++ b/internal/handlers/ussd/menuhandler_test.go @@ -2247,7 +2247,7 @@ func TestCountIncorrectPINAttempts(t *testing.T) { if err != nil { t.Logf(err.Error()) } - err = h.countIncorrectPINAttempts(ctx, sessionId) + err = h.incrementIncorrectPINAttempts(ctx, sessionId) if err != nil { t.Logf(err.Error()) } From efdb52bccde15e6639fdf1b7175da3b64863de37 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Wed, 8 Jan 2025 14:52:40 +0300 Subject: [PATCH 37/38] chore: add space after punctuation --- menutraversal_test/group_test.json | 6 +++--- services/registration/incorrect_pin | 2 +- services/registration/incorrect_pin_swa | 2 +- services/registration/locale/swa/default.po | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/menutraversal_test/group_test.json b/menutraversal_test/group_test.json index 0998c11..0ffb49f 100644 --- a/menutraversal_test/group_test.json +++ b/menutraversal_test/group_test.json @@ -54,7 +54,7 @@ }, { "input": "1235", - "expectedContent": "Incorrect PIN.You have: 2 remaining attempt(s).\n1:Retry\n9:Quit" + "expectedContent": "Incorrect PIN. You have: 2 remaining attempt(s).\n1:Retry\n9:Quit" }, { "input": "1", @@ -95,7 +95,7 @@ }, { "input": "1235", - "expectedContent": "Incorrect PIN.You have: 2 remaining attempt(s).\n1:Retry\n9:Quit" + "expectedContent": "Incorrect PIN. You have: 2 remaining attempt(s).\n1:Retry\n9:Quit" }, { "input": "1", @@ -140,7 +140,7 @@ }, { "input": "1235", - "expectedContent": "Incorrect PIN.You have: 2 remaining attempt(s).\n1:Retry\n9:Quit" + "expectedContent": "Incorrect PIN. You have: 2 remaining attempt(s).\n1:Retry\n9:Quit" }, { "input": "1", diff --git a/services/registration/incorrect_pin b/services/registration/incorrect_pin index 0e34cd1..13a9562 100644 --- a/services/registration/incorrect_pin +++ b/services/registration/incorrect_pin @@ -1 +1 @@ -Incorrect PIN.You have: {{.reset_incorrect}} remaining attempt(s). \ No newline at end of file +Incorrect PIN. You have: {{.reset_incorrect}} remaining attempt(s). \ No newline at end of file diff --git a/services/registration/incorrect_pin_swa b/services/registration/incorrect_pin_swa index 8a8b913..ed22beb 100644 --- a/services/registration/incorrect_pin_swa +++ b/services/registration/incorrect_pin_swa @@ -1 +1 @@ -PIN ulioeka sio sahihi,una majaribio: {{.reset_incorrect}} yaliyobaki \ No newline at end of file +PIN ulioeka sio sahihi, una majaribio: {{.reset_incorrect}} yaliyobaki \ No newline at end of file diff --git a/services/registration/locale/swa/default.po b/services/registration/locale/swa/default.po index 797c703..4effc1e 100644 --- a/services/registration/locale/swa/default.po +++ b/services/registration/locale/swa/default.po @@ -10,7 +10,7 @@ msgstr "Asante kwa kutumia huduma ya Sarafu. Kwaheri!" msgid "For more help, please call: 0757628885" msgstr "Kwa usaidizi zaidi, piga: 0757628885" -msgid "Your account has been locked.For help on how to unblock your account,contact support at: 0757628885" +msgid "Your account has been locked.For help on how to unblock your account, contact support at: 0757628885" msgstr "Akaunti yako imefungwa.Kwa usaidizi wa jinsi ya kufungua akaunti yako, wasiliana na usaidizi kwa: 0757628885" msgid "Balance: %s\n" From b698f081363514907082d9df29520849a37cd8b7 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Wed, 8 Jan 2025 15:27:10 +0300 Subject: [PATCH 38/38] chore: add space after punctuation --- internal/handlers/ussd/menuhandler.go | 2 +- services/registration/locale/swa/default.po | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index f831696..607b812 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -884,7 +884,7 @@ func (h *Handlers) ShowBlockedAccount(ctx context.Context, sym string, input []b code := codeFromCtx(ctx) l := gotext.NewLocale(translationDir, code) l.AddDomain("default") - res.Content = l.Get("Your account has been locked.For help on how to unblock your account, contact support at: 0757628885") + res.Content = l.Get("Your account has been locked. For help on how to unblock your account, contact support at: 0757628885") return res, nil } diff --git a/services/registration/locale/swa/default.po b/services/registration/locale/swa/default.po index 4effc1e..6155063 100644 --- a/services/registration/locale/swa/default.po +++ b/services/registration/locale/swa/default.po @@ -10,8 +10,8 @@ msgstr "Asante kwa kutumia huduma ya Sarafu. Kwaheri!" msgid "For more help, please call: 0757628885" msgstr "Kwa usaidizi zaidi, piga: 0757628885" -msgid "Your account has been locked.For help on how to unblock your account, contact support at: 0757628885" -msgstr "Akaunti yako imefungwa.Kwa usaidizi wa jinsi ya kufungua akaunti yako, wasiliana na usaidizi kwa: 0757628885" +msgid "Your account has been locked. For help on how to unblock your account, contact support at: 0757628885" +msgstr "Akaunti yako imefungwa. Kwa usaidizi wa jinsi ya kufungua akaunti yako, wasiliana na usaidizi kwa: 0757628885" msgid "Balance: %s\n" msgstr "Salio: %s\n"