Skip to content

Commit a88f19f

Browse files
authored
Expose SecurityLevel on server-side (#8943)
1 parent c61b4af commit a88f19f

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

api/src/main/java/io/grpc/ServerCall.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,21 @@ public void setCompression(String compressor) {
211211
// noop
212212
}
213213

214+
/**
215+
* Returns the level of security guarantee in communications
216+
*
217+
* <p>Determining the level of security offered by the transport for RPCs on server-side.
218+
* This can be approximated by looking for the SSLSession, but that doesn't work for ALTS and
219+
* maybe some future TLS approaches. May return a lower security level when it cannot be
220+
* determined precisely.
221+
*
222+
* @return non-{@code null} SecurityLevel enum
223+
*/
224+
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/4692")
225+
public SecurityLevel getSecurityLevel() {
226+
return SecurityLevel.NONE;
227+
}
228+
214229
/**
215230
* Returns properties of a single call.
216231
*

core/src/main/java/io/grpc/internal/ServerCallImpl.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.google.common.base.Preconditions.checkArgument;
2020
import static com.google.common.base.Preconditions.checkNotNull;
2121
import static com.google.common.base.Preconditions.checkState;
22+
import static io.grpc.internal.GrpcAttributes.ATTR_SECURITY_LEVEL;
2223
import static io.grpc.internal.GrpcUtil.ACCEPT_ENCODING_SPLITTER;
2324
import static io.grpc.internal.GrpcUtil.CONTENT_LENGTH_KEY;
2425
import static io.grpc.internal.GrpcUtil.MESSAGE_ACCEPT_ENCODING_KEY;
@@ -36,6 +37,7 @@
3637
import io.grpc.InternalDecompressorRegistry;
3738
import io.grpc.Metadata;
3839
import io.grpc.MethodDescriptor;
40+
import io.grpc.SecurityLevel;
3941
import io.grpc.ServerCall;
4042
import io.grpc.Status;
4143
import io.perfmark.PerfMark;
@@ -250,6 +252,16 @@ public MethodDescriptor<ReqT, RespT> getMethodDescriptor() {
250252
return method;
251253
}
252254

255+
@Override
256+
public SecurityLevel getSecurityLevel() {
257+
final Attributes attributes = getAttributes();
258+
if (attributes == null) {
259+
return super.getSecurityLevel();
260+
}
261+
final SecurityLevel securityLevel = attributes.get(ATTR_SECURITY_LEVEL);
262+
return securityLevel == null ? super.getSecurityLevel() : securityLevel;
263+
}
264+
253265
/**
254266
* Close the {@link ServerStream} because an internal error occurred. Allow the application to
255267
* run until completion, but silently ignore interactions with the {@link ServerStream} from now

core/src/test/java/io/grpc/internal/ServerCallImplTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import static org.mockito.Mockito.when;
3434

3535
import com.google.common.io.CharStreams;
36+
import io.grpc.Attributes;
3637
import io.grpc.CompressorRegistry;
3738
import io.grpc.Context;
3839
import io.grpc.DecompressorRegistry;
@@ -41,6 +42,7 @@
4142
import io.grpc.MethodDescriptor;
4243
import io.grpc.MethodDescriptor.Marshaller;
4344
import io.grpc.MethodDescriptor.MethodType;
45+
import io.grpc.SecurityLevel;
4446
import io.grpc.ServerCall;
4547
import io.grpc.Status;
4648
import io.grpc.internal.ServerCallImpl.ServerStreamListenerImpl;
@@ -352,6 +354,23 @@ public void getNullAuthority() {
352354
verify(stream).getAuthority();
353355
}
354356

357+
@Test
358+
public void getSecurityLevel() {
359+
Attributes attributes = Attributes.newBuilder()
360+
.set(GrpcAttributes.ATTR_SECURITY_LEVEL, SecurityLevel.INTEGRITY).build();
361+
when(stream.getAttributes()).thenReturn(attributes);
362+
assertEquals(SecurityLevel.INTEGRITY, call.getSecurityLevel());
363+
verify(stream).getAttributes();
364+
}
365+
366+
@Test
367+
public void getNullSecurityLevel() {
368+
when(stream.getAttributes()).thenReturn(null);
369+
assertEquals(SecurityLevel.NONE, call.getSecurityLevel());
370+
verify(stream).getAttributes();
371+
}
372+
373+
355374
@Test
356375
public void setMessageCompression() {
357376
call.setMessageCompression(true);

0 commit comments

Comments
 (0)