diff --git a/pkg/kopia/cli/errors.go b/pkg/kopia/cli/errors.go index 483302b57f..68153212d5 100644 --- a/pkg/kopia/cli/errors.go +++ b/pkg/kopia/cli/errors.go @@ -38,4 +38,6 @@ var ( ErrInvalidCredentialsFile = errors.New("credentials file cannot be empty") // ErrInvalidContainerName is returned when the containerName is empty. ErrInvalidContainerName = errors.New("container name cannot be empty") + // ErrInvalidServerURL is returned when the serverURL is empty. + ErrInvalidServerURL = errors.New("server URL cannot be empty") ) diff --git a/pkg/kopia/cli/repository/opts.go b/pkg/kopia/cli/repository/opts.go index 16581c427e..fdea7e3394 100644 --- a/pkg/kopia/cli/repository/opts.go +++ b/pkg/kopia/cli/repository/opts.go @@ -36,6 +36,7 @@ var ( subcmdCreate = command.NewArgument("create") subcmdConnect = command.NewArgument("connect") + subcmdServer = command.NewArgument("server") ) // optHostname creates a new option for the hostname of the repository. @@ -104,3 +105,19 @@ func optPointInTime(l internal.Location, pit strfmt.DateTime) command.Applier { } return command.NewOptionWithArgument("--point-in-time", pit.String()) } + +// optServerURL creates a new server URL flag with a given server URL. +func optServerURL(serverURL string) command.Applier { + if serverURL == "" { + return command.NewErrorArgument(cli.ErrInvalidServerURL) + } + return command.NewOptionWithArgument("--url", serverURL) +} + +// optServerCertFingerprint creates a new server certificate fingerprint flag with a given fingerprint. +func optServerCertFingerprint(fingerprint string) command.Applier { + if fingerprint == "" { + return command.NewNoopArgument() + } + return command.NewOptionWithRedactedArgument("--server-cert-fingerprint", fingerprint) +} diff --git a/pkg/kopia/cli/repository/opts_test.go b/pkg/kopia/cli/repository/opts_test.go index f5cc578d1a..0228834edc 100644 --- a/pkg/kopia/cli/repository/opts_test.go +++ b/pkg/kopia/cli/repository/opts_test.go @@ -112,4 +112,22 @@ var _ = check.Suite(&test.ArgumentSuite{Cmd: "cmd", Arguments: []test.ArgumentTe "--point-in-time=2021-02-04T01:02:03.000Z", }, }, + { + Name: "optServerURL with ServerURL should return option", + Argument: optServerURL("http://test-server"), + ExpectedCLI: []string{"cmd", "--url=http://test-server"}, + }, + { + Name: "optServerURL with empty ServerURL should return error", + Argument: optServerURL(""), + ExpectedErr: cli.ErrInvalidServerURL, + }, + { + Name: "optServerCertFingerprint", + Argument: command.NewArguments( + optServerCertFingerprint("fingerprint"), + optServerCertFingerprint(""), // no output + ), + ExpectedCLI: []string{"cmd", "--server-cert-fingerprint=fingerprint"}, + }, }}) diff --git a/pkg/kopia/cli/repository/repository_connect_server.go b/pkg/kopia/cli/repository/repository_connect_server.go new file mode 100644 index 0000000000..da4f2925b8 --- /dev/null +++ b/pkg/kopia/cli/repository/repository_connect_server.go @@ -0,0 +1,54 @@ +// Copyright 2024 The Kanister Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package repository + +import ( + "github.com/kanisterio/safecli" + + "github.com/kanisterio/kanister/pkg/kopia/cli/args" + "github.com/kanisterio/kanister/pkg/kopia/cli/internal" + "github.com/kanisterio/kanister/pkg/kopia/cli/internal/opts" + "github.com/kanisterio/kanister/pkg/log" +) + +// ConnectServerArgs defines the arguments for the `kopia repository connect server` command. +type ConnectServerArgs struct { + args.Common + args.Cache + + Hostname string // hostname of the repository + Username string // username of the repository + ServerURL string // URL of the Kopia Repository API server + Fingerprint string // fingerprint of the server's TLS certificate + ReadOnly bool // connect to a repository in read-only mode + + Logger log.Logger +} + +// ConnectServer creates a new `kopia repository connect server...` command. +func ConnectServer(args ConnectServerArgs) (*safecli.Builder, error) { + return internal.NewKopiaCommand( + opts.Common(args.Common), + cmdRepository, subcmdConnect, subcmdServer, + opts.CheckForUpdates(false), + opts.GRPC(false), + optReadOnly(args.ReadOnly), + opts.Cache(args.Cache), + optHostname(args.Hostname), + optUsername(args.Username), + optServerURL(args.ServerURL), + optServerCertFingerprint(args.Fingerprint), + ) +} diff --git a/pkg/kopia/cli/repository/repository_connect_server_test.go b/pkg/kopia/cli/repository/repository_connect_server_test.go new file mode 100644 index 0000000000..43a188637e --- /dev/null +++ b/pkg/kopia/cli/repository/repository_connect_server_test.go @@ -0,0 +1,50 @@ +package repository + +import ( + "testing" + + "github.com/kanisterio/safecli" + "gopkg.in/check.v1" + + "github.com/kanisterio/kanister/pkg/kopia/cli/internal/test" +) + +func TestRepositoryConnectServerCommand(t *testing.T) { check.TestingT(t) } + +// Test Repository Connect Server command +var _ = check.Suite(test.NewCommandSuite([]test.CommandTest{ + { + Name: "repository connect server", + Command: func() (*safecli.Builder, error) { + args := ConnectServerArgs{ + Common: common, + Cache: cache, + Hostname: "test-hostname", + Username: "test-username", + ServerURL: "http://test-server", + Fingerprint: "test-fingerprint", + ReadOnly: true, + } + return ConnectServer(args) + }, + ExpectedCLI: []string{"kopia", + "--config-file=path/kopia.config", + "--log-dir=cache/log", + "--log-level=error", + "--password=encr-key", + "repository", + "connect", + "server", + "--no-check-for-updates", + "--no-grpc", + "--readonly", + "--cache-directory=/tmp/cache.dir", + "--content-cache-size-limit-mb=0", + "--metadata-cache-size-limit-mb=0", + "--override-hostname=test-hostname", + "--override-username=test-username", + "--url=http://test-server", + "--server-cert-fingerprint=test-fingerprint", + }, + }, +}))