Skip to content

Commit 64873ea

Browse files
committed
feat(config): add support for config store buffers longer than 8k
1 parent 7f2a6b7 commit 64873ea

File tree

3 files changed

+71
-15
lines changed

3 files changed

+71
-15
lines changed

runtime/fastly/host-api/fastly.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ typedef fastly_host_http_response fastly_world_tuple2_handle_handle;
4747
#define HEADER_MAX_LEN 69000
4848
#define METHOD_MAX_LEN 1024
4949
#define URI_MAX_LEN 8192
50-
#define CONFIG_STORE_ENTRY_MAX_LEN 8000
51-
#define DICTIONARY_ENTRY_MAX_LEN CONFIG_STORE_ENTRY_MAX_LEN
50+
#define CONFIG_STORE_INITIAL_BUF_LEN 512
51+
#define DICTIONARY_ENTRY_MAX_LEN 8000
5252

5353
// Ensure that all the things we want to use the hostcall buffer for actually
5454
// fit into the buffer.

runtime/fastly/host-api/host_api.cpp

Lines changed: 67 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,6 +1619,8 @@ Result<std::optional<HostString>> HttpReq::http_req_downstream_tls_cipher_openss
16191619
auto status = fastly::req_downstream_tls_cipher_openssl_name(reinterpret_cast<char *>(ret.ptr),
16201620
default_size, &ret.len);
16211621
if (status == FASTLY_HOST_ERROR_BUFFER_LEN) {
1622+
// NB(@zkat): ERROR_BUFFER_LEN sets the expected length of the buffer to
1623+
// &ret.len, so we use that to inform our resize.
16221624
ret.ptr = static_cast<uint8_t *>(cabi_realloc(ret.ptr, default_size, 4, ret.len));
16231625
status = fastly::req_downstream_tls_cipher_openssl_name(reinterpret_cast<char *>(ret.ptr),
16241626
ret.len, &ret.len);
@@ -1650,6 +1652,8 @@ Result<std::optional<HostString>> HttpReq::http_req_downstream_tls_protocol() {
16501652
auto status = fastly::req_downstream_tls_protocol(reinterpret_cast<char *>(ret.ptr), default_size,
16511653
&ret.len);
16521654
if (status == FASTLY_HOST_ERROR_BUFFER_LEN) {
1655+
// NB(@zkat): ERROR_BUFFER_LEN sets the expected length of the buffer to
1656+
// &ret.len, so we use that to inform our resize.
16531657
ret.ptr = static_cast<uint8_t *>(cabi_realloc(ret.ptr, default_size, 4, ret.len));
16541658
status =
16551659
fastly::req_downstream_tls_protocol(reinterpret_cast<char *>(ret.ptr), ret.len, &ret.len);
@@ -1679,6 +1683,8 @@ Result<std::optional<HostBytes>> HttpReq::http_req_downstream_tls_client_hello()
16791683
ret.ptr = static_cast<uint8_t *>(cabi_malloc(default_size, 4));
16801684
auto status = fastly::req_downstream_tls_client_hello(ret.ptr, default_size, &ret.len);
16811685
if (status == FASTLY_HOST_ERROR_BUFFER_LEN) {
1686+
// NB(@zkat): ERROR_BUFFER_LEN sets the expected length of the buffer to
1687+
// &ret.len, so we use that to inform our resize.
16821688
ret.ptr = static_cast<uint8_t *>(cabi_realloc(ret.ptr, default_size, 4, ret.len));
16831689
status = fastly::req_downstream_tls_client_hello(ret.ptr, ret.len, &ret.len);
16841690
}
@@ -1708,6 +1714,8 @@ Result<std::optional<HostBytes>> HttpReq::http_req_downstream_tls_raw_client_cer
17081714
ret.ptr = static_cast<uint8_t *>(cabi_malloc(default_size, 4));
17091715
auto status = fastly::req_downstream_tls_raw_client_certificate(ret.ptr, default_size, &ret.len);
17101716
if (status == FASTLY_HOST_ERROR_BUFFER_LEN) {
1717+
// NB(@zkat): ERROR_BUFFER_LEN sets the expected length of the buffer to
1718+
// &ret.len, so we use that to inform our resize.
17111719
ret.ptr = static_cast<uint8_t *>(cabi_realloc(ret.ptr, default_size, 4, ret.len));
17121720
status = fastly::req_downstream_tls_raw_client_certificate(ret.ptr, ret.len, &ret.len);
17131721
}
@@ -1736,6 +1744,8 @@ Result<std::optional<HostBytes>> HttpReq::http_req_downstream_tls_ja3_md5() {
17361744
ret.ptr = static_cast<uint8_t *>(cabi_malloc(default_size, 4));
17371745
auto status = fastly::req_downstream_tls_ja3_md5(ret.ptr, &ret.len);
17381746
if (status == FASTLY_HOST_ERROR_BUFFER_LEN) {
1747+
// NB(@zkat): ERROR_BUFFER_LEN sets the expected length of the buffer to
1748+
// &ret.len, so we use that to inform our resize.
17391749
ret.ptr = static_cast<uint8_t *>(cabi_realloc(ret.ptr, default_size, 4, ret.len));
17401750
status = fastly::req_downstream_tls_ja3_md5(ret.ptr, &ret.len);
17411751
}
@@ -2686,25 +2696,47 @@ Result<ConfigStore> ConfigStore::open(std::string_view name) {
26862696
}
26872697

26882698
Result<std::optional<HostString>> ConfigStore::get(std::string_view name) {
2699+
return this->get(name, CONFIG_STORE_INITIAL_BUF_LEN);
2700+
}
2701+
2702+
Result<std::optional<HostString>> ConfigStore::get(std::string_view name,
2703+
uint32_t initial_buf_len) {
26892704
TRACE_CALL()
26902705
Result<std::optional<HostString>> res;
26912706

2707+
uint32_t buf_len{initial_buf_len};
26922708
auto name_str = string_view_to_world_string(name);
26932709
fastly::fastly_world_string ret;
26942710
fastly::fastly_host_error err;
2695-
ret.ptr = static_cast<uint8_t *>(cabi_malloc(CONFIG_STORE_ENTRY_MAX_LEN, 1));
2696-
if (!convert_result(fastly::config_store_get(this->handle, reinterpret_cast<char *>(name_str.ptr),
2697-
name_str.len, reinterpret_cast<char *>(ret.ptr),
2698-
CONFIG_STORE_ENTRY_MAX_LEN, &ret.len),
2699-
&err)) {
2711+
2712+
ret.ptr = static_cast<uint8_t *>(cabi_malloc(buf_len, 1));
2713+
2714+
bool succeeded{convert_result(
2715+
fastly::config_store_get(this->handle, reinterpret_cast<char *>(name_str.ptr), name_str.len,
2716+
reinterpret_cast<char *>(ret.ptr), buf_len, &ret.len),
2717+
&err)};
2718+
2719+
if (!succeeded && err == FASTLY_HOST_ERROR_BUFFER_LEN) {
2720+
// NB(@zkat): ERROR_BUFFER_LEN sets the expected length of the buffer to
2721+
// &ret.len, so we use that to inform our resize.
2722+
buf_len = ret.len;
2723+
ret.len = 0;
2724+
ret.ptr = static_cast<uint8_t *>(cabi_realloc(ret.ptr, initial_buf_len, 1, buf_len));
2725+
succeeded = convert_result(
2726+
fastly::config_store_get(this->handle, reinterpret_cast<char *>(name_str.ptr), name_str.len,
2727+
reinterpret_cast<char *>(ret.ptr), buf_len, &ret.len),
2728+
&err);
2729+
}
2730+
2731+
if (!succeeded) {
27002732
cabi_free(ret.ptr);
27012733
if (error_is_optional_none(err)) {
27022734
res.emplace(std::nullopt);
27032735
} else {
27042736
res.emplace_err(err);
27052737
}
27062738
} else {
2707-
ret.ptr = static_cast<uint8_t *>(cabi_realloc(ret.ptr, CONFIG_STORE_ENTRY_MAX_LEN, 1, ret.len));
2739+
ret.ptr = static_cast<uint8_t *>(cabi_realloc(ret.ptr, buf_len, 1, ret.len));
27082740
res.emplace(make_host_string(ret));
27092741
}
27102742

@@ -2842,25 +2874,43 @@ FastlyAsyncTask::Handle ObjectStorePendingDelete::async_handle() const {
28422874
}
28432875

28442876
Result<std::optional<HostBytes>> Secret::plaintext() const {
2877+
return this->plaintext(CONFIG_STORE_INITIAL_BUF_LEN);
2878+
}
2879+
2880+
Result<std::optional<HostBytes>> Secret::plaintext(uint32_t initial_buf_len) const {
28452881
TRACE_CALL()
28462882
Result<std::optional<HostBytes>> res;
28472883

2884+
uint32_t buf_len{initial_buf_len};
28482885
fastly::fastly_world_list_u8 ret;
28492886
fastly::fastly_host_error err;
2850-
ret.ptr = static_cast<uint8_t *>(JS_malloc(CONTEXT, DICTIONARY_ENTRY_MAX_LEN));
2851-
if (!convert_result(fastly::secret_store_plaintext(this->handle,
2852-
reinterpret_cast<char *>(ret.ptr),
2853-
DICTIONARY_ENTRY_MAX_LEN, &ret.len),
2854-
&err)) {
2887+
ret.ptr = static_cast<uint8_t *>(JS_malloc(CONTEXT, buf_len));
2888+
bool succeeded{
2889+
convert_result(fastly::secret_store_plaintext(this->handle, reinterpret_cast<char *>(ret.ptr),
2890+
buf_len, &ret.len),
2891+
&err)};
2892+
2893+
if (!succeeded && err == FASTLY_HOST_ERROR_BUFFER_LEN) {
2894+
// NB(@zkat): ERROR_BUFFER_LEN sets the expected length of the buffer to
2895+
// &ret.len, so we use that to inform our resize.
2896+
buf_len = ret.len;
2897+
ret.len = 0;
2898+
ret.ptr = static_cast<uint8_t *>(JS_realloc(CONTEXT, ret.ptr, initial_buf_len, buf_len));
2899+
succeeded =
2900+
convert_result(fastly::secret_store_plaintext(
2901+
this->handle, reinterpret_cast<char *>(ret.ptr), buf_len, &ret.len),
2902+
&err);
2903+
}
2904+
2905+
if (!succeeded) {
28552906
if (error_is_optional_none(err)) {
28562907
res.emplace(std::nullopt);
28572908
} else {
28582909
JS_free(CONTEXT, ret.ptr);
28592910
res.emplace_err(err);
28602911
}
28612912
} else {
2862-
ret.ptr =
2863-
static_cast<uint8_t *>(JS_realloc(CONTEXT, ret.ptr, DICTIONARY_ENTRY_MAX_LEN, ret.len));
2913+
ret.ptr = static_cast<uint8_t *>(JS_realloc(CONTEXT, ret.ptr, buf_len, ret.len));
28642914
res.emplace(make_host_bytes(ret.ptr, ret.len));
28652915
}
28662916

@@ -3363,6 +3413,8 @@ Result<HostBytes> CacheHandle::get_user_metadata() {
33633413
auto status = fastly::cache_get_user_metadata(handle, reinterpret_cast<char *>(ret.ptr),
33643414
default_size, &ret.len);
33653415
if (status == FASTLY_HOST_ERROR_BUFFER_LEN) {
3416+
// NB(@zkat): ERROR_BUFFER_LEN sets the expected length of the buffer to
3417+
// &ret.len, so we use that to inform our resize.
33663418
ret.ptr = static_cast<uint8_t *>(cabi_realloc(ret.ptr, default_size, 4, ret.len));
33673419
status = fastly::cache_get_user_metadata(handle, reinterpret_cast<char *>(ret.ptr), ret.len,
33683420
&ret.len);
@@ -4102,6 +4154,8 @@ Result<HostString> DeviceDetection::lookup(std::string_view user_agent) {
41024154
reinterpret_cast<char *>(user_agent_str.ptr), user_agent_str.len,
41034155
reinterpret_cast<char *>(ret.ptr), default_size, &ret.len);
41044156
if (status == FASTLY_HOST_ERROR_BUFFER_LEN) {
4157+
// NB(@zkat): ERROR_BUFFER_LEN sets the expected length of the buffer to
4158+
// &ret.len, so we use that to inform our resize.
41054159
ret.ptr = static_cast<uint8_t *>(cabi_realloc(ret.ptr, default_size, 4, ret.len));
41064160
status = fastly::device_detection_lookup(reinterpret_cast<char *>(user_agent_str.ptr),
41074161
user_agent_str.len, reinterpret_cast<char *>(ret.ptr),

runtime/fastly/host-api/host_api_fastly.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,7 @@ class ConfigStore final {
846846
static Result<ConfigStore> open(std::string_view name);
847847

848848
Result<std::optional<HostString>> get(std::string_view name);
849+
Result<std::optional<HostString>> get(std::string_view name, uint32_t initial_buf_len);
849850
};
850851

851852
class ObjectStorePendingLookup final {
@@ -918,6 +919,7 @@ class Secret final {
918919
explicit Secret(Handle handle) : handle{handle} {}
919920

920921
Result<std::optional<HostBytes>> plaintext() const;
922+
Result<std::optional<HostBytes>> plaintext(uint32_t initial_buf_len) const;
921923
};
922924

923925
class SecretStore final {

0 commit comments

Comments
 (0)