Skip to content

Commit 8e3766f

Browse files
authored
Merge pull request kubernetes#90215 from jasimmons/pr_agnhost_netexec_http
Adds HTTPS logic to agnhost netexec
2 parents 68cbb35 + d0f1981 commit 8e3766f

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

test/images/agnhost/README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ HTTP server:
375375

376376
### netexec
377377

378-
Starts a HTTP server on given port with the following endpoints:
378+
Starts a HTTP(S) server on given port with the following endpoints:
379379

380380
- `/`: Returns the request's timestamp.
381381
- `/clientip`: Returns the request's IP address.
@@ -407,6 +407,10 @@ Starts a HTTP server on given port with the following endpoints:
407407
Returns a JSON with the fields `output` (containing the file's name on the server) and
408408
`error` containing any potential server side errors.
409409

410+
If `--tls-cert-file` is added (ideally in conjunction with `--tls-private-key-file`, the HTTP server
411+
will be upgraded to HTTPS. The image has default, `localhost`-based cert/privkey files at
412+
`/localhost.crt` and `/localhost.key` (see: [`porter` subcommand](#porter))
413+
410414
It will also start a UDP server on the indicated UDP port that responds to the following commands:
411415

412416
- `hostname`: Returns the server's hostname
@@ -419,7 +423,7 @@ responding to the same commands as the UDP server.
419423
Usage:
420424

421425
```console
422-
kubectl exec test-agnhost -- /agnhost netexec [--http-port <http-port>] [--udp-port <udp-port>] [--sctp-port <sctp-port>]
426+
kubectl exec test-agnhost -- /agnhost netexec [--http-port <http-port>] [--udp-port <udp-port>] [--sctp-port <sctp-port>] [--tls-cert-file <cert-file>] [--tls-private-key-file <privkey-file>]
423427
```
424428

425429
### nettest

test/images/agnhost/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.14
1+
2.15

test/images/agnhost/agnhost.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import (
4949
)
5050

5151
func main() {
52-
rootCmd := &cobra.Command{Use: "app", Version: "2.14"}
52+
rootCmd := &cobra.Command{Use: "app", Version: "2.15"}
5353

5454
rootCmd.AddCommand(auditproxy.CmdAuditProxy)
5555
rootCmd.AddCommand(connect.CmdConnect)

test/images/agnhost/netexec/netexec.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,15 @@ var (
4444
sctpPort = -1
4545
shellPath = "/bin/sh"
4646
serverReady = &atomicBool{0}
47+
certFile = ""
48+
privKeyFile = ""
4749
)
4850

4951
// CmdNetexec is used by agnhost Cobra.
5052
var CmdNetexec = &cobra.Command{
5153
Use: "netexec",
52-
Short: "Creates HTTP, UDP, and (optionally) SCTP servers with various endpoints",
53-
Long: `Starts a HTTP server on given port with the following endpoints:
54+
Short: "Creates HTTP(S), UDP, and (optionally) SCTP servers with various endpoints",
55+
Long: `Starts a HTTP(S) server on given port with the following endpoints:
5456
5557
- /: Returns the request's timestamp.
5658
- /clientip: Returns the request's IP address.
@@ -97,6 +99,10 @@ responding to the same commands as the UDP server.
9799

98100
func init() {
99101
CmdNetexec.Flags().IntVar(&httpPort, "http-port", 8080, "HTTP Listen Port")
102+
CmdNetexec.Flags().StringVar(&certFile, "tls-cert-file", "",
103+
"File containing an x509 certificate for HTTPS. (CA cert, if any, concatenated after server cert)")
104+
CmdNetexec.Flags().StringVar(&privKeyFile, "tls-private-key-file", "",
105+
"File containing an x509 private key matching --tls-cert-file")
100106
CmdNetexec.Flags().IntVar(&udpPort, "udp-port", 8081, "UDP Listen Port")
101107
CmdNetexec.Flags().IntVar(&sctpPort, "sctp-port", -1, "SCTP Listen Port")
102108
}
@@ -125,10 +131,17 @@ func main(cmd *cobra.Command, args []string) {
125131
if sctpPort != -1 {
126132
go startSCTPServer(sctpPort)
127133
}
128-
startHTTPServer(httpPort)
134+
135+
addRoutes()
136+
if len(certFile) > 0 {
137+
// only start HTTPS server if a cert is provided
138+
startHTTPSServer(httpPort, certFile, privKeyFile)
139+
} else {
140+
startHTTPServer(httpPort)
141+
}
129142
}
130143

131-
func startHTTPServer(httpPort int) {
144+
func addRoutes() {
132145
http.HandleFunc("/", rootHandler)
133146
http.HandleFunc("/clientip", clientIPHandler)
134147
http.HandleFunc("/echo", echoHandler)
@@ -141,6 +154,13 @@ func startHTTPServer(httpPort int) {
141154
// older handlers
142155
http.HandleFunc("/hostName", hostNameHandler)
143156
http.HandleFunc("/shutdown", shutdownHandler)
157+
}
158+
159+
func startHTTPSServer(httpsPort int, certFile, privKeyFile string) {
160+
log.Fatal(http.ListenAndServeTLS(fmt.Sprintf(":%d", httpPort), certFile, privKeyFile, nil))
161+
}
162+
163+
func startHTTPServer(httpPort int) {
144164
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", httpPort), nil))
145165
}
146166

0 commit comments

Comments
 (0)