
package com.sic.plugins.kpp.model;

import hudson.Extension;
import hudson.model.Describable;
import hudson.model.Descriptor;
import hudson.model.Hudson;
import hudson.util.Secret;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Serializable;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.cert.CertificateException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.kohsuke.stapler.DataBoundConstructor;

/**
 * Represents a keychain.
 * @author michaelbar
 */
public final class KPPKeychain implements Describable<KPPKeychain>, Serializable {
    
    private final static Logger LOGGER = Logger.getLogger(KPPKeychain.class.getName());
    private final static String VARIABLE_NAME_KEYCHAIN = "KEYCHAIN";
    private final static String VARIABLE_NAME_CODE_SIGNING_IDENTITY ="CODE_SIGNING_IDENTITY";
    private final static String VARIABLE_NAME_KEYCHAIN_PASSWORD = "KEYCHAIN_PASSWORD";
    
    private final String fileName;
    private String description;
    private Secret password;
    private String varPrefix;
    private List<KPPCertificate> certificates;
    
    public KPPKeychain(String fileName) {
        this.fileName = fileName;
    }
    
    @DataBoundConstructor
    public KPPKeychain(String fileName, String password, String description, String varPrefix, List<KPPCertificate> certificates) {
        this.fileName = fileName;
        setPassword(password);
        setDescription(description);
        setVarPrefix(varPrefix);
        setCertificates(certificates);
    }
    
    public final String getFileName() {
        return fileName;
    }
    
    public final void setPassword(String password) {
        this.password = Secret.fromString(password);
    }
    
    public final String getPassword() {
        return Secret.toString(password);
    }
    
    public final Secret getPasswordAsSecret() {
        return password;
    }
    
    public final String getDescription() {
        getCertificates();
        return description;
    }
    
    public final void setDescription(String description) {
        this.description = description;
    }
    
    public final void setVarPrefix(String varPrefix) {
        this.varPrefix = varPrefix;
    }
    
    public final String getVarPrefix() {
        return varPrefix;
    }
    
    public final List<KPPCertificate> getCertificates() {
        return certificates;
    }
    
    public final void setCertificates(List<KPPCertificate>certificates){
        this.certificates = certificates;
    }
    
    public String getKeychainVariableName() {
        return getPrefixedVariableName(VARIABLE_NAME_KEYCHAIN);
    }
    
    public String getKeychainPasswordVariableName() {
        return getPrefixedVariableName(VARIABLE_NAME_KEYCHAIN_PASSWORD);
    }
    
    public String getCodeSigningIdentityVariableName() {
        return getPrefixedVariableName(VARIABLE_NAME_CODE_SIGNING_IDENTITY);
    }
    
