1 /* 2 * Copyright 2018 The gRPC Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package io.grpc.alts.internal; 18 19 import com.google.common.annotations.VisibleForTesting; 20 import io.grpc.alts.internal.Altscontext.AltsContext; 21 import io.grpc.alts.internal.Handshaker.HandshakerResult; 22 import io.grpc.alts.internal.TransportSecurityCommon.RpcProtocolVersions; 23 import io.grpc.alts.internal.TransportSecurityCommon.SecurityLevel; 24 25 /** AltsAuthContext contains security-related context information about an ALTs connection. */ 26 public final class AltsAuthContext { 27 final AltsContext context; 28 29 /** Create a new AltsAuthContext. */ 30 public AltsAuthContext(HandshakerResult result) { 31 context = 32 AltsContext.newBuilder() 33 .setApplicationProtocol(result.getApplicationProtocol()) 34 .setRecordProtocol(result.getRecordProtocol()) 35 // TODO: Set security level based on the handshaker result. 36 .setSecurityLevel(SecurityLevel.INTEGRITY_AND_PRIVACY) 37 .setPeerServiceAccount(result.getPeerIdentity().getServiceAccount()) 38 .setLocalServiceAccount(result.getLocalIdentity().getServiceAccount()) 39 .setPeerRpcVersions(result.getPeerRpcVersions()) 40 .build(); 41 } 42 43 @VisibleForTesting 44 public static AltsAuthContext getDefaultInstance() { 45 return new AltsAuthContext(HandshakerResult.newBuilder().build()); 46 } 47 48 /** 49 * Get application protocol. 50 * 51 * @return the context's application protocol. 52 */ 53 public String getApplicationProtocol() { 54 return context.getApplicationProtocol(); 55 } 56 57 /** 58 * Get negotiated record protocol. 59 * 60 * @return the context's negotiated record protocol. 61 */ 62 public String getRecordProtocol() { 63 return context.getRecordProtocol(); 64 } 65 66 /** 67 * Get security level. 68 * 69 * @return the context's security level. 70 */ 71 public SecurityLevel getSecurityLevel() { 72 return context.getSecurityLevel(); 73 } 74 75 /** 76 * Get peer service account. 77 * 78 * @return the context's peer service account. 79 */ 80 public String getPeerServiceAccount() { 81 return context.getPeerServiceAccount(); 82 } 83 84 /** 85 * Get local service account. 86 * 87 * @return the context's local service account. 88 */ 89 public String getLocalServiceAccount() { 90 return context.getLocalServiceAccount(); 91 } 92 93 /** 94 * Get peer RPC versions. 95 * 96 * @return the context's peer RPC versions. 97 */ 98 public RpcProtocolVersions getPeerRpcVersions() { 99 return context.getPeerRpcVersions(); 100 } 101 } 102