fix: Ffixes to console
This commit is contained in:
@@ -92,9 +92,23 @@ const (
|
||||
// ProxyEndChannel carries a console relay's terminal reason back to the pod
|
||||
// serving the WebSocket, which is the pod that has to write the audit event.
|
||||
ProxyEndChannel = prefix + "proxyend:"
|
||||
// ProxyAddrChannel carries the address of a console relay listener back to
|
||||
// the pod serving the WebSocket.
|
||||
//
|
||||
// The listener cannot be bound in advance on any particular pod. An agent's
|
||||
// ProxyStream is a separate HTTP/2 request from its CommandStream, and an
|
||||
// L7 proxy (Traefik) balances requests, not connections — so it may land on
|
||||
// any replica, not the one holding the command stream. The pod it does land
|
||||
// on binds the listener and announces it here.
|
||||
ProxyAddrChannel = prefix + "proxyaddr:"
|
||||
|
||||
// PresenceKey records which node holds an agent's command stream.
|
||||
PresenceKey = prefix + "agent:"
|
||||
// ProxyPendingKey authorises one not-yet-opened ProxyStream. It is the only
|
||||
// state tying a proxy_id to the instance and server it was minted for, and
|
||||
// it must be visible to every replica because any of them may receive the
|
||||
// stream.
|
||||
ProxyPendingKey = prefix + "proxypending:"
|
||||
// leaderKey records the holder of a named singleton job.
|
||||
leaderKey = prefix + "leader:"
|
||||
)
|
||||
@@ -212,6 +226,54 @@ func IsConnected(ctx context.Context, serverID string) bool {
|
||||
return err == nil && n > 0
|
||||
}
|
||||
|
||||
// SetPendingProxy records that proxyID has been minted for instanceID and
|
||||
// serverID, for ttl. Written before the OpenProxyCmd is dispatched, so it is in
|
||||
// place before any agent can act on it.
|
||||
func SetPendingProxy(ctx context.Context, proxyID, instanceID, serverID string, ttl time.Duration) error {
|
||||
b, err := json.Marshal(map[string]string{"instance_id": instanceID, "server_id": serverID})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return rdb.Set(ctx, ProxyPendingKey+proxyID, b, ttl).Err()
|
||||
}
|
||||
|
||||
// ClaimPendingProxy consumes proxyID's pending record and returns the instance
|
||||
// and server it was minted for. Get and delete are one Lua call rather than two
|
||||
// round trips: single use is the whole security property, and two agents
|
||||
// racing the same proxy_id must not both be served.
|
||||
//
|
||||
// A missing record is reported as "", "" rather than an error — an unknown
|
||||
// proxy_id, an expired one and a second claim are all the same refusal.
|
||||
func ClaimPendingProxy(ctx context.Context, proxyID string) (instanceID, serverID string) {
|
||||
v, err := claimPending.Run(ctx, rdb, []string{ProxyPendingKey + proxyID}).Text()
|
||||
if err != nil || v == "" {
|
||||
return "", ""
|
||||
}
|
||||
var rec struct {
|
||||
InstanceID string `json:"instance_id"`
|
||||
ServerID string `json:"server_id"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(v), &rec); err != nil {
|
||||
return "", ""
|
||||
}
|
||||
return rec.InstanceID, rec.ServerID
|
||||
}
|
||||
|
||||
// ClearPendingProxy drops a pending record whose command never reached an
|
||||
// agent, so a dead proxy_id is not left claimable for the rest of its TTL.
|
||||
func ClearPendingProxy(ctx context.Context, proxyID string) {
|
||||
_ = rdb.Del(ctx, ProxyPendingKey+proxyID).Err()
|
||||
}
|
||||
|
||||
var claimPending = redis.NewScript(`
|
||||
local v = redis.call("GET", KEYS[1])
|
||||
if v then
|
||||
redis.call("DEL", KEYS[1])
|
||||
return v
|
||||
end
|
||||
return ""
|
||||
`)
|
||||
|
||||
var releaseIfOwner = redis.NewScript(`
|
||||
if redis.call("GET", KEYS[1]) == ARGV[1] then
|
||||
return redis.call("DEL", KEYS[1])
|
||||
|
||||
Reference in New Issue
Block a user