Skip to content

Commit

Permalink
osc-sdk-C: support endpoint though conf file
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Gatto <matthias.gatto@outscale.com>
  • Loading branch information
outscale-mgo committed Jun 14, 2024
1 parent 15b4b83 commit 382811c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
36 changes: 30 additions & 6 deletions lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ int osc_load_region_from_conf(const char *profile, char **region)
}

static int osc_load_cert_from_conf_(const char *profile, char **cert, char **key,
char **proxy)
char **proxy, char **endpoint)
{
struct json_object *cert_obj, *key_obj, *js;
const char *cfg = cfg_path;
Expand Down Expand Up @@ -418,17 +418,30 @@ static int osc_load_cert_from_conf_(const char *profile, char **cert, char **key
if (proxy) {
key_obj = json_object_object_get(js, "proxy");
if (key_obj) {
*key = osc_strdup(json_object_get_string(key_obj));
*proxy = osc_strdup(json_object_get_string(key_obj));
ret |= OSC_ENV_FREE_PROXY;
}
}

if (endpoint) {
struct json_object *e = json_object_object_get(js, "endpoints");

if (e)
e = json_object_object_get(e, "api");
else
e = json_object_object_get(js, "endpoint");
if (e) {
*endpoint = osc_strdup(json_object_get_string(e));
ret |= OSC_ENV_FREE_ENDPOINT;
}
}

return 0;
}

int osc_load_cert_from_conf(const char *profile, char **cert, char **key)
{
return osc_load_cert_from_conf_(profile, cert, key, NULL);
return osc_load_cert_from_conf_(profile, cert, key, NULL, NULL);
}

/* Function that will write the data inside a variable */
Expand Down Expand Up @@ -503,6 +516,8 @@ int osc_init_sdk_ext(struct osc_env *e, const char *profile, unsigned int flag,
e->region = getenv("OSC_REGION");
e->flag = flag;
e->auth_method = cfg ? cfg->auth_method : OSC_AKSK_METHOD;
e->proxy = NULL;
e->endpoint_allocated_ = NULL;
endpoint = getenv("OSC_ENDPOINT_API");
osc_init_str(&e->endpoint);

Expand Down Expand Up @@ -563,7 +578,7 @@ int osc_init_sdk_ext(struct osc_env *e, const char *profile, unsigned int flag,
if (!osc_load_region_from_conf(profile, &e->region))
e->flag |= OSC_ENV_FREE_REGION;
f = osc_load_cert_from_conf_(profile, &e->cert, &e->sslkey,
&e->proxy);
&e->proxy, &e->endpoint_allocated_);
if (e->cert)
cert = e->cert;
if (e->sslkey)
Expand All @@ -576,12 +591,17 @@ int osc_init_sdk_ext(struct osc_env *e, const char *profile, unsigned int flag,
if (!e->region)
e->region = "eu-west-2";

if (!endpoint) {
if (!endpoint && !e->endpoint_allocated_) {
osc_str_append_string(&e->endpoint, "https://api.");
osc_str_append_string(&e->endpoint, e->region);
osc_str_append_string(&e->endpoint, ".outscale.com");
} else {
osc_str_append_string(&e->endpoint, endpoint);
if (e->endpoint_allocated_) {
osc_str_append_string(&e->endpoint,
e->endpoint_allocated_);
} else {
osc_str_append_string(&e->endpoint, endpoint);
}
}

if (e->auth_method == OSC_AKSK_METHOD) {
Expand Down Expand Up @@ -695,6 +715,10 @@ void osc_deinit_sdk(struct osc_env *e)
free(e->sslkey);
}

if (e->flag & OSC_ENV_FREE_ENDPOINT) {
free(e->endpoint_allocated_);
}

e->c = NULL;
e->flag = 0;
}
2 changes: 2 additions & 0 deletions lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ struct osc_str {
#define OSC_ENV_FREE_SSLKEY 1 << 5
#define OSC_ENV_FREE_SK 1 << 6
#define OSC_ENV_FREE_PROXY 1 << 7
#define OSC_ENV_FREE_ENDPOINT 1 << 8

#define OSC_ENV_FREE_AK_SK (OSC_ENV_FREE_AK | OSC_ENV_FREE_SK)

Expand All @@ -98,6 +99,7 @@ struct osc_env {
char *cert;
char *sslkey;
char *proxy;
char *endpoint_allocated_;
int flag;
enum osc_auth_method auth_method;
struct curl_slist *headers;
Expand Down

0 comments on commit 382811c

Please sign in to comment.