|
Glassfish example source code file (SetupSshKey.java)
The Glassfish SetupSshKey.java source code/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright (c) 1997-2010 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.admin.cli.cluster; import java.io.*; import java.util.Arrays; import java.util.Map; import java.util.HashMap; import java.util.logging.Level; import org.jvnet.hk2.annotations.*; import org.jvnet.hk2.component.*; import org.glassfish.api.Param; import org.glassfish.api.admin.*; import org.glassfish.internal.api.Globals; import org.glassfish.cluster.ssh.launcher.SSHLauncher; import org.glassfish.cluster.ssh.util.SSHUtil; import com.sun.enterprise.universal.glassfish.TokenResolver; /** * This is a local command that distributes the SSH public key to remote node(s) * */ @Service(name = "setup-ssh") @Scoped(PerLookup.class) @ExecuteOn({RuntimeType.DAS}) public final class SetupSshKey extends SSHCommandsBase { @Param(optional = true) private String sshpublickeyfile; @Param(optional = true, defaultValue="false") private boolean generatekey; @Inject private Habitat habitat; public SetupSshKey() { // Create a resolver that can replace system properties in strings Map<String, String> systemPropsMap = new HashMap<String, String>((Map)(System.getProperties())); resolver = new TokenResolver(systemPropsMap); } /** */ @Override protected void validate() throws CommandException { Globals.setDefaultHabitat(habitat); sshuser = resolver.resolve(sshuser); if (sshkeyfile == null) { //if user hasn't specified a key file and there is no key file at default //location, then generate one String existingKey = SSHUtil.getExistingKeyFile(); if (existingKey == null) { sshkeyfile = SSHUtil.getDefaultKeyFile(); if(promptForKeyGeneration()) { generatekey=true; } } else { //there is a key that requires to be distributed, hence need password promptPass = true; sshkeyfile = existingKey; if(isEncryptedKey()) { sshkeypassphrase=getSSHPassphrase(false); } } } else { validateKeyFile(sshkeyfile); if(isEncryptedKey()) { sshkeypassphrase=getSSHPassphrase(false); } } if (sshpublickeyfile != null) { validateKeyFile(sshpublickeyfile); } } /** */ @Override protected int executeCommand() throws CommandException { SSHLauncher sshL=habitat.getComponent(SSHLauncher.class); String previousPassword = null; boolean status = false; for (String node : hosts) { sshL.init(sshuser, node, sshport, sshpassword, sshkeyfile, sshkeypassphrase, logger); if (generatekey || promptPass) { //prompt for password iff required if (sshkeyfile != null || SSHUtil.getExistingKeyFile() != null) { try { if(sshL.checkConnection()) { logger.info(Strings.get("SSHAlreadySetup", sshuser, node)); continue; } } catch (IOException ioe) { //ignore } } if (previousPassword != null) { status = sshL.checkPasswordAuth(); } if (!status) { sshpassword=getSSHPassword(node); previousPassword=sshpassword; } } try { sshL.setupKey(node, sshpublickeyfile, generatekey, sshpassword); } catch (IOException ce) { //logger.fine("SSH key setup failed: " + ce.getMessage()); throw new CommandException(Strings.get("KeySetupFailed", ce.getMessage())); } catch (Exception e) { //handle KeyStoreException } boolean res = false; try { res = sshL.checkConnection(); } catch (IOException ioe) { if (logger.isLoggable(Level.FINER)) { ioe.printStackTrace(); } } if (!res) { throw new CommandException(Strings.get("ConnFailed")); } } return SUCCESS; } /** * Method that sets the prompt flag only if key file exists * @param file the key file */ private void validateKeyFile(String file) throws CommandException { //if key exists, set prompt flag File f = new File(file); if (f.exists()) { promptPass=true; } else { throw new CommandException(Strings.get("KeyDoesNotExist", file)); } } /** * Prompt for key generation */ private boolean promptForKeyGeneration() { if (generatekey) return true; if (!programOpts.isInteractive()) return false; Console cons = System.console(); if (cons != null) { String val = null; do { cons.printf("%s", Strings.get("GenerateKeyPairPrompt", sshuser, Arrays.toString(hosts))); val = cons.readLine(); if (val != null && (val.equalsIgnoreCase("yes") || val.equalsIgnoreCase("y"))) { if(logger.isLoggable(Level.FINER)) { logger.finer("Generate key!"); } return true; } else if ( val != null && (val.equalsIgnoreCase("no") || val.equalsIgnoreCase("n"))) { break; } } while (val != null && !isValidAnswer(val)); } return false; } } Other Glassfish examples (source code examples)Here is a short list of links related to this Glassfish SetupSshKey.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
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.