/*
 * Copyright 2018 Netflix, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License")
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.netflix.spinnaker.clouddriver.titus.client;

import com.netflix.spinnaker.security.AuthenticatedRequest;
import io.grpc.Metadata;
import io.grpc.stub.AbstractStub;
import io.grpc.stub.MetadataUtils;

public class TitusClientAuthenticationUtil {

  private static String CALLER_ID_HEADER = "X-Titus-CallerId";
  private static String CALL_REASON = "X-Titus-CallReason";
  private static Metadata.Key<String> CALLER_ID_KEY =
      Metadata.Key.of(CALLER_ID_HEADER, Metadata.ASCII_STRING_MARSHALLER);
  private static Metadata.Key<String> CALL_REASON_KEY =
      Metadata.Key.of(CALL_REASON, Metadata.ASCII_STRING_MARSHALLER);

  public static <STUB extends AbstractStub<STUB>> STUB attachCaller(STUB serviceStub) {
    Metadata metadata = new Metadata();
    metadata.put(CALLER_ID_KEY, AuthenticatedRequest.getSpinnakerUser().orElse("spinnaker"));
    metadata.put(CALL_REASON_KEY, AuthenticatedRequest.getSpinnakerExecutionId().orElse("unknown"));
    return serviceStub.withInterceptors(MetadataUtils.newAttachHeadersInterceptor(metadata));
  }
}

@@ -645,16 +645,56 @@
 lient;%0A%0A
+import com.google.common.base.Splitter;%0A
 import c
@@ -745,16 +745,16 @@
 equest;%0A
-
 import i
@@ -838,16 +838,94 @@
 taUtils;
+%0Aimport java.util.List;%0Aimport javax.annotation.Nonnull;%0Aimport org.slf4j.MDC;
 %0A%0Apublic
@@ -1578,24 +1578,33 @@
 etadata.put(
+%0A        
 CALL_REASON_
@@ -1611,168 +1611,705 @@
 KEY,
- AuthenticatedRequest.getSpinnakerExecutionId().orElse(%22unknown%22));%0A    return serviceStub.withInterceptors(MetadataUtils.newAttachHeadersInterceptor(metadata))
+%0A        String.format(%0A            %22Invoked by Spinnaker execution %25s, Application name %25s%22,%0A            getSpinnakerExecutionId(%0A                AuthenticatedRequest.getSpinnakerExecutionId().orElse(%22unknown%22)),%0A            MDC.get(AuthenticatedRequest.Header.APPLICATION.getHeader())));%0A    return serviceStub.withInterceptors(MetadataUtils.newAttachHeadersInterceptor(metadata));%0A  %7D%0A%0A  public static @Nonnull String getSpinnakerExecutionId(@Nonnull String executionIdHeader) %7B%0A    if (!%22unknown%22.equalsIgnoreCase(executionIdHeader) && !executionIdHeader.isEmpty()) %7B%0A      List%3CString%3E ids = Splitter.on(':').splitToList(executionIdHeader);%0A      return ids.get(0);%0A    %7D%0A    return %22unknown%22
 ;%0A  

Add more details in call reason header for Titus (#4031)