diff mbox series

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

Message ID 20250129183312.39268-1-a.broekhuis@gmail.com
State Accepted
Headers show
Series Add api key header and api key configuration for delta updates. | expand

Commit Message

Alexander Broekhuis Jan. 29, 2025, 6:33 p.m. UTC
From: Alexander Broekhuis <alexander.broekhuis@eijsink.nl>

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(+)

Comments

Fabio Estevam Jan. 29, 2025, 6:37 p.m. UTC | #1
Hi Alexander,

On Wed, Jan 29, 2025 at 3:34 PM <a.broekhuis@gmail.com> wrote:
>
> From: Alexander Broekhuis <alexander.broekhuis@eijsink.nl>
>
> Changes since V1:
> - added documentation for new configuration options
> - updated examples/swupdate.cfg

This text should go below the --- line.

Here, you should add the justification for this change.

> Signed-off-by: Alexander Broekhuis <alexander.broekhuis@eijsink.nl>
Alexander Broekhuis Jan. 29, 2025, 6:43 p.m. UTC | #2
Hi Fabio,

Thanks for the hints, this is an update of the patch sent earlier.
Stefano already replied to that one. I will take your remarks into
account for the next patch!

Op wo 29 jan 2025 om 19:37 schreef Fabio Estevam <festevam@gmail.com>:
>
> Hi Alexander,
>
> On Wed, Jan 29, 2025 at 3:34 PM <a.broekhuis@gmail.com> wrote:
> >
> > From: Alexander Broekhuis <alexander.broekhuis@eijsink.nl>
> >
> > Changes since V1:
> > - added documentation for new configuration options
> > - updated examples/swupdate.cfg
>
> This text should go below the --- line.
>
> Here, you should add the justification for this change.
>
> > Signed-off-by: Alexander Broekhuis <alexander.broekhuis@eijsink.nl>
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/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;