-
-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathdocker.go
More file actions
160 lines (133 loc) · 4.59 KB
/
docker.go
File metadata and controls
160 lines (133 loc) · 4.59 KB
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
//go:build full || e2e
package main
import (
"context"
"fmt"
"net"
"strings"
"sync"
"github.com/moby/moby/api/types/network"
"github.com/moby/moby/client"
log "github.com/sirupsen/logrus"
)
type pipe struct {
ClientUsername string
ContainerUsername string
Host string
DockerSshdCmd string
AuthorizedKeys string
TrustedUserCAKeys string
PrivateKey string
}
type plugin struct {
dockerCli *client.Client
// dockerSshdMu protects docker-sshd bridge state keyed by container ID.
dockerSshdMu sync.Mutex
dockerSshdBridgeAddr string
dockerSshdCmds map[string]string
dockerSshdKeys map[string][]byte
dockerSshdPrivateKeys map[string]string
dockerSshdKeyToContainer map[string]string
}
func newDockerPlugin() (*plugin, error) {
cli, err := client.New(client.FromEnv)
if err != nil {
return nil, err
}
return &plugin{
dockerCli: cli,
dockerSshdCmds: make(map[string]string),
dockerSshdKeys: make(map[string][]byte),
dockerSshdPrivateKeys: make(map[string]string),
dockerSshdKeyToContainer: make(map[string]string),
}, nil
}
func (p *plugin) list() ([]pipe, error) {
res, err := p.dockerCli.ContainerList(context.Background(), client.ContainerListOptions{
// Filters: make(client.Filters).Add("label", fmt.Sprintf("sshpiper.username=%v", username)),
})
if err != nil {
return nil, err
}
var pipes []pipe
activeDockerSshdContainers := make(map[string]struct{})
for _, c := range res.Items {
// TODO: support env?
pipe := pipe{
ClientUsername: c.Labels["sshpiper.username"],
ContainerUsername: c.Labels["sshpiper.container_username"],
AuthorizedKeys: c.Labels["sshpiper.authorized_keys"],
TrustedUserCAKeys: c.Labels["sshpiper.trusted_user_ca_keys"],
PrivateKey: c.Labels["sshpiper.private_key"],
DockerSshdCmd: c.Labels["sshpiper.docker_sshd_cmd"],
}
dockerExecEnabled := strings.EqualFold(c.Labels["sshpiper.docker_exec_cmd"], "true")
if pipe.ClientUsername == "" && pipe.AuthorizedKeys == "" && pipe.TrustedUserCAKeys == "" {
log.Debugf("skipping container %v without sshpiper.username or sshpiper.authorized_keys or sshpiper.trusted_user_ca_keys", c.ID)
continue
}
if dockerExecEnabled {
if pipe.AuthorizedKeys == "" && pipe.TrustedUserCAKeys == "" {
log.Errorf("skipping container %v with sshpiper.docker_exec_cmd=true but missing sshpiper.authorized_keys/sshpiper.trusted_user_ca_keys", c.ID)
continue
}
addr, err := p.ensureDockerSshdBridge()
if err != nil {
log.Errorf("skipping container %v unable to create docker-sshd bridge: %v", c.ID, err)
continue
}
privateKey, err := p.registerDockerSshdContainer(c.ID, pipe.DockerSshdCmd)
if err != nil {
log.Errorf("skipping container %v unable to register docker-sshd key: %v", c.ID, err)
continue
}
pipe.PrivateKey = privateKey
pipe.Host = addr
activeDockerSshdContainers[c.ID] = struct{}{}
pipes = append(pipes, pipe)
continue
}
// dockerExecEnabled path above supports generated private key; regular sshd path still requires explicit private key.
if (pipe.AuthorizedKeys != "" || pipe.TrustedUserCAKeys != "") && pipe.PrivateKey == "" {
log.Errorf("skipping container %v without sshpiper.private_key but has sshpiper.authorized_keys or sshpiper.trusted_user_ca_keys", c.ID)
continue
}
var hostcandidates []*network.EndpointSettings
for _, nw := range c.NetworkSettings.Networks {
if nw.IPAddress.IsValid() {
hostcandidates = append(hostcandidates, nw)
}
}
if len(hostcandidates) == 0 {
return nil, fmt.Errorf("no ip address found for container %v", c.ID)
}
// default to first one
pipe.Host = hostcandidates[0].IPAddress.String()
if len(hostcandidates) > 1 {
netname := c.Labels["sshpiper.network"]
if netname == "" {
return nil, fmt.Errorf("multiple networks found for container %v, please specify sshpiper.network", c.ID)
}
net, err := p.dockerCli.NetworkInspect(context.Background(), netname, client.NetworkInspectOptions{})
if err != nil {
log.Warnf("cannot list network %v for container %v: %v", netname, c.ID, err)
continue
}
for _, hostcandidate := range hostcandidates {
if hostcandidate.NetworkID == net.Network.ID {
pipe.Host = hostcandidate.IPAddress.String()
break
}
}
}
port := c.Labels["sshpiper.port"]
if port != "" {
pipe.Host = net.JoinHostPort(pipe.Host, port)
} else {
pipe.Host = net.JoinHostPort(pipe.Host, "22")
}
pipes = append(pipes, pipe)
}
p.syncDockerSshdState(activeDockerSshdContainers)
return pipes, nil
}