{"id":329555,"date":"2024-05-28T20:43:55","date_gmt":"2024-05-28T20:43:55","guid":{"rendered":"https:\/\/www.baeldung.com\/?p=182078"},"modified":"2024-05-28T20:43:55","modified_gmt":"2024-05-28T20:43:55","slug":"grpc-authentication-in-java-using-application-layer-transport-security-alts","status":"publish","type":"post","link":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/2024\/05\/28\/grpc-authentication-in-java-using-application-layer-transport-security-alts\/","title":{"rendered":"gRPC Authentication in Java Using Application Layer Transport Security (ALTS)"},"content":{"rendered":"<p class=\"syndicated-attribution\"><meta name= \\\"keywords \\\" content= \\\"\u96fb\u5b50\u8a08\u7b97\u6a5f, \u6559\u80b2, IT \u96fb\u8166\u73ed,\u96fb\u8166\u88dc\u7fd2\uff0c \u96fb\u8166\u73ed\uff0c \u5bb6\u6559\uff0c \u79c1\u4eba\u8001\u5e2b\uff0c \u8cc7\u8a0a\u6280\u8853\uff0c \u7a0b\u5e8f\u8a2d\u8a08\uff0c \u96fb\u5b50\u8a08\u7b97\u6a5f\uff0c \u904a\u6232\uff0c \u860b\u679c\uff0c \u96fb\u5f71\uff0c \u8a08\u7b97\u6a5f\uff0c\u7de8\u78bc\uff0c Java\uff0c C\/C++\uff0c JavaScript\uff0c PHP\uff0c HTML\uff0c CSS\uff0c MySQL\uff0c mobile\uff0c Android\uff0c \u52d5\u6f2b\uff0c Python\uff0c teacher\uff0c \u88dc\u7fd2\uff0c \u96fb\u8166\u88dc\u7fd2 \u8cc7\u8a0a, \u7535\u5b50\u8ba1\u7b97\u673a, IT ,Game, apple, movie, Computer,student,Java,\u6559\u80b2, ,\u5b66\u751f, \u5b66\u4e60, learn, \u6559\u5b66,  Android, apple,anime, animation, \u4fe1\u606f\u6280\u672f, \u7a0b\u5e8f\u8bbe\u8ba1, \u79fb\u52a8\u7535\u8bdd, \u8cc7\u8a0a\u79d1\u6280,Game, Jeu, Juego,Call Of Duty ,\u4f7f\u547d\u53ec\u559a , \u6e38\u620f, \u7535\u5b50\u6e38\u620f,, \u591a\u4eba\u7535\u5b50\u6e38\u620f, \u7f51\u7edc\u6e38\u620f\uff0conline\uff0conline game, \u624b\u673a\u6e38\u620f, mobile \\\"><\/p>\n<p><img src=\"https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/admin-ajax-2.png\" class=\"webfeedsFeaturedVisual wp-post-image\" alt=\"start here featured\" style=\"float: left; margin-right: 5px;\" decoding=\"async\" srcset=\"https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/admin-ajax-2.png 600w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/admin-ajax-2-300x157.png 300w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/admin-ajax-2-100x52.png 100w\" sizes=\"(max-width: 580px) 100vw, 580px\" \/><\/p>\n<h2 id=\"bd--overview\" data-id=\"-overview\">1.\u00a0 Overview<\/h2>\n<div class=\"bd-anchor\" id=\"-overview\"><\/div>\n<p>In this tutorial, we&#8217;ll explore the role of <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/cloud.google.com\/docs\/security\/encryption-in-transit\/application-layer-transport-security\">ALTS (Application Layer Transport Security)<\/a> in <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/grpc-introduction\">gRPC<\/a> applications. As we know, ensuring authentication and data security is difficult but essential in a distributed architecture.<\/p>\n<p>ALTS is a custom built-in mutual authentication and transport encryption solution from Google that is available exclusively in Google&#8217;s cloud infrastructure. ALTS simplifies authentication and data encryption between gRPC services and can be enabled with minimal code changes. Hence, it&#8217;s popular among developers as they can focus more on writing business logic.<\/p>\n<h2 id=\"bd-key-differences-between-alts-and-tls\" data-id=\"key-differences-between-alts-and-tls\">2. Key Differences Between ALTS and TLS<\/h2>\n<div class=\"bd-anchor\" id=\"key-differences-between-alts-and-tls\"><\/div>\n<p><strong>ALTS is similar to <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/cs\/ssl-vs-tls\">TLS<\/a> but has a different trust model optimized for Google&#8217;s infrastructure.<\/strong> Let&#8217;s quickly take a look at the key differences between them:<\/p>\n<table class=\"table-styled\" style=\"width: 98.3909%\">\n<thead>\n<tr>\n<th style=\"width: 23.3513%\">Features<\/th>\n<th style=\"width: 36.7415%\">ALTS<\/th>\n<th style=\"width: 39.9072%\">TLS<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"width: 23.3513%\">Trust Model<\/td>\n<td style=\"width: 36.7415%\">Identity-based relying on GCP IAM Service Accounts<\/td>\n<td style=\"width: 39.9072%\">Certificate-based, requires certificate management, including renewal and revocation<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 23.3513%\">Design<\/td>\n<td style=\"width: 36.7415%\">Simpler<\/td>\n<td style=\"width: 39.9072%\">Complex<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 23.3513%\">Usage Context<\/td>\n<td style=\"width: 36.7415%\">Used for securing gRPC services running on Google data centers<\/td>\n<td style=\"width: 39.9072%\">Used for securing Web browsing (HTTPS), email, instant messaging, VoIP, etc.<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 23.3513%\">Message Serialization<\/td>\n<td style=\"width: 36.7415%\">Uses Protocol Buffers<\/td>\n<td style=\"width: 39.9072%\">Uses X.509 certificates encoded with ASN.1<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 23.3513%\">Performance<\/td>\n<td style=\"width: 36.7415%\">Designed for general use<\/td>\n<td style=\"width: 39.9072%\">Optimized for low-latency, high-throughput communications in Google\u2019s data centers<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 id=\"bd-sample-application-using-alts\" data-id=\"sample-application-using-alts\">3. Sample Application Using ALTS<\/h2>\n<div class=\"bd-anchor\" id=\"sample-application-using-alts\"><\/div>\n<p><strong>The ALTS feature is enabled by default on the Google Cloud Platform (GCP). It uses <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/cloud.google.com\/iam\/docs\/service-account-overview\">GCP service accounts<\/a> to secure RPC calls between gRPC services.<\/strong> Specifically, it runs on <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/cloud.google.com\/products\/compute\">Google Compute Engine<\/a> or <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/cloud.google.com\/kubernetes-engine\">Kubernetes Engine (GKE)<\/a> within Google&#8217;s infrastructure.<\/p>\n<p>Let&#8217;s assume there&#8217;s an Operation Theater (OT) booking system in a hospital that consists of a front-end and a backend service:<\/p>\n<p><a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/gcp-grpc.svg\"><img decoding=\"async\" class=\"alignnone size-full wp-image-182079\" src=\"https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/gcp-grpc.svg\" alt=\"\" \/><\/a><\/p>\n<p>The OT Booking system comprises two services running in the Google Cloud Platform (GCP). A front-end service makes remote procedure calls to the backend service. We&#8217;ll develop the services using the gRPC framework. Considering the sensitive nature of the data, we&#8217;ll utilize the built-in ALTS feature in GCP to enable authentication and encryption for the transit data.<\/p>\n<p>First, let&#8217;s define the <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/google-protocol-buffer\">protobuf<\/a> <em>ot_booking.proto<\/em> file:<\/p>\n<pre><code class=\"language-json\">syntax = &quot;proto3&quot;;\r\npackage otbooking;\r\noption java_multiple_files = true;\r\noption java_package = &quot;com.baeldung.grpc.alts.otbooking&quot;;\r\nservice OtBookingService {\r\n  rpc getBookingInfo(BookingRequest) returns (BookingResponse) {}\r\n}\r\nmessage BookingRequest {\r\n  string patientID = 1;\r\n  string doctorID = 2;\r\n  string description = 3;\r\n}\r\nmessage BookingResponse {\r\n  string bookingDate = 1;\r\n  string condition = 2;\r\n}<\/code><\/pre>\n<p>Basically, we declared a service <em>OtBookingService<\/em> with the RPC <em>getBookingInfo()<\/em>, and two DTOs <em>BookingRequest<\/em> and <em>BookingResponse<\/em> in the protobuf file.<\/p>\n<p>Next, let&#8217;s have a look at the important classes of this application:<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/ot_booking_cld-1.svg\"><img decoding=\"async\" class=\"alignnone size-full wp-image-182080\" src=\"https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/ot_booking_cld-1.svg\" alt=\"\" \/><\/a><\/p>\n<p>The <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/maven\">Maven<\/a> plugin compiles the protobuf file and auto-generates some classes such as <em>OtBookingServiceGrpc<\/em>, <em>OtBookingServiceImplBase<\/em>, <em>BookingRequest,<\/em> and <em>BookingResponse<\/em>. <strong>We&#8217;ll use the gRPC library class <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/javadoc.io\/doc\/io.grpc\/grpc-alts\/latest\/io\/grpc\/alts\/AltsChannelBuilder.html\"><em>AltsChannelBuilder<\/em><\/a> to enable ALTS to create the <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/grpc.github.io\/grpc-java\/javadoc\/io\/grpc\/ManagedChannel.html\"><em>ManagedChannel<\/em><\/a> object on the client side<\/strong>. Finally, we&#8217;ll use <em>OtBookingServiceGrpc<\/em> to generate the <em>OtBookingServiceBlockingStub<\/em> to call the RPC <em>getBookingInfo()<\/em> method running on the server side.<\/p>\n<p><strong>Like <em>AltsChannelBuilder<\/em>,<\/strong> <strong>the <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/javadoc.io\/doc\/io.grpc\/grpc-alts\/latest\/io\/grpc\/alts\/AltsServerBuilder.html\"><em>AltsServerBuilder<\/em><\/a> class helps enable ALTS on the server side. We register the interceptor <em>ClientAuthInterceptor<\/em> to help authenticate the client<\/strong>. Finally, we register the <em>OtBookingService<\/em> to the <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/grpc.github.io\/grpc-java\/javadoc\/io\/grpc\/Server.html\"><em>io.grpc.Server<\/em><\/a> object and then start the service.<\/p>\n<p>Furthermore, we&#8217;ll discuss the implementation in the next section.<\/p>\n<h2 id=\"bd-application-implementation-using-alts\" data-id=\"application-implementation-using-alts\">4. Application Implementation Using ALTS<\/h2>\n<div class=\"bd-anchor\" id=\"application-implementation-using-alts\"><\/div>\n<p>Let&#8217;s implement the classes we discussed earlier. Then, we&#8217;ll demonstrate by running the services on the GCP virtual machines.<\/p>\n<h3 id=\"bd-1-prerequisite\" data-id=\"1-prerequisite\">4.1. Prerequisite<\/h3>\n<div class=\"bd-anchor\" id=\"1-prerequisite\"><\/div>\n<p>Since ALTS is a built-in feature in GCP, we&#8217;ll have to provision a few cloud resources for running the sample application.<\/p>\n<p>First, we&#8217;ll create two IAM service accounts to associate them with the front-end and back-end servers respectively:<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/gcp-service-account.png\"><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone wp-image-182081\" src=\"https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/gcp-service-account-300x128.png\" alt=\"\" width=\"741\" height=\"316\" srcset=\"https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/gcp-service-account-300x128.png 300w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/gcp-service-account-1024x436.png 1024w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/gcp-service-account-768x327.png 768w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/gcp-service-account-1536x654.png 1536w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/gcp-service-account-100x43.png 100w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/gcp-service-account.png 1754w\" sizes=\"(max-width: 741px) 100vw, 741px\" \/><\/a><\/p>\n<p>Then, we&#8217;ll create two virtual machines hosting the front-end and back-end services respectively:<\/p>\n<p><a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/ser-account-vm-e1716467268431.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-182082\" src=\"https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/ser-account-vm-e1716467268431-300x166.png\" alt=\"\" width=\"751\" height=\"416\" srcset=\"https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/ser-account-vm-e1716467268431-300x166.png 300w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/ser-account-vm-e1716467268431-1024x568.png 1024w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/ser-account-vm-e1716467268431-768x426.png 768w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/ser-account-vm-e1716467268431-100x55.png 100w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/ser-account-vm-e1716467268431.png 1130w\" sizes=\"(max-width: 751px) 100vw, 751px\" \/><\/a><\/p>\n<p>The virtual machine <em>prod-booking-client-vm<\/em> is associated with <em>prod-ot-booking-client-svc<\/em> service account. Similarly, <em>prod-booking-service-vm<\/em> is associated with <em>prod-ot-booking-svc<\/em> service account. The service accounts serve as the servers&#8217; identities and ALTS uses them for authorization and encryption.<\/p>\n<h3 id=\"bd-2-implementation\" data-id=\"2-implementation\">4.2. Implementation<\/h3>\n<div class=\"bd-anchor\" id=\"2-implementation\"><\/div>\n<p>Let&#8217;s first start with an entry into the <em>pom.xml<\/em> file to resolve the <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/mvnrepository.com\/artifact\/io.grpc\/grpc-alts\/\">Maven dependency<\/a>:<\/p>\n<pre><code class=\"language-xml\">&lt;dependency&gt;\r\n    &lt;groupId&gt;io.grpc&lt;\/groupId&gt;\r\n    &lt;artifactId&gt;grpc-alts&lt;\/artifactId&gt;\r\n    &lt;version&gt;1.63.0&lt;\/version&gt;\r\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>Then, we&#8217;ll implement the backend, starting with the <em>AltsBookingServer<\/em> class:<\/p>\n<pre><code class=\"language-java\">public class AltsOtBookingServer {\r\n    public static void main(String[] args) throws IOException, InterruptedException {\r\n        final String CLIENT_SERVICE_ACCOUNT = args[0];\r\n        Server server = AltsServerBuilder.forPort(8080)\r\n          .intercept(new ClientAuthInterceptor(CLIENT_SERVICE_ACCOUNT))\r\n          .addService(new OtBookingService())\r\n          .build();\r\n        server.start();\r\n        server.awaitTermination();\r\n    }\r\n}<\/code><\/pre>\n<p>gRPC provides a special class <em>AltsServerBuilder<\/em> for configuring the server in ALTS mode. We&#8217;ve registered the <em>ClientAuthInterceptor<\/em> on the server to intercept all the RPCs before they hit the endpoints in the <em>OtBookingService<\/em> class.<\/p>\n<p>Let&#8217;s take a look at the <em>ClientAuthInterceptor<\/em> class:<\/p>\n<pre><code class=\"language-java\">public class ClientAuthInterceptor implements ServerInterceptor {\r\n    String clientServiceAccount = null;\r\n    public ClientAuthInterceptor(String clientServiceAccount) {\r\n        this.clientServiceAccount = clientServiceAccount;\r\n    }\r\n    @Override\r\n    public &lt;ReqT, RespT&gt; ServerCall.Listener&lt;ReqT&gt; interceptCall(ServerCall&lt;ReqT, RespT&gt; serverCall, Metadata metadata,\r\n        ServerCallHandler&lt;ReqT, RespT&gt; serverCallHandler) {\r\n        Status status = AuthorizationUtil.clientAuthorizationCheck(serverCall,\r\n            Lists.newArrayList(this.clientServiceAccount));\r\n        if (!status.isOk()) {\r\n            serverCall.close(status, new Metadata());\r\n        }\r\n        return serverCallHandler.startCall(serverCall, metadata);\r\n    }\r\n}<\/code><\/pre>\n<p>All the RPCs hit the <em>intercept()<\/em> method in <em>ClientAuthInterceptor<\/em>. Then, we invoke the <em>clientAuthorizationCheck()<\/em> method of the gRPC library class <em>AuthorizationUtil<\/em> to authorize the client service account. Finally, the RPC moves further only when the authorization is successful.<\/p>\n<p>Next, let&#8217;s take a look at the front-end service:<\/p>\n<pre><code class=\"language-java\">public class AltsOtBookingClient {\r\n    public static void main(String[] args) {\r\n        final String SERVER_ADDRESS = args[0];\r\n        final String SERVER_ADDRESS_SERVICE_ACCOUNT = args[1];\r\n        ManagedChannel managedChannel = AltsChannelBuilder.forTarget(SERVER_ADDRESS)\r\n          .addTargetServiceAccount(SERVER_ADDRESS_SERVICE_ACCOUNT)\r\n          .build();\r\n        OtBookingServiceGrpc.OtBookingServiceBlockingStub OTBookingServiceStub = OtBookingServiceGrpc\r\n          .newBlockingStub(managedChannel);\r\n        BookingResponse bookingResponse = OTBookingServiceStub.getBookingInfo(BookingRequest.newBuilder()\r\n          .setPatientID(&quot;PT-1204&quot;)\r\n          .setDoctorID(&quot;DC-3904&quot;)\r\n          .build());\r\n        managedChannel.shutdown();\r\n    }\r\n}<\/code><\/pre>\n<p>Similar to <em>AltsServerBuilder<\/em>, gRPC offers an <em>AltsChannelBuilder<\/em> class for enabling ALTS on the client side. We can call the <em>addTargetServiceAccount()<\/em> method multiple times to add more than one potential target service account. Further, we initiate the RPC by calling the <em>getBookingInfo()<\/em> method on the stub.<\/p>\n<p><strong>The same service account can be associated with multiple virtual machines. Hence, it provides a certain degree of flexibility and agility to scale the services horizontally.<\/strong><\/p>\n<h3 id=\"bd-3-run-on-google-compute-engine\" data-id=\"3-run-on-google-compute-engine\">4.3. Run on Google Compute Engine<\/h3>\n<div class=\"bd-anchor\" id=\"3-run-on-google-compute-engine\"><\/div>\n<p>Let&#8217;s login to both servers and then clone the GitHub repository hosting the source code of the demo gRPC service:<\/p>\n<pre><code class=\"language-shell\">git clone https:\/\/github.com\/eugenp\/tutorials.git<\/code><\/pre>\n<p>After cloning, we&#8217;ll compile the code in the <em>tutorials\/grpc<\/em> directory:<\/p>\n<pre><code class=\"language-shell\">mvn clean compile<\/code><\/pre>\n<p>Post successful compilation, we&#8217;ll start the backend service in <em>prod-booking-service-vm<\/em>:<\/p>\n<pre><code class=\"language-shell\">mvn exec: java -Dexec.mainClass=&quot;com.baeldung.grpc.alts.server.AltsOtBookingServer&quot; \\\r\n-Dexec.arguments=&quot;prod-ot-booking-client-svc@grpc-alts-demo.iam.gserviceaccount.com&quot;<\/code><\/pre>\n<p>We ran the <em>AltsOtBookingServer<\/em> class with the service account of the front-end client as an argument.<\/p>\n<p>Once the service is up and running, we&#8217;ll initiate an RPC from the front-end service running on the virtual machine <em>prod-booking-client-vm<\/em>:<\/p>\n<pre><code class=\"language-shell\">mvn exec:java -Dexec.mainClass=&quot;com.baeldung.grpc.alts.client.AltsOtBookingClient&quot; \\\r\n-Dexec.arguments=&quot;10.128.0.2:8080,prod-ot-booking-svc@grpc-alts-demo.iam.gserviceaccount.com&quot;<\/code><\/pre>\n<p>We ran the <em>AltsOtBookingClient<\/em> class with two arguments. The first argument is the target server where the backend service is running and the second is the service account associated with the backend server.<\/p>\n<p>The command runs successfully and the service returns a response after authenticating the client:<\/p>\n<p><a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/service-demo-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-182086\" src=\"https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/service-demo-1-300x52.png\" alt=\"\" width=\"750\" height=\"130\" srcset=\"https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/service-demo-1-300x52.png 300w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/service-demo-1-1024x179.png 1024w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/service-demo-1-768x134.png 768w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/service-demo-1-1536x268.png 1536w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/service-demo-1-100x17.png 100w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/service-demo-1.png 1879w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/a><\/p>\n<p>Let&#8217;s suppose we disable the client service account:<\/p>\n<p><a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/service-account-disabled.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-182084\" src=\"https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/service-account-disabled-300x117.png\" alt=\"\" width=\"754\" height=\"294\" srcset=\"https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/service-account-disabled-300x117.png 300w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/service-account-disabled-1024x399.png 1024w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/service-account-disabled-768x300.png 768w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/service-account-disabled-1536x599.png 1536w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/service-account-disabled-100x39.png 100w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/service-account-disabled.png 1864w\" sizes=\"(max-width: 754px) 100vw, 754px\" \/><\/a><\/p>\n<p>As a result, the ALTS prevents the RPC from reaching the backend service:<\/p>\n<p><a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/demo-client-serviceacc-disabled.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-182085\" src=\"https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/demo-client-serviceacc-disabled-300x68.png\" alt=\"\" width=\"750\" height=\"170\" srcset=\"https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/demo-client-serviceacc-disabled-300x68.png 300w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/demo-client-serviceacc-disabled-1024x233.png 1024w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/demo-client-serviceacc-disabled-768x174.png 768w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/demo-client-serviceacc-disabled-1536x349.png 1536w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/demo-client-serviceacc-disabled-100x23.png 100w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/demo-client-serviceacc-disabled.png 1897w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/a><\/p>\n<p>The RPC fails with the status <em>UNAVAILABLE<\/em>.<\/p>\n<p>Now, let&#8217;s disable the service account of the backend server:<\/p>\n<p><a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/service-account-server-disabled.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-182089\" src=\"https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/service-account-server-disabled-300x118.png\" alt=\"\" width=\"755\" height=\"297\" srcset=\"https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/service-account-server-disabled-300x118.png 300w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/service-account-server-disabled-1024x403.png 1024w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/service-account-server-disabled-768x303.png 768w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/service-account-server-disabled-1536x605.png 1536w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/service-account-server-disabled-100x39.png 100w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/service-account-server-disabled.png 1866w\" sizes=\"(max-width: 755px) 100vw, 755px\" \/><\/a><\/p>\n<p>Surprisingly, the RPC goes through but after restarting the servers it fails like the earlier scenario:<\/p>\n<p><a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/service-account-server-disabled-RPC-fail.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-182090\" src=\"https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/service-account-server-disabled-RPC-fail-300x94.png\" alt=\"\" width=\"747\" height=\"234\" srcset=\"https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/service-account-server-disabled-RPC-fail-300x94.png 300w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/service-account-server-disabled-RPC-fail-1024x321.png 1024w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/service-account-server-disabled-RPC-fail-768x241.png 768w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/service-account-server-disabled-RPC-fail-1536x481.png 1536w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/service-account-server-disabled-RPC-fail-100x31.png 100w, https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/service-account-server-disabled-RPC-fail.png 1892w\" sizes=\"(max-width: 747px) 100vw, 747px\" \/><\/a><\/p>\n<p><strong>It seems that ALTS was caching the service account status earlier, but after the server restart, the RPC failed with the status <em>UNKNOWN<\/em><\/strong>.<\/p>\n<h2 id=\"bd-conclusion\" data-id=\"conclusion\">5. Conclusion<\/h2>\n<div class=\"bd-anchor\" id=\"conclusion\"><\/div>\n<p>In this article, we delved into the gRPC Java library supporting ALTS. With minimal code, ALTS can be enabled in gRPC services. It also provides greater flexibility in controlling the authorization of gRPC services with the help of GCP IAM service accounts.<\/p>\n<p><strong>However, it works only in GCP infrastructure as it&#8217;s provided out of the box. Hence, to run gRPC services outside of GCP infrastructure, TLS support in gRPC is crucial and must be manually configured.<\/strong><\/p>\n<p>As usual, the code used here is available <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/github.com\/eugenp\/tutorials\/tree\/master\/grpc\">over on GitHub<\/a>.<\/p>\n<p><Img align=\"left\" border=\"0\" height=\"1\" width=\"1\" alt=\"\" style=\"border:0;float:left;margin:0;padding:0;width:1px!important;height:1px!important;\" hspace=\"0\" src=\"https:\/\/feeds.feedblitz.com\/~\/i\/897875555\/0\/baeldung\"><\/p>\n<div style=\"clear:both;padding-top:0.2em;\"><a title=\"Like on Facebook\" href=\"https:\/\/feeds.feedblitz.com\/_\/28\/897875555\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/fblike20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Pin it!\" href=\"https:\/\/feeds.feedblitz.com\/_\/29\/897875555\/baeldung,https%3A%2F%2Fwww.baeldung.com%2Fwp-content%2Fuploads%2F2024%2F05%2Fadmin-ajax-2.png\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/pinterest20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Post to X.com\" href=\"https:\/\/feeds.feedblitz.com\/_\/24\/897875555\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/x.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Subscribe by email\" href=\"https:\/\/feeds.feedblitz.com\/_\/19\/897875555\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/email20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Subscribe by RSS\" href=\"https:\/\/feeds.feedblitz.com\/_\/20\/897875555\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/rss20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a rel=\"NOFOLLOW\" title=\"View Comments\" href=\"https:\/\/www.baeldung.com\/java-grpc-authentication-application-layer-transport-security-alts#respond\"><img decoding=\"async\" height=\"20\" style=\"border:0;margin:0;padding:0;\" src=\"https:\/\/assets.feedblitz.com\/i\/comments20.png\"><\/a>&#160;<a title=\"Follow Comments via RSS\" href=\"https:\/\/www.baeldung.com\/java-grpc-authentication-application-layer-transport-security-alts\/feed\"><img decoding=\"async\" height=\"20\" style=\"border:0;margin:0;padding:0;\" src=\"https:\/\/assets.feedblitz.com\/i\/commentsrss20.png\"><\/a>&#160;<\/div>\n\n<p class=\"syndicated-attribution\"><figure class= \\\"wp-block-image alignnone \\\"><img src= \\\"http:\/\/itteacheritfreelance.hk\/test\/wordpress\/wp-content\/uploads\/2016\/05\/logo2-2.png\\\" alt=\\\"IT\u96fb\u8166\u88dc\u7fd2 java\u88dc\u7fd2 \u70ba\u5927\u5bb6\u914d\u5c0d\u96fb\u8166\u88dc\u7fd2,IT freelance, \u79c1\u4eba\u8001\u5e2b, PHP\u88dc\u7fd2,CSS\u88dc\u7fd2,XML,Java\u88dc\u7fd2,MySQL\u88dc\u7fd2,graphic design\u88dc\u7fd2,\u4e2d\u5c0f\u5b78ICT\u88dc\u7fd2,\u4e00\u5c0d\u4e00\u79c1\u4eba\u88dc\u7fd2\u548cFreelance\u81ea\u7531\u5de5\u4f5c\u914d\u5c0d\u3002\\\"\/><figcaption>\u7acb\u523b\u8a3b\u518a\u53ca\u5831\u540d\u96fb\u8166\u88dc\u7fd2\u8ab2\u7a0b\u5427!<\/figcaption><\/figure>\r\n<\/br>Find A Teacher Form:\r\n<\/br>https:\/\/docs.google.com\/forms\/d\/1vREBnX5n262umf4wU5U2pyTwvk9O-JrAgblA-wH9GFQ\/viewform?edit_requested=true#responses\r\n<\/br><\/br>Email:\r\n<\/br>public1989two@gmail.com<br><br><br><br><br><br><br>\r\n<a href=www.itsec.hk style=color:#FFFFFF;>www.itsec.hk<\/a><br>\r\n<a href=\\\"www.itsec.vip\\\" style=color:#FFFFFF;>www.itsec.vip<\/a><br>\r\n<a href=\\\"www.itseceu.uk\\\" style=color:#FFFFFF;>www.itseceu.uk<\/a><br><\/p>","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p><img decoding=\"async\" src=\"https:\/\/www.baeldung.com\/wp-content\/uploads\/2024\/05\/admin-ajax-2.png\" class=\"webfeedsFeaturedVisual wp-post-image\" alt=\"start here featured\"><\/p>\n<p>Explore the role of ALTS (Application Layer Transport Security) in gRPC applications.<\/p>\n<div><a title=\"Like on Facebook\" href=\"https:\/\/feeds.feedblitz.com\/_\/28\/897875555\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/fblike20.png\"><\/a>\u00a0<a title=\"Pin it!\" href=\"https:\/\/feeds.feedblitz.com\/_\/29\/897875555\/baeldung,https%3A%2F%2Fwww.baeldung.com%2Fwp-content%2Fuploads%2F2024%2F05%2Fadmin-ajax-2.png\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/pinterest20.png\"><\/a>\u00a0<a title=\"Post to X.com\" href=\"https:\/\/feeds.feedblitz.com\/_\/24\/897875555\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/x.png\"><\/a>\u00a0<a title=\"Subscribe by email\" href=\"https:\/\/feeds.feedblitz.com\/_\/19\/897875555\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/email20.png\"><\/a>\u00a0<a title=\"Subscribe by RSS\" href=\"https:\/\/feeds.feedblitz.com\/_\/20\/897875555\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/rss20.png\"><\/a>\u00a0<a rel=\"NOFOLLOW\" title=\"View Comments\" href=\"https:\/\/www.baeldung.com\/java-grpc-authentication-application-layer-transport-security-alts#respond\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/comments20.png\"><\/a>\u00a0<a title=\"Follow Comments via RSS\" href=\"https:\/\/www.baeldung.com\/java-grpc-authentication-application-layer-transport-security-alts\/feed\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/commentsrss20.png\"><\/a>\u00a0<\/div>\n<\/div>","protected":false},"author":2057,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"gRPC Authentication in Java Using Application Layer Transport Security (ALTS) - ITTeacherITFreelance.hk","description":"Explore the role of ALTS (Application Layer Transport Security) in gRPC applications. \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0"},"footnotes":""},"categories":[6,1307],"tags":[10799],"_links":{"self":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329555"}],"collection":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/2057"}],"replies":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=329555"}],"version-history":[{"count":1,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329555\/revisions"}],"predecessor-version":[{"id":329556,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329555\/revisions\/329556"}],"wp:attachment":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=329555"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=329555"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=329555"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}