/
main.go
313 lines (255 loc) · 6.57 KB
/
main.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package main
import (
"archive/tar"
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
goHttp "net/http"
"os"
"os/signal"
"strings"
"syscall"
"github.com/joho/godotenv"
"github.com/docker/docker/api/types/container"
authWebhook "go.containerssh.io/libcontainerssh/auth/webhook"
"go.containerssh.io/libcontainerssh/config"
configWebhook "go.containerssh.io/libcontainerssh/config/webhook"
"go.containerssh.io/libcontainerssh/http"
"go.containerssh.io/libcontainerssh/log"
"go.containerssh.io/libcontainerssh/metadata"
"go.containerssh.io/libcontainerssh/service"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
)
type authHandler struct {
}
func getImage(username string) string {
postBody, _ := json.Marshal(map[string]string{
"username": username,
"api-token": getEnv("API_TOKEN"),
})
responseBody := bytes.NewBuffer(postBody)
resp, err := http.Post(getEnv("API_URI")+"/status", "application/json", responseBody)
if err != nil {
log.Fatalf("An Error Occured %v", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
fmt.Println(body)
// Process body
fileName := ""
nextPassword := ""
newName := username + "-" + fileName
content, err := ioutil.ReadFile(getEnv("IMAGE_FOLDER") + "/" + fileName)
if err != nil {
log.Fatal(err)
}
// Convert []byte to string
text := string(content)
strings.ReplaceAll(text, "<{{{NEXT_PASSWORD}}}>", nextPassword)
newFile, err := os.Create(getEnv("TMP_FOLDER") + "/" + newName)
_, err2 := newFile.WriteString(text)
if err2 != nil {
log.Fatal(err2)
}
genImage(username, newName, getEnv("TMP_FOLDER")+"/"+newName)
return newName
}
func genImage(username string, fileName string, filePath string) {
ctx := context.Background()
cli, err := client.NewEnvClient()
if err != nil {
log.Fatal(err, " :unable to init client")
}
buf := new(bytes.Buffer)
tw := tar.NewWriter(buf)
defer tw.Close()
dockerFile := fileName
dockerFileReader, err := os.Open(filePath)
if err != nil {
log.Fatal(err, " :unable to open Dockerfile")
}
readDockerFile, err := ioutil.ReadAll(dockerFileReader)
if err != nil {
log.Fatal(err, " :unable to read dockerfile")
}
tarHeader := &tar.Header{
Name: dockerFile,
Size: int64(len(readDockerFile)),
}
err = tw.WriteHeader(tarHeader)
if err != nil {
log.Fatal(err, " :unable to write tar header")
}
_, err = tw.Write(readDockerFile)
if err != nil {
log.Fatal(err, " :unable to write tar body")
}
dockerFileTarReader := bytes.NewReader(buf.Bytes())
imageBuildResponse, err := cli.ImageBuild(
ctx,
dockerFileTarReader,
types.ImageBuildOptions{
Context: dockerFileTarReader,
Dockerfile: dockerFile,
Remove: true})
if err != nil {
log.Fatal(err, " :unable to build docker image")
}
defer imageBuildResponse.Body.Close()
_, err = io.Copy(os.Stdout, imageBuildResponse.Body)
if err != nil {
log.Fatal(err, " :unable to read image build response")
}
}
func verifyPassword(username string, password []byte) bool {
pass := string(password[:])
postBody, _ := json.Marshal(map[string]string{
"username": username,
"password": pass,
"api_token": getEnv("API_TOKEN"),
})
responseBody := bytes.NewBuffer(postBody)
resp, err := http.Post(getEnv("API_URI")+"/auth", "application/json", responseBody)
if err != nil {
log.Fatalf("An Error Occured %v", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
type Response struct {
status string
}
var response Response
json.Unmarshal(body, &response)
fmt.Println(response)
if response.status == "OK" {
return true
}
return false
}
func (a *authHandler) OnAuthorization(meta metadata.ConnectionAuthenticatedMetadata) (
bool,
metadata.ConnectionAuthenticatedMetadata,
error,
) {
return true, meta.Authenticated(meta.Username), nil
}
func (a *authHandler) OnPassword(metadata metadata.ConnectionAuthPendingMetadata, password []byte) (
bool,
metadata.ConnectionAuthenticatedMetadata,
error,
) {
if verifyPassword(metadata.Username, password) {
return true, metadata.Authenticated(metadata.Username), nil
}
return false, metadata.AuthFailed(), nil
}
type configHandler struct {
}
func (c *configHandler) OnConfig(request config.Request) (config.AppConfig, error) {
cfg := config.AppConfig{}
cfg.Docker.Execution.Launch.ContainerConfig = &container.Config{}
cfg.Docker.Execution.Launch.ContainerConfig.Image = getImage(request.Username)
cfg.Docker.Execution.DisableAgent = true
cfg.Docker.Execution.Mode = config.DockerExecutionModeSession
cfg.Docker.Execution.ShellCommand = []string{"/bin/sh"}
return cfg, nil
}
type handler struct {
auth goHttp.Handler
config goHttp.Handler
}
func (h *handler) ServeHTTP(writer goHttp.ResponseWriter, request *goHttp.Request) {
switch request.URL.Path {
case "/password":
fallthrough
case "/pubkey":
h.auth.ServeHTTP(writer, request)
case "/config":
h.config.ServeHTTP(writer, request)
default:
writer.WriteHeader(404)
}
}
func initEnv() {
err := godotenv.Load(".env")
if err != nil {
log.Fatalf("Error loading .env file")
}
}
func getEnv(key string) string {
return os.Getenv(key)
}
func main() {
initEnv()
logger, err := log.NewLogger(
config.LogConfig{
Level: config.LogLevelDebug,
Format: config.LogFormatLJSON,
Destination: config.LogDestinationStdout,
},
)
if err != nil {
panic(err)
}
authHTTPHandler := authWebhook.NewHandler(&authHandler{}, logger)
configHTTPHandler, err := configWebhook.NewHandler(&configHandler{}, logger)
if err != nil {
panic(err)
}
srv, err := http.NewServer(
"authconfig",
config.HTTPServerConfiguration{
Listen: "0.0.0.0:8080",
},
&handler{
auth: authHTTPHandler,
config: configHTTPHandler,
},
logger,
func(s string) {
},
)
if err != nil {
panic(err)
}
running := make(chan struct{})
stopped := make(chan struct{})
lifecycle := service.NewLifecycle(srv)
lifecycle.OnRunning(
func(s service.Service, l service.Lifecycle) {
println("Auth-Config Server is now running...")
close(running)
},
).OnStopped(
func(s service.Service, l service.Lifecycle) {
close(stopped)
},
)
exitSignalList := []os.Signal{os.Interrupt, os.Kill, syscall.SIGINT, syscall.SIGTERM}
exitSignals := make(chan os.Signal, 1)
signal.Notify(exitSignals, exitSignalList...)
go func() {
if err := lifecycle.Run(); err != nil {
panic(err)
}
}()
select {
case <-running:
if _, ok := <-exitSignals; ok {
println("Stopping Auth-Config Server...")
lifecycle.Stop(context.Background())
}
case <-stopped:
}
}