Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache location config #120

Merged
merged 14 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 173 additions & 5 deletions src/scitokens.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
#include <atomic>
#include <exception>
#include <string.h>

#include <sys/stat.h>

#include "scitokens.h"
#include "scitokens_internal.h"

/**
* GLOBALS
*/

// Cache timeout config
std::atomic_int configurer::Configuration::m_next_update_delta{600};
std::atomic_int configurer::Configuration::m_expiry_delta{4 * 24 * 3600};

// SciTokens cache home config
std::shared_ptr<std::string> configurer::Configuration::m_cache_home =
std::make_shared<std::string>("");

SciTokenKey scitoken_key_create(const char *key_id, const char *alg,
const char *public_contents,
const char *private_contents, char **err_msg) {
Expand Down Expand Up @@ -958,7 +964,45 @@ int config_set_int(const char *key, int value, char **err_msg) {
}

std::string _key = key;
if (_key == "keycache.update_interval_s") {
if (value < 0) {
if (err_msg) {
*err_msg = strdup("Update interval must be positive.");
}
return -1;
}
configurer::Configuration::set_next_update_delta(value);
return 0;
}

else if (_key == "keycache.expiration_interval_s") {
if (value < 0) {
if (err_msg) {
*err_msg = strdup("Expiry interval must be positive.");
}
return -1;
}
configurer::Configuration::set_expiry_delta(value);
return 0;
}

else {
if (err_msg) {
*err_msg = strdup("Key not recognized.");
}
return -1;
}
}

