Skip to content

Commit

Permalink
Fix "~" is not expanded on Windows (#550)
Browse files Browse the repository at this point in the history
* Run tests on macOS and Windows

* Use filepath and client-go/util/homedir package
  • Loading branch information
int128 authored May 4, 2021
1 parent ea78452 commit d0364f0
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 51 deletions.
31 changes: 29 additions & 2 deletions .github/workflows/go.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
14 changes: 3 additions & 11 deletions pkg/cmd/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion pkg/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"context"
"path/filepath"
"runtime"

"github.com/google/wire"
Expand All @@ -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

Expand Down
13 changes: 7 additions & 6 deletions pkg/cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"context"
"os"
"path/filepath"
"testing"
"time"

Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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")},
},
},
},
Expand Down
27 changes: 3 additions & 24 deletions pkg/cmd/get_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package cmd
import (
"errors"
"fmt"
"os"
"strings"

"github.com/int128/kubelogin/pkg/infrastructure/logger"
"github.com/int128/kubelogin/pkg/oidc"
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
15 changes: 15 additions & 0 deletions pkg/cmd/homedir.go
Original file line number Diff line number Diff line change
@@ -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, "~"))
}
9 changes: 2 additions & 7 deletions pkg/cmd/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"crypto/tls"
"fmt"

"github.com/int128/kubelogin/pkg/tlsclientconfig"
"github.com/spf13/pflag"
Expand All @@ -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 {
Expand Down

0 comments on commit d0364f0

Please sign in to comment.