|
| 1 | +// Copyright 2020-2025 Politecnico di Torino |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// Package main contains the entrypoint for webSSH, a WebSocket SSH bridge for CrownLabs. |
| 16 | +package main |
| 17 | + |
| 18 | +import ( |
| 19 | + "flag" |
| 20 | + "log" |
| 21 | + "os" |
| 22 | + "strconv" |
| 23 | + "time" |
| 24 | + |
| 25 | + "github.com/go-logr/stdr" |
| 26 | + "k8s.io/apimachinery/pkg/runtime" |
| 27 | + clientgoscheme "k8s.io/client-go/kubernetes/scheme" |
| 28 | + _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" |
| 29 | + |
| 30 | + crownlabsv1alpha1 "github.com/netgroup-polito/CrownLabs/operators/api/v1alpha1" |
| 31 | + crownlabsv1alpha2 "github.com/netgroup-polito/CrownLabs/operators/api/v1alpha2" |
| 32 | + "github.com/netgroup-polito/CrownLabs/operators/pkg/utils" |
| 33 | + "github.com/netgroup-polito/CrownLabs/operators/pkg/webssh-bastion" |
| 34 | +) |
| 35 | + |
| 36 | +var ( |
| 37 | + scheme = runtime.NewScheme() |
| 38 | +) |
| 39 | + |
| 40 | +func init() { |
| 41 | + _ = clientgoscheme.AddToScheme(scheme) |
| 42 | + _ = crownlabsv1alpha1.AddToScheme(scheme) |
| 43 | + _ = crownlabsv1alpha2.AddToScheme(scheme) |
| 44 | +} |
| 45 | + |
| 46 | +func main() { |
| 47 | + sshUserFlag := flag.String("websshuser", "crownlabs", "The user to use for SSH connections.") |
| 48 | + websshprivatekeypathFlag := flag.String("websshprivatekeypath", "", "The path to the private key file for SSH authentication.") |
| 49 | + websshtimeoutdurationFlag := flag.String("websshtimeoutduration", "0", "The timeout duration for SSH connections. In minutes.") |
| 50 | + websshmaxconncountFlag := flag.String("websshmaxconncount", "1000", "The maximum number of concurrent SSH connections.") |
| 51 | + websshvmport := flag.String("websshvmport", "22", "The default SSH port for VMs.") |
| 52 | + websshwebsocketportFlag := flag.String("websshwebsocketport", "8085", "The port on which the WebSocket server listens.") |
| 53 | + |
| 54 | + flag.Parse() |
| 55 | + |
| 56 | + timeout64, err := strconv.ParseInt(*websshtimeoutdurationFlag, 10, 32) // parse directly as int32 |
| 57 | + if err != nil { |
| 58 | + timeout64 = 30 |
| 59 | + } |
| 60 | + |
| 61 | + maxConn64, err := strconv.ParseInt(*websshmaxconncountFlag, 10, 32) |
| 62 | + if err != nil { |
| 63 | + maxConn64 = 1000 |
| 64 | + } |
| 65 | + |
| 66 | + stdLogger := log.New(os.Stderr, "", log.LstdFlags) |
| 67 | + baseLogger := stdr.New(stdLogger) |
| 68 | + |
| 69 | + webSSHCtx := &webssh.ServerContext{} |
| 70 | + webSSHCtx.BaseLogger = baseLogger |
| 71 | + webSSHCtx.SSHUser = *sshUserFlag |
| 72 | + webSSHCtx.PrivateKeyPath = *websshprivatekeypathFlag |
| 73 | + webSSHCtx.TimeoutDuration = time.Duration(timeout64) * time.Minute |
| 74 | + webSSHCtx.MaxConnectionCount = int32(maxConn64) |
| 75 | + webSSHCtx.WebsocketPort = *websshwebsocketportFlag |
| 76 | + webSSHCtx.VMSSHPort = *websshvmport |
| 77 | + webSSHCtx.BaseConfig, err = utils.GetRestConfig() |
| 78 | + |
| 79 | + if err != nil { |
| 80 | + webSSHCtx.BaseLogger.Error(err, "Failed to get REST config") |
| 81 | + return |
| 82 | + } |
| 83 | + |
| 84 | + info, err := os.Stat(webSSHCtx.PrivateKeyPath) |
| 85 | + if err != nil { |
| 86 | + webSSHCtx.BaseLogger.Error(err, "Cannot access private key file") |
| 87 | + return |
| 88 | + } |
| 89 | + |
| 90 | + if info.IsDir() { |
| 91 | + webSSHCtx.BaseLogger.Error(nil, "Private key path points to a directory, not a file") |
| 92 | + return |
| 93 | + } |
| 94 | + |
| 95 | + webSSHCtx.BaseLogger.Info("Config loaded", |
| 96 | + "SSHUser", webSSHCtx.SSHUser, |
| 97 | + "PrivateKeyPath", webSSHCtx.PrivateKeyPath, |
| 98 | + "TimeoutDuration", webSSHCtx.TimeoutDuration, |
| 99 | + "MaxConnectionCount", webSSHCtx.MaxConnectionCount, |
| 100 | + "WebsocketPort", webSSHCtx.WebsocketPort, |
| 101 | + "VMSSHPort", webSSHCtx.VMSSHPort) |
| 102 | + |
| 103 | + webSSHCtx.StartWebSSH() |
| 104 | +} |
0 commit comments