/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.camel.quarkus.jolokia.util;

import org.apache.camel.quarkus.jolokia.config.JolokiaRuntimeConfig.Kubernetes;
import org.eclipse.microprofile.config.ConfigProvider;

public final class JolokiaKubernetesUtils {

    private JolokiaKubernetesUtils() {
        // Utility class
    }

    /**
     * Determines whether the agent authenticates its clients with Kubernetes SSL client authentication.
     *
     * Both the bind address default and the restrictor depend on this, and they must agree, so it is derived
     * in one place rather than at each of them.
     *
     * The result describes what the extension configures. `additional-properties` could still switch client
     * authentication back off, but `JolokiaRecorder` fails startup when that leaves a non-loopback bind
     * unauthenticated, so anything reachable from off the machine has been authenticated by the time this is
     * consulted.
     */
    public static boolean isClientAuthenticationConfigured(Kubernetes kubernetes) {
        return ConfigProvider.getConfig().getOptionalValue("kubernetes.service.host", String.class).isPresent()
                && kubernetes.clientAuthenticationEnabled()
                && kubernetes.serviceCaCert().exists();
    }
}
