forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_with_roles.go
230 lines (200 loc) · 6.47 KB
/
api_with_roles.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
Copyright 2015 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package auth
import (
"io"
"net"
"net/http"
"sync"
"time"
"github.com/gravitational/teleport"
"github.com/gravitational/teleport/lib/events"
"github.com/gravitational/teleport/lib/session"
"github.com/gravitational/teleport/lib/utils"
log "github.com/Sirupsen/logrus"
"github.com/gravitational/trace"
"golang.org/x/crypto/ssh"
)
// APIWithRoles is a wrapper around several APIServer objects. For security
// benefits, every "teleport role" talks to its own instance of an APIServer.
//
// Every client always authenticates with a SSH certificate, which has a role
// encoded in it. Based on that role they get their own instance of APIServer
// to talk to.
//
// This allows APIServer to exist in a more trustworthy, separated-by-role
// context.
type APIWithRoles struct {
config APIConfig
listeners map[teleport.Role]*fakeSocket
servers map[teleport.Role]*APIServer
askedToStop bool
}
// APIConfig is a configuration file
type APIConfig struct {
AuthServer *AuthServer
SessionService session.Service
Roles []teleport.Role
PermissionChecker PermissionChecker
AuditLog events.IAuditLog
}
func NewAPIWithRoles(config APIConfig) *APIWithRoles {
api := APIWithRoles{}
api.listeners = make(map[teleport.Role]*fakeSocket)
api.servers = make(map[teleport.Role]*APIServer)
api.config = config
// create a new APIServer instance for every possible telerpot
// role and have them listen on fake sockets (we'll proxy requests
// for them based on client's roles - see HandleNewChannel)
for _, role := range config.Roles {
api.servers[role] = NewAPIServer(&AuthWithRoles{
authServer: config.AuthServer,
permChecker: config.PermissionChecker,
sessions: config.SessionService,
role: role,
alog: config.AuditLog,
})
api.listeners[role] = makefakeSocket()
}
return &api
}
func (api *APIWithRoles) Serve() {
wg := sync.WaitGroup{}
for role := range api.listeners {
wg.Add(1)
go func(listener net.Listener, handler http.Handler) {
defer wg.Done()
if err := http.Serve(listener, handler); (err != nil) && (err != io.EOF) {
if !api.askedToStop {
log.Errorf(err.Error())
}
}
}(api.listeners[role], api.servers[role])
}
wg.Wait()
log.Infof("[AUTH] API exited")
}
func (api *APIWithRoles) Close() {
api.askedToStop = true
for _, listener := range api.listeners {
listener.Close()
}
}
// HandleNewChannel is called when a new SSH channel (SSH connection) wants to communicate via HTTP API
// to one of the API servers
func (api *APIWithRoles) HandleNewChannel(remoteAddr net.Addr, channel ssh.Channel, role teleport.Role) error {
// find a listener mapped to the requested role:
listener, ok := api.listeners[role]
if !ok {
channel.Close()
return trace.Errorf("no such role '%v'", role)
}
// create a bridge between the incoming SSH channel to the HTTP-based API server
return listener.CreateBridge(remoteAddr, channel)
}
// Implements a fake "socket" (net.Listener interface) on top of exisitng ssh.Channel
type fakeSocket struct {
closed chan int
connections chan net.Conn
closeOnce sync.Once
}
func makefakeSocket() *fakeSocket {
return &fakeSocket{
closed: make(chan int),
connections: make(chan net.Conn),
}
}
type FakeSSHConnection struct {
remoteAddr net.Addr
sshChan ssh.Channel
closeOnce sync.Once
closed chan int
}
func (conn *FakeSSHConnection) Read(b []byte) (n int, err error) {
return conn.sshChan.Read(b)
}
func (conn *FakeSSHConnection) Write(b []byte) (n int, err error) {
return conn.sshChan.Write(b)
}
func (conn *FakeSSHConnection) Close() error {
// broadcast the closing signal to all waiting parties
conn.closeOnce.Do(func() {
close(conn.closed)
})
return trace.Wrap(conn.sshChan.Close())
}
func (conn *FakeSSHConnection) RemoteAddr() net.Addr {
return conn.remoteAddr
}
func (conn *FakeSSHConnection) LocalAddr() net.Addr {
return &utils.NetAddr{AddrNetwork: "tcp", Addr: "socket.over.ssh"}
}
// SetDeadline is needed to implement net.Conn interface
func (conn *FakeSSHConnection) SetDeadline(t time.Time) error {
return nil
}
// SetReadDeadline is needed to implement net.Conn interface
func (conn *FakeSSHConnection) SetReadDeadline(t time.Time) error {
return nil
}
// SetWriteDeadline is needed to implement net.Conn interface
func (conn *FakeSSHConnection) SetWriteDeadline(t time.Time) error {
return nil
}
// CreateBridge takes an incoming SSH connection and creates an SSH-to-HTTP "bridge connection"
// and waits for that connection to be closed either by the client or by the server
func (socket *fakeSocket) CreateBridge(remoteAddr net.Addr, sshChan ssh.Channel) error {
if sshChan == nil {
return trace.BadParameter("sshChan: supply ssh channel")
}
// wrap sshChan into a 'fake connection' which allows us to
// a) preserve the original address of the connected client
// b) sit and wait until client closes the ssh channel, so we'll close this fake socket
connection := &FakeSSHConnection{
remoteAddr: remoteAddr,
sshChan: sshChan,
closed: make(chan int),
}
select {
// Accept() will unblock this select
case socket.connections <- connection:
}
// wait for the connection to close:
select {
case <-connection.closed:
}
return nil
}
// Accept waits for new connections to arrive (via CreateBridge) and returns them to
// the blocked http.Serve()
func (socket *fakeSocket) Accept() (c net.Conn, err error) {
select {
case newConnection := <-socket.connections:
return newConnection, nil
case <-socket.closed:
return nil, io.EOF
}
}
// Close closes the listener.
// Any blocked Accept operations will be unblocked and return errors.
func (socket *fakeSocket) Close() error {
socket.closeOnce.Do(func() {
// broadcast that listener has closed to all listening parties
close(socket.closed)
})
return nil
}
// Addr returns the listener's network address.
func (socket *fakeSocket) Addr() net.Addr {
return &utils.NetAddr{AddrNetwork: "tcp", Addr: "socket.over.ssh"}
}