<commit_before>/*
 *  Copyright (C) 2010 <NAME> <romain.reuillon at openmole.org>
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.openmole.plugin.environment.glite;

import fr.in2p3.jsaga.adaptor.base.usage.UDuration;
import fr.in2p3.jsaga.adaptor.security.VOMSContext;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.GZIPInputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.ogf.saga.context.Context;
import org.ogf.saga.error.AuthenticationFailedException;
import org.ogf.saga.error.AuthorizationFailedException;
import org.ogf.saga.error.BadParameterException;
import org.ogf.saga.error.DoesNotExistException;
import org.ogf.saga.error.IncorrectStateException;
import org.ogf.saga.error.NoSuccessException;
import org.ogf.saga.error.NotImplementedException;
import org.ogf.saga.error.PermissionDeniedException;
import org.openmole.commons.aspect.caching.Cachable;
import org.openmole.commons.exception.InternalProcessingError;
import org.openmole.commons.exception.UserBadDataError;
import org.openmole.commons.tools.io.FileUtil;
import org.openmole.core.file.URIFile;
import org.openmole.core.model.execution.batch.IBatchServiceAuthentication;
import org.openmole.core.model.file.IURIFile;
import org.openmole.misc.executorservice.ExecutorType;
import org.openmole.plugin.environment.glite.internal.Activator;
import org.openmole.plugin.environment.glite.internal.ProxyChecker;

/**
 *
 * @author <NAME> <romain.reuillon at openmole.org>
 */
public class GliteAuthentication implements IBatchServiceAuthentication {

    final private String voName;
    final private String vomsURL;
    
    transient private File proxy = null;
    transient volatile private long proxyExpiresTime = Long.MAX_VALUE;

    public GliteAuthentication(String voName, String vomsURL) {
        this.voName = voName;
        this.vomsURL = vomsURL;
    }

    @Cachable
    private File getCACertificatesDir() throws InternalProcessingError, InterruptedException {
        try {
            String X509_CERT_DIR = System.getenv("X509_CERT_DIR");

            File caDir;
            if (X509_CERT_DIR != null && (caDir = new File(X509_CERT_DIR)).exists()) {
                return caDir;
            } else {
                caDir = new File("/etc/grid-security/certificates/");
                if (caDir.exists()) {
                    return caDir;
                } else {
                    File tmp = Activator.getWorkspace().getFile("CACertificates");

                    if (!tmp.exists() || !new File(tmp, ".complete").exists()) {
                        tmp.mkdir();
                        dowloadCACertificates(new URI(Activator.getWorkspace().getPreference(GliteEnvironment.CACertificatesSiteLocation)), tmp);
                        new File(tmp, ".complete").createNewFile();
                    }
                    return tmp;
                }
            }

        } catch (URISyntaxException e) {
            throw new InternalProcessingError(e);
        } catch (IOException e) {
            throw new InternalProcessingError(e);
        }

    }

    @Override
    public void initialize() throws UserBadDataError, InternalProcessingError, InterruptedException {
        File proxyFile;

        if (System.getenv().containsKey("X509_USER_PROXY") && (proxyFile = new File(System.getenv().get("X509_USER_PROXY"))).exists()) {
            createContextFromFile(proxyFile);
        } else {
            createContextFromPreferences();
        }
    }

    private void createContextFromPreferences() throws InternalProcessingError, UserBadDataError, InterruptedException {

        Context ctx = Activator.getJSagaSessionService().createContext();
        initContext(ctx);
        
        long interval = (long) (getTime() * Activator.getWorkspace().getPreferenceAsDouble(GliteEnvironment.ProxyRenewalRatio)) * 1000;
       
        Logger.getLogger(GliteAuthentication.class.getName()).log(Level.FINE, "Proxy renewal in {0} ms.", interval);
        Activator.getUpdater().delay(new ProxyChecker(this, ctx), ExecutorType.OWN, interval);

        Activator.getJSagaSessionService().addContext(ctx);
    }

    public void initContext(Context ctx) throws InternalProcessingError, InterruptedException, UserBadDataError {

        try {
            ctx.setAttribute(Context.TYPE, "VOMS");
            ctx.setAttribute(VOMSContext.VOMSDIR, "");
            ctx.setAttribute(Context.CERTREPOSITORY, getCACertificatesDir().getCanonicalPath());

            ctx.setAttribute(Context.LIFETIME, getTimeString());

            File oldProxy = proxy;
            proxy = Activator.getWorkspace().newFile("proxy", ".x509");
                
            ctx.setAttribute(Context.USERPROXY, proxy.getAbsolutePath());

            if (getCertType().equalsIgnoreCase("p12")) {
                ctx.setAttribute(VOMSContext.USERCERTKEY, getP12CertPath());
            } else if (getCertType().equalsIgnoreCase("pem")) {
                ctx.setAttribute(Context.USERCERT, getCertPath());
                ctx.setAttribute(Context.USERKEY, getKeyPath());
            } else {
                throw new UserBadDataError("Unknown certificate type " + getCertType());
            }

            ctx.setAttribute(Context.SERVER, vomsURL);
            ctx.setAttribute(Context.USERVO, voName);

            String fqan = getFQAN();

            if (!fqan.isEmpty()) {
                ctx.setAttribute(VOMSContext.USERFQAN, fqan);
            }

            String keyPassword = Activator.getWorkspace().getPreference(GliteEnvironment.PasswordLocation);
            if (keyPassword == null) {
                keyPassword = "";
            }
            ctx.setAttribute(Context.USERPASS, keyPassword);
            
            ctx.getAttribute(Context.USERID);
            proxyExpiresTime = System.currentTimeMillis() + getTime();
            
            if(oldProxy != null) oldProxy.delete();
            
        } catch (NoSuccessException e) {
            throw new InternalProcessingError(e);
        } catch (NotImplementedException e) {
            throw new InternalProcessingError(e);
        } catch (AuthenticationFailedException e) {
            throw new InternalProcessingError(e);
        } catch (AuthorizationFailedException e) {
            throw new InternalProcessingError(e);
        } catch (PermissionDeniedException e) {
            throw new InternalProcessingError(e);
        } catch (IncorrectStateException e) {
            throw new InternalProcessingError(e);
        } catch (BadParameterException e) {
            throw new InternalProcessingError(e);
        } catch (DoesNotExistException e) {
            throw new InternalProcessingError(e);
        } catch (org.ogf.saga.error.TimeoutException e) {
            throw new InternalProcessingError(e);
        } catch (IOException e) {
            throw new InternalProcessingError(e);<commit_msg>Hopefully jsaga delete the file if it allready exists.<commit_after>/*
 *  Copyright (C) 2010 <NAME> <<EMAIL>ain.reuillon at openmole.org>
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.openmole.plugin.environment.glite;

import fr.in2p3.jsaga.adaptor.base.usage.UDuration;
import fr.in2p3.jsaga.adaptor.security.VOMSContext;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.GZIPInputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.ogf.saga.context.Context;
import org.ogf.saga.error.AuthenticationFailedException;
import org.ogf.saga.error.AuthorizationFailedException;
import org.ogf.saga.error.BadParameterException;
import org.ogf.saga.error.DoesNotExistException;
import org.ogf.saga.error.IncorrectStateException;
import org.ogf.saga.error.NoSuccessException;
import org.ogf.saga.error.NotImplementedException;
import org.ogf.saga.error.PermissionDeniedException;
import org.openmole.commons.aspect.caching.Cachable;
import org.openmole.commons.exception.InternalProcessingError;
import org.openmole.commons.exception.UserBadDataError;
import org.openmole.commons.tools.io.FileUtil;
import org.openmole.core.file.URIFile;
import org.openmole.core.model.execution.batch.IBatchServiceAuthentication;
import org.openmole.core.model.file.IURIFile;
import org.openmole.misc.executorservice.ExecutorType;
import org.openmole.plugin.environment.glite.internal.Activator;
import org.openmole.plugin.environment.glite.internal.ProxyChecker;

/**
 *
 * @author <NAME> <romain.reuillon at openmole.org>
 */
public class GliteAuthentication implements IBatchServiceAuthentication {

    final private String voName;
    final private String vomsURL;
    
    transient private File proxy = null;
    transient volatile private long proxyExpiresTime = Long.MAX_VALUE;

    public GliteAuthentication(String voName, String vomsURL) {
        this.voName = voName;
        this.vomsURL = vomsURL;
    }

    @Cachable
    private File getCACertificatesDir() throws InternalProcessingError, InterruptedException {
        try {
            String X509_CERT_DIR = System.getenv("X509_CERT_DIR");

            File caDir;
            if (X509_CERT_DIR != null && (caDir = new File(X509_CERT_DIR)).exists()) {
                return caDir;
            } else {
                caDir = new File("/etc/grid-security/certificates/");
                if (caDir.exists()) {
                    return caDir;
                } else {
                    File tmp = Activator.getWorkspace().getFile("CACertificates");

                    if (!tmp.exists() || !new File(tmp, ".complete").exists()) {
                        tmp.mkdir();
                        dowloadCACertificates(new URI(Activator.getWorkspace().getPreference(GliteEnvironment.CACertificatesSiteLocation)), tmp);
                        new File(tmp, ".complete").createNewFile();
                    }
                    return tmp;
                }
            }

        } catch (URISyntaxException e) {
            throw new InternalProcessingError(e);
        } catch (IOException e) {
            throw new InternalProcessingError(e);
        }

    }

    @Override
    public void initialize() throws UserBadDataError, InternalProcessingError, InterruptedException {
        File proxyFile;

        if (System.getenv().containsKey("X509_USER_PROXY") && (proxyFile = new File(System.getenv().get("X509_USER_PROXY"))).exists()) {
            createContextFromFile(proxyFile);
        } else {
            createContextFromPreferences();
        }
    }

    private void createContextFromPreferences() throws InternalProcessingError, UserBadDataError, InterruptedException {

        Context ctx = Activator.getJSagaSessionService().createContext();
        initContext(ctx);
        
        long interval = (long) (getTime() * Activator.getWorkspace().getPreferenceAsDouble(GliteEnvironment.ProxyRenewalRatio)) * 1000;
       
        Logger.getLogger(GliteAuthentication.class.getName()).log(Level.FINE, "Proxy renewal in {0} ms.", interval);
        Activator.getUpdater().delay(new ProxyChecker(this, ctx), ExecutorType.OWN, interval);

        Activator.getJSagaSessionService().addContext(ctx);
    }

    public void initContext(Context ctx) throws InternalProcessingError, InterruptedException, UserBadDataError {

        try {
            ctx.setAttribute(Context.TYPE, "VOMS");
            ctx.setAttribute(VOMSContext.VOMSDIR, "");
            ctx.setAttribute(Context.CERTREPOSITORY, getCACertificatesDir().getCanonicalPath());

            ctx.setAttribute(Context.LIFETIME, getTimeString());

            if(proxy == null) proxy = Activator.getWorkspace().newFile("proxy", ".x509");
                
            ctx.setAttribute(Context.USERPROXY, proxy.getAbsolutePath());

            if (getCertType().equalsIgnoreCase("p12")) {
                ctx.setAttribute(VOMSContext.USERCERTKEY, getP12CertPath());
            } else if (getCertType().equalsIgnoreCase("pem")) {
                ctx.setAttribute(Context.USERCERT, getCertPath());
                ctx.setAttribute(Context.USERKEY, getKeyPath());
            } else {
                throw new UserBadDataError("Unknown certificate type " + getCertType());
            }

            ctx.setAttribute(Context.SERVER, vomsURL);
            ctx.setAttribute(Context.USERVO, voName);

            String fqan = getFQAN();

            if (!fqan.isEmpty()) {
                ctx.setAttribute(VOMSContext.USERFQAN, fqan);
            }

            String keyPassword = Activator.getWorkspace().getPreference(GliteEnvironment.PasswordLocation);
            if (keyPassword == null) {
                keyPassword = "";
            }
            ctx.setAttribute(Context.USERPASS, keyPassword);
            
            ctx.getAttribute(Context.USERID);
            proxyExpiresTime = System.currentTimeMillis() + getTime();
            
        } catch (NoSuccessException e) {
            throw new InternalProcessingError(e);
        } catch (NotImplementedException e) {
            throw new InternalProcessingError(e);
        } catch (AuthenticationFailedException e) {
            throw new InternalProcessingError(e);
        } catch (AuthorizationFailedException e) {
            throw new InternalProcessingError(e);
        } catch (PermissionDeniedException e) {
            throw new InternalProcessingError(e);
        } catch (IncorrectStateException e) {
            throw new InternalProcessingError(e);
        } catch (BadParameterException e) {
            throw new InternalProcessingError(e);
        } catch (DoesNotExistException e) {
            throw new InternalProcessingError(e);
        } catch (org.ogf.saga.error.TimeoutException e) {
            throw new InternalProcessingError(e);
        } catch (IOException e) {
            throw new InternalProcessingError(e);
{'fraction_non_alphanumeric': 0.09855570401476109, 'fraction_numerical': 0.004962779156327543, 'mean_word_length': 2.834593803366675, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, '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': '989697', 'n_tokens_mistral': 4335, 'n_tokens_neox': 4243, 'n_words': 905}