-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
112 lines (87 loc) · 2.64 KB
/
client.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
package podman
import (
"context"
"fmt"
"os"
"time"
"github.com/docker/docker/client"
"github.com/pkg/errors"
"github.com/khulnasoft-labs/goscope/internal/log"
)
var (
ErrNoSocketAddress = errors.New("no socket address")
ErrNoHostAddress = errors.New("no host address")
)
func ClientOverSSH() (*client.Client, error) {
var clientOpts = []client.Opt{
client.WithAPIVersionNegotiation(),
}
host, identity := getSSHAddress(configPaths)
if v, found := os.LookupEnv("CONTAINER_HOST"); found && v != "" {
host = v
}
if v, found := os.LookupEnv("CONTAINER_SSHKEY"); found && v != "" {
identity = v
}
passPhrase := ""
if v, found := os.LookupEnv("CONTAINER_PASSPHRASE"); found {
passPhrase = v
}
sshConf, err := newSSHConf(host, identity, passPhrase)
if err != nil {
return nil, err
}
httpClient, err := httpClientOverSSH(sshConf)
if err != nil {
return nil, fmt.Errorf("making http client: %w", err)
}
clientOpts = append(clientOpts, func(c *client.Client) error {
return client.WithHTTPClient(httpClient)(c)
})
// This http path is defined by podman's docs: https://github.com/containers/podman/blob/main/pkg/api/server/docs.go#L31-L34
clientOpts = append(clientOpts, client.WithHost("http://d"))
c, err := client.NewClientWithOpts(clientOpts...)
if err != nil {
return nil, fmt.Errorf("failed create remote client for podman: %w", err)
}
ctx, cancel := context.WithTimeout(context.TODO(), time.Second*3)
defer cancel()
_, err = c.Ping(ctx)
return c, err
}
func ClientOverUnixSocket() (*client.Client, error) {
var clientOpts = []client.Opt{
client.WithAPIVersionNegotiation(),
}
addr := getUnixSocketAddress(configPaths)
if v, found := os.LookupEnv("CONTAINER_HOST"); found && v != "" {
addr = v
}
if addr == "" { // in some cases there might not be any config file
// we can try guessing; podman CLI does that
socketPath := fmt.Sprintf("/run/user/%d/podman/podman.sock", os.Getuid())
log.Debugf("no socket address was found. Trying default address: %s", socketPath)
_, err := os.Stat(socketPath)
if err != nil {
log.Debugf("looking for socket file: %v", err)
return nil, ErrNoSocketAddress
}
addr = fmt.Sprintf("unix://%s", socketPath)
}
clientOpts = append(clientOpts, client.WithHost(addr))
c, err := client.NewClientWithOpts(clientOpts...)
if err != nil {
return nil, fmt.Errorf("creating local client for podman: %w", err)
}
ctx, cancel := context.WithTimeout(context.TODO(), time.Second*3)
defer cancel()
_, err = c.Ping(ctx)
return c, err
}
func GetClient() (*client.Client, error) {
c, err := ClientOverUnixSocket()
if err == nil {
return c, nil
}
return ClientOverSSH()
}