-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
credentials: local creds implementation #3517
Merged
Merged
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f15d4ae
Fix conflicts with upstream master
yihuazhang 9a865e9
add end2end test
yihuazhang 9482a13
fix a test failure
yihuazhang b81b36c
address doug's 2nd round comments
yihuazhang 876fdb9
address doug's 3rd round of comments
yihuazhang e417e60
fix travis error
yihuazhang 11d7567
2nd attempt to fix travis error
yihuazhang 3533a21
Address doug's 4th round of comments
yihuazhang 2f261a3
give a more meaningfule name to functions
yihuazhang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ import ( | |
"google.golang.org/grpc/credentials/local" | ||
"google.golang.org/grpc/peer" | ||
"google.golang.org/grpc/status" | ||
|
||
testpb "google.golang.org/grpc/test/grpc_testing" | ||
) | ||
|
||
|
@@ -48,11 +49,11 @@ func testE2ESucceed(network, address string) error { | |
switch network { | ||
case "unix": | ||
if secLevel != credentials.PrivacyAndIntegrity { | ||
return nil, status.Errorf(codes.Unauthenticated, "Wrong security level: got %q, want %q", secLevel.String(), credentials.PrivacyAndIntegrity.String()) | ||
return nil, status.Errorf(codes.Unauthenticated, fmt.Sprintf("Wrong security level: got %q, want %q", secLevel, credentials.PrivacyAndIntegrity)) | ||
} | ||
case "tcp": | ||
if secLevel != credentials.NoSecurity { | ||
return nil, status.Errorf(codes.Unauthenticated, "Wrong security level: got %q, want %q", secLevel.String(), credentials.NoSecurity.String()) | ||
return nil, status.Errorf(codes.Unauthenticated, fmt.Sprintf("Wrong security level: got %q, want %q", secLevel, credentials.NoSecurity)) | ||
} | ||
} | ||
return &testpb.Empty{}, nil | ||
|
@@ -65,6 +66,7 @@ func testE2ESucceed(network, address string) error { | |
|
||
testpb.RegisterTestServiceServer(s, ss) | ||
|
||
var err error | ||
lis, err := net.Listen(network, address) | ||
if err != nil { | ||
return fmt.Errorf("Failed to create listener: %v", err) | ||
|
@@ -73,27 +75,34 @@ func testE2ESucceed(network, address string) error { | |
go s.Serve(lis) | ||
|
||
var cc *grpc.ClientConn | ||
lisAddr := address | ||
|
||
switch network { | ||
case "unix": | ||
cc, err = grpc.Dial(address, grpc.WithTransportCredentials(local.NewCredentials()), grpc.WithContextDialer( | ||
cc, err = grpc.Dial(lisAddr, grpc.WithTransportCredentials(local.NewCredentials()), grpc.WithContextDialer( | ||
func(ctx context.Context, addr string) (net.Conn, error) { | ||
return net.Dial("unix", address) | ||
return net.Dial("unix", addr) | ||
})) | ||
case "tcp": | ||
cc, err = grpc.Dial(lis.Addr().String(), grpc.WithTransportCredentials(local.NewCredentials())) | ||
_, port, err := net.SplitHostPort(lis.Addr().String()) | ||
if err != nil { | ||
return fmt.Errorf("Failed to parse listener address: %v", err) | ||
} | ||
lisAddr = "localhost:" + port | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you not do: |
||
cc, err = grpc.Dial(lisAddr, grpc.WithTransportCredentials(local.NewCredentials())) | ||
default: | ||
return fmt.Errorf("unsupported network %q", network) | ||
} | ||
if err != nil { | ||
return fmt.Errorf("Failed to dial server: %v, %v", err, lis.Addr().String()) | ||
return fmt.Errorf("Failed to dial server: %v, %v", err, lisAddr) | ||
} | ||
defer cc.Close() | ||
|
||
c := testpb.NewTestServiceClient(cc) | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Second) | ||
defer cancel() | ||
|
||
if _, err := c.EmptyCall(ctx, &testpb.Empty{}); err != nil { | ||
if _, err = c.EmptyCall(ctx, &testpb.Empty{}); err != nil { | ||
return fmt.Errorf("EmptyCall(_, _) = _, %v; want _, <nil>", err) | ||
} | ||
return nil | ||
|
@@ -193,10 +202,7 @@ func testE2EFail(dopts []grpc.DialOption) error { | |
} | ||
|
||
func isExpected(got, want error) bool { | ||
if status.Code(got) == status.Code(want) && strings.Contains(status.Convert(got).Message(), status.Convert(want).Message()) { | ||
return true | ||
} | ||
return false | ||
return status.Code(got) == status.Code(want) && strings.Contains(status.Convert(got).Message(), status.Convert(want).Message()) | ||
} | ||
|
||
func (s) TestClientFail(t *testing.T) { | ||
|
@@ -211,7 +217,7 @@ func (s) TestClientFail(t *testing.T) { | |
func (s) TestServerFail(t *testing.T) { | ||
// Use insecure at client-side which should lead to server-side failure. | ||
opts := []grpc.DialOption{grpc.WithInsecure()} | ||
want := status.Error(codes.Unavailable, "connection closed") | ||
want := status.Error(codes.Unavailable, "") | ||
if err := testE2EFail(opts); !isExpected(err, want) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Simplify: if err := testE2EFail(opts); status.Code(err) != codes.Unavailable { |
||
t.Fatalf("testE2EFail() = %v; want %v", err, want) | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
status.Errorf
usesfmt.Sprintf
already. Remove this call here.