Use built-in WinSW features for permission elevation ans strong cryptography (#43) @NextTurn
open the node connection page -> Launch -> File -> Install as a server
1.5.1.1.2 -- start server manually
setup jenkins-slave.exe
1.5.1.1.3 -- run as administrator
jenkins-slave.xml
<!--
The MIT LicenseCopyright (c) 2004-2017, Sun Microsystems, Inc., Kohsuke Kawaguchi, Oleg Nenashev and other contributors
Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included inall copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS INTHE SOFTWARE.
-->
<!--
Windows service definition for Jenkins agent.This service is powered by the WinSW project: https://github.com/kohsuke/winsw/You can find more information about available options here: https://github.com/kohsuke/winsw/blob/master/doc/xmlConfigFile.mdConfiguration examples are available here: https://github.com/kohsuke/winsw/tree/master/examplesTo uninstall, run "jenkins-slave.exe stop" to stop the service, then "jenkins-slave.exe uninstall" to uninstall the service.Both commands don't produce any output if the execution is successful.
-->
<service>
<id>jenkins</id>
<name>jenkins</name>
<description>the windows agent for jenkins https://jenkins.sample.com</description>
<!--
if you'd like to run Jenkins with a specific version of Java, specify a full path to java.exe.The following value assumes that you have java in your PATH.
-->
<executable>C:\Program Files\Java\jdk-11.0.16.1\bin\java.exe</executable>
<arguments>-Xrs -jar "%BASE%\agent.jar" -jnlpUrl https://jenkins.sample.com/computer/win-server-01/slave-agent.jnlp -secret c**************************************************************1</arguments>
<!--
interactive flag causes the empty black Java window to be displayed.I'm still debugging this.
<interactive />
-->
<logmode>rotate</logmode>
<onfailure action="restart" />
<!--
If uncommented, download the Remoting version provided by the Jenkins master.Enabling for HTTP implies security risks (e.g. replacement of JAR via DNS poisoning). Use on your own risk.
NOTE: This option may fail to work correctly (e.g. if Jenkins is located behind HTTPS with untrusted certificate).
In such case the old agent version will be used; you can replace slave.jar manually or to specify another download URL.
-->
<download from="https://jenkins.sample.com/jnlpJars/agent.jar" to="%BASE%\agent.jar"/>
<!--
In the case WinSW gets terminated and leaks the process, we want to abortthese runaway JAR processes on startup to prevent "Slave is already connected errors" (JENKINS-28492).
-->
<extensions>
<!-- This is a sample configuration for the RunawayProcessKiller extension. -->
<extension enabled="true"
className="winsw.Plugins.RunawayProcessKiller.RunawayProcessKillerExtension"id="killOnStartup">
<pidfile>%BASE%\jenkins_agent.pid</pidfile>
<stopTimeout>5000</stopTimeout>
<stopParentFirst>false</stopParentFirst>
</extension>
</extensions>
<!-- See referenced examples for more options -->
</service>
J:\jenkins> jenkins-agent.exe --helpA wrapper binary that can be used to host executables as Windows servicesUsage: winsw <command> [<args>]Missing arguments triggers the service modeAvailable commands:install install the service to Windows Service Controlleruninstall uninstall the servicestart start the service (must be installed before)stop stop the servicestopwait stop the service and wait until it's actually stoppedrestart restart the servicerestart! self-restart (can be called from child processes)status check the current status of the servicetest check if the service can be started and then stoppedtestwait starts the service and waits until a key is pressed then stops the serviceversion print the version infohelp print the help info (aliases: -h,--help,-?,/?)Extra options:
/redirect redirect the wrapper's STDOUT and STDERR to the specified file
WinSW2.11.0.0More info: https://github.com/winsw/winswBug tracker: https://github.com/winsw/winsw/issues
2022-11-30 06:31:37,126 DEBUG - Starting WinSW in console mode
2022-11-30 06:31:37,166 INFO - Starting service'jenkins (jenkins)'...
2022-11-30 06:31:37,903 DEBUG - Starting WinSW inservice mode
2022-11-30 06:31:37,940 INFO - Service 'jenkins (jenkins)' started successfully.
2022-11-30 06:31:38,022 INFO - Downloading: https://jenkins.sample.com/jnlpJars/agent.jar to J:\jenkins\agent.jar. failOnError=False
2022-11-30 06:31:38,945 INFO - Skipped downloading unmodified resource 'https://jenkins.sample.com/jnlpJars/agent.jar'2022-11-30 06:31:39,029 INFO - Starting C:\Program Files\Java\jdk-11.0.16.1\bin\java.exe -jar agent.jar -jnlpUrl https://jenkins.sample.com/computer/windows-agent/jenkins-agent.jnlp -secret f**************************************************************8 -workDir"J:\jenkins"2022-11-30 06:31:39,066 INFO - Started process 49282022-11-30 06:31:39,079 DEBUG - Forwarding logs of the process System.Diagnostics.Process (java) to WinSW.RollingLogAppender
2022-11-30 06:31:44,302 DEBUG - Starting WinSW in console mode
jenkins-agent.err.log
Nov 30, 20226:31:39 AM org.jenkinsci.remoting.engine.WorkDirManager initializeWorkDir
INFO: Using J:\jenkins\remoting as a remoting work directory
Nov 30, 20226:31:39 AM org.jenkinsci.remoting.engine.WorkDirManager setupLogging
INFO: Both error and output logs will be printed to J:\jenkins\remoting
Nov 30, 20226:31:40 AM hudson.remoting.jnlp.Main createEngine
INFO: Setting up agent: sc-jenkins112
Nov 30, 20226:31:40 AM hudson.remoting.Engine startEngine
INFO: Using Remoting version: 3044.vb_940a_a_e4f72e
Nov 30, 20226:31:40 AM org.jenkinsci.remoting.engine.WorkDirManager initializeWorkDir
INFO: Using J:\jenkins\remoting as a remoting work directory
Nov 30, 20226:31:40 AM hudson.remoting.jnlp.Main$CuiListener status
INFO: WebSocket connection open
Nov 30, 20226:31:40 AM hudson.remoting.jnlp.Main$CuiListener status
INFO: Connected
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.jenkinsci.plugins.envinject.service.EnvInjectMasterEnvVarsSetter to method java.lang.Class.getDeclaredFields0(boolean)
WARNING: Please consider reporting this to the maintainers of org.jenkinsci.plugins.envinject.service.EnvInjectMasterEnvVarsSetter
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
stop services
J:\jenkins> jenkins-agent.exe stop2022-11-3006:15:28,194 INFO - Stopping service 'jenkins (jenkins)'...
2022-11-3006:15:28,216 INFO - Service 'jenkins (jenkins)' stopped successfully.
J:\jenkins> sc query jenkinsSERVICE_NAME: jenkinsTYPE : 10 WIN32_OWN_PROCESSSTATE : 1 STOPPEDWIN32_EXIT_CODE : 0 (0x0)SERVICE_EXIT_CODE : 0 (0x0)CHECKPOINT : 0x0WAIT_HINT : 0x0
Q&A
a windows service must first be installed ( using installutil.exe )
C:\WINDOWS\system32> cd C:\Windows\Microsoft.NET\Framework\v4.0.30319C:\Windows\Microsoft.NET\Framework\v4.0.30319> InstallUtil.exe J:\jenkins\jenkins-slave.exeMicrosoft (R) .NET Framework Installation utility Version 4.8.3761.0
Copyright (C) Microsoft Corporation. All rights reserved.
Running a transacted installation.Beginning the Install phase of the installation.See the contents of the log file for the J:\jenkins\jenkins-slave.exe assembly's progress.The file is located at J:\jenkins\jenkins-slave.InstallLog.Installing assembly 'J:\jenkins\jenkins-slave.exe'.Affected parameters are:logtoconsole =logfile = J:\jenkins\jenkins-slave.InstallLogassemblypath = J:\jenkins\jenkins-slave.exeNo public installers with the RunInstallerAttribute.Yes attribute could be found in the J:\jenkins\jenkins-slave.exe assembly.The Install phase completed successfully, and the Commit phase is beginning.See the contents of the log file for the J:\jenkins\jenkins-slave.exe assembly's progress.The file is located at J:\jenkins\jenkins-slave.InstallLog.Committing assembly 'J:\jenkins\jenkins-slave.exe'.Affected parameters are:logtoconsole =logfile = J:\jenkins\jenkins-slave.InstallLogassemblypath = J:\jenkins\jenkins-slave.exeNo public installers with the RunInstallerAttribute.Yes attribute could be found in the J:\jenkins\jenkins-slave.exe assembly.Remove InstallState file because there are no installers.The Commit phase completed successfully.The transacted install has completed.