Skip to content

Commit

Permalink
Make server URL file object local
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjala committed Oct 24, 2023
1 parent ba9220a commit 9872181
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 189 deletions.
129 changes: 14 additions & 115 deletions src/rest_vol.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,6 @@ char curl_err_buf[CURL_ERROR_SIZE];
*/
struct curl_slist *curl_headers = NULL;

/*
* Saved copy of the base URL for operating on
*/
char *base_URL = NULL;

#ifdef RV_TRACK_MEM_USAGE
/*
* Counter to keep track of the currently allocated amount of bytes
Expand Down Expand Up @@ -153,7 +148,7 @@ const char *server_version_keys[] = {"version", (const char *)0};
static herr_t H5_rest_init(hid_t vipl_id);
static herr_t H5_rest_term(void);

static herr_t H5_rest_authenticate_with_AD(H5_rest_ad_info_t *ad_info);
static herr_t H5_rest_authenticate_with_AD(H5_rest_ad_info_t *ad_info, const char *base_URL);

/* Introspection callbacks */
static herr_t H5_rest_get_conn_cls(void *obj, H5VL_get_conn_lvl_t lvl, const struct H5VL_class_t **conn_cls);
Expand Down Expand Up @@ -600,12 +595,6 @@ H5_rest_term(void)
if (!H5_rest_initialized_g)
FUNC_GOTO_DONE(SUCCEED);

/* Free base URL */
if (base_URL) {
RV_free(base_URL);
base_URL = NULL;
}

