diff mbox series

Add api key header and api key configuration for delta updates.

Message ID CA+T-u8cUbqZeOMsr4upzXS_kJURwJNx58NiZJADvTMEuGy8_Ng@mail.gmail.com
State Changes Requested
Headers show
Series Add api key header and api key configuration for delta updates. | expand

Commit Message

Alexander Broekhuis Jan. 28, 2025, 9:35 a.m. UTC
Signed-off-by: Alexander Broekhuis <a.broekhuis@gmail.com>
---
 corelib/channel_curl.c | 15 +++++++++++++++
 corelib/server_utils.c |  6 ++++++
 include/channel_curl.h |  2 ++
 3 files changed, 23 insertions(+)

Comments

Stefano Babic Jan. 28, 2025, 12:25 p.m. UTC | #1
Hi Alexander,

On 1/28/25 10:35, Alexander Broekhuis wrote:
> Signed-off-by: Alexander Broekhuis <a.broekhuis@gmail.com>

Not a stopping point, but I would like that companies won't hide behind
generic mail address and developers will sign with there work address,
also to testify their contribution to the project. Do you think is it
possible ? As I said, it is just a wish...

> ---
>   corelib/channel_curl.c | 15 +++++++++++++++
>   corelib/server_utils.c |  6 ++++++
>   include/channel_curl.h |  2 ++
>   3 files changed, 23 insertions(+)
>
> diff --git a/corelib/channel_curl.c b/corelib/channel_curl.c
> index 0428fb5..4673b7c 100644
> --- a/corelib/channel_curl.c
> +++ b/corelib/channel_curl.c
> @@ -693,6 +693,21 @@ channel_op_res_t channel_set_options(channel_t
> *this, channel_data_t *channel_da
>         goto cleanup;
>      }
>
> +    if ((channel_data->api_key_header) && (channel_data->api_key)) {
> +       char *header;
> +       if (ENOMEM_ASPRINTF == asprintf(&header, "%s: %s",
> channel_data->api_key_header, channel_data->api_key)) {
> +          result = CHANNEL_EINIT;
> +          goto cleanup;
> +      }
> +       if (((channel_curl->header = curl_slist_append(
> +            channel_curl->header, header)) == NULL)) {
> +           free(header);
> +          result = CHANNEL_EINIT;
> +          goto cleanup;
> +      }
> +       free(header);
> +   }
> +

Ok

