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
package sshserver
import (
"bytes"
"context"
"errors"
"net/http"
"time"
"github.com/johnandersen777/socialweb-computer-kcp/internal/bus"
"github.com/johnandersen777/socialweb-computer-kcp/internal/gateway/custody"
"github.com/johnandersen777/socialweb-computer-kcp/internal/gatewayhttp"
"github.com/johnandersen777/socialweb-computer-kcp/internal/ssh/ownersession"
"github.com/johnandersen777/socialweb-computer-kcp/internal/sshtransport"
)
const (
vaultObjectKind = "Vault"
signInRetryAttempts = 6
signInRetryMax = 30 * time.Second
)
var oauthRoutes = []string{"GET /oauth-client-metadata.json", "POST /oauth/start", "GET " + gatewayhttp.WebOAuthCallbackPath, "GET /session", "DELETE /session"}
type liveAuth struct {
s *Service
}
type liveAgents struct {
s *Service
}
var (
_ sshtransport.Authenticator = liveAuth{}
_ sshtransport.AgentProvider = liveAgents{}
)
func (a liveAuth) Authenticate(r *http.Request) (*gatewayhttp.WebCaller, error) {
web := a.s.currentWeb()
if web == nil {
return nil, nil
}
return web.auth.Authenticate(r)
}
func (a liveAgents) AgentFor(caller *gatewayhttp.WebCaller) gatewayhttp.WebAccountAgent {
web := a.s.currentWeb()
if web == nil {
return ownersession.Unavailable{}
}
return web.signIn.AgentFor(caller)
}
func (s *Service) currentWeb() *webSignIn {
s.mu.Lock()
defer s.mu.Unlock()
return s.web
}
func (s *Service) oauthRoute(w http.ResponseWriter, r *http.Request) {
web := s.currentWeb()
if web == nil {
http.Error(w, "the sign-in is not available", http.StatusServiceUnavailable)
return
}
web.handler.ServeHTTP(w, r)
}
func sameEndpoint(a, b *webSignIn) bool {
return a != nil && b != nil && a.endpoint.Address == b.endpoint.Address && bytes.Equal(a.endpoint.CACert, b.endpoint.CACert) &&
bytes.Equal(a.endpoint.ClientCert, b.endpoint.ClientCert) && bytes.Equal(a.endpoint.ClientKey, b.endpoint.ClientKey)
}
func (s *Service) awaitVault(ctx context.Context) error {
if s.spec.VaultRef == nil || s.spec.VaultRef.Name == "" || s.deps.Bus == nil {
return nil
}
workspace, name := s.workspace(), s.spec.VaultRef.Name
wake := make(chan struct{}, 1)
cancel := s.deps.Bus.Subscribe(func(key bus.Key) {
if key.Kind != vaultObjectKind || key.Name != name || key.Workspace != workspace {
return
}
select {
case wake <- struct{}{}:
default:
}
})
defer cancel()
waiting := false
for {
_, err := s.vaultEndpoint(ctx)
switch {
case err == nil:
if waiting {
s.log.Info("sshserver_vault_ready", "vault", workspace+"/"+name)
}
return nil
case !errors.Is(err, custody.ErrNotReady):
return err
case !waiting:
waiting = true
s.log.Info("sshserver_waits_for_vault", "vault", workspace+"/"+name, "reason", err.Error())
}
select {
case <-ctx.Done():
return ctx.Err()
case <-wake:
}
}
}
func (s *Service) followVault(ctx context.Context) func() {
if s.spec.VaultRef == nil || s.deps.Bus == nil || s.currentWeb() == nil {
return func() {}
}
workspace, name := s.workspace(), s.spec.VaultRef.Name
wake := make(chan struct{}, 1)
cancel := s.deps.Bus.Subscribe(func(key bus.Key) {
if key.Kind != vaultObjectKind || key.Name != name || key.Workspace != workspace {
return
}
select {
case wake <- struct{}{}:
default:
}
})
go func() {
for {
select {
case <-ctx.Done():
return
case <-wake:
}
for attempt := 1; !s.refreshSignIn(ctx) && attempt < signInRetryAttempts; attempt++ {
select {
case <-ctx.Done():
return
case <-wake:
case <-time.After(signInRetryDelay(attempt)):
}
}
}
}()
return cancel
}
func signInRetryDelay(attempt int) time.Duration {
return min(time.Second<<(attempt-1), signInRetryMax)
}
func (s *Service) refreshSignIn(ctx context.Context) bool {
next, err := s.openSignIn(ctx)
if err != nil {
s.log.Warn("sshserver_sign_in_refresh_failed", "err", err.Error())
return false
}
if next == nil {
return true
}
s.mu.Lock()
old := s.web
if sameEndpoint(old, next) {
s.mu.Unlock()
next.close()
return true
}
s.web = next
s.mu.Unlock()
old.close()
s.log.Info("sshserver_sign_in_follows_vault", "address", next.endpoint.Address)
return true
}