private static String getRoleName(final MongoCredential credential, final boolean readOnly) { return credential.getSource().equals("admin") ? (readOnly ? "readAnyDatabase" : "root") : (readOnly ? "read" : "dbOwner"); }
private void sendSaslStartAsync(final byte[] outToken, final InternalConnection connection, final SingleResultCallback<BsonDocument> callback) { executeCommandAsync(getMongoCredential().getSource(), createSaslStartCommandDocument(outToken), connection, callback); }
private void sendSaslContinueAsync(final BsonInt32 conversationId, final byte[] outToken, final InternalConnection connection, final SingleResultCallback<BsonDocument> callback) { executeCommandAsync(getMongoCredential().getSource(), createSaslContinueDocument(conversationId, outToken), connection, callback); }
private BsonDocument sendSaslContinue(final BsonInt32 conversationId, final byte[] outToken, final InternalConnection connection) { return executeCommand(getMongoCredential().getSource(), createSaslContinueDocument(conversationId, outToken), connection); }
private BsonDocument sendSaslStart(final byte[] outToken, final InternalConnection connection) { return executeCommand(getMongoCredential().getSource(), createSaslStartCommandDocument(outToken), connection); }
@Override void authenticateAsync(final InternalConnection connection, final ConnectionDescription connectionDescription, final SingleResultCallback<Void> callback) { executeCommandAsync(getMongoCredential().getSource(), getNonceCommand(), connection, new SingleResultCallback<BsonDocument>() { @Override public void onResult(final BsonDocument nonceResult, final Throwable t) { if (t != null) { callback.onResult(null, translateThrowable(t)); } else { executeCommandAsync(getMongoCredential().getSource(), getAuthCommand(getUserNameNonNull(), getPasswordNonNull(), ((BsonString) nonceResult.get("nonce")).getValue()), connection, new SingleResultCallback<BsonDocument>() { @Override public void onResult(final BsonDocument result, final Throwable t) { if (t != null) { callback.onResult(null, translateThrowable(t)); } else { callback.onResult(null, null); } } }); } } }); }
@Override public Void call(final Connection connection) { try { executeWrappedCommandProtocol(binding, getCredential().getSource(), getCommand(connection.getDescription()), connection, writeConcernErrorTransformer()); } catch (MongoCommandException e) { translateUserCommandException(e); } return null; } });
@Override public Void call(final Connection connection) { try { executeWrappedCommandProtocol(binding, getCredential().getSource(), getCommand(connection.getDescription()), connection, writeConcernErrorTransformer()); } catch (MongoCommandException e) { translateUserCommandException(e); } return null; } });
private BsonDocument createIsMasterCommand() { BsonDocument isMasterCommandDocument = new BsonDocument("ismaster", new BsonInt32(1)); isMasterCommandDocument.append("saslSupportedMechs", new BsonString(format("%s.%s", getMongoCredential().getSource(), getMongoCredential().getUserName()))); return isMasterCommandDocument; }
@Override public void authenticate(final InternalConnection connection, final ConnectionDescription connectionDescription) { try { BsonDocument nonceResponse = executeCommand(getMongoCredential().getSource(), getNonceCommand(), connection); BsonDocument authCommand = getAuthCommand(getUserNameNonNull(), getPasswordNonNull(), ((BsonString) nonceResponse.get("nonce")).getValue()); executeCommand(getMongoCredential().getSource(), authCommand, connection); } catch (MongoCommandException e) { throw new MongoSecurityException(getMongoCredential(), "Exception authenticating", e); } }
@Override public void call(final AsyncConnection connection, final Throwable t) { SingleResultCallback<Void> errHandlingCallback = errorHandlingCallback(callback, LOGGER); if (t != null) { errHandlingCallback.onResult(null, t); } else { final SingleResultCallback<Void> wrappedCallback = releasingCallback(errHandlingCallback, connection); executeWrappedCommandProtocolAsync(binding, credential.getSource(), getCommand(connection.getDescription()), connection, writeConcernErrorTransformer(), userCommandCallback(wrappedCallback)); } } });
@Override public void call(final AsyncConnection connection, final Throwable t) { SingleResultCallback<Void> errHandlingCallback = errorHandlingCallback(callback, LOGGER); if (t != null) { errHandlingCallback.onResult(null, t); } else { final SingleResultCallback<Void> wrappedCallback = releasingCallback(errHandlingCallback, connection); executeWrappedCommandProtocolAsync(binding, credential.getSource(), getCommand(connection.getDescription()), connection, writeConcernErrorTransformer(), userCommandCallback(wrappedCallback)); } } });
@Override void authenticate(final InternalConnection connection, final ConnectionDescription connectionDescription) { try { validateUserName(connectionDescription); BsonDocument authCommand = getAuthCommand(getMongoCredential().getUserName()); executeCommand(getMongoCredential().getSource(), authCommand, connection); } catch (MongoCommandException e) { throw new MongoSecurityException(getMongoCredential(), "Exception authenticating", e); } }
private BsonDocument createIsMasterCommand() { BsonDocument isMasterCommandDocument = new BsonDocument("ismaster", new BsonInt32(1)); if (clientMetadataDocument != null) { isMasterCommandDocument.append("client", clientMetadataDocument); } if (!requestedCompressors.isEmpty()) { BsonArray compressors = new BsonArray(); for (MongoCompressor cur : this.requestedCompressors) { compressors.add(new BsonString(cur.getName())); } isMasterCommandDocument.append("compression", compressors); } if (checkSaslSupportedMechs) { MongoCredential credential = authenticators.get(0).getMongoCredential(); isMasterCommandDocument.append("saslSupportedMechs", new BsonString(credential.getSource() + "." + credential.getUserName())); } return isMasterCommandDocument; }
@Override void authenticateAsync(final InternalConnection connection, final ConnectionDescription connectionDescription, final SingleResultCallback<Void> callback) { try { validateUserName(connectionDescription); executeCommandAsync(getMongoCredential().getSource(), getAuthCommand(getMongoCredential().getUserName()), connection, new SingleResultCallback<BsonDocument>() { @Override public void onResult(final BsonDocument nonceResult, final Throwable t) { if (t != null) { callback.onResult(null, translateThrowable(t)); } else { callback.onResult(null, null); } } }); } catch (Throwable t) { callback.onResult(null, t); } }
@Override public void onResult(final BsonDocument nonceResult, final Throwable t) { if (t != null) { callback.onResult(null, translateThrowable(t)); } else { executeCommandAsync(getMongoCredential().getSource(), getAuthCommand(getUserNameNonNull(), getPasswordNonNull(), ((BsonString) nonceResult.get("nonce")).getValue()), connection, new SingleResultCallback<BsonDocument>() { @Override public void onResult(final BsonDocument result, final Throwable t) { if (t != null) { callback.onResult(null, translateThrowable(t)); } else { callback.onResult(null, null); } } }); } } });
@Override public Map<String, ?> properties(RequestContext ctx) throws Exception { Map properties = new HashMap<>(); properties.put(MECHANISM, mongoCredential.getMechanism()); properties.put(USERNAME, mongoCredential.getUserName()); //only the MONGODB CR has a password and database option, the GSS one only accepts a username if (mongoCredential.getMechanism().equals(MongoCredential.MONGODB_CR_MECHANISM)) { properties.put(PASSWORD, new String(mongoCredential.getPassword())); properties.put(DB, mongoCredential.getSource()); } return properties; }
private void sendSaslContinueAsync(final BsonInt32 conversationId, final byte[] outToken, final InternalConnection connection, final SingleResultCallback<BsonDocument> callback) { executeCommandAsync(getMongoCredential().getSource(), createSaslContinueDocument(conversationId, outToken), connection, callback); }
@Override public Void call(final Connection connection) { try { executeWrappedCommandProtocol(binding, getCredential().getSource(), getCommand(connection.getDescription()), connection, writeConcernErrorTransformer()); } catch (MongoCommandException e) { translateUserCommandException(e); } return null; } });
@Override public void call(final AsyncConnection connection, final Throwable t) { SingleResultCallback<Void> errHandlingCallback = errorHandlingCallback(callback, LOGGER); if (t != null) { errHandlingCallback.onResult(null, t); } else { final SingleResultCallback<Void> wrappedCallback = releasingCallback(errHandlingCallback, connection); executeWrappedCommandProtocolAsync(binding, credential.getSource(), getCommand(connection.getDescription()), connection, writeConcernErrorTransformer(), userCommandCallback(wrappedCallback)); } } });