/* Free memory for cURL response buffer */
if (response_buffer.buffer) {
RV_free(response_buffer.buffer);
Expand Down Expand Up @@ -709,13 +698,12 @@ herr_t
H5_rest_set_connection_information(server_info_t *server_info)
{
H5_rest_ad_info_t ad_info;
const char *URL;
size_t URL_len = 0;
FILE *config_file = NULL;
herr_t ret_value = SUCCEED;

const char *username = NULL;
const char *password = NULL;
const char *base_URL = NULL;

memset(&ad_info, 0, sizeof(ad_info));

Expand All @@ -724,70 +712,12 @@ H5_rest_set_connection_information(server_info_t *server_info)
* the environment.
*/

if ((URL = getenv("HSDS_ENDPOINT"))) {

if (!strncmp(URL, UNIX_SOCKET_PREFIX, strlen(UNIX_SOCKET_PREFIX))) {
/* This is just a placeholder URL for curl's syntax, its specific value is unimportant */
URL = "0";
URL_len = 1;

if (!base_URL || (0 != (strncmp(base_URL, URL, strlen(URL))))) {

/* If previous value is incorrect, reassign */
if (base_URL) {
free(base_URL);
base_URL = NULL;
}

if (NULL == (base_URL = (char *)RV_malloc(URL_len + 1)))
FUNC_GOTO_ERROR(H5E_VOL, H5E_CANTALLOC, FAIL,
"can't allocate space necessary for placeholder base URL");

strncpy(base_URL, URL, URL_len);
base_URL[URL_len] = '\0';
}
}
else {
/*
* Save a copy of the base URL being worked on so that operations like
* creating a Group can be redirected to "base URL"/groups by building
* off of the base URL supplied.
*/
URL_len = strlen(URL);

if (!base_URL || (0 != (strncmp(base_URL, URL, strlen(URL))))) {

/* If previous value is incorrect, reassign */
if (base_URL) {
free(base_URL);
base_URL = NULL;
}

if (NULL == (base_URL = (char *)RV_malloc(URL_len + 1)))
FUNC_GOTO_ERROR(H5E_VOL, H5E_CANTALLOC, FAIL,
"can't allocate space necessary for placeholder base URL");

strncpy(base_URL, URL, URL_len);
base_URL[URL_len] = '\0';
}
}
if (base_URL = getenv("HSDS_ENDPOINT")) {

username = getenv("HSDS_USERNAME");
password = getenv("HSDS_PASSWORD");

if (username || password) {
/* Attempt to set authentication information */
if (username && strlen(username)) {
if (CURLE_OK != curl_easy_setopt(curl, CURLOPT_USERNAME, username))
FUNC_GOTO_ERROR(H5E_ARGS, H5E_CANTSET, FAIL, "can't set username: %s", curl_err_buf);
} /* end if */

if (password && strlen(password)) {
if (CURLE_OK != curl_easy_setopt(curl, CURLOPT_PASSWORD, password))
FUNC_GOTO_ERROR(H5E_ARGS, H5E_CANTSET, FAIL, "can't set password: %s", curl_err_buf);
} /* end if */
} /* end if */
else {
if (!username && !password) {
const char *clientID = getenv("HSDS_AD_CLIENT_ID");
const char *tenantID = getenv("HSDS_AD_TENANT_ID");
const char *resourceID = getenv("HSDS_AD_RESOURCE_ID");
Expand All @@ -805,7 +735,7 @@ H5_rest_set_connection_information(server_info_t *server_info)
} /* end if */

/* Attempt authentication with Active Directory */
if (H5_rest_authenticate_with_AD(&ad_info) < 0)
if (H5_rest_authenticate_with_AD(&ad_info, base_URL) < 0)
FUNC_GOTO_ERROR(H5E_VOL, H5E_CANTINIT, FAIL, "can't authenticate with Active Directory");
} /* end else */
} /* end if */
Expand Down Expand Up @@ -881,43 +811,9 @@ H5_rest_set_connection_information(server_info_t *server_info)

if (!strcmp(key, "hs_endpoint")) {
if (val) {
/*
* Save a copy of the base URL being worked on so that operations like
* creating a Group can be redirected to "base URL"/groups by building
* off of the base URL supplied.
*/
URL_len = strlen(val);

if (!base_URL || (0 != (strncmp(base_URL, val, URL_len)))) {

/* If previous value is incorrect, reassign */
if (base_URL) {
free(base_URL);
base_URL = NULL;
}

if (NULL == (base_URL = (char *)RV_malloc(URL_len + 1)))
FUNC_GOTO_ERROR(H5E_VOL, H5E_CANTALLOC, FAIL,
"can't allocate space necessary for placeholder base URL");

strncpy(base_URL, val, URL_len);
base_URL[URL_len] = '\0';
}

base_URL = val;
} /* end if */
} /* end if */
else if (!strcmp(key, "hs_username")) {
if (val && strlen(val)) {
if (CURLE_OK != curl_easy_setopt(curl, CURLOPT_USERNAME, val))
FUNC_GOTO_ERROR(H5E_ARGS, H5E_CANTSET, FAIL, "can't set username: %s", curl_err_buf);
} /* end if */
} /* end else if */
else if (!strcmp(key, "hs_password")) {
if (val && strlen(val)) {
if (CURLE_OK != curl_easy_setopt(curl, CURLOPT_PASSWORD, val))
FUNC_GOTO_ERROR(H5E_ARGS, H5E_CANTSET, FAIL, "can't set password: %s", curl_err_buf);
} /* end if */
} /* end else if */
else if (!strcmp(key, "hs_ad_app_id")) {
if (val && strlen(val))
strncpy(ad_info.clientID, val, sizeof(ad_info.clientID) - 1);
Expand All @@ -940,7 +836,7 @@ H5_rest_set_connection_information(server_info_t *server_info)

/* Attempt authentication with Active Directory if ID values are present */
if (ad_info.clientID[0] != '\0' && ad_info.tenantID[0] != '\0' && ad_info.resourceID[0] != '\0')
if (H5_rest_authenticate_with_AD(&ad_info) < 0)
if (!base_URL || (H5_rest_authenticate_with_AD(&ad_info, base_URL) < 0))
FUNC_GOTO_ERROR(H5E_VOL, H5E_CANTINIT, FAIL, "can't authenticate with Active Directory");
} /* end else */

Expand All @@ -965,8 +861,9 @@ H5_rest_set_connection_information(server_info_t *server_info)
strcpy(server_info->password, password);
}

if ((server_info->base_URL = RV_calloc(strlen(base_URL) + 1)) == NULL) {
FUNC_GOTO_ERROR(H5E_FILE, H5E_CANTALLOC, FAIL, "can't allocate space for URL");
if (base_URL) {
if ((server_info->base_URL = RV_calloc(strlen(base_URL) + 1)) == NULL)
FUNC_GOTO_ERROR(H5E_FILE, H5E_CANTALLOC, FAIL, "can't allocate space for URL");

strcpy(server_info->base_URL, base_URL);
}
Expand Down Expand Up @@ -1010,7 +907,7 @@ H5_rest_set_connection_information(server_info_t *server_info)
*-------------------------------------------------------------------------
*/
static herr_t
H5_rest_authenticate_with_AD(H5_rest_ad_info_t *ad_info)
H5_rest_authenticate_with_AD(H5_rest_ad_info_t *ad_info, const char *base_URL)
{
const char *access_token_key[] = {"access_token", (const char *)0};
const char *refresh_token_key[] = {"refresh_token", (const char *)0};
Expand Down Expand Up @@ -2076,6 +1973,7 @@ RV_find_object_by_path(RV_object_t *parent_obj, const char *obj_path, H5I_type_t
char *url_encoded_path_name = NULL;
const char *ext_filename = NULL;
const char *ext_obj_path = NULL;
const char *base_URL;
char request_url[URL_MAX_LENGTH];
long http_response;
int url_len = 0;
Expand All @@ -2092,7 +1990,8 @@ RV_find_object_by_path(RV_object_t *parent_obj, const char *obj_path, H5I_type_t
if (H5I_FILE != parent_obj->obj_type && H5I_GROUP != parent_obj->obj_type &&
H5I_DATATYPE != parent_obj->obj_type && H5I_DATASET != parent_obj->obj_type)
FUNC_GOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "parent object not a file, group, datatype or dataset");

if ((base_URL = parent_obj->domain->u.file.server_info.base_URL) == NULL)
FUNC_GOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "parent object does not have valid server URL");
#ifdef RV_CONNECTOR_DEBUG
printf("-> Finding object by path '%s' from parent object of type %s with URI %s\n\n", obj_path,
object_type_to_string(parent_obj->obj_type), parent_obj->URI);
Expand Down
15 changes: 5 additions & 10 deletions src/rest_vol.h
Original file line number Diff line number Diff line change
Expand Up @@ -375,11 +375,6 @@ extern struct curl_slist *curl_headers;
*/
#define CURL_RESPONSE_BUFFER_DEFAULT_SIZE 1024

/*
* Saved copy of the base URL for operating on
*/
extern char *base_URL;

#ifdef RV_TRACK_MEM_USAGE
/*
* Counter to keep track of the currently allocated amount of bytes
Expand Down Expand Up @@ -490,7 +485,8 @@ typedef struct server_api_version {
size_t patch;
} server_api_version;

// TODO
/* Structure containing information to connect to and evaluate
* features of a server */
typedef struct server_info_t {
char *username;
char *password;
Expand All @@ -507,10 +503,9 @@ typedef struct server_info_t {
typedef struct RV_object_t RV_object_t;

typedef struct RV_file_t {
unsigned intent;
unsigned ref_count;
char *filepath_name;
/* TODO - Username/password in global curl handle are still used for now */
unsigned intent;
unsigned ref_count;
char *filepath_name;
server_info_t server_info;
hid_t fcpl_id;
hid_t fapl_id;
Expand Down
24 changes: 24 additions & 0 deletions src/rest_vol_attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ RV_attr_create(void *obj, const H5VL_loc_params_t *loc_params, const char *attr_
char *url_encoded_attr_name = NULL;
int create_request_body_len = 0;
int url_len = 0;
const char *base_URL = NULL;
void *ret_value = NULL;

#ifdef RV_CONNECTOR_DEBUG
Expand Down Expand Up @@ -97,6 +98,9 @@ RV_attr_create(void *obj, const H5VL_loc_params_t *loc_params, const char *attr_
H5I_DATASET != parent->obj_type)
FUNC_GOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "parent object not a file, group, datatype or dataset");

if ((base_URL = parent->domain->u.file.server_info.base_URL) == NULL)
FUNC_GOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "parent object does not have valid server URL");

/* Check for write access */
if (!(parent->domain->u.file.intent & H5F_ACC_RDWR))
FUNC_GOTO_ERROR(H5E_FILE, H5E_BADVALUE, NULL, "no write intent on file");
Expand Down Expand Up @@ -434,6 +438,7 @@ RV_attr_open(void *obj, const H5VL_loc_params_t *loc_params, const char *attr_na
char request_url[URL_MAX_LENGTH];
char *url_encoded_attr_name = NULL;
const char *parent_obj_type_header = NULL;
const char *base_URL = NULL;
int url_len = 0;
void *ret_value = NULL;

Expand Down Expand Up @@ -467,6 +472,9 @@ RV_attr_open(void *obj, const H5VL_loc_params_t *loc_params, const char *attr_na
H5I_DATASET != parent->obj_type)
FUNC_GOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "parent object not a file, group, datatype or dataset");

if ((base_URL = parent->domain->u.file.server_info.base_URL) == NULL)
FUNC_GOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "parent object does not have valid server URL");

if (aapl_id == H5I_INVALID_HID)
FUNC_GOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "invalid AAPL");

Expand Down Expand Up @@ -831,6 +839,7 @@ RV_attr_read(void *attr, hid_t dtype_id, void *buf, hid_t dxpl_id, void **req)
char *host_header = NULL;
char *url_encoded_attr_name = NULL;
char request_url[URL_MAX_LENGTH];
const char *base_URL = NULL;
int url_len = 0;
herr_t ret_value = SUCCEED;

Expand All @@ -847,6 +856,9 @@ RV_attr_read(void *attr, hid_t dtype_id, void *buf, hid_t dxpl_id, void **req)
if (!buf)
FUNC_GOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "read buffer was NULL");

if ((base_URL = attribute->domain->u.file.server_info.base_URL) == NULL)
FUNC_GOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "attribute does not have valid server URL");

/* Determine whether it's possible to receive the data as a binary blob instead of as JSON */
if (H5T_NO_CLASS == (dtype_class = H5Tget_class(dtype_id)))
FUNC_GOTO_ERROR(H5E_DATATYPE, H5E_BADVALUE, FAIL, "memory datatype is invalid");
Expand Down Expand Up @@ -1024,6 +1036,7 @@ RV_attr_write(void *attr, hid_t dtype_id, const void *buf, hid_t dxpl_id, void *
char *host_header = NULL;
char *url_encoded_attr_name = NULL;
char request_url[URL_MAX_LENGTH];
const char *base_URL = NULL;
int url_len = 0;
herr_t ret_value = SUCCEED;

Expand All @@ -1040,6 +1053,9 @@ RV_attr_write(void *attr, hid_t dtype_id, const void *buf, hid_t dxpl_id, void *
if (!buf)
FUNC_GOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "write buffer was NULL");

if ((base_URL = attribute->domain->u.file.server_info.base_URL) == NULL)
FUNC_GOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "attribute does not have valid server URL");

/* Check for write access */
if (!(attribute->domain->u.file.intent & H5F_ACC_RDWR))
FUNC_GOTO_ERROR(H5E_FILE, H5E_BADVALUE, FAIL, "no write intent on file");
Expand Down Expand Up @@ -1237,6 +1253,7 @@ RV_attr_get(void *obj, H5VL_attr_get_args_t *args, hid_t dxpl_id, void **req)
int url_len = 0;
const char *parent_obj_type_header = NULL;
const char *request_idx_type = NULL;
const char *base_URL = NULL;
herr_t ret_value = SUCCEED;

#ifdef RV_CONNECTOR_DEBUG
Expand All @@ -1249,6 +1266,9 @@ RV_attr_get(void *obj, H5VL_attr_get_args_t *args, hid_t dxpl_id, void **req)
FUNC_GOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL,
"parent object not an attribute, file, group, datatype or dataset");

if ((base_URL = loc_obj->domain->u.file.server_info.base_URL) == NULL)
FUNC_GOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "location object does not have valid server URL");

