Skip to content

Commit b1bc0a9

Browse files
committed
alts: Add ClientCall support to AltsContextUtil
This adds a createFrom(Attributes) to mirror the check(Attributes) added in ba8ab79. It also adds conveniences for ClientCall for both createFrom() and check(). This allows getting peer information from ClientCall and CallCredentials.RequestInfo, as was already available from ServerCall. The tests were reworked to test the Attribute-based methods and then only basic tests for client/server. Fixes #11042
1 parent 04f1cc5 commit b1bc0a9

File tree

2 files changed

+95
-19
lines changed

2 files changed

+95
-19
lines changed

alts/src/main/java/io/grpc/alts/AltsContextUtil.java

+35-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package io.grpc.alts;
1818

1919
import io.grpc.Attributes;
20+
import io.grpc.ClientCall;
2021
import io.grpc.ExperimentalApi;
2122
import io.grpc.ServerCall;
2223
import io.grpc.alts.internal.AltsInternalContext;
@@ -29,14 +30,36 @@ public final class AltsContextUtil {
2930
private AltsContextUtil() {}
3031

3132
/**
32-
* Creates a {@link AltsContext} from ALTS context information in the {@link ServerCall}.
33+
* Creates an {@link AltsContext} from ALTS context information in the {@link ServerCall}.
3334
*
3435
* @param call the {@link ServerCall} containing the ALTS information
3536
* @return the created {@link AltsContext}
3637
* @throws IllegalArgumentException if the {@link ServerCall} has no ALTS information.
3738
*/
3839
public static AltsContext createFrom(ServerCall<?, ?> call) {
39-
Object authContext = call.getAttributes().get(AltsProtocolNegotiator.AUTH_CONTEXT_KEY);
40+
return createFrom(call.getAttributes());
41+
}
42+
43+
/**
44+
* Creates an {@link AltsContext} from ALTS context information in the {@link ClientCall}.
45+
*
46+
* @param call the {@link ClientCall} containing the ALTS information
47+
* @return the created {@link AltsContext}
48+
* @throws IllegalArgumentException if the {@link ClientCall} has no ALTS information.
49+
*/
50+
public static AltsContext createFrom(ClientCall<?, ?> call) {
51+
return createFrom(call.getAttributes());
52+
}
53+
54+
/**
55+
* Creates an {@link AltsContext} from ALTS context information in the {@link Attributes}.
56+
*
57+
* @param attributes the {@link Attributes} containing the ALTS information
58+
* @return the created {@link AltsContext}
59+
* @throws IllegalArgumentException if the {@link Attributes} has no ALTS information.
60+
*/
61+
public static AltsContext createFrom(Attributes attributes) {
62+
Object authContext = attributes.get(AltsProtocolNegotiator.AUTH_CONTEXT_KEY);
4063
if (!(authContext instanceof AltsInternalContext)) {
4164
throw new IllegalArgumentException("No ALTS context information found");
4265
}
@@ -53,6 +76,16 @@ public static boolean check(ServerCall<?, ?> call) {
5376
return check(call.getAttributes());
5477
}
5578

79+
/**
80+
* Checks if the {@link ClientCall} contains ALTS information.
81+
*
82+
* @param call the {@link ClientCall} to check
83+
* @return true, if the {@link ClientCall} contains ALTS information and false otherwise.
84+
*/
85+
public static boolean check(ClientCall<?, ?> call) {
86+
return check(call.getAttributes());
87+
}
88+
5689
/**
5790
* Checks if the {@link Attributes} contains ALTS information.
5891
*

alts/src/test/java/io/grpc/alts/AltsContextUtilTest.java

+60-17
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import static org.mockito.Mockito.when;
2525

2626
import io.grpc.Attributes;
27+
import io.grpc.ClientCall;
2728
import io.grpc.ServerCall;
2829
import io.grpc.alts.AltsContext.SecurityLevel;
2930
import io.grpc.alts.internal.AltsInternalContext;
@@ -37,27 +38,38 @@
3738
/** Unit tests for {@link AltsContextUtil}. */
3839
@RunWith(JUnit4.class)
3940
public class AltsContextUtilTest {
40-
41-
private final ServerCall<?,?> call = mock(ServerCall.class);
42-
4341
@Test
4442
public void check_noAttributeValue() {
45-
when(call.getAttributes()).thenReturn(Attributes.newBuilder().build());
43+
assertFalse(AltsContextUtil.check(Attributes.newBuilder().build()));
44+
}
4645

47-
assertFalse(AltsContextUtil.check(call));
46+
@Test
47+
public void check_unexpectedAttributeValueType() {
48+
assertFalse(AltsContextUtil.check(Attributes.newBuilder()
49+
.set(AltsProtocolNegotiator.AUTH_CONTEXT_KEY, new Object())
50+
.build()));
4851
}
4952

5053
@Test
51-
public void contains_unexpectedAttributeValueType() {
54+
public void check_altsInternalContext() {
55+
assertTrue(AltsContextUtil.check(Attributes.newBuilder()
56+
.set(AltsProtocolNegotiator.AUTH_CONTEXT_KEY, AltsInternalContext.getDefaultInstance())
57+
.build()));
58+
}
59+
60+
@Test
61+
public void checkServer_altsInternalContext() {
62+
ServerCall<?,?> call = mock(ServerCall.class);
5263
when(call.getAttributes()).thenReturn(Attributes.newBuilder()
53-
.set(AltsProtocolNegotiator.AUTH_CONTEXT_KEY, new Object())
64+
.set(AltsProtocolNegotiator.AUTH_CONTEXT_KEY, AltsInternalContext.getDefaultInstance())
5465
.build());
5566

56-
assertFalse(AltsContextUtil.check(call));
67+
assertTrue(AltsContextUtil.check(call));
5768
}
5869

5970
@Test
60-
public void contains_altsInternalContext() {
71+
public void checkClient_altsInternalContext() {
72+
ClientCall<?,?> call = mock(ClientCall.class);
6173
when(call.getAttributes()).thenReturn(Attributes.newBuilder()
6274
.set(AltsProtocolNegotiator.AUTH_CONTEXT_KEY, AltsInternalContext.getDefaultInstance())
6375
.build());
@@ -66,26 +78,57 @@ public void contains_altsInternalContext() {
6678
}
6779

6880
@Test
69-
public void from_altsInternalContext() {
81+
public void createFrom_altsInternalContext() {
7082
HandshakerResult handshakerResult =
7183
HandshakerResult.newBuilder()
7284
.setPeerIdentity(Identity.newBuilder().setServiceAccount("remote@peer"))
7385
.setLocalIdentity(Identity.newBuilder().setServiceAccount("local@peer"))
7486
.build();
75-
when(call.getAttributes()).thenReturn(Attributes.newBuilder()
76-
.set(AltsProtocolNegotiator.AUTH_CONTEXT_KEY, new AltsInternalContext(handshakerResult))
77-
.build());
7887

79-
AltsContext context = AltsContextUtil.createFrom(call);
88+
AltsContext context = AltsContextUtil.createFrom(Attributes.newBuilder()
89+
.set(AltsProtocolNegotiator.AUTH_CONTEXT_KEY, new AltsInternalContext(handshakerResult))
90+
.build());
8091
assertEquals("remote@peer", context.getPeerServiceAccount());
8192
assertEquals("local@peer", context.getLocalServiceAccount());
8293
assertEquals(SecurityLevel.INTEGRITY_AND_PRIVACY, context.getSecurityLevel());
8394
}
8495

8596
@Test(expected = IllegalArgumentException.class)
86-
public void from_noAttributeValue() {
87-
when(call.getAttributes()).thenReturn(Attributes.newBuilder().build());
97+
public void createFrom_noAttributeValue() {
98+
AltsContextUtil.createFrom(Attributes.newBuilder().build());
99+
}
88100

89-
AltsContextUtil.createFrom(call);
101+
@Test
102+
public void createFromServer_altsInternalContext() {
103+
HandshakerResult handshakerResult =
104+
HandshakerResult.newBuilder()
105+
.setPeerIdentity(Identity.newBuilder().setServiceAccount("remote@peer"))
106+
.setLocalIdentity(Identity.newBuilder().setServiceAccount("local@peer"))
107+
.build();
108+
109+
ServerCall<?,?> call = mock(ServerCall.class);
110+
when(call.getAttributes()).thenReturn(Attributes.newBuilder()
111+
.set(AltsProtocolNegotiator.AUTH_CONTEXT_KEY, new AltsInternalContext(handshakerResult))
112+
.build());
113+
114+
AltsContext context = AltsContextUtil.createFrom(call);
115+
assertEquals("remote@peer", context.getPeerServiceAccount());
116+
}
117+
118+
@Test
119+
public void createFromClient_altsInternalContext() {
120+
HandshakerResult handshakerResult =
121+
HandshakerResult.newBuilder()
122+
.setPeerIdentity(Identity.newBuilder().setServiceAccount("remote@peer"))
123+
.setLocalIdentity(Identity.newBuilder().setServiceAccount("local@peer"))
124+
.build();
125+
126+
ClientCall<?,?> call = mock(ClientCall.class);
127+
when(call.getAttributes()).thenReturn(Attributes.newBuilder()
128+
.set(AltsProtocolNegotiator.AUTH_CONTEXT_KEY, new AltsInternalContext(handshakerResult))
129+
.build());
130+
131+
AltsContext context = AltsContextUtil.createFrom(call);
132+
assertEquals("remote@peer", context.getPeerServiceAccount());
90133
}
91134
}

0 commit comments

Comments
 (0)