    private String getPrefixedVariableName(String variableName) {
        String name = variableName;
        if (varPrefix==null || varPrefix.length()==0) {
            return String.format("%s", name);
        }
        name = String.format("%s_%s", varPrefix, name);
        return name;
    }
    
    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final KPPKeychain other = (KPPKeychain) obj;
        if ((this.fileName == null) ? (other.fileName != null) : !this.fileName.equals(other.fileName)) {
            return false;
        }
        return true;
    }
    
    @Override
    public int hashCode() {
        int hash = 3;
        hash = 67 * hash + (this.fileName != null ? this.fileName.hashCode() : 0);
        return hash;
    }

    public Descriptor getDescriptor() {
        Descriptor ds = Hudson.getInstance().getDescriptorOrDie(getClass());
        return ds;
    }
    
    @Extension
    public static final class DescriptorImpl extends Descriptor<KPPKeychain> {

        @Override
        public String getDisplayName() {
            return "Keychain";
        }
    }
    
    /**
     * This method loads the uplaoded keystore from a file.
     *
     * @return loaded Keystore
     */
    private static KeyStore loadKeystoreFromFile(String keyStoreFilePath, String keyStorePW) throws
            KeyStoreException,
            FileNotFoundException,
            IOException,
            NoSuchAlgorithmException,
            CertificateException,
            NoSuchProviderException {
 
        //KeyStore ks = KeyStore.getInstance("KeychainStore", "Apple");
        //KeyStore ks = KeyStore.getInstance("KeychainStore");
        KeyStore ks = KeyStore.getInstance("KeychainStore");

        //keyStoreFilePath = "/Users/michaelbar/Documents/Projekte/SIC/Jenkins_ZPP_Plugin/source/kpp/work/kpp_upload/empty.keychain";
        
        keyStorePW = "123456";
        char[] password = keyStorePW.toCharArray();
        File keychainFile = new File(keyStoreFilePath);
        LOGGER.log(Level.INFO, "content: {0}", keychainFile.toString());
        if (!keychainFile.isFile()) {
            if (keychainFile.isDirectory()) {
                String[] files = keychainFile.list();
                for (String filename : files) {
                    LOGGER.log(Level.INFO, filename);
                }
            }
        }

        FileInputStream fis = new FileInputStream(keychainFile);
        ks.load(fis, password);
        fis.close();
        
        LOGGER.log(Level.INFO, "Provider1: {0}", ks.getProvider());
        LOGGER.log(Level.INFO, "Provider2: {0}", ks.getProvider().toString());
 
        return ks;
    }
    
}
@@ -194,406 +194,43 @@
 .io.
-File;%0Aimport java.io.FileInputStream;%0Aimport java.io.FileNotFoundException;%0Aimport java.io.IOException;%0Aimport java.io.Serializable;%0Aimport java.security.KeyStore;%0Aimport java.security.KeyStoreException;%0Aimport java.security.NoSuchAlgorithmException;%0Aimport java.security.NoSuchProviderException;%0Aimport java.security.cert.CertificateException;%0Aimport java.util.List;%0Aimport java.util.logging.Level
+Serializable;%0Aimport java.util.List
 ;%0Aim
@@ -4055,1568 +4055,5 @@
   %7D%0A
-    %0A    /**%0A     * This method loads the uplaoded keystore from a file.%0A     *%0A     * @return loaded Keystore%0A     */%0A    private static KeyStore loadKeystoreFromFile(String keyStoreFilePath, String keyStorePW) throws%0A            KeyStoreException,%0A            FileNotFoundException,%0A            IOException,%0A            NoSuchAlgorithmException,%0A            CertificateException,%0A            NoSuchProviderException %7B%0A %0A        //KeyStore ks = KeyStore.getInstance(%22KeychainStore%22, %22Apple%22);%0A        //KeyStore ks = KeyStore.getInstance(%22KeychainStore%22);%0A        KeyStore ks = KeyStore.getInstance(%22KeychainStore%22);%0A%0A        //keyStoreFilePath = %22/Users/michaelbar/Documents/Projekte/SIC/Jenkins_ZPP_Plugin/source/kpp/work/kpp_upload/empty.keychain%22;%0A        %0A        keyStorePW = %22123456%22;%0A        char%5B%5D password = keyStorePW.toCharArray();%0A        File keychainFile = new File(keyStoreFilePath);%0A        LOGGER.log(Level.INFO, %22content: %7B0%7D%22, keychainFile.toString());%0A        if (!keychainFile.isFile()) %7B%0A            if (keychainFile.isDirectory()) %7B%0A                String%5B%5D files = keychainFile.list();%0A                for (String filename : files) %7B%0A                    LOGGER.log(Level.INFO, filename);%0A                %7D%0A            %7D%0A        %7D%0A%0A        FileInputStream fis = new FileInputStream(keychainFile);%0A        ks.load(fis, password);%0A        fis.close();%0A        %0A        LOGGER.log(Level.INFO, %22Provider1: %7B0%7D%22, ks.getProvider());%0A        LOGGER.log(Level.INFO, %22Provider2: %7B0%7D%22, ks.getProvider().toString());%0A %0A        return ks;%0A    %7D%0A    %0A
 %7D

remove reading keystore from keychain