>      if (channel_data->received_headers || channel_data->headers) {
>         if ((curl_easy_setopt(channel_curl->handle,
>                  CURLOPT_HEADERFUNCTION,
> diff --git a/corelib/server_utils.c b/corelib/server_utils.c
> index 95b72a4..ff78aa2 100644
> --- a/corelib/server_utils.c
> +++ b/corelib/server_utils.c
> @@ -59,6 +59,12 @@ int channel_settings(void *elem, void *data)
>      GET_FIELD_STRING_RESET(LIBCFG_PARSER, elem, "interface", tmp);
>      if (strlen(tmp))
>         SETSTRING(chan->iface, tmp);
> +   GET_FIELD_STRING_RESET(LIBCFG_PARSER, elem, "api_key_header", tmp);
> +   if (strlen(tmp))
> +      SETSTRING(chan->api_key_header, tmp);
> +   GET_FIELD_STRING_RESET(LIBCFG_PARSER, elem, "api_key", tmp);
> +   if (strlen(tmp))
> +      SETSTRING(chan->api_key, tmp);
>
>      return 0;
>   }
> diff --git a/include/channel_curl.h b/include/channel_curl.h
> index d787787..bb568ea 100644
> --- a/include/channel_curl.h
> +++ b/include/channel_curl.h
> @@ -95,4 +95,6 @@ typedef struct {
>      size_t upload_filesize;
>      char *range; /* Range request for get_file in any */
>      void *user;
> +   char *api_key_header;
> +       char *api_key;
>   } channel_data_t;

This is quite poor. You are adding a new feature without any
documentation. I am expecting:

- the new keyword for swupdate.cfg must be documented in
examples/swupdate.cfg.
- there is no track about how to use it. There should be a chapter /
something in the documentation with an example how to use it.

Thanks,
Stefano Babic
Alexander Broekhuis Jan. 29, 2025, 3:37 p.m. UTC | #2
I just noticed I replied to stefano directly, so here it is for the
list as well.

Add api key header and api key configuration for delta updates.

Changes since V1:
- added documentation for new configuration options
- updated examples/swupdate.cfg

Signed-off-by: Alexander Broekhuis <alexander.broekhuis@eijsink.nl>
---
 corelib/channel_curl.c              | 15 +++++++++++++++
 corelib/server_utils.c              |  6 ++++++
 doc/source/delta-update.rst         | 11 +++++++++++
 examples/configuration/swupdate.cfg |  4 ++++
 include/channel_curl.h              |  2 ++
 5 files changed, 38 insertions(+)

diff --git a/corelib/channel_curl.c b/corelib/channel_curl.c
index 0428fb5..4673b7c 100644
--- a/corelib/channel_curl.c
+++ b/corelib/channel_curl.c
@@ -693,6 +693,21 @@ channel_op_res_t channel_set_options(channel_t
*this, channel_data_t *channel_da
       goto cleanup;
    }

+    if ((channel_data->api_key_header) && (channel_data->api_key)) {
+       char *header;
+       if (ENOMEM_ASPRINTF == asprintf(&header, "%s: %s",
channel_data->api_key_header, channel_data->api_key)) {
+          result = CHANNEL_EINIT;
+          goto cleanup;
+      }
+       if (((channel_curl->header = curl_slist_append(
+            channel_curl->header, header)) == NULL)) {
+           free(header);
+          result = CHANNEL_EINIT;
+          goto cleanup;
+      }
+       free(header);
+   }
+
    if (channel_data->received_headers || channel_data->headers) {
       if ((curl_easy_setopt(channel_curl->handle,
                CURLOPT_HEADERFUNCTION,
diff --git a/corelib/server_utils.c b/corelib/server_utils.c
index 95b72a4..ff78aa2 100644
--- a/corelib/server_utils.c
+++ b/corelib/server_utils.c
@@ -59,6 +59,12 @@ int channel_settings(void *elem, void *data)
    GET_FIELD_STRING_RESET(LIBCFG_PARSER, elem, "interface", tmp);
    if (strlen(tmp))
       SETSTRING(chan->iface, tmp);
+   GET_FIELD_STRING_RESET(LIBCFG_PARSER, elem, "api_key_header", tmp);
+   if (strlen(tmp))
+      SETSTRING(chan->api_key_header, tmp);
+   GET_FIELD_STRING_RESET(LIBCFG_PARSER, elem, "api_key", tmp);
+   if (strlen(tmp))
+      SETSTRING(chan->api_key, tmp);

    return 0;
 }
diff --git a/doc/source/delta-update.rst b/doc/source/delta-update.rst
index 481ba7d..8ac6a15 100644
--- a/doc/source/delta-update.rst
+++ b/doc/source/delta-update.rst
@@ -220,6 +220,17 @@ The downloader is thought as dummy servant: it
starts the connection, retrieves
 and sends them back to the caller. The delta handler is then
responsible to parse the answer, and to
 retrieve the missing chunks from the multipart HTTP body.

+Configuration_
+..............
+
+The delta handler is configured in the runtime configuration file
(swupdate.cfg). The configuration is done in the
+`delta` section. See also example/configuration/swupdate.cfg for details.
+
+api_key_ authentication can be setup by providing the api_key_header
and api_key in the configuration file. The
+api_key_header is the header name that will be used to send the
api_key. The api_key is the value that will be sent in
+the header.
+
+
 Creation of ZCK Header and ZCK file for SWUpdate
 ------------------------------------------------

diff --git a/examples/configuration/swupdate.cfg
b/examples/configuration/swupdate.cfg
index 2702396..b07207c 100644
--- a/examples/configuration/swupdate.cfg
+++ b/examples/configuration/swupdate.cfg
@@ -282,6 +282,10 @@ webserver :
 # sslcert     : string
 #          path of the file containing the certificate for SSL
connection or pkcs11 URI
 #          (ex.
"pkcs11:model=ATECC608B;token=0ABC;serial=0123456789abcdef;object=device;type=cert")
+# api_key_header   : string
+#          HTTP header name for the API key
+# api_key        : string
+#          API key for the delta update server
 delta :
 {
    sslkey    = "/etc/ssl/sslkey";
diff --git a/include/channel_curl.h b/include/channel_curl.h
index d787787..bb568ea 100644
--- a/include/channel_curl.h
+++ b/include/channel_curl.h
@@ -95,4 +95,6 @@ typedef struct {
    size_t upload_filesize;
    char *range; /* Range request for get_file in any */
    void *user;
+   char *api_key_header;
+       char *api_key;
 } channel_data_t;
Stefano Babic Jan. 29, 2025, 6:17 p.m. UTC | #3
Hi Alexander,

On 29.01.25 16:37, Alexander Broekhuis wrote:
> I just noticed I replied to stefano directly, so here it is for the
> list as well.

Well, but the patch is now malformed, and I cannot get it. It wasn't
registered into patchwork (V1 was).

See https://patchwork.ozlabs.org/project/swupdate/list/

Just V1 is listed. Could you resend in the same way you did for V1 ?
Please do not copy & paste, send the patch with git send-email.

Thanks,
Stefano

>
> Add api key header and api key configuration for delta updates.
>
> Changes since V1:
> - added documentation for new configuration options
> - updated examples/swupdate.cfg
>
> Signed-off-by: Alexander Broekhuis <alexander.broekhuis@eijsink.nl>
> ---
>   corelib/channel_curl.c              | 15 +++++++++++++++
>   corelib/server_utils.c              |  6 ++++++
>   doc/source/delta-update.rst         | 11 +++++++++++
>   examples/configuration/swupdate.cfg |  4 ++++
>   include/channel_curl.h              |  2 ++
>   5 files changed, 38 insertions(+)
>
> diff --git a/corelib/channel_curl.c b/corelib/channel_curl.c
> index 0428fb5..4673b7c 100644
> --- a/corelib/channel_curl.c
> +++ b/corelib/channel_curl.c
> @@ -693,6 +693,21 @@ channel_op_res_t channel_set_options(channel_t
> *this, channel_data_t *channel_da
>         goto cleanup;
>      }
>
> +    if ((channel_data->api_key_header) && (channel_data->api_key)) {
> +       char *header;
> +       if (ENOMEM_ASPRINTF == asprintf(&header, "%s: %s",
> channel_data->api_key_header, channel_data->api_key)) {
> +          result = CHANNEL_EINIT;
> +          goto cleanup;
> +      }
> +       if (((channel_curl->header = curl_slist_append(
> +            channel_curl->header, header)) == NULL)) {
> +           free(header);
> +          result = CHANNEL_EINIT;
> +          goto cleanup;
> +      }
> +       free(header);
> +   }
> +
>      if (channel_data->received_headers || channel_data->headers) {
>         if ((curl_easy_setopt(channel_curl->handle,
>                  CURLOPT_HEADERFUNCTION,
> diff --git a/corelib/server_utils.c b/corelib/server_utils.c
> index 95b72a4..ff78aa2 100644
> --- a/corelib/server_utils.c
> +++ b/corelib/server_utils.c
> @@ -59,6 +59,12 @@ int channel_settings(void *elem, void *data)
>      GET_FIELD_STRING_RESET(LIBCFG_PARSER, elem, "interface", tmp);
>      if (strlen(tmp))
>         SETSTRING(chan->iface, tmp);
> +   GET_FIELD_STRING_RESET(LIBCFG_PARSER, elem, "api_key_header", tmp);
> +   if (strlen(tmp))
> +      SETSTRING(chan->api_key_header, tmp);
> +   GET_FIELD_STRING_RESET(LIBCFG_PARSER, elem, "api_key", tmp);
> +   if (strlen(tmp))
> +      SETSTRING(chan->api_key, tmp);
>
>      return 0;
>   }
> diff --git a/doc/source/delta-update.rst b/doc/source/delta-update.rst
> index 481ba7d..8ac6a15 100644
> --- a/doc/source/delta-update.rst
> +++ b/doc/source/delta-update.rst
> @@ -220,6 +220,17 @@ The downloader is thought as dummy servant: it
> starts the connection, retrieves
>   and sends them back to the caller. The delta handler is then
> responsible to parse the answer, and to
>   retrieve the missing chunks from the multipart HTTP body.
>
> +Configuration_
> +..............
> +
> +The delta handler is configured in the runtime configuration file
> (swupdate.cfg). The configuration is done in the
> +`delta` section. See also example/configuration/swupdate.cfg for details.
> +
> +api_key_ authentication can be setup by providing the api_key_header
> and api_key in the configuration file. The
> +api_key_header is the header name that will be used to send the
> api_key. The api_key is the value that will be sent in
> +the header.
> +
> +
>   Creation of ZCK Header and ZCK file for SWUpdate
>   ------------------------------------------------
>
> diff --git a/examples/configuration/swupdate.cfg
> b/examples/configuration/swupdate.cfg
> index 2702396..b07207c 100644
> --- a/examples/configuration/swupdate.cfg
> +++ b/examples/configuration/swupdate.cfg
> @@ -282,6 +282,10 @@ webserver :
>   # sslcert     : string
>   #          path of the file containing the certificate for SSL
> connection or pkcs11 URI
>   #          (ex.
> "pkcs11:model=ATECC608B;token=0ABC;serial=0123456789abcdef;object=device;type=cert")
> +# api_key_header   : string
> +#          HTTP header name for the API key
> +# api_key        : string
> +#          API key for the delta update server
>   delta :
>   {
>      sslkey    = "/etc/ssl/sslkey";
> diff --git a/include/channel_curl.h b/include/channel_curl.h
> index d787787..bb568ea 100644
> --- a/include/channel_curl.h
> +++ b/include/channel_curl.h
> @@ -95,4 +95,6 @@ typedef struct {
>      size_t upload_filesize;
>      char *range; /* Range request for get_file in any */
>      void *user;
> +   char *api_key_header;
> +       char *api_key;
>   } channel_data_t;
diff mbox series

Patch

diff --git a/corelib/channel_curl.c b/corelib/channel_curl.c
index 0428fb5..4673b7c 100644
--- a/corelib/channel_curl.c
+++ b/corelib/channel_curl.c
@@ -693,6 +693,21 @@  channel_op_res_t channel_set_options(channel_t
*this, channel_data_t *channel_da
       goto cleanup;
    }

+    if ((channel_data->api_key_header) && (channel_data->api_key)) {
+       char *header;
+       if (ENOMEM_ASPRINTF == asprintf(&header, "%s: %s",
channel_data->api_key_header, channel_data->api_key)) {
+          result = CHANNEL_EINIT;
+          goto cleanup;
+      }
+       if (((channel_curl->header = curl_slist_append(
+            channel_curl->header, header)) == NULL)) {
+           free(header);
+          result = CHANNEL_EINIT;
+          goto cleanup;
+      }
+       free(header);
+   }
+
    if (channel_data->received_headers || channel_data->headers) {
       if ((curl_easy_setopt(channel_curl->handle,
                CURLOPT_HEADERFUNCTION,
diff --git a/corelib/server_utils.c b/corelib/server_utils.c
index 95b72a4..ff78aa2 100644
--- a/corelib/server_utils.c
+++ b/corelib/server_utils.c
@@ -59,6 +59,12 @@  int channel_settings(void *elem, void *data)
    GET_FIELD_STRING_RESET(LIBCFG_PARSER, elem, "interface", tmp);
    if (strlen(tmp))
       SETSTRING(chan->iface, tmp);
+   GET_FIELD_STRING_RESET(LIBCFG_PARSER, elem, "api_key_header", tmp);
+   if (strlen(tmp))
+      SETSTRING(chan->api_key_header, tmp);
+   GET_FIELD_STRING_RESET(LIBCFG_PARSER, elem, "api_key", tmp);
+   if (strlen(tmp))
+      SETSTRING(chan->api_key, tmp);

    return 0;
 }
diff --git a/include/channel_curl.h b/include/channel_curl.h
index d787787..bb568ea 100644
--- a/include/channel_curl.h
+++ b/include/channel_curl.h
@@ -95,4 +95,6 @@  typedef struct {
    size_t upload_filesize;
    char *range; /* Range request for get_file in any */
    void *user;
+   char *api_key_header;
+       char *api_key;
 } channel_data_t;