Skip to content

Commit

Permalink
feat(cli): allow token from stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
Shikhar Mall committed Jul 26, 2023
1 parent bfedb25 commit 69aeba9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
26 changes: 19 additions & 7 deletions cli/command_repository_connect_from_config.go
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"context"
"io"
"os"

"github.com/alecthomas/kingpin/v2"
Expand All @@ -15,6 +16,7 @@ type storageFromConfigFlags struct {
connectFromConfigFile string
connectFromConfigToken string
connectFromTokenFile string
connectFromTokenStdin bool

sps StorageProviderServices
}
Expand All @@ -23,18 +25,15 @@ func (c *storageFromConfigFlags) Setup(sps StorageProviderServices, cmd *kingpin
cmd.Flag("file", "Path to the configuration file").StringVar(&c.connectFromConfigFile)
cmd.Flag("token", "Configuration token").StringVar(&c.connectFromConfigToken)
cmd.Flag("token-file", "Path to the configuration token file").StringVar(&c.connectFromTokenFile)
cmd.Flag("token-stdin", "Read configuration token from stdin").BoolVar(&c.connectFromTokenStdin)

c.sps = sps
}

func (c *storageFromConfigFlags) Connect(ctx context.Context, isCreate bool, formatVersion int) (blob.Storage, error) {
_ = formatVersion

if isCreate {
return nil, errors.New("not supported")
}

if c.connectFromConfigFile != "" {
if !isCreate && c.connectFromConfigFile != "" {
return c.connectToStorageFromConfigFile(ctx)
}

Expand All @@ -46,11 +45,15 @@ func (c *storageFromConfigFlags) Connect(ctx context.Context, isCreate bool, for
return c.connectToStorageFromStorageConfigFile(ctx)
}

if c.connectFromTokenStdin {
return c.connectToStorageFromStorageConfigStdin(ctx)
}

if isCreate {
return nil, errors.New("either --token-file or --token must be provided")
return nil, errors.New("one of --token-file, --token-stdin or --token must be provided")
}

return nil, errors.New("one of --file, --token-file or --token must be provided")
return nil, errors.New("one of --file, --token-file, --token-stdin or --token must be provided")

Check warning on line 56 in cli/command_repository_connect_from_config.go

View check run for this annotation

Codecov / codecov/patch

cli/command_repository_connect_from_config.go#L56

Added line #L56 was not covered by tests
}

func (c *storageFromConfigFlags) connectToStorageFromConfigFile(ctx context.Context) (blob.Storage, error) {
Expand Down Expand Up @@ -89,3 +92,12 @@ func (c *storageFromConfigFlags) connectToStorageFromStorageConfigFile(ctx conte

return c.connectToStorageFromConfigToken(ctx, string(tokenData))
}

func (c *storageFromConfigFlags) connectToStorageFromStorageConfigStdin(ctx context.Context) (blob.Storage, error) {
tokenData, err := io.ReadAll(c.sps.stdin())
if err != nil {
return nil, errors.Wrap(err, "unable to read token from stdin")
}

Check warning on line 100 in cli/command_repository_connect_from_config.go

View check run for this annotation

Codecov / codecov/patch

cli/command_repository_connect_from_config.go#L99-L100

Added lines #L99 - L100 were not covered by tests

return c.connectToStorageFromConfigToken(ctx, string(tokenData))
}
20 changes: 19 additions & 1 deletion cli/command_repository_create_test.go
Expand Up @@ -3,6 +3,7 @@ package cli_test
import (
"os"
"path"
"strings"
"testing"

"github.com/kopia/kopia/repo"
Expand All @@ -17,7 +18,7 @@ func TestRepositoryCreateWithConfigFile(t *testing.T) {
env := testenv.NewCLITest(t, nil, testenv.NewInProcRunner(t))

_, stderr := env.RunAndExpectFailure(t, "repo", "create", "from-config", "--file", path.Join(env.ConfigDir, "does_not_exist.config"))
require.Contains(t, stderr, "can't connect to storage: either --token-file or --token must be provided")
require.Contains(t, stderr, "can't connect to storage: one of --token-file, --token-stdin or --token must be provided")

_, stderr = env.RunAndExpectFailure(t, "repo", "create", "from-config", "--token", "bad-token")
require.Contains(t, stderr, "can't connect to storage: invalid token: unable to decode token")
Expand All @@ -36,3 +37,20 @@ func TestRepositoryCreateWithConfigFile(t *testing.T) {

env.RunAndExpectSuccess(t, "repo", "create", "from-config", "--token-file", storageCfgFName)
}

func TestRepositoryCreateWithConfigFromStdin(t *testing.T) {
runner := testenv.NewInProcRunner(t)
env := testenv.NewCLITest(t, nil, runner)

ci := blob.ConnectionInfo{
Type: "filesystem",
Config: filesystem.Options{Path: env.RepoDir},
}
token, err := repo.EncodeToken("12345678", ci)
require.Nil(t, err)

// set stdin
runner.SetNextStdin(strings.NewReader(token))

env.RunAndExpectSuccess(t, "repo", "create", "from-config", "--token-stdin")
}
2 changes: 2 additions & 0 deletions cli/storage_providers.go
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"context"
"io"

"github.com/alecthomas/kingpin/v2"

Expand All @@ -15,6 +16,7 @@ type StorageProviderServices interface {
EnvName(s string) string
setPasswordFromToken(pwd string)
storageProviders() []StorageProvider
stdin() io.Reader
}

// StorageFlags is implemented by cli storage providers which need to support a
Expand Down

0 comments on commit 69aeba9

Please sign in to comment.