<commit_before>/*
 * Copyright 2013 Netherlands eScience Center
 *
 * 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 nl.esciencecenter.xenon.adaptors.gftp;

import java.security.cert.X509Certificate;
import java.util.EnumSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import nl.esciencecenter.xenon.UnknownPropertyException;
import nl.esciencecenter.xenon.XenonException;
import nl.esciencecenter.xenon.XenonPropertyDescription;
import nl.esciencecenter.xenon.XenonPropertyDescription.Component;
import nl.esciencecenter.xenon.XenonPropertyDescription.Type;
import nl.esciencecenter.xenon.credentials.Credential;
import nl.esciencecenter.xenon.credentials.Credentials;
import nl.esciencecenter.xenon.engine.PropertyTypeException;
import nl.esciencecenter.xenon.engine.XenonProperties;
import nl.esciencecenter.xenon.engine.XenonPropertyDescriptionImplementation;
import nl.esciencecenter.xenon.engine.util.ImmutableArray;

import org.globus.gsi.GlobusCredential;
import org.globus.gsi.GlobusCredentialException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Grid Proxy Credential Factory based on Globus (Proxy) Credentials. <br>
 * Typically a GlobusProxyCrential object is linked to one user account and (proxy) credential.
 * 
 * @author <NAME>er
 */
public class GlobusProxyCredentials implements Credentials {

    public static final Logger logger = LoggerFactory.getLogger(GlobusProxyCredentials.class);

    public static final String PROPERTY_PREFIX = "xenon.globus.";

    /**
     * Location of 'userkey.pem' file. Ussualy it is ~/.globus/userkey.pem.
     */
    public static final String PROPERTY_USER_KEY_FILE = PROPERTY_PREFIX + "user.userkey";

    /**
     * Location of 'usercert.pem' file. Ussualy it is ~/.globus/usercert.pem.
     */
    public static final String PROPERTY_USER_CERT_FILE = PROPERTY_PREFIX + "user.usercert";

    /**
     * Location of already created user proxy file (if not at default location).
     */
    public static final String PROPERTY_USER_X509_PROXY = PROPERTY_PREFIX + "user.x509proxy";

    /**
     * Default proxy life time in hours when creating a new proxy. Typically between 1 and 24 hours.
     */
    public static final String PROPERTY_USER_PROXY_LIFETIME_HOURS = PROPERTY_PREFIX + "user.proxy.lifeTimeInHours";

    public static final String USERKEY_PEM = "userkey.pem";

    public static final String USERCERT_PEM = "usercert.pem";

    public static final ImmutableArray<XenonPropertyDescription> GLOBUS_CREDENTIAL_PROPERTIES = new ImmutableArray<XenonPropertyDescription>(

    new XenonPropertyDescriptionImplementation(PROPERTY_USER_KEY_FILE, Type.STRING, EnumSet.of(Component.CREDENTIALS), null,
            "Location of User Key file or 'userkey.pem' file. Default location is: ~/.globus/userkey.pem"),

    new XenonPropertyDescriptionImplementation(PROPERTY_USER_CERT_FILE, Type.STRING, EnumSet.of(Component.CREDENTIALS), null,
            "Location of user Certificate file or 'usercert.pem' file. Default location is: ~/.globus/usercert.pem"),

    new XenonPropertyDescriptionImplementation(PROPERTY_USER_X509_PROXY, Type.STRING, EnumSet.of(Component.CREDENTIALS), null,
            "Location of user X509 proxy file. Default location is: /tmp/x509_<userid>"),

    new XenonPropertyDescriptionImplementation(PROPERTY_USER_PROXY_LIFETIME_HOURS, Type.STRING,
            EnumSet.of(Component.CREDENTIALS), null, "Default Lifetime of create Grid Proxy in hours. "));

    public static XenonPropertyDescription[] getSupportedProperties() {
        return GLOBUS_CREDENTIAL_PROPERTIES.asArray();
    }

    // --- 
    // Instance
    // ---

    protected List<X509Certificate> certificates;
    protected String userKeyfile;
    protected String userCertfile;
    protected String userProxyfile;
    protected String userCertificatesDir;
    int proxyLifeTimeInHours = 24;

    public GlobusProxyCredentials(XenonProperties properties, GftpAdaptor gftpAdaptor) {

        reloadCACertificates();
        // Optional properties, if none defined Globus defaults will be used. 
        updateDefaultProperties(properties);
    }

    protected void updateDefaultProperties(XenonProperties properties) {
        // Defaults: 
        userKeyfile = getPropertyOrDefault(properties, PROPERTY_USER_KEY_FILE, null);
        userCertfile = getPropertyOrDefault(properties, PROPERTY_USER_CERT_FILE, null);
        userProxyfile = getPropertyOrDefault(properties, PROPERTY_USER_X509_PROXY, null);
        proxyLifeTimeInHours = parseInt(getPropertyOrDefault(properties, PROPERTY_USER_PROXY_LIFETIME_HOURS, "24"), 24);
    }

    /**
     * Create Properties object containing current configured properties.
     * 
     * @return Properties Map of current credential Properties.
     */
    protected Map<String, String> createDefaultCredentialProperties() {

        return createCredentialProperties(userCertfile, userKeyfile, userProxyfile);
    }

