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

cockpit-certificate-ensure: soften the certificate generation #21069

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
54 changes: 46 additions & 8 deletions src/tls/cockpit-certificate-ensure.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
// We tolerate the deprecated merged cert/key files only for cockpit-tls.
static bool tolerate_merged_cert_key;

// Depending on the mode, some errors are recoverable.
static bool exit_on_error;
jmlemetayer marked this conversation as resolved.
Show resolved Hide resolved

typedef struct
{
char *certificate_filename;
Expand All @@ -66,13 +69,16 @@ typedef struct
char *filename_for_errors;
} CertificateKeyPair;

static void
static int
read_file (const char *filename,
gnutls_datum_t *result)
{
int fd = open (filename, O_RDONLY);
if (fd == -1)
err (EXIT_FAILURE, "open: %s", filename);
{
warn ("open: %s", filename);
return -1;
}

struct stat buf;
if (fstat (fd, &buf) != 0)
Expand All @@ -94,6 +100,7 @@ read_file (const char *filename,
result->data[s] = '\0';

close (fd);
return 0;
}

static void
Expand Down Expand Up @@ -277,12 +284,20 @@ certificate_and_key_split (CertificateKeyPair *self)
return false;
}

static void
static int
certificate_and_key_read (CertificateKeyPair *self,
const char *certificate_filename)
{
self->certificate_filename = strdupx (certificate_filename);
read_file (self->certificate_filename, &self->certificate);

if (read_file (self->certificate_filename, &self->certificate) < 0)
return -1;

if (self->certificate.size == 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels arbitrary and too brittle. If the concern is short writes after a power loss, then "0 bytes" feels like a "lucky unlucky" case.

The normal way to avoid that is to write the file with a temp name, fsync() it, and then rename it to the final name. Your third commit aims at that, I'll comment on that separately.

On the reading side, the gnutls functions to parse the certificate should already fail (in certificate_and_key_parse_to_creds()) if the certificate is empty, or if the file is truncated, so this check here ought to be redundant?

{
warnx ("empty: %s", self->certificate_filename);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likewise.

return -1;
}

if (certificate_and_key_split (self))
{
Expand All @@ -298,13 +313,23 @@ certificate_and_key_read (CertificateKeyPair *self,
else
{
self->key_filename = cockpit_certificate_key_path (self->certificate_filename);
read_file (self->key_filename, &self->key);

if (read_file (self->key_filename, &self->key) < 0)
return -1;

if (self->key.size == 0)
{
warnx ("empty: %s", self->key_filename);
return -1;
}
}

if (self->key_filename)
asprintfx (&self->filename_for_errors, "%s/.key", self->certificate_filename);
else
self->filename_for_errors = strdupx (self->certificate_filename);

return 0;
}

static gnutls_certificate_credentials_t
Expand Down Expand Up @@ -343,7 +368,16 @@ cockpit_certificate_find (CertificateKeyPair *result,
return false;
}

certificate_and_key_read (result, certificate_filename);
if (certificate_and_key_read (result, certificate_filename) < 0)
{
if (verbose)
printf ("Unable to read certificate file or key file\n");
jmlemetayer marked this conversation as resolved.
Show resolved Hide resolved

if (exit_on_error)
exit (EXIT_FAILURE);

return false;
}

gnutls_certificate_credentials_t creds = certificate_and_key_parse_to_creds (result);

Expand Down Expand Up @@ -387,7 +421,8 @@ cockpit_certificate_selfsign (CertificateKeyPair *result)
errx (EXIT_FAILURE, "%s exited with non-zero status %d",
COCKPIT_CERTIFICATE_HELPER, WEXITSTATUS (status));

certificate_and_key_read (result, COCKPIT_SELFSIGNED_PATH);
if (certificate_and_key_read (result, COCKPIT_SELFSIGNED_PATH) < 0)
exit (EXIT_FAILURE);

/* We just generated this ourselves, so we don't bother to check it
* for validity.
Expand All @@ -408,7 +443,10 @@ main (int argc, char **argv)
else if (argc == 2 && strcmp (argv[1], "--for-cockpit-tls") == 0)
for_cockpit_tls = true;
else
errx (EXIT_FAILURE, "usage: %s [--check]", argv[0]);
errx (EXIT_FAILURE, "usage: %s [--check | --for-cockpit-tls]", argv[0]);
jmlemetayer marked this conversation as resolved.
Show resolved Hide resolved

if (check)
exit_on_error = true;

if (for_cockpit_tls)
tolerate_merged_cert_key = true;
Expand Down
3 changes: 3 additions & 0 deletions src/tls/cockpit-certificate-helper.in
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ cmd_selfsign() {
test ! -e "${CA_FILE}" || install_cert "${CA_FILE}"
install_cert "${CERTFILE}"
install_key "${KEYFILE}"

# Force writing of new files on disk.
sync
jmlemetayer marked this conversation as resolved.
Show resolved Hide resolved
}

cmd_ipa_request() {
Expand Down
4 changes: 2 additions & 2 deletions src/tls/test-cockpit-certificate-ensure.c
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ static const TestCase case_bad_file = {
static const TestCase case_bad_file2 = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests need to be extended to cover the cases you mention (empty/truncated file and such). But let's sort out the above questions first.

.files = bad_files2,

.check_stdout = "",
.check_stdout = "Unable to read*",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a symptom of the above "should go to stderr" comment. The stderr check already covers having a more precise error message.

.check_stderr = "*open*mock-server.key*No such file*",
.check_exit = EXIT_FAILURE,

Expand All @@ -334,7 +334,7 @@ static const TestCase case_invalid1 = {
static const TestCase case_invalid2 = {
.files = invalid_files2,

.check_stdout = "",
.check_stdout = "Unable to read*",
.check_stderr = "*open*mock-server.key*No such file*",
.check_exit = EXIT_FAILURE,

Expand Down