Skip to content
This repository has been archived by the owner on Mar 5, 2024. It is now read-only.

Commit

Permalink
Treat ErrPolicyForbidden and ErrPodNotFound as permanent errors durin…
Browse files Browse the repository at this point in the history
…g retry backoff (#250)

* Treat forbidden policies as permanent errors

* Translate from gRPC to internal errors in gateway

fetchCredentials shouldn't need to know that interaction with the server
is happening over gRPC

* Add test for assuming a forbidden role
  • Loading branch information
hoelzro authored and Joseph-Irving committed Jul 3, 2019
1 parent 419eaed commit 02f63bc
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/aws/metadata/handler_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ func (c *credentialsHandler) fetchCredentials(ctx context.Context, ip, requested
op := func() error {
creds, err := c.client.GetCredentials(ctx, ip, requestedRole)
if err != nil {
if err == server.ErrPolicyForbidden {
return backoff.Permanent(err)
}
return err
}
credsCh <- creds
Expand Down
26 changes: 26 additions & 0 deletions pkg/aws/metadata/handler_credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,29 @@ func TestReturnsCredentialsWithRetryAfterError(t *testing.T) {
t.Error("unexpected status", rr.Code)
}
}

func TestForbiddenRole(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
defer leaktest.Check(t)()

r, _ := http.NewRequest("GET", "/latest/meta-data/iam/security-credentials/role", nil)
rr := httptest.NewRecorder()

valid := st.GetCredentialsResult{&sts.Credentials{}, nil}
e := st.GetCredentialsResult{nil, server.ErrPolicyForbidden}
client := st.NewStubClient().WithRoles(st.GetRoleResult{"role", nil}).WithCredentials(e, valid)
handler := newCredentialsHandler(client, getBlankClientIP)
router := mux.NewRouter()
handler.Install(router)

router.ServeHTTP(rr, r.WithContext(ctx))

if rr.Code != http.StatusInternalServerError {
t.Error("unexpected status", rr.Code)
}

if !strings.Contains(rr.Body.String(), "forbidden by policy") {
t.Error("unexpected error", rr.Body.String())
}
}
11 changes: 11 additions & 0 deletions pkg/server/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/balancer/roundrobin"
"google.golang.org/grpc/credentials"

status "google.golang.org/grpc/status"
)

// Client is the Server's client interface
Expand Down Expand Up @@ -123,6 +125,15 @@ func (g *KiamGateway) GetCredentials(ctx context.Context, ip, role string) (*sts
}
credentials, err := g.client.GetPodCredentials(ctx, &pb.GetPodCredentialsRequest{Ip: ip, Role: role})
if err != nil {
if grpcStatus, ok := status.FromError(err); ok {
switch grpcStatus.Message() {
case ErrPolicyForbidden.Error():
return nil, ErrPolicyForbidden
case ErrPodNotFound.Error():
return nil, ErrPodNotFound
}
}

return nil, err
}
return &sts.Credentials{
Expand Down

0 comments on commit 02f63bc

Please sign in to comment.