use of com.google.spanner.v1.Session in project pentaho-kettle by pentaho.
the class SSH method processRow.
@Override
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {
meta = (SSHMeta) smi;
data = (SSHData) sdi;
Object[] row;
if (meta.isDynamicCommand()) {
row = getRow();
if (row == null) {
setOutputDone();
return false;
}
if (first) {
first = false;
data.outputRowMeta = getInputRowMeta().clone();
data.nrInputFields = data.outputRowMeta.size();
meta.getFields(data.outputRowMeta, getStepname(), null, null, this, repository, metaStore);
data.nrOutputFields = data.outputRowMeta.size();
// Check if commands field is provided
if (meta.isDynamicCommand()) {
if (Utils.isEmpty(meta.getcommandfieldname())) {
throw new KettleException(BaseMessages.getString(PKG, "SSH.Error.CommandFieldMissing"));
}
// cache the position of the source filename field
data.indexOfCommand = data.outputRowMeta.indexOfValue(meta.getcommandfieldname());
if (data.indexOfCommand < 0) {
// The field is unreachable !
throw new KettleException(BaseMessages.getString(PKG, "SSH.Exception.CouldnotFindField", meta.getcommandfieldname()));
}
}
}
} else {
if (!data.wroteOneRow) {
// empty row
row = new Object[] {};
incrementLinesRead();
data.wroteOneRow = true;
if (first) {
first = false;
data.outputRowMeta = new RowMeta();
data.nrInputFields = 0;
meta.getFields(data.outputRowMeta, getStepname(), null, null, this, repository, metaStore);
data.nrOutputFields = data.outputRowMeta.size();
data.commands = environmentSubstitute(meta.getCommand());
}
} else {
// signal end to receiver(s)
setOutputDone();
return false;
}
}
RowMetaInterface imeta = getInputRowMeta();
if (imeta == null) {
imeta = new RowMeta();
this.setInputRowMeta(imeta);
}
// Reserve room
Object[] rowData = new Object[data.nrOutputFields];
for (int i = 0; i < data.nrInputFields; i++) {
// no data is changed, clone is not needed here.
rowData[i] = row[i];
}
int index = data.nrInputFields;
Session session = null;
try {
if (meta.isDynamicCommand()) {
// get commands
data.commands = data.outputRowMeta.getString(row, data.indexOfCommand);
if (Utils.isEmpty(data.commands)) {
throw new KettleException(BaseMessages.getString(PKG, "SSH.Error.MessageEmpty"));
}
}
// Open a session
session = data.conn.openSession();
if (log.isDebug()) {
logDebug(BaseMessages.getString(PKG, "SSH.Log.SessionOpened"));
}
// execute commands
if (log.isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "SSH.Log.RunningCommand", data.commands));
}
session.execCommand(data.commands);
// Read Stdout, Sterr and exitStatus
SessionResult sessionresult = new SessionResult(session);
if (log.isDebug()) {
logDebug(BaseMessages.getString(PKG, "SSH.Log.CommandRunnedCommand", data.commands, sessionresult.getStdOut(), sessionresult.getStdErr()));
}
// Add stdout to output
rowData[index++] = sessionresult.getStd();
if (!Utils.isEmpty(data.stdTypeField)) {
// Add stdtype to output
rowData[index++] = sessionresult.isStdTypeErr();
}
if (log.isRowLevel()) {
logRowlevel(BaseMessages.getString(PKG, "SSH.Log.OutputLine", data.outputRowMeta.getString(rowData)));
}
putRow(data.outputRowMeta, rowData);
if (checkFeedback(getLinesRead())) {
if (log.isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "SSH.LineNumber", "" + getLinesRead()));
}
}
} catch (Exception e) {
boolean sendToErrorRow = false;
String errorMessage = null;
if (getStepMeta().isDoingErrorHandling()) {
sendToErrorRow = true;
errorMessage = e.toString();
} else {
logError(BaseMessages.getString(PKG, "SSH.ErrorInStepRunning") + e.getMessage());
setErrors(1);
stopAll();
// signal end to receiver(s)
setOutputDone();
return false;
}
if (sendToErrorRow) {
// Simply add this row to the error row
putError(getInputRowMeta(), row, 1, errorMessage, null, "SSH001");
}
} finally {
if (session != null) {
session.close();
if (log.isDebug()) {
logDebug(BaseMessages.getString(PKG, "SSH.Log.SessionClosed"));
}
}
}
return true;
}
use of com.google.spanner.v1.Session in project warn-report by saaavsaaa.
the class LinuxExecUtil method execute.
public void execute(final String commandText) {
try {
System.out.println("exec : " + commandText);
Session sess = conn.openSession();
sess.execCommand(commandText);
System.out.println("Here is some information about the remote host:");
InputStream standardOut = new StreamGobbler(sess.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(standardOut));
while (true) {
String line = br.readLine();
if (line == null) {
break;
}
System.out.println(line);
}
System.out.println("ExitCode: " + sess.getExitStatus());
sess.close();
} catch (IOException e) {
e.printStackTrace(System.err);
System.exit(2);
}
}
use of com.google.spanner.v1.Session in project Payara by payara.
the class SSHLauncher method runCommandAsIs.
private int runCommandAsIs(String command, OutputStream os, List<String> stdinLines) throws IOException, InterruptedException {
if (logger.isLoggable(Level.FINER)) {
logger.finer("Running command " + command + " on host: " + this.host);
}
openConnection();
final Session sess = connection.openSession();
int status = exec(sess, command, os, listInputStream(stdinLines));
// XXX: Should we close connection after each command or cache it
// and re-use it?
SSHUtil.unregister(connection);
connection = null;
return status;
}
use of com.google.spanner.v1.Session in project xipki by xipki.
the class IaikP11Slot method refresh0.
@Override
protected P11SlotRefreshResult refresh0() throws P11TokenException {
Mechanism[] mechanisms;
try {
mechanisms = slot.getToken().getMechanismList();
} catch (TokenException ex) {
throw new P11TokenException("could not getMechanismList: " + ex.getMessage(), ex);
}
P11SlotRefreshResult ret = new P11SlotRefreshResult();
if (mechanisms != null) {
for (Mechanism mech : mechanisms) {
ret.addMechanism(mech.getMechanismCode());
}
}
ConcurrentBagEntry<Session> session = borrowSession();
try {
// secret keys
List<SecretKey> secretKeys = getAllSecretKeyObjects(session.value());
for (SecretKey secKey : secretKeys) {
byte[] keyId = secKey.getId().getByteArrayValue();
if (keyId == null || keyId.length == 0) {
continue;
}
analyseSingleKey(secKey, ret);
}
// first get the list of all CA certificates
List<X509PublicKeyCertificate> p11Certs = getAllCertificateObjects(session.value());
for (X509PublicKeyCertificate p11Cert : p11Certs) {
P11ObjectIdentifier objId = new P11ObjectIdentifier(p11Cert.getId().getByteArrayValue(), toString(p11Cert.getLabel()));
ret.addCertificate(objId, parseCert(p11Cert));
}
List<PrivateKey> privKeys = getAllPrivateObjects(session.value());
for (PrivateKey privKey : privKeys) {
byte[] keyId = privKey.getId().getByteArrayValue();
if (keyId == null || keyId.length == 0) {
break;
}
try {
analyseSingleKey(session.value(), privKey, ret);
} catch (XiSecurityException ex) {
LogUtil.error(LOG, ex, "XiSecurityException while initializing private key " + "with id " + hex(keyId));
continue;
} catch (Throwable th) {
String label = "";
if (privKey.getLabel() != null) {
label = new String(privKey.getLabel().getCharArrayValue());
}
LOG.error("unexpected exception while initializing private key with id " + hex(keyId) + " and label " + label, th);
continue;
}
}
return ret;
} finally {
sessions.requite(session);
}
}
use of com.google.spanner.v1.Session in project xipki by xipki.
the class IaikP11Slot method generateSecretKey0.
@Override
protected P11Identity generateSecretKey0(long keyType, int keysize, String label, P11NewKeyControl control) throws P11TokenException {
if (keysize % 8 != 0) {
throw new IllegalArgumentException("keysize is not multiple of 8: " + keysize);
}
long mech;
if (PKCS11Constants.CKK_AES == keyType) {
mech = PKCS11Constants.CKM_AES_KEY_GEN;
} else if (PKCS11Constants.CKK_DES3 == keyType) {
mech = PKCS11Constants.CKM_DES3_KEY_GEN;
} else if (PKCS11Constants.CKK_GENERIC_SECRET == keyType) {
mech = PKCS11Constants.CKM_GENERIC_SECRET_KEY_GEN;
} else if (PKCS11Constants.CKK_SHA_1_HMAC == keyType || PKCS11Constants.CKK_SHA224_HMAC == keyType || PKCS11Constants.CKK_SHA256_HMAC == keyType || PKCS11Constants.CKK_SHA384_HMAC == keyType || PKCS11Constants.CKK_SHA512_HMAC == keyType || PKCS11Constants.CKK_SHA3_224_HMAC == keyType || PKCS11Constants.CKK_SHA3_256_HMAC == keyType || PKCS11Constants.CKK_SHA3_384_HMAC == keyType || PKCS11Constants.CKK_SHA3_512_HMAC == keyType) {
mech = PKCS11Constants.CKM_GENERIC_SECRET_KEY_GEN;
} else {
throw new IllegalArgumentException("unsupported key type 0x" + Functions.toFullHex((int) keyType));
}
assertMechanismSupported(mech);
ValuedSecretKey template = new ValuedSecretKey(keyType);
template.getToken().setBooleanValue(true);
template.getLabel().setCharArrayValue(label.toCharArray());
template.getSign().setBooleanValue(true);
template.getSensitive().setBooleanValue(true);
template.getExtractable().setBooleanValue(control.isExtractable());
template.getValueLen().setLongValue((long) (keysize / 8));
Mechanism mechanism = Mechanism.get(mech);
SecretKey key;
Session session = borrowWritableSession();
try {
if (labelExists(session, label)) {
throw new IllegalArgumentException("label " + label + " exists, please specify another one");
}
byte[] id = generateKeyId(session);
template.getId().setByteArrayValue(id);
try {
key = (SecretKey) session.generateKey(mechanism, template);
} catch (TokenException ex) {
throw new P11TokenException("could not generate generic secret key using " + mechanism.getName(), ex);
}
P11ObjectIdentifier objId = new P11ObjectIdentifier(id, label);
P11EntityIdentifier entityId = new P11EntityIdentifier(slotId, objId);
return new IaikP11Identity(this, entityId, key);
} finally {
returnWritableSession(session);
}
}
Aggregations