Skip to content

Commit ff90561

Browse files
authored
Use the request context for Kubernetes API call (#104) (#105)
* Use the request context for kubernetes API call * Upgrade go version
1 parent 006df47 commit ff90561

File tree

4 files changed

+40
-8
lines changed

4 files changed

+40
-8
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ version: 2
22
jobs:
33
build:
44
docker:
5-
- image: circleci/golang:1.12
5+
- image: docker.mirror.hashicorp.services/circleci/golang:1.15
66
working_directory: /go/src/github.com/hashicorp/vault-plugin-auth-kubernetes
77
steps:
88
- checkout

path_login.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func (b *kubeAuthBackend) pathLogin(ctx context.Context, req *logical.Request, d
103103
}
104104

105105
// look up the JWT token in the kubernetes API
106-
err = serviceAccount.lookup(jwtStr, b.reviewFactory(config))
106+
err = serviceAccount.lookup(ctx, jwtStr, b.reviewFactory(config))
107107
if err != nil {
108108
b.Logger().Error(`login unauthorized due to: ` + err.Error())
109109
return nil, logical.ErrPermissionDenied
@@ -350,8 +350,8 @@ type projectedServiceAccountPod struct {
350350

351351
// lookup calls the TokenReview API in kubernetes to verify the token and secret
352352
// still exist.
353-
func (s *serviceAccount) lookup(jwtStr string, tr tokenReviewer) error {
354-
r, err := tr.Review(jwtStr, s.Audience)
353+
func (s *serviceAccount) lookup(ctx context.Context, jwtStr string, tr tokenReviewer) error {
354+
r, err := tr.Review(ctx, jwtStr, s.Audience)
355355
if err != nil {
356356
return err
357357
}

path_login_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,33 @@ func TestLogin(t *testing.T) {
263263
}
264264
}
265265

266+
func TestLogin_ContextError(t *testing.T) {
267+
b, storage := setupBackend(t, testDefaultPEMs, testName, testNamespace)
268+
269+
data := map[string]interface{}{
270+
"role": "plugin-test",
271+
"jwt": jwtData,
272+
}
273+
274+
req := &logical.Request{
275+
Operation: logical.UpdateOperation,
276+
Path: "login",
277+
Storage: storage,
278+
Data: data,
279+
Connection: &logical.Connection{
280+
RemoteAddr: "127.0.0.1",
281+
},
282+
}
283+
284+
ctx, cancel := context.WithCancel(context.Background())
285+
cancel()
286+
287+
_, err := b.HandleRequest(ctx, req)
288+
if err != context.Canceled {
289+
t.Fatalf("expected context canceled error, got: %v", err)
290+
}
291+
}
292+
266293
func TestLogin_ECDSA_PEM(t *testing.T) {
267294
b, storage := setupBackend(t, testNoPEMs, testName, testNamespace)
268295

token_review.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package kubeauth
22

33
import (
44
"bytes"
5+
"context"
56
"crypto/tls"
67
"crypto/x509"
78
"encoding/json"
@@ -28,7 +29,7 @@ type tokenReviewResult struct {
2829

2930
// This exists so we can use a mock TokenReview when running tests
3031
type tokenReviewer interface {
31-
Review(string, []string) (*tokenReviewResult, error)
32+
Review(context.Context, string, []string) (*tokenReviewResult, error)
3233
}
3334

3435
type tokenReviewFactory func(*kubeConfig) tokenReviewer
@@ -44,7 +45,7 @@ func tokenReviewAPIFactory(config *kubeConfig) tokenReviewer {
4445
}
4546
}
4647

47-
func (t *tokenReviewAPI) Review(jwt string, aud []string) (*tokenReviewResult, error) {
48+
func (t *tokenReviewAPI) Review(ctx context.Context, jwt string, aud []string) (*tokenReviewResult, error) {
4849

4950
client := cleanhttp.DefaultClient()
5051

@@ -75,7 +76,7 @@ func (t *tokenReviewAPI) Review(jwt string, aud []string) (*tokenReviewResult, e
7576

7677
// Build the request to the token review API
7778
url := fmt.Sprintf("%s/apis/authentication.k8s.io/v1/tokenreviews", strings.TrimSuffix(t.config.Host, "/"))
78-
req, err := http.NewRequest("POST", url, bytes.NewBuffer(trJSON))
79+
req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(trJSON))
7980
if err != nil {
8081
return nil, err
8182
}
@@ -188,7 +189,11 @@ func mockTokenReviewFactory(name, namespace, UID string) tokenReviewFactory {
188189
}
189190
}
190191

191-
func (t *mockTokenReview) Review(jwt string, aud []string) (*tokenReviewResult, error) {
192+
func (t *mockTokenReview) Review(ctx context.Context, cjwt string, aud []string) (*tokenReviewResult, error) {
193+
if ctx.Err() != nil {
194+
return nil, ctx.Err()
195+
}
196+
192197
return &tokenReviewResult{
193198
Name: t.saName,
194199
Namespace: t.saNamespace,

0 commit comments

Comments
 (0)