Skip to content

Improve errors when TLS files cannot be read #44787

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

Merged
merged 5 commits into from
Jul 31, 2019

Conversation

tvernum
Copy link
Contributor

@tvernum tvernum commented Jul 24, 2019

This change improves the exception messages that are thrown when the
system cannot read TLS resources such as keystores, truststores,
certificates, keys or certificate-chains (CAs).

This change specifically handles:

  • Files that do not exist
  • Files that cannot be read due to file-system permissions
  • Files that cannot be read due to the ES security-manager

Relates: #43079

tvernum added 2 commits July 24, 2019 17:29
This change updates the error messages when SSL/TLS is configured
using a file that does not exist.
It adds tests for the expected error message(s) when various files are
missing.
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-security

@@ -176,7 +177,7 @@ private static PrivateKey parsePKCS8(BufferedReader bReader) throws IOException,
line = bReader.readLine();
}
if (null == line || PKCS8_FOOTER.equals(line.trim()) == false) {
throw new IOException("Malformed PEM file, PEM footer is invalid or missing");
throw new KeyException("Malformed PEM file, PEM footer is invalid or missing");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This change was needed because readPrivateKey no longer catches & wraps IOException (which, in turn, was needed in order to have specific handling of NoSuchFileException)

Copy link
Member

@jkakavas jkakavas left a comment

Choose a reason for hiding this comment

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

LGTM, I like this a lot ! How about libs/ssl-config, will you replicate the changes there in a separate PR ?

} else {
final String pathString = paths.stream().map(Path::toAbsolutePath).map(Path::toString).collect(Collectors.joining(", "));
return new ElasticsearchException(
"failed to initialize SSL TrustManager - access to read {} files [{}] is blocked;" +
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"failed to initialize SSL TrustManager - access to read {} files [{}] is blocked;" +
"failed to initialize SSL TrustManager - access to read one of the {} files [{}] is blocked;" +

try {
return CertParsingUtils.readCertificates(Collections.singletonList(certificate));
} catch (FileNotFoundException | NoSuchFileException fileException) {
throw missingKeyConfigFile(fileException, "certificate", certificate);
Copy link
Member

Choose a reason for hiding this comment

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

nit: Maybe have a constant for certificate and truststore in TrustConfig

try {
return PemUtils.readPrivateKey(key, keyPassword::getChars);
} catch (FileNotFoundException | NoSuchFileException fileException) {
throw missingKeyConfigFile(fileException, "key", key);
Copy link
Member

Choose a reason for hiding this comment

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

nit: Maybe have a constant for key and keystore in KeyConfig

Copy link
Contributor

@bizybot bizybot left a comment

Choose a reason for hiding this comment

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

LGTM, Thank you.

@tvernum
Copy link
Contributor Author

tvernum commented Jul 29, 2019

How about libs/ssl-config, will you replicate the changes there in a separate PR ?

It's not my plan. The implementation is sufficiently different there that it would end up being a lot of effort to duplicate the testing & changes for minimal benefit (since ssl-config is not widely used).
I would prefer to spend that time fixing more error messages in x-pack.

However, we can't stay in a situation where we are maintaining two overlapping implementations of the same code.
For 8.0, I want to move x-pack over to using ssl-config, at which point all these tests will start testing that implementation instead.

@tvernum
Copy link
Contributor Author

tvernum commented Jul 30, 2019

@elasticmachine run elasticsearch-ci/bwc

@tvernum tvernum merged commit 724a0e4 into elastic:master Jul 31, 2019
jkakavas pushed a commit that referenced this pull request Jul 31, 2019
This change improves the exception messages that are thrown when the
system cannot read TLS resources such as keystores, truststores,
certificates, keys or certificate-chains (CAs).

This change specifically handles:

- Files that do not exist
- Files that cannot be read due to file-system permissions
- Files that cannot be read due to the ES security-manager

Relates: #43079
tvernum added a commit to tvernum/elasticsearch that referenced this pull request Aug 1, 2019
This change improves the exception messages that are thrown when the
system cannot read TLS resources such as keystores, truststores,
certificates, keys or certificate-chains (CAs).

This change specifically handles:

- Files that do not exist
- Files that cannot be read due to file-system permissions
- Files that cannot be read due to the ES security-manager

Relates: elastic#43079
Backport of: elastic#44787
tvernum added a commit that referenced this pull request Aug 2, 2019
This change improves the exception messages that are thrown when the
system cannot read TLS resources such as keystores, truststores,
certificates, keys or certificate-chains (CAs).

This change specifically handles:

- Files that do not exist
- Files that cannot be read due to file-system permissions
- Files that cannot be read due to the ES security-manager

Backport of: #44787
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants