Search in sources :

Example 1 with SshPublicKey

use of com.cloudera.thunderhead.service.usermanagement.UserManagementProto.SshPublicKey in project cloudbreak by hortonworks.

the class MockUserManagementService method getSshPublicKey.

private Optional<SshPublicKey> getSshPublicKey() {
    if (null != sshPublicKeyFilePath) {
        if (Files.exists(Paths.get(sshPublicKeyFilePath))) {
            try {
                String publicKey = Files.readString(Path.of(sshPublicKeyFilePath));
                if (publicKey.matches(SSH_PUBLIC_KEY_PATTERN)) {
                    byte[] keyData = Base64.getDecoder().decode(publicKey.trim().split(" ")[1]);
                    try {
                        MessageDigest digest = MessageDigest.getInstance("SHA-256");
                        byte[] keyDigest = digest.digest(keyData);
                        String fingerprint = Base64.getEncoder().encodeToString(keyDigest);
                        while (fingerprint.endsWith("=")) {
                            fingerprint = fingerprint.substring(0, fingerprint.length() - 1);
                        }
                        SshPublicKey sshPublicKey = SshPublicKey.newBuilder().setPublicKey(publicKey).setPublicKeyFingerprint(fingerprint).build();
                        LOGGER.info("Ssh public key file loaded for mocking");
                        return Optional.of(sshPublicKey);
                    } catch (NoSuchAlgorithmException ex) {
                        LOGGER.warn("Unable to calculate public ssh key fingerprint. Proceeding without ssh public key.", ex);
                    }
                } else {
                    LOGGER.warn("The provided ssh public key at path '{}' is invalid. It must be an RSA or ED25519 key." + "Proceeding without ssh public key.", sshPublicKeyFilePath);
                }
            } catch (IOException e) {
                LOGGER.warn("Unable to load ssh public key from '{}'. Proceeding without ssh public key", sshPublicKeyFilePath);
            }
        } else {
            LOGGER.warn("ssh public key not available at path '{}'. Proceeding without ssh public key", sshPublicKeyFilePath);
        }
    } else {
        LOGGER.warn("ssh public key file path not specified. Proceeding without ssh public key");
    }
    return Optional.empty();
}
Also used : SshPublicKey(com.cloudera.thunderhead.service.usermanagement.UserManagementProto.SshPublicKey) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) MessageDigest(java.security.MessageDigest)

Aggregations

SshPublicKey (com.cloudera.thunderhead.service.usermanagement.UserManagementProto.SshPublicKey)1 IOException (java.io.IOException)1 MessageDigest (java.security.MessageDigest)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1