private static HmacAlgorithms getHmacAlgorithm(final String signatureScheme) { if (isBlank(signatureScheme)) { return null; } final String algo = StringUtils.remove(signatureScheme, '-'); for (final HmacAlgorithms ha : HmacAlgorithms.values()) { if (equalsIgnoreCase(ha.toString(), algo)) { return ha; } } throw new IllegalArgumentException("Unsupported signature scheme: " + signatureScheme); }
private static void testSessionFromUri(final URI shellUri, final byte[] hmacKey, final HmacAlgorithms hmacAlgorithm) throws IOException { LOGGER.info("\n\n*** CONNECT BY URI ***\n"); LOGGER.info("Attempting connection by URI: {}...", shellUri); try (Japyter japyter = Japyter.fromUri(shellUri, hmacKey, hmacAlgorithm) .withUserName("it-test2") .build()) { Validate.notNull(japyter.getControl()); Validate.notNull(japyter.getHeartbeat()); Validate.notNull(japyter.getIoPub()); Validate.notNull(japyter.getSession()); Validate.notNull(japyter.getShell()); Validate.isTrue(japyter.getStdin() == null); LOGGER.info("All good! Closing test session now...\n"); } }
/** * Creates a new {@link Builder} instance for configuring and instantiating a new * {@link Japyter} instance, which first retrieves all the connection information from a call ( * <code>connect_request</code> request) to the provided control or shell ZeroMQ router. * * @param controlOrShell a {@link URI} that points to either a shell or control ZeroMQ router. * @param hmacKey the request HMAC signing key, or null if request signature is disabled on the * kernel. * @param hmacAlgorithm the request HMAC signing algorithm, or null if request signature is * disabled on the kernel. * @throws IOException in case anything goes wrong when retrieving the network configuration. */ public static Builder fromUri(final URI controlOrShell, final byte[] hmacKey, final HmacAlgorithms hmacAlgorithm) throws IOException { LOGGER.info("Fetching connection information from: {}", notNull(controlOrShell, "controlOrShell can't be null")); final Protocol protocol = new Protocol(hmacKey, hmacAlgorithm); try (Session tempSession = new Session(Japyter.class.getName(), protocol, 3000, 1)) { final Shell tempShell = new Shell(controlOrShell.toString(), tempSession); final ConnectReply connectReply = tempShell.connect(); final Config config = new Config(); config.withControlPort(connectReply.getControl()) .withHbPort(connectReply.getHb()) .withIopubPort(connectReply.getIopub()) .withIp(controlOrShell.getHost()) .withKey(hmacKey != null ? new String(hmacKey, Protocol.ENCODING) : null) .withShellPort(connectReply.getShell()) .withSignatureScheme(hmacAlgorithm != null ? hmacAlgorithm.toString() : null) .withStdinPort(connectReply.getStdin()) .withTransport(controlOrShell.getScheme()); LOGGER.info("Connection information received: {}, losing temporary session", connectReply); return fromConfig(config); } }
public Protocol(final byte[] hmacKey, final HmacAlgorithms hmacAlgorithm) { this.hmacAlgorithm = hmacAlgorithm; this.hmacKey = ArrayUtils.clone(hmacKey); if (hmacAlgorithm != null && hmacKey != null) { LOGGER.info("HMAC signature enabled with: {}", hmacAlgorithm); } else { LOGGER.info("HMAC signature is disabled"); } }
public Protocol() { this(null, (HmacAlgorithms) null); }
public HmacAlgorithms getHmacAlgorithm() { return hmacAlgorithm; }
private static String computeHMAC(CustomerName customerName, String email, String billingAddress, Event event) { return new HmacUtils(HmacAlgorithms.HMAC_SHA_256, event.getPrivateKey()).hmacHex(StringUtils.trimToEmpty(customerName.getFullName()) + StringUtils.trimToEmpty(email) + StringUtils.trimToEmpty(billingAddress)); }
private static String hmacSHA256Base64(String key, String code) { return Base64.getEncoder().encodeToString(new HmacUtils(HmacAlgorithms.HMAC_SHA_256, key).hmac(code)); }
/** * do a HMac sha256 sign * * @param stringData * data as string * @param key * key * @return signature */ protected byte[] sign(final String stringData, final byte[] key) { return new HmacUtils(HmacAlgorithms.HMAC_SHA_256, key).hmac(stringData); }