Use built-in WinSW features for permission elevation ans strong cryptography (#43) @NextTurn
open the node connection page -> Launch -> File -> Install as a server
setup jenkins-slave.exe
<!--The MIT LicenseCopyright (c) 2004-2017, Sun Microsystems, Inc., Kohsuke Kawaguchi, Oleg Nenashev and other contributorsPermission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin 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.md
Configuration examples are available here: https://github.com/kohsuke/winsw/tree/master/examples To 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 abort these 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 formore options --></service>
J:\jenkins> cat > j:\jenkins\jenkins-agent.xml << EOF<service><id>jenkins-agent</id><name>jenkins-agent</name><description>the jenkins agent process connect to https://jenkins.sample.com.</description><!-- to specific the java version --><executable>C:\Program Files\Java\jdk-11.0.16.1\bin\java.exe</executable><!-- get from jenkins agent page --> <arguments>-jar %BASE%\agent.jar -jnlpUrl https://jenkins.sample.com/computer/windows-agent/jenkins-agent.jnlp -secret f**************************************************************8 -workDir "J:\jenkins"</arguments>
<log mode="roll" /><onfailure action="restart" /><!-- download the Remoting version provided by the Jenkins master. --><download from="https://jenkins.sample.com/jnlpJars/agent.jar" to="%BASE%\agent.jar"/></service>EOF
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 Controller uninstall uninstall the servicestartstart the service (must be installed before) stop stop the service stopwait stop the service and wait until it's actually stopped restart restart the service restart! self-restart (can be called from child processes) status check the current status of the service test check if the service can be started and then stopped testwait starts the service and waits until a key is pressed then stops the service version print the version info help print the help info (aliases: -h,--help,-?,/?)Extra options: /redirect redirect the wrapper's STDOUT and STDERR to the specified fileWinSW 2.11.0.0More info: https://github.com/winsw/winswBug tracker: https://github.com/winsw/winsw/issues
J:\jenkins> jenkins-agent.exe install2022-11-30 06:09:59,907 INFO - Installing service 'jenkins (jenkins)'...2022-11-30 06:10:00,033 INFO - Service 'jenkins (jenkins)' was installed successfully.J:\jenkins>scquery jenkinsSERVICE_NAME: jenkinsTYPE : 10 WIN32_OWN_PROCESS STATE : 1 STOPPED WIN32_EXIT_CODE : 1077 (0x435) SERVICE_EXIT_CODE : 0 (0x0) CHECKPOINT : 0x0 WAIT_HINT : 0x0REM start serviceJ:\jenkins> jenkins-agent.exe start2022-11-30 06:11:26,118 INFO - Starting service 'jenkins (jenkins)'...2022-11-30 06:11:27,510 INFO - Service 'jenkins (jenkins)' started successfully.J:\jenkins>scquery jenkinsSERVICE_NAME: jenkinsTYPE : 10 WIN32_OWN_PROCESS STATE : 2 START_PENDING (STOPPABLE, NOT_PAUSABLE, ACCEPTS_SHUTDOWN) WIN32_EXIT_CODE : 0 (0x0) SERVICE_EXIT_CODE : 0 (0x0) CHECKPOINT : 0x0 WAIT_HINT : 0x0J:\jenkins>scquery jenkinsSERVICE_NAME: jenkinsTYPE : 10 WIN32_OWN_PROCESS STATE : 4 RUNNING (STOPPABLE, NOT_PAUSABLE, ACCEPTS_SHUTDOWN) WIN32_EXIT_CODE : 0 (0x0) SERVICE_EXIT_CODE : 0 (0x0) CHECKPOINT : 0x0 WAIT_HINT : 0x0
2022-11-3006:31:37,126DEBUG-StartingWinSWinconsolemode2022-11-3006:31:37,166INFO-Startingservice'jenkins (jenkins)'...2022-11-3006:31:37,903DEBUG-StartingWinSWinservicemode2022-11-3006:31:37,940INFO-Service'jenkins (jenkins)'startedsuccessfully.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-3006:31:38,945INFO-Skippeddownloadingunmodifiedresource'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-3006:31:39,066INFO-Startedprocess49282022-11-30 06:31:39,079 DEBUG - Forwarding logs of the process System.Diagnostics.Process (java) to WinSW.RollingLogAppender
2022-11-3006:31:44,302DEBUG-StartingWinSWinconsolemode
Nov30,20226:31:39AMorg.jenkinsci.remoting.engine.WorkDirManagerinitializeWorkDirINFO:UsingJ:\jenkins\remotingasaremotingworkdirectoryNov30,20226:31:39AMorg.jenkinsci.remoting.engine.WorkDirManagersetupLoggingINFO:BotherrorandoutputlogswillbeprintedtoJ:\jenkins\remotingNov30,20226:31:40AMhudson.remoting.jnlp.MaincreateEngineINFO:Settingupagent:sc-jenkins112Nov30,20226:31:40AMhudson.remoting.EnginestartEngineINFO:UsingRemotingversion:3044.vb_940a_a_e4f72eNov30,20226:31:40AMorg.jenkinsci.remoting.engine.WorkDirManagerinitializeWorkDirINFO:UsingJ:\jenkins\remotingasaremotingworkdirectoryNov30,20226:31:40AMhudson.remoting.jnlp.Main$CuiListener statusINFO:WebSocketconnectionopenNov30,20226:31:40AMhudson.remoting.jnlp.Main$CuiListener statusINFO:ConnectedWARNING:AnillegalreflectiveaccessoperationhasoccurredWARNING: 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=warntoenablewarningsoffurtherillegalreflectiveaccessoperationsWARNING:Allillegalaccessoperationswillbedeniedinafuturerelease
stop services
J:\jenkins> jenkins-agent.exe stop2022-11-30 06:15:28,194 INFO - Stopping service 'jenkins (jenkins)'...2022-11-30 06:15:28,216 INFO - Service 'jenkins (jenkins)' stopped successfully.J:\jenkins>scquery jenkinsSERVICE_NAME: jenkinsTYPE : 10 WIN32_OWN_PROCESS STATE : 1 STOPPED WIN32_EXIT_CODE : 0 (0x0) SERVICE_EXIT_CODE : 0 (0x0) CHECKPOINT : 0x0 WAIT_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.0Copyright (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.InstallLog assemblypath = 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.InstallLog assemblypath = 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.