    protected Map<String, String> createCredentialProperties(String certFile, String keyFile, String proxyFile) {

        Map<String, String> props = new Hashtable<String, String>();
        if (certFile != null) {
            props.put(PROPERTY_USER_KEY_FILE, certFile);<commit_msg>Removed actual (re)load of the certificates during initialization. <commit_after>/*
 * Copyright 2013 Netherlands eScience Center
 *
 * 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 nl.esciencecenter.xenon.adaptors.gftp;

import java.security.cert.X509Certificate;
import java.util.EnumSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import nl.esciencecenter.xenon.UnknownPropertyException;
import nl.esciencecenter.xenon.XenonException;
import nl.esciencecenter.xenon.XenonPropertyDescription;
import nl.esciencecenter.xenon.XenonPropertyDescription.Component;
import nl.esciencecenter.xenon.XenonPropertyDescription.Type;
import nl.esciencecenter.xenon.credentials.Credential;
import nl.esciencecenter.xenon.credentials.Credentials;
import nl.esciencecenter.xenon.engine.PropertyTypeException;
import nl.esciencecenter.xenon.engine.XenonProperties;
import nl.esciencecenter.xenon.engine.XenonPropertyDescriptionImplementation;
import nl.esciencecenter.xenon.engine.util.ImmutableArray;

import org.globus.gsi.GlobusCredential;
import org.globus.gsi.GlobusCredentialException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Grid Proxy Credential Factory based on Globus (Proxy) Credentials. <br>
 * Typically a GlobusProxyCrential object is linked to one user account and (proxy) credential.
 * 
 * @author <NAME>
 */
public class GlobusProxyCredentials implements Credentials {

    public static final Logger logger = LoggerFactory.getLogger(GlobusProxyCredentials.class);

    public static final String PROPERTY_PREFIX = "xenon.globus.";

    /**
     * Location of 'userkey.pem' file. Ussualy it is ~/.globus/userkey.pem.
     */
    public static final String PROPERTY_USER_KEY_FILE = PROPERTY_PREFIX + "user.userkey";

    /**
     * Location of 'usercert.pem' file. Ussualy it is ~/.globus/usercert.pem.
     */
    public static final String PROPERTY_USER_CERT_FILE = PROPERTY_PREFIX + "user.usercert";

    /**
     * Location of already created user proxy file (if not at default location).
     */
    public static final String PROPERTY_USER_X509_PROXY = PROPERTY_PREFIX + "user.x509proxy";

    /**
     * Default proxy life time in hours when creating a new proxy. Typically between 1 and 24 hours.
     */
    public static final String PROPERTY_USER_PROXY_LIFETIME_HOURS = PROPERTY_PREFIX + "user.proxy.lifeTimeInHours";

    public static final String USERKEY_PEM = "userkey.pem";

    public static final String USERCERT_PEM = "usercert.pem";

    public static final ImmutableArray<XenonPropertyDescription> GLOBUS_CREDENTIAL_PROPERTIES = new ImmutableArray<XenonPropertyDescription>(

    new XenonPropertyDescriptionImplementation(PROPERTY_USER_KEY_FILE, Type.STRING, EnumSet.of(Component.CREDENTIALS), null,
            "Location of User Key file or 'userkey.pem' file. Default location is: ~/.globus/userkey.pem"),

    new XenonPropertyDescriptionImplementation(PROPERTY_USER_CERT_FILE, Type.STRING, EnumSet.of(Component.CREDENTIALS), null,
            "Location of user Certificate file or 'usercert.pem' file. Default location is: ~/.globus/usercert.pem"),

    new XenonPropertyDescriptionImplementation(PROPERTY_USER_X509_PROXY, Type.STRING, EnumSet.of(Component.CREDENTIALS), null,
            "Location of user X509 proxy file. Default location is: /tmp/x509_<userid>"),

    new XenonPropertyDescriptionImplementation(PROPERTY_USER_PROXY_LIFETIME_HOURS, Type.STRING,
            EnumSet.of(Component.CREDENTIALS), null, "Default Lifetime of create Grid Proxy in hours. "));

    public static XenonPropertyDescription[] getSupportedProperties() {
        return GLOBUS_CREDENTIAL_PROPERTIES.asArray();
    }

    // --- 
    // Instance
    // ---

    protected List<X509Certificate> certificates;
    protected String userKeyfile;
    protected String userCertfile;
    protected String userProxyfile;
    protected String userCertificatesDir;
    int proxyLifeTimeInHours = 24;

    public GlobusProxyCredentials(XenonProperties properties, GftpAdaptor gftpAdaptor) {

        // Optional properties, if none defined Globus defaults will be used. 
        updateDefaultProperties(properties);
    }

    protected void updateDefaultProperties(XenonProperties properties) {
        // Defaults: 
        userKeyfile = getPropertyOrDefault(properties, PROPERTY_USER_KEY_FILE, null);
        userCertfile = getPropertyOrDefault(properties, PROPERTY_USER_CERT_FILE, null);
        userProxyfile = getPropertyOrDefault(properties, PROPERTY_USER_X509_PROXY, null);
        proxyLifeTimeInHours = parseInt(getPropertyOrDefault(properties, PROPERTY_USER_PROXY_LIFETIME_HOURS, "24"), 24);
    }

    /**
     * Create Properties object containing current configured properties.
     * 
     * @return Properties Map of current credential Properties.
     */
    protected Map<String, String> createDefaultCredentialProperties() {

        return createCredentialProperties(userCertfile, userKeyfile, userProxyfile);
    }

    protected Map<String, String> createCredentialProperties(String certFile, String keyFile, String proxyFile) {

        Map<String, String> props = new Hashtable<String, String>();
        if (certFile != null) {
            props.put(PROPERTY_USER_KEY_FILE, certFile);
{'fraction_non_alphanumeric': 0.08164677391454765, 'fraction_numerical': 0.007438159487977858, 'mean_word_length': 4.291990846681922, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '982945', 'n_tokens_mistral': 3446, 'n_tokens_neox': 3330, 'n_words': 862}