Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
Gitlab Shell
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Deploy
Releases
Package Registry
Container Registry
Operate
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to JiHu GitLab
Provide feedback
Keyboard shortcuts
?
What's new
5
Snippets
Groups
Projects
gitlab-org
Gitlab Shell
Commits
7e55ecd6
Commit
7e55ecd6
authored
4 years ago
by
Nick Thomas
Browse files
Options
Downloads
Patches
Plain Diff
sshd: Extract connections into their own file
parent
31920be4
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
internal/sshd/connection.go
+52
-0
52 additions, 0 deletions
internal/sshd/connection.go
internal/sshd/session.go
+7
-8
7 additions, 8 deletions
internal/sshd/session.go
internal/sshd/sshd.go
+15
-40
15 additions, 40 deletions
internal/sshd/sshd.go
with
74 additions
and
48 deletions
internal/sshd/connection.go
0 → 100644
+
52
−
0
View file @
7e55ecd6
package
sshd
import
(
"context"
log
"github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh"
"golang.org/x/sync/semaphore"
"gitlab.com/gitlab-org/gitlab-shell/internal/config"
)
type
connection
struct
{
// State set up by the sshd
cfg
*
config
.
Config
gitlabKeyId
string
remoteAddr
string
}
func
(
c
*
connection
)
handle
(
ctx
context
.
Context
,
chans
<-
chan
ssh
.
NewChannel
)
{
concurrentSessions
:=
semaphore
.
NewWeighted
(
c
.
cfg
.
Server
.
ConcurrentSessionsLimit
)
for
newChannel
:=
range
chans
{
if
newChannel
.
ChannelType
()
!=
"session"
{
newChannel
.
Reject
(
ssh
.
UnknownChannelType
,
"unknown channel type"
)
continue
}
if
!
concurrentSessions
.
TryAcquire
(
1
)
{
newChannel
.
Reject
(
ssh
.
ResourceShortage
,
"too many concurrent sessions"
)
sshdHitMaxSessions
.
Inc
()
continue
}
channel
,
requests
,
err
:=
newChannel
.
Accept
()
if
err
!=
nil
{
log
.
Infof
(
"Could not accept channel: %v"
,
err
)
concurrentSessions
.
Release
(
1
)
continue
}
go
func
()
{
defer
concurrentSessions
.
Release
(
1
)
session
:=
&
session
{
cfg
:
c
.
cfg
,
channel
:
channel
,
gitlabKeyId
:
c
.
gitlabKeyId
,
remoteAddr
:
c
.
remoteAddr
,
}
session
.
handle
(
ctx
,
requests
)
}()
}
}
This diff is collapsed.
Click to expand it.
internal/sshd/session.go
+
7
−
8
View file @
7e55ecd6
...
...
@@ -3,7 +3,6 @@ package sshd
import
(
"context"
"fmt"
"net"
"golang.org/x/crypto/ssh"
...
...
@@ -15,11 +14,11 @@ import (
)
type
session
struct
{
// State set up by
handleCon
n
cfg
*
config
.
Config
channel
ssh
.
Channel
sconn
*
ssh
.
ServerConn
nconn
net
.
Conn
// State set up by
the connectio
n
cfg
*
config
.
Config
channel
ssh
.
Channel
gitlabKeyId
string
remoteAddr
string
// State managed by the session
execCmd
string
...
...
@@ -106,12 +105,12 @@ func (s *session) handleShell(ctx context.Context, req *ssh.Request) uint32 {
}
args
:=
&
commandargs
.
Shell
{
GitlabKeyId
:
s
.
sconn
.
Permissions
.
Extensions
[
"key-id"
]
,
GitlabKeyId
:
s
.
gitlabKeyId
,
Env
:
sshenv
.
Env
{
IsSSHConnection
:
true
,
OriginalCommand
:
s
.
execCmd
,
GitProtocolVersion
:
s
.
gitProtocolVersion
,
RemoteAddr
:
s
.
nconn
.
R
emoteAddr
()
.
(
*
net
.
TCPAddr
)
.
String
()
,
RemoteAddr
:
s
.
r
emoteAddr
,
},
}
...
...
This diff is collapsed.
Click to expand it.
internal/sshd/sshd.go
+
15
−
40
View file @
7e55ecd6
...
...
@@ -16,7 +16,6 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"golang.org/x/crypto/ssh"
"golang.org/x/sync/semaphore"
"gitlab.com/gitlab-org/gitlab-shell/internal/config"
"gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet/authorizedkeys"
...
...
@@ -81,7 +80,7 @@ func Run(cfg *config.Config) error {
log
.
Infof
(
"Listening on %v"
,
sshListener
.
Addr
()
.
String
())
confi
g
:=
&
ssh
.
ServerConfig
{
sshCf
g
:=
&
ssh
.
ServerConfig
{
PublicKeyCallback
:
func
(
conn
ssh
.
ConnMetadata
,
key
ssh
.
PublicKey
)
(
*
ssh
.
Permissions
,
error
)
{
if
conn
.
User
()
!=
cfg
.
User
{
return
nil
,
errors
.
New
(
"unknown user"
)
...
...
@@ -118,7 +117,7 @@ func Run(cfg *config.Config) error {
continue
}
loadedHostKeys
++
confi
g
.
AddHostKey
(
key
)
sshCf
g
.
AddHostKey
(
key
)
}
if
loadedHostKeys
==
0
{
return
fmt
.
Errorf
(
"No host keys could be loaded, aborting"
)
...
...
@@ -131,55 +130,31 @@ func Run(cfg *config.Config) error {
continue
}
go
handleConn
(
nconn
,
config
,
cfg
)
go
acceptConn
(
cfg
,
sshCfg
,
nconn
)
}
}
func
handleConn
(
nconn
net
.
Con
n
,
sshCfg
*
ssh
.
ServerConfig
,
cfg
*
config
.
Con
fig
)
{
func
acceptConn
(
cfg
*
config
.
Con
fig
,
sshCfg
*
ssh
.
ServerConfig
,
nconn
net
.
Con
n
)
{
begin
:=
time
.
Now
()
defer
func
()
{
sshdConnectionDuration
.
Observe
(
time
.
Since
(
begin
)
.
Seconds
())
}()
ctx
,
cancel
:=
context
.
WithCancel
(
context
.
Background
())
defer
cancel
()
defer
sshdConnectionDuration
.
Observe
(
time
.
Since
(
begin
)
.
Seconds
())
defer
nconn
.
Close
()
conn
,
chans
,
reqs
,
err
:=
ssh
.
NewServerConn
(
nconn
,
sshCfg
)
defer
cancel
()
sconn
,
chans
,
reqs
,
err
:=
ssh
.
NewServerConn
(
nconn
,
sshCfg
)
if
err
!=
nil
{
log
.
Infof
(
"Failed to initialize SSH connection: %v"
,
err
)
return
}
concurrentSessions
:=
semaphore
.
NewWeighted
(
cfg
.
Server
.
ConcurrentSessionsLimit
)
go
ssh
.
DiscardRequests
(
reqs
)
for
newChannel
:=
range
chans
{
if
newChannel
.
ChannelType
()
!=
"session"
{
newChannel
.
Reject
(
ssh
.
UnknownChannelType
,
"unknown channel type"
)
continue
}
if
!
concurrentSessions
.
TryAcquire
(
1
)
{
newChannel
.
Reject
(
ssh
.
ResourceShortage
,
"too many concurrent sessions"
)
sshdHitMaxSessions
.
Inc
()
continue
}
ch
,
requests
,
err
:=
newChannel
.
Accept
()
if
err
!=
nil
{
log
.
Infof
(
"Could not accept channel: %v"
,
err
)
concurrentSessions
.
Release
(
1
)
continue
}
go
func
()
{
defer
concurrentSessions
.
Release
(
1
)
session
:=
&
session
{
cfg
:
cfg
,
channel
:
ch
,
sconn
:
conn
,
nconn
:
nconn
,
}
session
.
handle
(
ctx
,
requests
)
}()
conn
:=
&
connection
{
cfg
:
cfg
,
gitlabKeyId
:
sconn
.
Permissions
.
Extensions
[
"key-id"
],
remoteAddr
:
nconn
.
RemoteAddr
()
.
(
*
net
.
TCPAddr
)
.
String
(),
}
conn
.
handle
(
ctx
,
chans
)
}
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment