package enmasse.controller.auth;

import io.fabric8.kubernetes.api.model.Secret;
import io.fabric8.openshift.client.OpenShiftClient;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.Base64;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Controller that creates self-signed certificates for instances.
 */
public class SelfSignedCertManager implements CertManager {
    private static final Logger log = LoggerFactory.getLogger(SelfSignedCertManager.class.getName());
    private final OpenShiftClient client;

    public SelfSignedCertManager(OpenShiftClient controllerClient) {
        this.client = controllerClient;
    }

    @Override
    public void issueCert(String secretName, String namespace, String ... hostnames) throws Exception {
        Secret secret = client.secrets().inNamespace(namespace).withName(secretName).get();
        if (secret != null) {
            // TODO: Have this sign certificates with OpenShift CA

            String keyKey = "server-key.pem";
            String certKey = "server-cert.pem";
            if (secret.getData() != null && secret.getData().containsKey(keyKey) && secret.getData().containsKey(certKey)) {
                return;
            }

            log.info("Creating self-signed certificates for " + hostnames);
            File keyFile = new File("/tmp/server-key.pem");
            File certFile = new File("/tmp/server-cert.pem");
            ProcessBuilder keyGenBuilder = new ProcessBuilder("openssl", "req", "-new", "-x509", "-batch", "-nodes",
                    "-out", certFile.getAbsolutePath(), "-keyout", keyFile.getAbsolutePath()); //, "-subj", "/CN=" + addressspace.messagingHost().get() + "," + addressspace.mqttHost().get() + "," + addressspace.consoleHost().get());
            log.info("Generating keys using " + keyGenBuilder.command());
            Process keyGen = keyGenBuilder.start();
            if (!keyGen.waitFor(1, TimeUnit.MINUTES)) {
                throw new RuntimeException("Key generation timed out");
            }

            Map<String, String> data = new LinkedHashMap<>();
            Base64.Encoder encoder = Base64.getEncoder();
            data.put(keyKey, encoder.encodeToString(FileUtils.readFileToByteArray(keyFile)));
            data.put(certKey, encoder.encodeToString(FileUtils.readFileToByteArray(certFile)));
            client.secrets().inNamespace(namespace).withName(secretName).edit()
                    .addToData(data)
                    .done();
        }
    }

    public static SelfSignedCertManager create(OpenShiftClient controllerClient) {
        return new SelfSignedCertManager(controllerClient);
    }

}

@@ -1,8 +1,604 @@
+/*%0A * Copyright 2017 Red Hat Inc.%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *     http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A
 package 
@@ -841,16 +841,41 @@
 o.File;%0A
+import java.util.Arrays;%0A
 import j
@@ -2010,16 +2010,32 @@
 for %22 + 
+Arrays.toString(
 hostname
@@ -2036,16 +2036,17 @@
 stnames)
+)
 ;%0A      

Print contents rather than array reference