This repository has been archived by the owner on Aug 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
/
config.go
170 lines (158 loc) · 4.97 KB
/
config.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
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
package main
import (
"fmt"
"io/ioutil"
"os"
"gopkg.in/gcfg.v1"
)
type Configuration struct {
ImageName string
EnableUserImageName bool
ContainerName string
EnableUserContainerName bool
MountHomeFrom string
EnableUserMountHomeFrom bool
MountHomeTo string
EnableUserMountHomeTo bool
UserCwd string
EnableUserUserCwd bool
ContainerUsername string
EnableUserContainerUsername bool
Shell string
EnableUserShell bool
EnableUserConfig bool
MountHome bool
EnableUserMountHome bool
MountTmp bool
EnableUserMountTmp bool
MountDockerSocket bool
EnableUserMountDockerSocket bool
DockerSocket string
EnableUserDockerSocket bool
Entrypoint string
EnableUserEntrypoint bool
Cmd []string
EnableUserCmd bool
DockerOpt []string
EnableUserDockerOpt bool
ReverseForward []string
EnableUserReverseForward bool
}
func (c Configuration) Dump() string {
return fmt.Sprintf("ImageName %s MountHomeTo %s ContainerUsername %s Shell %s DockerSocket %s", c.ImageName, c.MountHomeTo, c.ContainerUsername, c.Shell, c.DockerSocket)
}
type configInterpolation struct {
Home string
User string
}
var defaultConfig = Configuration{
ImageName: "busybox",
ContainerName: "%u_dockersh",
MountHomeFrom: "%h",
MountHomeTo: "%h",
UserCwd: "%h",
ContainerUsername: "%u",
Shell: "/bin/ash",
DockerSocket: "/var/run/docker.sock",
Entrypoint: "internal",
}
func loadAllConfig(user string, homedir string) (config Configuration, err error) {
globalconfig, err := loadConfig(loadableFile("/etc/dockersh"), user)
if err != nil {
return config, err
}
if globalconfig.EnableUserConfig == true {
localconfig, err := loadConfig(loadableFile(fmt.Sprintf("%s/.dockersh", homedir)), user)
if err != nil {
return config, err
}
return mergeConfigs(mergeConfigs(defaultConfig, globalconfig, false), localconfig, true), nil
}
return mergeConfigs(defaultConfig, globalconfig, false), nil
}
type loadableFile string
func (fn loadableFile) Getcontents() ([]byte, error) {
localConfigFile, err := os.Open(string(fn))
var b []byte
if err != nil {
return b, fmt.Errorf("Could not open: %s", string(fn))
}
b, err = ioutil.ReadAll(localConfigFile)
if err != nil {
return b, err
}
localConfigFile.Close()
return b, nil
}
func loadConfig(filename loadableFile, user string) (config Configuration, err error) {
bytes, err := filename.Getcontents()
if err != nil {
return config, err
}
return loadConfigFromString(bytes, user)
}
func mergeConfigs(old Configuration, new Configuration, blacklist bool) (ret Configuration) {
if (!blacklist || old.EnableUserShell) && new.Shell != "" {
old.Shell = new.Shell
}
if (!blacklist || old.EnableUserContainerUsername) && new.ContainerUsername != "" {
old.ContainerUsername = new.ContainerUsername
}
if (!blacklist || old.EnableUserImageName) && new.ImageName != "" {
old.ImageName = new.ImageName
}
if (!blacklist || old.EnableUserMountHomeTo) && new.MountHomeTo != "" {
old.MountHomeTo = new.MountHomeTo
}
if (!blacklist || old.EnableUserMountHomeFrom) && new.MountHomeFrom != "" {
old.MountHomeFrom = new.MountHomeFrom
}
if (!blacklist || old.EnableUserDockerSocket) && new.DockerSocket != "" {
old.DockerSocket = new.DockerSocket
}
if (!blacklist || old.EnableUserMountHome) && new.MountHome == true {
old.MountHome = true
}
if (!blacklist || old.EnableUserMountTmp) && new.MountTmp == true {
old.MountTmp = true
}
if (!blacklist || old.EnableUserMountDockerSocket) && new.MountDockerSocket == true {
old.MountDockerSocket = true
}
if (!blacklist || old.EnableUserEntrypoint) && new.Entrypoint != "" {
old.Entrypoint = new.Entrypoint
}
if (!blacklist || old.EnableUserUserCwd) && new.UserCwd != "" {
old.UserCwd = new.UserCwd
}
if (!blacklist || old.EnableUserContainerName) && new.ContainerName != "" {
old.ContainerName = new.ContainerName
}
if (!blacklist || old.EnableUserCmd) && len(new.Cmd) > 0 {
old.Cmd = new.Cmd
}
if (!blacklist || old.EnableUserDockerOpt) && len(new.DockerOpt) > 0 {
old.DockerOpt = new.DockerOpt
}
if (!blacklist || old.EnableUserReverseForward) && len(new.ReverseForward) > 0 {
old.ReverseForward = new.ReverseForward
}
if !blacklist && new.EnableUserConfig == true {
old.EnableUserConfig = true
}
return old
}
func loadConfigFromString(bytes []byte, user string) (config Configuration, err error) {
inicfg := struct {
Dockersh Configuration
User map[string]*Configuration
}{}
err = gcfg.ReadStringInto(&inicfg, string(bytes))
if err != nil {
return config, err
}
if inicfg.User[user] == nil {
return inicfg.Dockersh, nil
}
return mergeConfigs(inicfg.Dockersh, *inicfg.User[user], false), nil
}