switch (args->op_type) {
/* H5Aget_create_plist */
case H5VL_ATTR_GET_ACPL: {
Expand Down Expand Up @@ -1859,6 +1879,7 @@ RV_attr_specific(void *obj, const H5VL_loc_params_t *loc_params, H5VL_attr_speci
char attr_name_to_delete[ATTRIBUTE_NAME_MAX_LENGTH];
char *url_encoded_attr_name = NULL;
const char *parent_obj_type_header = NULL;
const char *base_URL = NULL;
int url_len = 0;
herr_t ret_value = SUCCEED;

Expand All @@ -1871,6 +1892,9 @@ RV_attr_specific(void *obj, const H5VL_loc_params_t *loc_params, H5VL_attr_speci
H5I_DATATYPE != loc_obj->obj_type && H5I_DATASET != loc_obj->obj_type)
FUNC_GOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "parent object not a file, group, datatype or dataset");

if ((base_URL = loc_obj->domain->u.file.server_info.base_URL) == NULL)
FUNC_GOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "location object does not have valid server URL");

switch (args->op_type) {
/* H5Adelete (_by_name/_by_idx) */
case H5VL_ATTR_DELETE_BY_IDX: {
Expand Down
12 changes: 6 additions & 6 deletions src/rest_vol_config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@
/* Define WORDS_BIGENDIAN to 1 if your processor stores words with the most
significant byte first (like Motorola and SPARC, unlike Intel). */
#if defined AC_APPLE_UNIVERSAL_BUILD
# if defined __BIG_ENDIAN__
# define WORDS_BIGENDIAN 1
# endif
#if defined __BIG_ENDIAN__
#define WORDS_BIGENDIAN 1
#endif
#else
# ifndef WORDS_BIGENDIAN
# undef WORDS_BIGENDIAN
# endif
#ifndef WORDS_BIGENDIAN
#undef WORDS_BIGENDIAN
#endif
#endif

/* Define to empty if `const' does not conform to ANSI C. */
Expand Down
Loading

0 comments on commit 9872181

Please sign in to comment.