Skip to content

Commit

Permalink
Machine ID Override
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Jan 18, 2025
1 parent 62c5493 commit d9f9e4d
Show file tree
Hide file tree
Showing 25 changed files with 148 additions and 359 deletions.
11 changes: 5 additions & 6 deletions cmd/slackdump/internal/cfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var (
Browser browser.Browser
LegacyBrowser bool
ForceEnterprise bool
MachineID string // Machine ID override
MachineIDOvr string // Machine ID override

MemberOnly bool
DownloadFiles bool
Expand Down Expand Up @@ -91,7 +91,6 @@ const (
OmitChunkCacheFlag
OmitMemberOnlyFlag
OmitRecordFilesFlag
OmitMachineIDOvrFlag

OmitAll = OmitConfigFlag |
OmitDownloadFlag |
Expand All @@ -103,8 +102,7 @@ const (
OmitTimeframeFlag |
OmitChunkCacheFlag |
OmitMemberOnlyFlag |
OmitRecordFilesFlag |
OmitMachineIDOvrFlag
OmitRecordFilesFlag
)

// SetBaseFlags sets base flags
Expand All @@ -125,8 +123,9 @@ func SetBaseFlags(fs *flag.FlagSet, mask FlagMask) {
fs.StringVar(&RODUserAgent, "user-agent", "", "override the user agent string for EZ-Login 3000")
fs.BoolVar(&LoadSecrets, "load-env", false, "load secrets from the .env, .env.txt or secrets.txt file")
}
if mask&OmitMachineIDOvrFlag == 0 {
fs.StringVar(&MachineID, "machine-id", osenv.Secret("MACHINE_ID", ""), "override the machine ID for encryption")
if mask&OmitAuthFlags == 0 || mask&OmitCacheDir == 0 {
// machine-id flag will be automatically enabled if auth flags or cache dir flags are enabled.
fs.StringVar(&MachineIDOvr, "machine-id", osenv.Secret("MACHINE_ID_OVERRIDE", ""), "override the machine ID for encryption")
}
if mask&OmitDownloadFlag == 0 {
fs.BoolVar(&DownloadFiles, "files", true, "enables file attachments download (to disable, specify: -files=false)")
Expand Down
11 changes: 6 additions & 5 deletions cmd/slackdump/internal/diag/hydrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import (
"github.com/rusq/slack"
gomock "go.uber.org/mock/gomock"

"github.com/rusq/slackdump/v3/internal/mocks/mock_fsadapter"
"github.com/rusq/fsadapter/mocks/mock_fsadapter"

"github.com/rusq/slackdump/v3/mocks/mock_downloader"
)

Expand Down Expand Up @@ -148,12 +149,12 @@ func (f *fakewritecloser) Close() error {
func Test_downloadFiles(t *testing.T) {
tests := []struct {
name string
expectFn func(m *Mocksourcer, fs *mock_fsadapter.MockFS, d *mock_downloader.MockGetFiler)
expectFn func(m *Mocksourcer, fs *mock_fsadapter.MockFSCloser, d *mock_downloader.MockGetFiler)
wantErr bool
}{
{
"single message w 2 files",
func(m *Mocksourcer, fs *mock_fsadapter.MockFS, d *mock_downloader.MockGetFiler) {
func(m *Mocksourcer, fs *mock_fsadapter.MockFSCloser, d *mock_downloader.MockGetFiler) {
m.EXPECT().Channels().Return(TestChannels, nil)
m.EXPECT().AllMessages("C01").Return([]slack.Message{TestMsgWFile1}, nil)

Expand All @@ -167,7 +168,7 @@ func Test_downloadFiles(t *testing.T) {
},
{
"all ok",
func(m *Mocksourcer, fs *mock_fsadapter.MockFS, d *mock_downloader.MockGetFiler) {
func(m *Mocksourcer, fs *mock_fsadapter.MockFSCloser, d *mock_downloader.MockGetFiler) {
m.EXPECT().Channels().Return(TestChannels, nil)
m.EXPECT().AllMessages("C01").Return(TestMessages, nil)
m.EXPECT().AllThreadMessages("C01", "2").Return(TestThreadMessages, nil)
Expand All @@ -189,7 +190,7 @@ func Test_downloadFiles(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
ctrl := gomock.NewController(t)
ms := NewMocksourcer(ctrl)
fs := mock_fsadapter.NewMockFS(ctrl)
fs := mock_fsadapter.NewMockFSCloser(ctrl)
d := mock_downloader.NewMockGetFiler(ctrl)
if tt.expectFn != nil {
tt.expectFn(ms, fs, d)
Expand Down
7 changes: 3 additions & 4 deletions cmd/slackdump/internal/diag/info/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@ import (

"github.com/rusq/slackdump/v3/auth"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/cfg"
"github.com/rusq/slackdump/v3/internal/cache"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/workspace"
)

func CollectAuth(ctx context.Context, w io.Writer) error {
// lg := logger.FromContext(ctx)
fmt.Fprintln(os.Stderr, "To confirm the operation, please enter your OS password.")
if err := osValidateUser(ctx, os.Stderr); err != nil {
return err
}
m, err := cache.NewManager(cfg.CacheDir())
m, err := workspace.CacheMgr()
if err != nil {
return fmt.Errorf("cache error: %w", err)
}
Expand All @@ -34,7 +33,7 @@ func CollectAuth(ctx context.Context, w io.Writer) error {
if err != nil {
return fmt.Errorf("cache error: %w", err)
}
f, err := encio.Open(filepath.Join(cfg.CacheDir(), fi.Name()))
f, err := encio.Open(filepath.Join(cfg.CacheDir(), fi.Name()), encio.WithID(cfg.MachineIDOvr))
if err != nil {
return fmt.Errorf("cache error: %w", err)
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/slackdump/internal/diag/info/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package info

import (
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/cfg"
"github.com/rusq/slackdump/v3/internal/cache"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/workspace"
)

type Workspace struct {
Expand All @@ -13,10 +13,10 @@ type Workspace struct {
}

func (inf *Workspace) collect(replaceFn PathReplFunc) {
inf.Path = replaceFn(cfg.LocalCacheDir)
inf.Path = replaceFn(cfg.CacheDir())
inf.Count = -1
// Workspace information
m, err := cache.NewManager(cfg.LocalCacheDir)
m, err := workspace.CacheMgr()
if err != nil {
inf.Path = loser(err)
return
Expand Down
9 changes: 5 additions & 4 deletions cmd/slackdump/internal/diag/obfuscate.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,12 @@ func runObfuscate(ctx context.Context, cmd *base.Command, args []string) error {
inType := objtype(obfparam.input)

var fn func(context.Context) error
if inType == otFile || inType == otTerm {
switch inType {
case otFile, otTerm:
fn = obfFile
} else if inType == otDir {
case otDir:
fn = obfDir
} else {
default:
base.SetExitStatus(base.SInvalidParameters)
return fmt.Errorf("input %s is invalid", obfparam.input)
}
Expand Down Expand Up @@ -194,7 +195,7 @@ func obfDir(ctx context.Context) error {
InName: obfparam.input,
}
case otNotExist:
if err := os.MkdirAll(obfparam.output, 0755); err != nil {
if err := os.MkdirAll(obfparam.output, 0o755); err != nil {
return err
}
case otDir:
Expand Down
5 changes: 3 additions & 2 deletions cmd/slackdump/internal/diag/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import (
"strings"

"github.com/rusq/slackauth"

"github.com/rusq/slackdump/v3/cmd/slackdump/internal/cfg"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/diag/info"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/golang/base"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/ui"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/ui/cfgui"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/ui/dumpui"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/ui/updaters"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/workspace"
"github.com/rusq/slackdump/v3/internal/cache"
)

Expand Down Expand Up @@ -84,7 +86,6 @@ func init() {
cmdUninstall.Flag.BoolVar(&uninstParams.purge, "purge", false, "remove everything (same as -rod -playwright -cache)")
cmdUninstall.Flag.BoolVar(&uninstParams.dry, "dry", false, "dry run")
cmdUninstall.Flag.BoolVar(&uninstParams.noConfirm, "no-confirm", false, "no confirmation from the user")

}

func runUninstall(ctx context.Context, cmd *base.Command, args []string) error {
Expand All @@ -96,7 +97,7 @@ func runUninstall(ctx context.Context, cmd *base.Command, args []string) error {
return errors.New("nothing to uninstall")
}

m, err := cache.NewManager(cfg.CacheDir())
m, err := workspace.CacheMgr()
if err != nil {
base.SetExitStatus(base.SCacheError)
return err
Expand Down
3 changes: 2 additions & 1 deletion cmd/slackdump/internal/format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/rusq/slack"

"github.com/rusq/slackdump/v3/cmd/slackdump/internal/bootstrap"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/workspace"

"github.com/rusq/slackdump/v3/cmd/slackdump/internal/cfg"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/golang/base"
Expand Down Expand Up @@ -263,7 +264,7 @@ var errNoMatch = errors.New("no matching users")
func searchCache(ctx context.Context, cacheDir string, ids []string) ([]slack.User, error) {
_, task := trace.NewTask(ctx, "searchCache")
defer task.End()
m, err := cache.NewManager(cacheDir)
m, err := workspace.CacheMgr()
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/slackdump/internal/list/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/rusq/slackdump/v3"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/cfg"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/golang/base"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/workspace"
"github.com/rusq/slackdump/v3/internal/cache"
"github.com/rusq/slackdump/v3/internal/format"
"github.com/rusq/slackdump/v3/types"
Expand Down Expand Up @@ -86,7 +87,7 @@ func addCommonFlags(fs *flag.FlagSet) {
}

func list[T any](ctx context.Context, sess *slackdump.Session, l lister[T], filename string) error {
m, err := cache.NewManager(cfg.CacheDir())
m, err := workspace.CacheMgr()
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/slackdump/internal/workspace/del.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var (
)

func runWspDel(ctx context.Context, cmd *base.Command, args []string) error {
m, err := cache.NewManager(cfg.CacheDir())
m, err := CacheMgr()
if err != nil {
base.SetExitStatus(base.SCacheError)
return err
Expand Down
3 changes: 1 addition & 2 deletions cmd/slackdump/internal/workspace/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/rusq/slackdump/v3/auth"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/cfg"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/golang/base"
"github.com/rusq/slackdump/v3/internal/cache"
)

//go:embed assets/import.md
Expand Down Expand Up @@ -44,7 +43,7 @@ func importFile(ctx context.Context, filename string) error {
base.SetExitStatus(base.SUserError)
return err
}
m, err := cache.NewManager(cfg.CacheDir())
m, err := CacheMgr()
if err != nil {
base.SetExitStatus(base.SCacheError)
return err
Expand Down
4 changes: 2 additions & 2 deletions cmd/slackdump/internal/workspace/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func init() {
}

func runList(ctx context.Context, cmd *base.Command, args []string) error {
m, err := cache.NewManager(cfg.CacheDir())
m, err := CacheMgr()
if err != nil {
base.SetExitStatus(base.SCacheError)
return err
Expand Down Expand Up @@ -128,7 +128,7 @@ func printBare(_ context.Context, w io.Writer, _ manager, current string, worksp
}

func wspInfo(ctx context.Context, m manager, current string, wsps []string) [][]string {
var rows = [][]string{}
rows := [][]string{}

var (
wg sync.WaitGroup
Expand Down
5 changes: 2 additions & 3 deletions cmd/slackdump/internal/workspace/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var CmdWspNew = &base.Command{
UsageLine: baseCommand + " new [flags] <name>",
Short: "authenticate in a Slack Workspace",
Long: newMD,
FlagMask: flagmask &^ cfg.OmitAuthFlags, // only auth flags.
FlagMask: flagmask &^ cfg.OmitAuthFlags, // only auth and machine-id override flags.
PrintFlags: true,
Wizard: workspaceui.WorkspaceNew,
}
Expand All @@ -39,8 +39,7 @@ func init() {

// runWspNew authenticates in the new workspace.
func runWspNew(ctx context.Context, cmd *base.Command, args []string) error {
m, err := cache.NewManager(
cfg.CacheDir(),
m, err := CacheMgr(
cache.WithAuthOpts(
auth.BrowserWithBrowser(cfg.Browser),
auth.BrowserWithTimeout(cfg.LoginTimeout),
Expand Down
2 changes: 1 addition & 1 deletion cmd/slackdump/internal/workspace/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func runSelect(ctx context.Context, cmd *base.Command, args []string) error {
base.SetExitStatus(base.SInvalidParameters)
return cache.ErrNameRequired
}
m, err := cache.NewManager(cfg.CacheDir())
m, err := CacheMgr()
if err != nil {
base.SetExitStatus(base.SCacheError)
return fmt.Errorf("unable to initialise cache: %s", err)
Expand Down
6 changes: 3 additions & 3 deletions cmd/slackdump/internal/workspace/wiz_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"

"github.com/rusq/slackdump/v3/cmd/slackdump/internal/cfg"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/golang/base"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/workspace/workspaceui"
Expand All @@ -16,7 +17,7 @@ import (
// TODO: organise as a self-sufficient model with proper error handling.

func wizSelect(ctx context.Context, cmd *base.Command, args []string) error {
m, err := cache.NewManager(cfg.CacheDir())
m, err := CacheMgr()
if err != nil {
base.SetExitStatus(base.SCacheError)
return err
Expand Down Expand Up @@ -61,8 +62,7 @@ func wizSelect(ctx context.Context, cmd *base.Command, args []string) error {

// newWspSelectModel creates a new workspace selection model.
func newWspSelectModel(ctx context.Context, m *cache.Manager) (tea.Model, error) {

var refreshFn = func() (cols []table.Column, rows []table.Row, err error) {
refreshFn := func() (cols []table.Column, rows []table.Row, err error) {
cols = []table.Column{
{Title: "C", Width: 1},
{Title: "Name", Width: 14},
Expand Down
9 changes: 7 additions & 2 deletions cmd/slackdump/internal/workspace/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func AuthCurrent(ctx context.Context, cacheDir string, overrideWsp string, usePl
// configuration values. If cfg.Workspace is set, it checks if the workspace
// cfg.Workspace exists in the directory dir, and returns it.
func Current(cacheDir string, override string) (wsp string, err error) {
m, err := cache.NewManager(cacheDir)
m, err := cache.NewManager(cacheDir, cache.WithMachineID(cfg.MachineIDOvr))
if err != nil {
return "", err
}
Expand Down Expand Up @@ -131,7 +131,7 @@ var yesno = base.YesNo
// credentials in the cacheDir. It returns ErrNotExists if the workspace
// doesn't exist in the cacheDir.
func authWsp(ctx context.Context, cacheDir string, wsp string, usePlaywright bool) (auth.Provider, error) {
m, err := cache.NewManager(cacheDir)
m, err := cache.NewManager(cacheDir, cache.WithMachineID(cfg.MachineIDOvr))
if err != nil {
return nil, err
}
Expand All @@ -145,3 +145,8 @@ func authWsp(ctx context.Context, cacheDir string, wsp string, usePlaywright boo
}
return prov, nil
}

func CacheMgr(opts ...cache.Option) (*cache.Manager, error) {
opts = append([]cache.Option{cache.WithMachineID(cfg.MachineIDOvr)}, opts...)
return cache.NewManager(cfg.CacheDir(), opts...)
}
6 changes: 3 additions & 3 deletions cmd/slackdump/internal/workspace/workspaceui/workspaceui.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ func ShowUI(ctx context.Context, opts ...UIOption) error {
actExit = "exit"
)

mgr, err := cache.NewManager(cfg.CacheDir())
mgr, err := cache.NewManager(cfg.CacheDir(), cache.WithMachineID(cfg.MachineIDOvr)) // avoiding import cycle
if err != nil {
return err
}

var uiOpts = options{
uiOpts := options{
title: "New Workspace",
}
for _, o := range opts {
Expand Down Expand Up @@ -113,7 +113,7 @@ func ShowUI(ctx context.Context, opts ...UIOption) error {
}

// new workspace methods
var methods = map[string]func(context.Context, manager) error{
methods := map[string]func(context.Context, manager) error{
actLogin: brwsLogin(&brwsOpts),
actToken: prgTokenCookie,
actTokenFile: prgTokenCookieFile,
Expand Down
Loading

0 comments on commit d9f9e4d

Please sign in to comment.