int scitokens_config_set_int(const char *key, int value, char **err_msg) {
if (!key) {
if (err_msg) {
*err_msg = strdup("A key must be provided.");
}
return -1;
}

std::string _key = key;
if (_key == "keycache.update_interval_s") {
if (value < 0) {
if (err_msg) {
Expand All @@ -970,8 +1014,8 @@ int config_set_int(const char *key, int value, char **err_msg) {
return 0;
}

if (_key == "keycache.expiration_interval_s") {
if (value < 0 ) {
else if (_key == "keycache.expiration_interval_s") {
if (value < 0) {
if (err_msg) {
*err_msg = strdup("Expiry interval must be positive.");
}
Expand All @@ -998,12 +1042,36 @@ int config_get_int(const char *key, char **err_msg) {
}

std::string _key = key;
if (_key == "keycache.update_interval_s") {
return configurer::Configuration::get_next_update_delta();
}

else if (_key == "keycache.expiration_interval_s") {
return configurer::Configuration::get_expiry_delta();
}

else {
if (err_msg) {
*err_msg = strdup("Key not recognized.");
}
return -1;
}
}

int scitokens_config_get_int(const char *key, char **err_msg) {
if (!key) {
if (err_msg) {
*err_msg = strdup("A key must be provided.");
}
return -1;
}

std::string _key = key;
if (_key == "keycache.update_interval_s") {
return configurer::Configuration::get_next_update_delta();
}

if (_key == "keycache.expiration_interval_s") {
else if (_key == "keycache.expiration_interval_s") {
return configurer::Configuration::get_expiry_delta();
}

Expand All @@ -1013,4 +1081,104 @@ int config_get_int(const char *key, char **err_msg) {
}
return -1;
}
}
}

int config_set_str(const char *key, const char *value, char **err_msg) {
if (!key) {
if (err_msg) {
*err_msg = strdup("A key must be provided.");
}
return -1;
}

std::string _key = key;
if (_key == "keycache.cache_home") {
auto rp = configurer::Configuration::set_cache_home(value);
if (!rp.first) { // There was an error, pass rp.second to err_msg
if (err_msg) {
*err_msg = strdup(rp.second.c_str());
}
return -1;
}
}

else {
if (err_msg) {
*err_msg = strdup("Key not recognized.");
}
return -1;
}
return 0;
}

int scitokens_config_set_str(const char *key, const char *value, char **err_msg) {
jhiemstrawisc marked this conversation as resolved.
Show resolved Hide resolved
if (!key) {
if (err_msg) {
*err_msg = strdup("A key must be provided.");
}
return -1;
}

std::string _key = key;
if (_key == "keycache.cache_home") {
auto rp = configurer::Configuration::set_cache_home(value);
if (!rp.first) { // There was an error, pass rp.second to err_msg
if (err_msg) {
*err_msg = strdup(rp.second.c_str());
}
return -1;
}
}

else {
if (err_msg) {
*err_msg = strdup("Key not recognized.");
}
return -1;
}
return 0;
}

int config_get_str(const char *key, char **output, char **err_msg) {
if (!key) {
if (err_msg) {
*err_msg = strdup("A key must be provided.");
}
return -1;
}

std::string _key = key;
if (_key == "keycache.cache_home") {
*output = strdup(configurer::Configuration::get_cache_home().c_str());
}

else {
if (err_msg) {
*err_msg = strdup("Key not recognized.");
}
return -1;
}
return 0;
}

int scitokens_config_get_str(const char *key, char **output, char **err_msg) {
if (!key) {
if (err_msg) {
*err_msg = strdup("A key must be provided.");
}
return -1;
}

std::string _key = key;
if (_key == "keycache.cache_home") {
*output = strdup(configurer::Configuration::get_cache_home().c_str());
}

else {
if (err_msg) {
*err_msg = strdup("Key not recognized.");
}
return -1;
}
return 0;
}
29 changes: 27 additions & 2 deletions src/scitokens.h
Original file line number Diff line number Diff line change
Expand Up @@ -295,21 +295,46 @@ int keycache_set_jwks(const char *issuer, const char *jwks, char **err_msg);
*/

/**
* Update scitokens parameters.
* Update scitokens int parameters.
* Takes in key/value pairs and assigns the input value to whatever
* configuration variable is indicated by the key.
* Returns 0 on success, and non-zero for invalid keys or values.
*/
int config_set_int(const char *key, int value, char **err_msg);

// Prefixed version of the same API to avoid potential symbol collisions
int scitokens_config_set_int(const char *key, int value, char **err_msg);

/**
* Get current scitokens parameters.
* Get current scitokens int parameters.
* Returns the value associated with the supplied input key on success, and -1
* on failure This assumes there are no keys for which a negative return value
* is permissible.
*/
int config_get_int(const char *key, char **err_msg);

// Prefixed version of the same API to avoid potential symbol collisions
int scitokens_config_get_int(const char *key, char **err_msg);

/**
* Set current scitokens str parameters.
* Returns 0 on success, nonzero on failure
*/
int config_set_str(const char *key, const char *value, char **err_msg);
jhiemstrawisc marked this conversation as resolved.
Show resolved Hide resolved

// Prefixed version of the same API to avoid potential symbol collisions
int scitokens_config_set_str(const char *key, const char *value, char **err_msg);
jhiemstrawisc marked this conversation as resolved.
Show resolved Hide resolved

/**
* Get current scitokens str parameters.
* Returns 0 on success, nonzero on failure, and populates the value associated
* with the input key to output.
*/
int config_get_str(const char *key, char **output, char **err_msg);

// Prefixed version of the same API to avoid potential symbol collisions
int scitokens_config_get_str(const char *key, char **output, char **err_msg);

#ifdef __cplusplus
}
#endif
17 changes: 13 additions & 4 deletions src/scitokens_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ void initialize_cachedb(const std::string &keycache_file) {

/**
* Get the Cache file location
*
* 1. $XDG_CACHE_HOME
* 2. .cache subdirectory of home directory as returned by the password
* 1. User-defined through config api
* 2. $XDG_CACHE_HOME
* 3. .cache subdirectory of home directory as returned by the password
* database
*/
std::string get_cache_file() {
Expand All @@ -64,7 +64,16 @@ std::string get_cache_file() {
home_dir += "/.cache";
}

std::string cache_dir(xdg_cache_home ? xdg_cache_home : home_dir.c_str());
// Figure out where to plop the cache based on priority
std::string cache_dir;
std::string configured_cache_dir =
configurer::Configuration::get_cache_home();
if (configured_cache_dir.length() > 0) { // The variable has been configured
cache_dir = configured_cache_dir;
} else {
cache_dir = xdg_cache_home ? xdg_cache_home : home_dir.c_str();
}

if (cache_dir.size() == 0) {
return "";
}
Expand Down
Loading