示例#1
0
static gboolean
afmongodb_dd_connect(MongoDBDestDriver *self, gboolean reconnect)
{
  if (reconnect && self->conn)
    return TRUE;

  self->conn = mongo_sync_connect_recovery_cache(self->recovery_cache, FALSE);

  if (!self->conn)
    {
      msg_error ("Error connecting to MongoDB", evt_tag_str("driver", self->super.super.super.id), NULL);
      return FALSE;
    }

  mongo_connection_set_timeout((mongo_connection*) self->conn, SOCKET_TIMEOUT_FOR_MONGO_CONNECTION_IN_MILLISECS);

  mongo_sync_conn_set_safe_mode(self->conn, self->safe_mode);


  if (self->user || self->password)
    {
      if (!mongo_sync_cmd_authenticate (self->conn, self->db,
                                        self->user, self->password))
        {
          msg_error("MongoDB authentication failed", evt_tag_str("driver", self->super.super.super.id), NULL);
          return FALSE;
        }
    }

  return TRUE;
}
示例#2
0
static gboolean
afmongodb_dd_connect(MongoDBDestDriver *self, gboolean reconnect)
{
  GList *l;

  if (reconnect && self->conn)
    return TRUE;

  self->conn = mongo_sync_connect(self->host, self->port, FALSE);
  if (!self->conn)
    {
      msg_error ("Error connecting to MongoDB", NULL);
      return FALSE;
    }

  mongo_sync_conn_set_safe_mode(self->conn, self->safe_mode);

  l = self->servers;
  while ((l = g_list_next(l)) != NULL)
    {
      gchar *host = NULL;
      gint port = 27017;

      if (!mongo_util_parse_addr(l->data, &host, &port))
	{
	  msg_warning("Cannot parse MongoDB server address, ignoring",
		      evt_tag_str("address", l->data),
		      NULL);
	  continue;
	}
      mongo_sync_conn_seed_add (self->conn, host, port);
      msg_verbose("Added MongoDB server seed",
		  evt_tag_str("host", host),
		  evt_tag_int("port", port),
		  NULL);
      g_free(host);
    }

  /*
  if (self->user || self->password)
    {
      if (!self->user || !self->password)
	{
	  msg_error("Neither the username, nor the password can be empty", NULL);
	  return FALSE;
	}

      if (mongo_cmd_authenticate(&self->mongo_conn, self->db, self->user, self->password) != 1)
	{
	  msg_error("MongoDB authentication failed", NULL);
	  return FALSE;
	}
    }
  */

  return TRUE;
}
示例#3
0
void
test_func_mongo_sync_safe_mode (void)
{
  mongo_sync_connection *conn;
  const bson *docs[10];
  bson *b1, *b2, *b3, *b4, *cmd;
  mongo_packet *p;
  gchar *error;

  mongo_util_oid_init (0);

  b1 = bson_new ();
  bson_append_string (b1, "func_mongo_sync_safe_mode", "works", -1);
  bson_finish (b1);

  b2 = bson_new ();
  bson_append_int32 (b2, "int32", 1984);
  bson_finish (b2);

  b3 = test_bson_generate_full ();
  b4 = test_bson_generate_full ();

  docs[0] = b1;
  docs[1] = b2;
  docs[2] = b3;
  docs[3] = b4;

  conn = mongo_sync_connect (config.primary_host, config.primary_port,
			     FALSE);

  /* Test inserts */
  mongo_sync_conn_set_safe_mode (conn, FALSE);
  ok (mongo_sync_cmd_insert_n (conn, config.ns, 4, docs) == TRUE,
      "mongo_sync_cmd_insert_n() should not fail with safe mode off");

  mongo_sync_conn_set_safe_mode (conn, TRUE);
  ok (mongo_sync_cmd_insert_n (conn, config.ns, 4, docs) == FALSE,
      "mongo_sync_cmd_insert_n() should fail with safe mode on");

  /* Test a custom command */
  cmd = bson_new ();
  bson_append_int32 (cmd, "bogusCommand", 1);
  bson_finish (cmd);

  mongo_sync_cmd_reset_error (conn, config.db);
  mongo_sync_conn_set_safe_mode (conn, FALSE);
  p = mongo_sync_cmd_custom (conn, config.db, cmd);
  mongo_sync_cmd_get_last_error (conn, config.db, &error);
  ok (p == NULL && strcmp (error, "no such cmd: bogusCommand") == 0,
      "mongo_sync_cmd_custom() with a bogus command fails with safe-mode off");
  bson_free (cmd);

  cmd = bson_new ();
  bson_append_int32 (cmd, "bogusCommand2", 1);
  bson_finish (cmd);
  mongo_sync_cmd_reset_error (conn, config.db);
  mongo_sync_conn_set_safe_mode (conn, TRUE);
  p = mongo_sync_cmd_custom (conn, config.db, cmd);
  mongo_sync_cmd_get_last_error (conn, config.db, &error);
  ok (p == NULL && strcmp (error, "no such cmd: bogusCommand2") == 0,
      "mongo_sync_cmd_custom() with a bogus command fails with safe-mode on");
  bson_free (cmd);

  mongo_sync_disconnect (conn);
  bson_free (b1);
  bson_free (b2);
  bson_free (b3);
  bson_free (b4);
}