-
Notifications
You must be signed in to change notification settings - Fork 1
/
data.go
177 lines (172 loc) · 5.02 KB
/
data.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
package routes
import (
"bytes"
"encoding/json"
"errors"
"mime/multipart"
"net/http"
"github.com/go-chi/chi"
"github.com/jackc/pgx/v5"
"github.com/rs/zerolog/log"
"github.com/samber/do"
"github.com/samber/lo"
"github.com/therealpaulgg/ssh-sync-server/pkg/database/models"
"github.com/therealpaulgg/ssh-sync-server/pkg/database/query"
"github.com/therealpaulgg/ssh-sync-server/pkg/database/repository"
"github.com/therealpaulgg/ssh-sync-server/pkg/web/middleware"
"github.com/therealpaulgg/ssh-sync-server/pkg/web/middleware/context_keys"
"github.com/therealpaulgg/ssh-sync/pkg/dto"
)
func getData(i *do.Injector) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
user, ok := r.Context().Value(context_keys.UserContextKey).(*models.User)
if !ok {
log.Err(errors.New("could not get user from context"))
w.WriteHeader(http.StatusInternalServerError)
return
}
userRepo := do.MustInvoke[repository.UserRepository](i)
keys, err := userRepo.GetUserKeys(user.ID)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
user.Keys = keys
config, err := userRepo.GetUserConfig(user.ID)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
user.Config = config
dto := dto.DataDto{
ID: user.ID,
Username: user.Username,
Keys: lo.Map(user.Keys, func(key models.SshKey, index int) dto.KeyDto {
return dto.KeyDto{
ID: key.ID,
UserID: key.UserID,
Filename: key.Filename,
Data: key.Data,
}
}),
SshConfig: lo.Map(user.Config, func(conf models.SshConfig, index int) dto.SshConfigDto {
return dto.SshConfigDto{
Host: conf.Host,
Values: conf.Values,
IdentityFiles: conf.IdentityFiles,
}
}),
}
json.NewEncoder(w).Encode(dto)
}
}
func addData(i *do.Injector) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
user, ok := r.Context().Value(context_keys.UserContextKey).(*models.User)
if !ok {
log.Err(errors.New("could not get user from context"))
w.WriteHeader(http.StatusInternalServerError)
return
}
machine, ok := r.Context().Value(context_keys.MachineContextKey).(*models.Machine)
if !ok {
log.Err(errors.New("could not get machine from context"))
w.WriteHeader(http.StatusInternalServerError)
return
}
userRepo := do.MustInvoke[repository.UserRepository](i)
err := r.ParseMultipartForm(32 << 20)
if err != nil {
log.Err(err).Msg("could not parse multipart form")
w.WriteHeader(http.StatusBadRequest)
return
}
m := r.MultipartForm
sshConfigDataRaw := r.FormValue("ssh_config")
if sshConfigDataRaw == "" {
log.Debug().Msg("ssh config is empty")
w.WriteHeader(http.StatusBadRequest)
return
}
var sshConfig []dto.SshConfigDto
if err := json.NewDecoder(bytes.NewBufferString(sshConfigDataRaw)).Decode(&sshConfig); err != nil {
log.Debug().Err(err).Msg("could not decode ssh config")
w.WriteHeader(http.StatusBadRequest)
return
}
sshConfigData := lo.Map(sshConfig, func(conf dto.SshConfigDto, i int) models.SshConfig {
return models.SshConfig{
UserID: user.ID,
MachineID: machine.ID,
Host: conf.Host,
Values: conf.Values,
IdentityFiles: conf.IdentityFiles,
}
})
user.Config = sshConfigData
txQueryService := do.MustInvoke[query.TransactionService](i)
tx, err := txQueryService.StartTx(pgx.TxOptions{})
if err != nil {
log.Err(err).Msg("error starting transaction")
w.WriteHeader(http.StatusInternalServerError)
return
}
defer func() {
rb := func(tx pgx.Tx) {
err := txQueryService.Rollback(tx)
if err != nil {
log.Err(err).Msg("error rolling back transaction")
}
}
if err != nil {
rb(tx)
} else {
internalErr := txQueryService.Commit(tx)
if internalErr != nil {
log.Err(err).Msg("error committing transaction")
rb(tx)
w.WriteHeader(http.StatusInternalServerError)
}
}
}()
if err = userRepo.AddAndUpdateConfigTx(user, tx); err != nil {
log.Err(err).Msg("could not add config")
w.WriteHeader(http.StatusInternalServerError)
return
}
var files []*multipart.FileHeader
for _, filelist := range m.File {
files = append(files, filelist...)
}
for i := range files {
file, err := files[i].Open()
if err != nil {
log.Err(err).Msg("could not open file")
w.WriteHeader(http.StatusInternalServerError)
return
}
defer file.Close()
user.Keys = append(user.Keys, models.SshKey{
UserID: user.ID,
Filename: files[i].Filename,
Data: make([]byte, files[i].Size),
})
if _, err = file.Read(user.Keys[i].Data); err != nil {
log.Err(err).Msg("could not open file")
w.WriteHeader(http.StatusInternalServerError)
return
}
}
if err = userRepo.AddAndUpdateKeysTx(user, tx); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
}
}
func DataRoutes(i *do.Injector) chi.Router {
r := chi.NewRouter()
r.Use(middleware.ConfigureAuth(i))
r.Get("/", getData(i))
r.Post("/", addData(i))
return r
}