From d0364f094274e409a1f222b92000595983881766 Mon Sep 17 00:00:00 2001 From: Hidetake Iwata Date: Tue, 4 May 2021 11:12:10 +0900 Subject: [PATCH] Fix "~" is not expanded on Windows (#550) * Run tests on macOS and Windows * Use filepath and client-go/util/homedir package --- .github/workflows/go.yaml | 31 +++++++++++++++++++++++++++++-- pkg/cmd/authentication.go | 14 +++----------- pkg/cmd/cmd.go | 3 ++- pkg/cmd/cmd_test.go | 13 +++++++------ pkg/cmd/get_token.go | 27 +++------------------------ pkg/cmd/homedir.go | 15 +++++++++++++++ pkg/cmd/tls.go | 9 ++------- 7 files changed, 61 insertions(+), 51 deletions(-) create mode 100644 pkg/cmd/homedir.go diff --git a/.github/workflows/go.yaml b/.github/workflows/go.yaml index 36fd4ec9..efcb9326 100644 --- a/.github/workflows/go.yaml +++ b/.github/workflows/go.yaml @@ -42,12 +42,39 @@ jobs: - uses: actions/cache@v2 with: path: ~/go/pkg/mod - key: go-${{ runner.os }}-${{ hashFiles('**/go.sum') }} + key: go-linux-amd64-${{ hashFiles('**/go.sum') }} restore-keys: | - go-${{ runner.os }}- + go-linux-amd64- - run: go test -v -race -cover -coverprofile=coverage.out ./... - uses: codecov/codecov-action@v1 + test-platform-dependent: + strategy: + matrix: + platform: + - os: windows-latest + GOOS: windows + GOARCH: amd64 + - os: macos-latest + GOOS: darwin + GOARCH: amd64 + runs-on: ${{ matrix.platform.os }} + env: + GOOS: ${{ matrix.platform.GOOS }} + GOARCH: ${{ matrix.platform.GOARCH }} + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-go@v2 + with: + go-version: 1.16 + - uses: actions/cache@v2 + with: + path: ~/go/pkg/mod + key: go-${{ matrix.platform.GOOS }}-${{ matrix.platform.GOARCH }}-${{ hashFiles('**/go.sum') }} + restore-keys: | + go-${{ matrix.platform.GOOS }}-${{ matrix.platform.GOARCH }}- + - run: go test -race ./... + release: strategy: matrix: diff --git a/pkg/cmd/authentication.go b/pkg/cmd/authentication.go index 03730fe9..a67f13b8 100644 --- a/pkg/cmd/authentication.go +++ b/pkg/cmd/authentication.go @@ -67,17 +67,9 @@ func (o *authenticationOptions) addFlags(f *pflag.FlagSet) { f.StringVar(&o.Password, "password", "", "[password] Password for resource owner password credentials grant") } -func (o *authenticationOptions) expandHomedir() error { - var err error - o.LocalServerCertFile, err = expandHomedir(o.LocalServerCertFile) - if err != nil { - return fmt.Errorf("invalid --local-server-cert: %w", err) - } - o.LocalServerKeyFile, err = expandHomedir(o.LocalServerKeyFile) - if err != nil { - return fmt.Errorf("invalid --local-server-key: %w", err) - } - return nil +func (o *authenticationOptions) expandHomedir() { + o.LocalServerCertFile = expandHomedir(o.LocalServerCertFile) + o.LocalServerKeyFile = expandHomedir(o.LocalServerKeyFile) } func (o *authenticationOptions) grantOptionSet() (s authentication.GrantOptionSet, err error) { diff --git a/pkg/cmd/cmd.go b/pkg/cmd/cmd.go index fb812b41..e4953c08 100644 --- a/pkg/cmd/cmd.go +++ b/pkg/cmd/cmd.go @@ -2,6 +2,7 @@ package cmd import ( "context" + "path/filepath" "runtime" "github.com/google/wire" @@ -23,7 +24,7 @@ type Interface interface { } var defaultListenAddress = []string{"127.0.0.1:8000", "127.0.0.1:18000"} -var defaultTokenCacheDir = "~/.kube/cache/oidc-login" +var defaultTokenCacheDir = filepath.Join("~", ".kube", "cache", "oidc-login") const defaultAuthenticationTimeoutSec = 180 diff --git a/pkg/cmd/cmd_test.go b/pkg/cmd/cmd_test.go index c41f3273..81c10786 100644 --- a/pkg/cmd/cmd_test.go +++ b/pkg/cmd/cmd_test.go @@ -3,6 +3,7 @@ package cmd import ( "context" "os" + "path/filepath" "testing" "time" @@ -116,7 +117,7 @@ func TestCmd_Run(t *testing.T) { "--oidc-client-id", "YOUR_CLIENT_ID", }, in: credentialplugin.Input{ - TokenCacheDir: userHomeDir + "/.kube/cache/oidc-login", + TokenCacheDir: filepath.Join(userHomeDir, ".kube/cache/oidc-login"), Provider: oidc.Provider{ IssuerURL: "https://issuer.example.com", ClientID: "YOUR_CLIENT_ID", @@ -141,7 +142,7 @@ func TestCmd_Run(t *testing.T) { "-v1", }, in: credentialplugin.Input{ - TokenCacheDir: userHomeDir + "/.kube/cache/oidc-login", + TokenCacheDir: filepath.Join(userHomeDir, ".kube/cache/oidc-login"), Provider: oidc.Provider{ IssuerURL: "https://issuer.example.com", ClientID: "YOUR_CLIENT_ID", @@ -168,7 +169,7 @@ func TestCmd_Run(t *testing.T) { "--token-cache-dir", "~/.kube/oidc-cache", }, in: credentialplugin.Input{ - TokenCacheDir: userHomeDir + "/.kube/oidc-cache", + TokenCacheDir: filepath.Join(userHomeDir, ".kube/oidc-cache"), Provider: oidc.Provider{ IssuerURL: "https://issuer.example.com", ClientID: "YOUR_CLIENT_ID", @@ -178,12 +179,12 @@ func TestCmd_Run(t *testing.T) { BindAddress: defaultListenAddress, AuthenticationTimeout: defaultAuthenticationTimeoutSec * time.Second, RedirectURLHostname: "localhost", - LocalServerCertFile: userHomeDir + "/.kube/oidc-server.crt", - LocalServerKeyFile: userHomeDir + "/.kube/oidc-server.key", + LocalServerCertFile: filepath.Join(userHomeDir, ".kube/oidc-server.crt"), + LocalServerKeyFile: filepath.Join(userHomeDir, ".kube/oidc-server.key"), }, }, TLSClientConfig: tlsclientconfig.Config{ - CACertFilename: []string{userHomeDir + "/.kube/ca.crt"}, + CACertFilename: []string{filepath.Join(userHomeDir, ".kube/ca.crt")}, }, }, }, diff --git a/pkg/cmd/get_token.go b/pkg/cmd/get_token.go index b49dd2b4..1f9fa501 100644 --- a/pkg/cmd/get_token.go +++ b/pkg/cmd/get_token.go @@ -3,8 +3,6 @@ package cmd import ( "errors" "fmt" - "os" - "strings" "github.com/int128/kubelogin/pkg/infrastructure/logger" "github.com/int128/kubelogin/pkg/oidc" @@ -35,17 +33,9 @@ func (o *getTokenOptions) addFlags(f *pflag.FlagSet) { } func (o *getTokenOptions) expandHomedir() error { - var err error - o.TokenCacheDir, err = expandHomedir(o.TokenCacheDir) - if err != nil { - return fmt.Errorf("invalid --token-cache-dir: %w", err) - } - if err = o.authenticationOptions.expandHomedir(); err != nil { - return err - } - if err = o.tlsOptions.expandHomedir(); err != nil { - return err - } + o.TokenCacheDir = expandHomedir(o.TokenCacheDir) + o.authenticationOptions.expandHomedir() + o.tlsOptions.expandHomedir() return nil } @@ -100,14 +90,3 @@ func (cmd *GetToken) New() *cobra.Command { o.addFlags(c.Flags()) return c } - -func expandHomedir(s string) (string, error) { - if !strings.HasPrefix(s, "~"+string(os.PathSeparator)) { - return s, nil - } - userHomeDir, err := os.UserHomeDir() - if err != nil { - return "", fmt.Errorf("could not expand homedir: %w", err) - } - return userHomeDir + strings.TrimPrefix(s, "~"), nil -} diff --git a/pkg/cmd/homedir.go b/pkg/cmd/homedir.go new file mode 100644 index 00000000..57831bab --- /dev/null +++ b/pkg/cmd/homedir.go @@ -0,0 +1,15 @@ +package cmd + +import ( + "path/filepath" + "strings" + + "k8s.io/client-go/util/homedir" +) + +func expandHomedir(s string) string { + if !strings.HasPrefix(s, "~") { + return s + } + return filepath.Join(homedir.HomeDir(), strings.TrimPrefix(s, "~")) +} diff --git a/pkg/cmd/tls.go b/pkg/cmd/tls.go index 4cbf3afb..7ab6e49c 100644 --- a/pkg/cmd/tls.go +++ b/pkg/cmd/tls.go @@ -2,7 +2,6 @@ package cmd import ( "crypto/tls" - "fmt" "github.com/int128/kubelogin/pkg/tlsclientconfig" "github.com/spf13/pflag" @@ -24,17 +23,13 @@ func (o *tlsOptions) addFlags(f *pflag.FlagSet) { f.BoolVar(&o.RenegotiateFreelyAsClient, "tls-renegotiation-freely", false, "If set, allow a remote server to repeatedly request renegotiation") } -func (o *tlsOptions) expandHomedir() error { +func (o *tlsOptions) expandHomedir() { var caCertFilenames []string for _, caCertFilename := range o.CACertFilename { - expanded, err := expandHomedir(caCertFilename) - if err != nil { - return fmt.Errorf("invalid --certificate-authority: %w", err) - } + expanded := expandHomedir(caCertFilename) caCertFilenames = append(caCertFilenames, expanded) } o.CACertFilename = caCertFilenames - return nil } func (o tlsOptions) tlsClientConfig() tlsclientconfig.Config {