-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_vsock.go
57 lines (44 loc) · 1.15 KB
/
api_vsock.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
package main
import (
"context"
"crypto/x509"
"fmt"
"net/http"
"github.com/lxc/lxd/lxd/db"
"github.com/lxc/lxd/lxd/db/cluster"
"github.com/lxc/lxd/lxd/instance"
"github.com/lxc/lxd/lxd/util"
)
func authenticateAgentCert(d *Daemon, r *http.Request) (bool, instance.Instance, error) {
s := d.State()
var vsockID int
trusted := false
_, err := fmt.Sscanf(r.RemoteAddr, "vm(%d)", &vsockID)
if err != nil {
return false, nil, err
}
var clusterInst *cluster.Instance
err = s.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
var err error
clusterInst, err = tx.GetLocalInstanceWithVsockID(ctx, vsockID)
if err != nil {
return err
}
return nil
})
if err != nil {
return false, nil, err
}
inst, err := instance.LoadByProjectAndName(s, clusterInst.Project, clusterInst.Name)
if err != nil {
return false, nil, err
}
agentCert := inst.(instance.VM).AgentCertificate()
for _, cert := range r.TLS.PeerCertificates {
trusted, _ = util.CheckTrustState(*cert, map[string]x509.Certificate{"0": *agentCert}, nil, false)
if trusted {
return true, inst, nil
}
}
return false, nil, nil
}