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

Move initialization to common method for Swarm #693

Merged
merged 4 commits into from Oct 25, 2023

Conversation

basil
Copy link
Member

@basil basil commented Oct 24, 2023

Found a minor issue when trying to refactor Swarm to take advantage of these recent changes: Swarm directly calls Launcher#parseJnlpArguments to get the secret from the server (and can't easily be changed to stop doing this), and that currently means that the initialization logic from Launcher#run doesn't get run, so things like -noCertificateCheck are working only by accident in that their initialization is done lazily rather than eagerly. This PR fixes that problem and makes the code more easy to understand by putting all initialization logic in one place and ensuring that all public entrypoints invoke it exactly once, and eagerly at the beginning of program launch.

We also took the various initialization paradigms and made them consistent: eager rather than lazy, using the same set of fields everywhere, the same set of arguments everywhere in the same order, etc.

Testing done

Ran both Remoting and Swarm tests with these changes.

@basil basil added the internal label Oct 24, 2023
Comment on lines +191 to +192
@CheckForNull
private HostnameVerifier hostnameVerifier;
Copy link
Member Author

Choose a reason for hiding this comment

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

Following a general paradigm where everytime a boolean field exists to disable validation, a HostNameVerifier field exists beneath it. Following this convention consistently in all classes that do this allows for certain common utilities like JnlpAgentEndpointResolver#openURLConnection to be simplified.

@@ -928,8 +937,8 @@ private JnlpEndpointResolver createEndpointResolver(List<String> jenkinsUrls, St
} catch (Exception e) {
events.error(e);
}
resolver = new JnlpAgentEndpointResolver(jenkinsUrls, credentials, proxyCredentials, tunnel,
sslSocketFactory, disableHttpsCertValidation, agentName);
resolver = new JnlpAgentEndpointResolver(jenkinsUrls, agentName, credentials, proxyCredentials, tunnel,
Copy link
Member Author

Choose a reason for hiding this comment

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

Following a general paradigm where user-specified flags come before computed objects and mandatory arguments come before optional arguments. Using a consistent order helps improve readability as you go from one file to the next, which otherwise used different ordering which was confusing.

Comment on lines +441 to +442
if (noCertificateCheck) {
hostnameVerifier = new NoCheckHostnameVerifier();
Copy link
Member Author

Choose a reason for hiding this comment

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

Initializing this eagerly rather than lazily for consistency with other code. Of course, this necessitates that all public methods in this file now call initialization, which is why I factored out initialization into its own method.

@@ -624,6 +643,7 @@ private void runAsInboundAgent() throws CmdLineException, IOException, Interrupt
*/
@SuppressFBWarnings(value = {"CIPHER_INTEGRITY", "STATIC_IV"}, justification = "Integrity not needed here. IV used for decryption only, loaded from encryptor.")
public List<String> parseJnlpArguments() throws ParserConfigurationException, SAXException, IOException, InterruptedException {
initialize(); // For Swarm, or anyone else who invokes this public method directly rather than going through main() or run()
Copy link
Member Author

Choose a reason for hiding this comment

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

With lazy initialization of the noCertificateCheck functionality, this was working, but only by accident. Now, we explicitly do all initialization in one place, and from every entrypoint.

@@ -633,7 +653,7 @@ public List<String> parseJnlpArguments() throws ParserConfigurationException, SA
while (true) {
URLConnection con = null;
try {
con = JnlpAgentEndpointResolver.openURLConnection(agentJnlpURL, agentJnlpCredentials, proxyCredentials, sslSocketFactory, noCertificateCheck, null);
con = JnlpAgentEndpointResolver.openURLConnection(agentJnlpURL, null, agentJnlpCredentials, proxyCredentials, sslSocketFactory, hostnameVerifier);
Copy link
Member Author

Choose a reason for hiding this comment

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

As elsewhere, putting mandatory user-specified arguments before optional ones, and putting user-specified arguments before computed things like SSL socket factories and hostname verifiers.

Comment on lines +89 to +101
private final String agentName;

private String credentials;

private String proxyCredentials;

private String tunnel;

private SSLSocketFactory sslSocketFactory;

private boolean disableHttpsCertValidation;

private final String agentName;
private HostnameVerifier hostnameVerifier;
Copy link
Member Author

Choose a reason for hiding this comment

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

As elsewhere, putting mandatory user-specified arguments before optional ones, and putting user-specified arguments before computed things like SSL socket factories and hostname verifiers.

after calling `Launcher#parseJnlpArguments` in which case
re-initialization needs to take place
I tested that nothing in Remoting or Swarm does this today.
@jglick
Copy link
Member

jglick commented Oct 24, 2023

internal seems inappropriate if this is needed for the Swarm plugin and thus merits a release; enhancement perhaps?

Copy link
Member

@jglick jglick left a comment

Choose a reason for hiding this comment

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

Looks right.

if (connectionTarget != null) {
initialize();
runAsTcpClient();
} else if (agentJnlpURL != null || !urls.isEmpty() || directConnection != null) {
Copy link
Member

@jglick jglick Oct 24, 2023

Choose a reason for hiding this comment

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

Fortunately this now has test coverage! (Though jenkinsci/bom#2417 is of no help since the relevant tests require special setup to run. And anyway the change needs to propagate through remotingdocker-agentdocker-inbound-agentkubernetes-plugin rather than the normal Maven dependency chain.)

@basil basil added bug For changelog: Fixes a bug. and removed internal labels Oct 25, 2023
@basil basil merged commit 9ee6d61 into jenkinsci:master Oct 25, 2023
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug For changelog: Fixes a bug.
Projects
None yet
2 participants