alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Glassfish example source code file (IdmService.java)

This example Glassfish source code file (IdmService.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Glassfish tags/keywords

as_admin_masterpassword, asmain, bufferedreader, bufferedreader, file, file, inject, inject, io, log, logging, masterpassword, password, service, ssl, string, string, util

The Glassfish IdmService.java source code

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2008-2011 Oracle and/or its affiliates. All rights reserved.
 *
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common Development
 * and Distribution License("CDDL") (collectively, the "License").  You
 * may not use this file except in compliance with the License.  You can
 * obtain a copy of the License at
 * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
 * or packager/legal/LICENSE.txt.  See the License for the specific
 * language governing permissions and limitations under the License.
 *
 * When distributing the software, include this License Header Notice in each
 * file and include the License file at packager/legal/LICENSE.txt.
 *
 * GPL Classpath Exception:
 * Oracle designates this particular file as subject to the "Classpath"
 * exception as provided by Oracle in the GPL Version 2 section of the License
 * file that accompanied this code.
 *
 * Modifications:
 * If applicable, add the following below the License Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyright [year] [name of copyright owner]"
 *
 * Contributor(s):
 * If you wish your version of this file to be governed by only the CDDL or
 * only the GPL Version 2, indicate your decision by adding "[Contributor]
 * elects to include this software in this distribution under the [CDDL or GPL
 * Version 2] license."  If you don't indicate a single choice of license, a
 * recipient has the option to distribute your version of this file under
 * either the CDDL, the GPL Version 2 or to extend the choice of license to
 * its licensees as provided above.  However, if you add GPL Version 2 code
 * and therefore, elected the GPL Version 2 license, then the option applies
 * only if the new code is made subject to such option by the copyright
 * holder.
 */

package com.sun.enterprise.v3.admin;

import com.sun.enterprise.glassfish.bootstrap.StartupContextUtil;
import com.sun.enterprise.module.bootstrap.StartupContext;
import com.sun.enterprise.security.store.PasswordAdapter;
import org.glassfish.internal.api.Init;
import org.glassfish.security.common.MasterPassword;
import org.glassfish.server.ServerEnvironmentImpl;
import org.jvnet.hk2.annotations.Inject;
import org.jvnet.hk2.annotations.Scoped;
import org.jvnet.hk2.annotations.Service;
import org.jvnet.hk2.component.PostConstruct;
import org.jvnet.hk2.component.Singleton;

import java.io.*;
import java.util.Arrays;
import java.util.Properties;
import java.util.logging.Logger;

/** An implementation of the @link {IdentityManagement} that manages the password needs of the server.
 *  This implementation consults the Java KeyStore and assumes that the stores are available in server's
 *  configuration area.
 * @author केदार (km@dev.java.net)
 */
@Service(name="jks-based")
@Scoped(Singleton.class)
public class IdmService implements Init, PostConstruct/*, IdentityManagement*/ {

    private final Logger logger = Logger.getAnonymousLogger();

    @Inject
    private volatile StartupContext sc = null;

    @Inject
    private volatile ServerEnvironmentImpl env = null;

    @Inject(name="Security SSL Password Provider Service", optional=true)
    private MasterPassword masterPasswordHelper=null;

    @Inject(name="JMX SSL Password Provider Service", optional=true)
    private MasterPassword jmxMasterPasswordHelper=null;

    private volatile char[] masterPassword;

    //private final String[] masterPasswordServices = {"Security SSL Password Provider Service"};

    //private final Map<String, String> otherPasswords = Collections.synchronizedMap(new HashMap());  //TODO later

    private static final String FIXED_KEY = "master-password"; //the fixed key for master-password file
    private static final String PASSWORDFILE_OPTION_TO_ASMAIN = "-passwordfile"; //note single hyphen, in line with other args to ASMain!
    private static final String STDIN_OPTION_TO_ASMAIN        = "-read-stdin"; //note single hyphen, in line with other args to ASMain!

    private static final String MP_PROPERTY = "AS_ADMIN_MASTERPASSWORD";

    public void postConstruct() {
        boolean success;
        boolean readStdin = sc.getArguments().containsKey(STDIN_OPTION_TO_ASMAIN);
        if (readStdin) {
            success = setFromStdin();
        } else {
            success = setFromMasterPasswordFile();
            if (!success) {
                success = setFromAsMainArguments();
            }
        }
        if (!success) {
            masterPassword = "changeit".toCharArray(); //the default;
        }
        //success = verify();            //See 9592 for details. This saves some time
        //if (!success)
            //logger.warning("THIS SHOULD BE FIXED, IN EMBEDDED CASE, THERE IS NO MASTER PASSWORD SET OR KEYSTORE DOES NOT EXIST ...");

        if (masterPasswordHelper!=null)
            masterPasswordHelper.setMasterPassword(masterPassword);

        if (jmxMasterPasswordHelper != null)
          jmxMasterPasswordHelper.setMasterPassword(masterPassword);

        Arrays.fill(masterPassword, ' ');
        masterPassword = null;
    }

    ///// All Private
    
    private boolean setFromMasterPasswordFile() {
//        long t0 = System.currentTimeMillis();
        try {
            File mp = env.getMasterPasswordFile();
            if (!mp.isFile()) {
                logger.fine("The JCEKS file: " + mp.getAbsolutePath() + " does not exist, master password was not saved on disk during domain creation");
                return false;
            }
            PasswordAdapter p   = new PasswordAdapter(mp.getAbsolutePath(), FIXED_KEY.toCharArray());
            this.masterPassword = p.getPasswordForAlias(FIXED_KEY).toCharArray();
//            long t1 = System.currentTimeMillis();
//            System.out.println("time spent in setFromMasterPasswordFile(): " + (t1-t0) + " ms");
            if (masterPassword == null) {
                return false;
            }
            return true;
        } catch (Exception ex) {
            logger.fine("Error in master-password processing: " + ex.getMessage());
            return false;
        }

    }

    private boolean setFromAsMainArguments() {
        File pwf = null;
        try {
            String[] args = StartupContextUtil.getOriginalArguments(sc);
            int index = 0;
            for (String arg : args) {
                if (PASSWORDFILE_OPTION_TO_ASMAIN.equals(arg)) {
                    if (index == (args.length-1)) {  //-passwordfile is the last argument
                        logger.warning("-passwordfile specified, but the actual file was not, ignoring ...");
                        return false;
                    }
                    pwf = new File(args[index+1]);
                    return readPasswordFile(pwf);
                }
                index++;
            }
            //no -passwordfile found
            return false;
        } catch (Exception ex) {
            String s = "Something wrong with given password file: ";
            String msg = pwf == null ? s : s + pwf.getAbsolutePath();
            logger.fine(msg);
            return false;
        }
    }

    private boolean readPasswordFile(File pwf) {
        Properties p = new Properties();
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(pwf));
            p.load(br);
            if (p.getProperty(MP_PROPERTY) == null)
                return false;
            masterPassword = p.getProperty(MP_PROPERTY).toCharArray();  //this would stay in memory, so this needs some security audit, frankly
            return true;
        } catch (IOException e) {
            logger.fine("Passwordfile: " + pwf.getAbsolutePath() + " (a simple property file) could not be processed, ignoring ...");
            return false;
        } finally {
            try {
                if (br != null)
                    br.close();
            } catch(Exception e) {
                // ignore, I know
            }
        }
    }

    private boolean setFromStdin() {
        logger.fine("Reading the master password from stdin> ");
        String s;
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            while ((s = br.readLine()) != null) {
                int ind = s.indexOf(MP_PROPERTY);
                if (ind == -1) {
                    return false; // this means stdin isn't behaving. That's bad and shouldn't happen.
                }
                masterPassword = s.substring(MP_PROPERTY.length() + 1).toCharArray(); //begIndex is that of "AS_ADMIN_MASTERPASSWORD=; consider trailing '='
            }
            // We don't want reveal the master password in the logs.
            //logger.fine("******************* Password from stdin: " + new String(masterPassword));
            if (masterPassword == null) {
                return false;
            }
            return true;
        } catch(Exception e) {
            logger.fine("Stdin isn't behaving, ignoring it ..." + e.getMessage());
            return false;
        }
    }

//    private boolean verify() {
////        long t0 = System.currentTimeMillis();
//        //only tries to open the keystore
//        FileInputStream fis = null;
//        try {
//            fis = new FileInputStream(env.getJKS());
//            KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
//            ks.load(fis, masterPassword);
////            long t1 = System.currentTimeMillis();
////            System.out.println("time spent in verify(): " + (t1-t0) + " ms");
//            return true;
//        } catch (Exception e) {
//            logger.warning(e.getMessage());
//            return false;
//        } finally {
//            try {
//                if (fis != null)
//                    fis.close();
//            } catch(IOException ioe) {
//                //ignore, I know ...
//            }
//        }
//    }
}

Other Glassfish examples (source code examples)

Here is a short list of links related to this Glassfish IdmService.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.