From 72f8a1fd5e7fbd5224e3f1393f36cc9b0d58eb78 Mon Sep 17 00:00:00 2001 From: Jeff Halter <868228+jhalter@users.noreply.github.com> Date: Sun, 28 Jul 2024 12:43:23 -0700 Subject: Improve human readability of account config files --- internal/mobius/account_manager.go | 3 +- internal/mobius/account_manager_test.go | 89 ++++++++++++++++++++ internal/mobius/test/config/Users/admin.yaml | 52 +++++++++--- internal/mobius/test/config/Users/guest.yaml | 51 +++++++++--- .../config/Users/user-with-old-access-format.yaml | 12 +++ internal/mobius/transaction_handlers.go | 6 +- internal/mobius/transaction_handlers_test.go | 94 ++++++++++++++++++++++ 7 files changed, 285 insertions(+), 22 deletions(-) create mode 100644 internal/mobius/account_manager_test.go create mode 100644 internal/mobius/test/config/Users/user-with-old-access-format.yaml (limited to 'internal') diff --git a/internal/mobius/account_manager.go b/internal/mobius/account_manager.go index cba33b2..3bb5cbc 100644 --- a/internal/mobius/account_manager.go +++ b/internal/mobius/account_manager.go @@ -38,7 +38,7 @@ func NewYAMLAccountManager(accountDir string) (*YAMLAccountManager, error) { matches, err := filepath.Glob(filepath.Join(accountDir, "*.yaml")) if err != nil { - return nil, err + return nil, fmt.Errorf("list account files: %w", err) } if len(matches) == 0 { @@ -50,7 +50,6 @@ func NewYAMLAccountManager(accountDir string) (*YAMLAccountManager, error) { if err = loadFromYAMLFile(file, &account); err != nil { return nil, fmt.Errorf("error loading account %s: %w", file, err) } - accountMgr.accounts[account.Login] = account } diff --git a/internal/mobius/account_manager_test.go b/internal/mobius/account_manager_test.go new file mode 100644 index 0000000..fe834a4 --- /dev/null +++ b/internal/mobius/account_manager_test.go @@ -0,0 +1,89 @@ +package mobius + +import ( + "fmt" + "github.com/jhalter/mobius/hotline" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestNewYAMLAccountManager(t *testing.T) { + type args struct { + accountDir string + } + tests := []struct { + name string + args args + want *YAMLAccountManager + wantErr assert.ErrorAssertionFunc + }{ + { + name: "loads accounts from a directory", + args: args{ + accountDir: "test/config/Users", + }, + want: &YAMLAccountManager{ + accountDir: "test/config/Users", + accounts: map[string]hotline.Account{ + "admin": { + Name: "admin", + Login: "admin", + Password: "$2a$04$2itGEYx8C1N5bsfRSoC9JuonS3I4YfnyVPZHLSwp7kEInRX0yoB.a", + Access: hotline.AccessBitmap{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00}, + }, + "Test User Name": { + Name: "test-user", + Login: "Test User Name", + Password: "$2a$04$9P/jgLn1fR9TjSoWL.rKxuN6g.1TSpf2o6Hw.aaRuBwrWIJNwsKkS", + Access: hotline.AccessBitmap{0x7d, 0xf0, 0x0c, 0xef, 0xab, 0x80, 0x00, 0x00}, + }, + "guest": { + Name: "guest", + Login: "guest", + Password: "$2a$04$6Yq/TIlgjSD.FbARwtYs9ODnkHawonu1TJ5W2jJKfhnHwBIQTk./y", + Access: hotline.AccessBitmap{0x7d, 0xf0, 0x0c, 0xef, 0xab, 0x80, 0x00, 0x00}, + }, + }, + }, + wantErr: assert.NoError, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := NewYAMLAccountManager(tt.args.accountDir) + if !tt.wantErr(t, err, fmt.Sprintf("NewYAMLAccountManager(%v)", tt.args.accountDir)) { + return + } + + assert.Equal(t, + &hotline.Account{ + Name: "admin", + Login: "admin", + Password: "$2a$04$2itGEYx8C1N5bsfRSoC9JuonS3I4YfnyVPZHLSwp7kEInRX0yoB.a", + Access: hotline.AccessBitmap{0xff, 0xff, 0xef, 0xff, 0xff, 0x80, 0x00, 0x00}, + }, + got.Get("admin"), + ) + + assert.Equal(t, + &hotline.Account{ + Login: "guest", + Name: "guest", + Password: "$2a$04$6Yq/TIlgjSD.FbARwtYs9ODnkHawonu1TJ5W2jJKfhnHwBIQTk./y", + Access: hotline.AccessBitmap{0x60, 0x70, 0x0c, 0x20, 0x03, 0x80, 0x00, 0x00}, + }, + got.Get("guest"), + ) + + assert.Equal(t, + &hotline.Account{ + Login: "test-user", + Name: "Test User Name", + Password: "$2a$04$9P/jgLn1fR9TjSoWL.rKxuN6g.1TSpf2o6Hw.aaRuBwrWIJNwsKkS", + Access: hotline.AccessBitmap{0x7d, 0xf0, 0x0c, 0xef, 0xab, 0x80, 0x00, 0x00}, + }, + got.Get("test-user"), + ) + }) + } +} diff --git a/internal/mobius/test/config/Users/admin.yaml b/internal/mobius/test/config/Users/admin.yaml index 1bf656b..1c33eff 100644 --- a/internal/mobius/test/config/Users/admin.yaml +++ b/internal/mobius/test/config/Users/admin.yaml @@ -1,13 +1,45 @@ Login: admin Name: admin Password: $2a$04$2itGEYx8C1N5bsfRSoC9JuonS3I4YfnyVPZHLSwp7kEInRX0yoB.a -Access: -- 255 -- 255 -- 255 -- 255 -- 255 -- 255 -- 0 -- 0 - +Access: + DownloadFile: true + DownloadFolder: true + UploadFile: true + UploadFolder: true + DeleteFile: true + RenameFile: true + MoveFile: true + CreateFolder: true + DeleteFolder: true + RenameFolder: true + MoveFolder: true + ReadChat: true + SendChat: true + OpenChat: true + CloseChat: true + ShowInList: true + CreateUser: true + DeleteUser: true + OpenUser: true + ModifyUser: true + ChangeOwnPass: true + NewsReadArt: true + NewsPostArt: true + DisconnectUser: true + CannotBeDisconnected: true + GetClientInfo: true + UploadAnywhere: true + AnyName: true + NoAgreement: true + SetFileComment: true + SetFolderComment: true + ViewDropBoxes: true + MakeAlias: true + Broadcast: true + NewsDeleteArt: true + NewsCreateCat: true + NewsDeleteCat: true + NewsCreateFldr: true + NewsDeleteFldr: true + SendPrivMsg: true +FileRoot: "" diff --git a/internal/mobius/test/config/Users/guest.yaml b/internal/mobius/test/config/Users/guest.yaml index 57117bd..a0dd376 100644 --- a/internal/mobius/test/config/Users/guest.yaml +++ b/internal/mobius/test/config/Users/guest.yaml @@ -1,12 +1,45 @@ Login: guest Name: guest -Password: $2a$04$9P/jgLn1fR9TjSoWL.rKxuN6g.1TSpf2o6Hw.aaRuBwrWIJNwsKkS +Password: $2a$04$6Yq/TIlgjSD.FbARwtYs9ODnkHawonu1TJ5W2jJKfhnHwBIQTk./y Access: -- 125 -- 240 -- 12 -- 239 -- 171 -- 128 -- 0 -- 0 + DownloadFile: true + DownloadFolder: true + UploadFile: true + UploadFolder: true + DeleteFile: false + RenameFile: false + MoveFile: false + CreateFolder: false + DeleteFolder: false + RenameFolder: false + MoveFolder: false + ReadChat: true + SendChat: true + OpenChat: true + CloseChat: false + ShowInList: false + CreateUser: false + DeleteUser: false + OpenUser: false + ModifyUser: false + ChangeOwnPass: false + NewsReadArt: true + NewsPostArt: true + DisconnectUser: false + CannotBeDisconnected: false + GetClientInfo: false + UploadAnywhere: false + AnyName: true + NoAgreement: false + SetFileComment: false + SetFolderComment: false + ViewDropBoxes: false + MakeAlias: false + Broadcast: false + NewsDeleteArt: false + NewsCreateCat: false + NewsDeleteCat: false + NewsCreateFldr: false + NewsDeleteFldr: false + SendPrivMsg: true +FileRoot: "" diff --git a/internal/mobius/test/config/Users/user-with-old-access-format.yaml b/internal/mobius/test/config/Users/user-with-old-access-format.yaml new file mode 100644 index 0000000..fa2f5cb --- /dev/null +++ b/internal/mobius/test/config/Users/user-with-old-access-format.yaml @@ -0,0 +1,12 @@ +Login: test-user +Name: Test User Name +Password: $2a$04$9P/jgLn1fR9TjSoWL.rKxuN6g.1TSpf2o6Hw.aaRuBwrWIJNwsKkS +Access: + - 125 + - 240 + - 12 + - 239 + - 171 + - 128 + - 0 + - 0 diff --git a/internal/mobius/transaction_handlers.go b/internal/mobius/transaction_handlers.go index 6f734c7..d18dba5 100644 --- a/internal/mobius/transaction_handlers.go +++ b/internal/mobius/transaction_handlers.go @@ -1329,7 +1329,7 @@ func HandleDownloadFile(cc *hotline.ClientConn, t *hotline.Transaction) (res []h // Download all files from the specified folder and sub-folders func HandleDownloadFolder(cc *hotline.ClientConn, t *hotline.Transaction) (res []hotline.Transaction) { - if !cc.Authorize(hotline.AccessDownloadFile) { + if !cc.Authorize(hotline.AccessDownloadFolder) { return cc.NewErrReply(t, "You are not allowed to download folders.") } @@ -1372,6 +1372,10 @@ func HandleDownloadFolder(cc *hotline.ClientConn, t *hotline.Transaction) (res [ // 220 Folder item count // 204 File transfer options "Optional Currently set to 1" (TODO: ??) func HandleUploadFolder(cc *hotline.ClientConn, t *hotline.Transaction) (res []hotline.Transaction) { + if !cc.Authorize(hotline.AccessUploadFolder) { + return cc.NewErrReply(t, "You are not allowed to upload folders.") + } + var fp hotline.FilePath if t.GetField(hotline.FieldFilePath).Data != nil { if _, err := fp.Write(t.GetField(hotline.FieldFilePath).Data); err != nil { diff --git a/internal/mobius/transaction_handlers_test.go b/internal/mobius/transaction_handlers_test.go index 26ea950..43c033f 100644 --- a/internal/mobius/transaction_handlers_test.go +++ b/internal/mobius/transaction_handlers_test.go @@ -3774,3 +3774,97 @@ func TestHandlePostNewsArt(t *testing.T) { }) } } + +func TestHandleUploadFolder(t *testing.T) { + type args struct { + cc *hotline.ClientConn + t hotline.Transaction + } + tests := []struct { + name string + args args + wantRes []hotline.Transaction + }{ + { + name: "when user does not have required access", + args: args{ + cc: &hotline.ClientConn{ + Account: &hotline.Account{ + Access: hotline.AccessBitmap{}, + }, + }, + t: hotline.NewTransaction( + hotline.TranUploadFldr, [2]byte{0, 1}, + hotline.NewField(hotline.FieldFileName, []byte("testFile")), + hotline.NewField(hotline.FieldFilePath, []byte{ + 0x00, 0x01, + 0x00, 0x00, + 0x03, + 0x2e, 0x2e, 0x2f, + }), + ), + }, + wantRes: []hotline.Transaction{ + { + IsReply: 0x01, + ErrorCode: [4]byte{0, 0, 0, 1}, + Fields: []hotline.Field{ + hotline.NewField(hotline.FieldError, []byte("You are not allowed to upload folders.")), + }, + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + TranAssertEqual(t, tt.wantRes, HandleUploadFolder(tt.args.cc, &tt.args.t)) + }) + } +} + +func TestHandleDownloadFolder(t *testing.T) { + type args struct { + cc *hotline.ClientConn + t hotline.Transaction + } + tests := []struct { + name string + args args + wantRes []hotline.Transaction + }{ + { + name: "when user does not have required access", + args: args{ + cc: &hotline.ClientConn{ + Account: &hotline.Account{ + Access: hotline.AccessBitmap{}, + }, + }, + t: hotline.NewTransaction( + hotline.TranDownloadFldr, [2]byte{0, 1}, + hotline.NewField(hotline.FieldFileName, []byte("testFile")), + hotline.NewField(hotline.FieldFilePath, []byte{ + 0x00, 0x01, + 0x00, 0x00, + 0x03, + 0x2e, 0x2e, 0x2f, + }), + ), + }, + wantRes: []hotline.Transaction{ + { + IsReply: 0x01, + ErrorCode: [4]byte{0, 0, 0, 1}, + Fields: []hotline.Field{ + hotline.NewField(hotline.FieldError, []byte("You are not allowed to download folders.")), + }, + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + TranAssertEqual(t, tt.wantRes, HandleDownloadFolder(tt.args.cc, &tt.args.t)) + }) + } +} -- cgit