Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lock dedicated file instead of token cache file #1146

Merged
merged 4 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion .github/workflows/go.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ on:
paths:
- .github/workflows/go.yaml
- pkg/**
- integration_test/**
- mocks/**
- tools/**
- go.*
tags:
Expand All @@ -17,6 +19,8 @@ on:
paths:
- .github/workflows/go.yaml
- pkg/**
- integration_test/**
- mocks/**
- tools/**
- go.*

Expand All @@ -37,7 +41,25 @@ jobs:
with:
go-version-file: go.mod
cache-dependency-path: go.sum
- run: go test -v -race ./...
- run: go test -v -race ./pkg/...

integration-test:
strategy:
fail-fast: false
matrix:
os:
- ubuntu-latest
- macos-latest
- windows-latest
runs-on: ${{ matrix.os }}
timeout-minutes: 10
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version-file: go.mod
cache-dependency-path: go.sum
- run: go test -v -race ./integration_test/...

generate:
runs-on: ubuntu-latest
Expand Down
20 changes: 11 additions & 9 deletions pkg/tokencache/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,22 @@ func (r *Repository) Save(dir string, key tokencache.Key, tokenSet oidc.TokenSet
return nil
}

func (r *Repository) Lock(dir string, key tokencache.Key) (io.Closer, error) {
if err := os.MkdirAll(dir, 0700); err != nil {
return nil, fmt.Errorf("could not create directory %s: %w", dir, err)
func (r *Repository) Lock(tokenCacheDir string, key tokencache.Key) (io.Closer, error) {
if err := os.MkdirAll(tokenCacheDir, 0700); err != nil {
return nil, fmt.Errorf("could not create directory %s: %w", tokenCacheDir, err)
}
filename, err := computeFilename(key)
keyDigest, err := computeFilename(key)
if err != nil {
return nil, fmt.Errorf("could not compute the key: %w", err)
}
p := filepath.Join(dir, filename)
f := flock.New(p)
if err := f.Lock(); err != nil {
return nil, fmt.Errorf("could not lock the cache file %s: %w", p, err)
// Do not lock the token cache file.
// https://github.com/int128/kubelogin/issues/1144
lockFilepath := filepath.Join(tokenCacheDir, keyDigest+".lock")
lockFile := flock.New(lockFilepath)
if err := lockFile.Lock(); err != nil {
return nil, fmt.Errorf("could not lock the cache file %s: %w", lockFilepath, err)
}
return f, nil
return lockFile, nil
}

func computeFilename(key tokencache.Key) (string, error) {
Expand Down
Loading