blob: c5bc177adf13a43e3b994b4316d3527d830c5a2e [file] [log] [blame]
gio59946282024-10-07 12:55:51 +04001package dodoapp
gio0eaf2712024-04-14 13:08:46 +04002
3import (
gio94904702024-07-26 16:58:34 +04004 "bytes"
gio81246f02024-07-10 12:02:15 +04005 "context"
gio23bdc1b2024-07-11 16:07:47 +04006 "embed"
gio0eaf2712024-04-14 13:08:46 +04007 "encoding/json"
gio9d66f322024-07-06 13:45:10 +04008 "errors"
gio0eaf2712024-04-14 13:08:46 +04009 "fmt"
gio23bdc1b2024-07-11 16:07:47 +040010 "html/template"
gio0eaf2712024-04-14 13:08:46 +040011 "io"
gio9d66f322024-07-06 13:45:10 +040012 "io/fs"
gio0eaf2712024-04-14 13:08:46 +040013 "net/http"
gio23bdc1b2024-07-11 16:07:47 +040014 "slices"
giof078f462024-10-14 09:07:33 +040015 "sort"
gio7fbd4ad2024-08-27 10:06:39 +040016 "strconv"
gio0eaf2712024-04-14 13:08:46 +040017 "strings"
gio9d66f322024-07-06 13:45:10 +040018 "sync"
gio9f6b27d2024-10-14 10:08:40 +040019 ttemplate "text/template"
giocafd4e62024-07-31 10:53:40 +040020 "time"
gio0eaf2712024-04-14 13:08:46 +040021
Davit Tabidzea5ea5092024-08-01 15:28:09 +040022 "golang.org/x/crypto/bcrypt"
23 "golang.org/x/exp/rand"
24
gio0eaf2712024-04-14 13:08:46 +040025 "github.com/giolekva/pcloud/core/installer"
gio59946282024-10-07 12:55:51 +040026 "github.com/giolekva/pcloud/core/installer/server"
gio0eaf2712024-04-14 13:08:46 +040027 "github.com/giolekva/pcloud/core/installer/soft"
gio43b0f422024-08-21 10:40:13 +040028 "github.com/giolekva/pcloud/core/installer/tasks"
gio33059762024-07-05 13:19:07 +040029
gio7fbd4ad2024-08-27 10:06:39 +040030 "cuelang.org/go/cue"
gio33059762024-07-05 13:19:07 +040031 "github.com/gorilla/mux"
gio81246f02024-07-10 12:02:15 +040032 "github.com/gorilla/securecookie"
gio0eaf2712024-04-14 13:08:46 +040033)
34
gio59946282024-10-07 12:55:51 +040035//go:embed templates/*
36var templates embed.FS
gio23bdc1b2024-07-11 16:07:47 +040037
gio59946282024-10-07 12:55:51 +040038//go:embed all:app-templates
gio5e49bb62024-07-20 10:43:19 +040039var appTmplsFS embed.FS
40
gio59946282024-10-07 12:55:51 +040041//go:embed static/*
42var staticAssets embed.FS
43
gio9f6b27d2024-10-14 10:08:40 +040044//go:embed schemas/app.schema.json
45var dodoAppJsonSchema string
gioc81a8472024-09-24 13:06:19 +020046
gio9d66f322024-07-06 13:45:10 +040047const (
gioa60f0de2024-07-08 10:49:48 +040048 ConfigRepoName = "config"
giod8ab4f52024-07-26 16:58:34 +040049 appConfigsFile = "/apps.json"
gio81246f02024-07-10 12:02:15 +040050 loginPath = "/login"
51 logoutPath = "/logout"
gio59946282024-10-07 12:55:51 +040052 staticPath = "/static/"
gioc81a8472024-09-24 13:06:19 +020053 schemasPath = "/schemas/"
gio8fae3af2024-07-25 13:43:31 +040054 apiPublicData = "/api/public-data"
55 apiCreateApp = "/api/apps"
gio81246f02024-07-10 12:02:15 +040056 sessionCookie = "dodo-app-session"
57 userCtx = "user"
giob4a3a192024-08-19 09:55:47 +040058 initCommitMsg = "init"
gio9d66f322024-07-06 13:45:10 +040059)
60
gio59946282024-10-07 12:55:51 +040061type tmplts struct {
giob4a3a192024-08-19 09:55:47 +040062 index *template.Template
63 appStatus *template.Template
64 commitStatus *template.Template
gio183e8342024-08-20 06:01:24 +040065 logs *template.Template
gio23bdc1b2024-07-11 16:07:47 +040066}
67
gio59946282024-10-07 12:55:51 +040068func parseTemplates(fs embed.FS) (tmplts, error) {
69 base, err := template.ParseFS(fs, "templates/base.html")
gio23bdc1b2024-07-11 16:07:47 +040070 if err != nil {
gio59946282024-10-07 12:55:51 +040071 return tmplts{}, err
gio23bdc1b2024-07-11 16:07:47 +040072 }
gio5e49bb62024-07-20 10:43:19 +040073 parse := func(path string) (*template.Template, error) {
74 if b, err := base.Clone(); err != nil {
75 return nil, err
76 } else {
77 return b.ParseFS(fs, path)
78 }
79 }
gio59946282024-10-07 12:55:51 +040080 index, err := parse("templates/index.html")
gio5e49bb62024-07-20 10:43:19 +040081 if err != nil {
gio59946282024-10-07 12:55:51 +040082 return tmplts{}, err
gio5e49bb62024-07-20 10:43:19 +040083 }
gio59946282024-10-07 12:55:51 +040084 appStatus, err := parse("templates/app_status.html")
gio5e49bb62024-07-20 10:43:19 +040085 if err != nil {
gio59946282024-10-07 12:55:51 +040086 return tmplts{}, err
gio5e49bb62024-07-20 10:43:19 +040087 }
gio59946282024-10-07 12:55:51 +040088 commitStatus, err := parse("templates/commit_status.html")
giob4a3a192024-08-19 09:55:47 +040089 if err != nil {
gio59946282024-10-07 12:55:51 +040090 return tmplts{}, err
giob4a3a192024-08-19 09:55:47 +040091 }
gio59946282024-10-07 12:55:51 +040092 logs, err := parse("templates/logs.html")
gio183e8342024-08-20 06:01:24 +040093 if err != nil {
gio59946282024-10-07 12:55:51 +040094 return tmplts{}, err
gio183e8342024-08-20 06:01:24 +040095 }
gio59946282024-10-07 12:55:51 +040096 return tmplts{index, appStatus, commitStatus, logs}, nil
gio23bdc1b2024-07-11 16:07:47 +040097}
98
gio59946282024-10-07 12:55:51 +040099type Server struct {
giocb34ad22024-07-11 08:01:13 +0400100 l sync.Locker
101 st Store
gio11617ac2024-07-15 16:09:04 +0400102 nf NetworkFilter
103 ug UserGetter
giocb34ad22024-07-11 08:01:13 +0400104 port int
105 apiPort int
106 self string
gioc81a8472024-09-24 13:06:19 +0200107 selfPublic string
gio11617ac2024-07-15 16:09:04 +0400108 repoPublicAddr string
giocb34ad22024-07-11 08:01:13 +0400109 sshKey string
110 gitRepoPublicKey string
111 client soft.Client
112 namespace string
113 envAppManagerAddr string
114 env installer.EnvConfig
115 nsc installer.NamespaceCreator
116 jc installer.JobCreator
gio864b4332024-09-05 13:56:47 +0400117 vpnKeyGen installer.VPNAPIClient
giof6ad2982024-08-23 17:42:49 +0400118 cnc installer.ClusterNetworkConfigurator
giocb34ad22024-07-11 08:01:13 +0400119 workers map[string]map[string]struct{}
giod8ab4f52024-07-26 16:58:34 +0400120 appConfigs map[string]appConfig
gio59946282024-10-07 12:55:51 +0400121 tmplts tmplts
gio5e49bb62024-07-20 10:43:19 +0400122 appTmpls AppTmplStore
giocafd4e62024-07-31 10:53:40 +0400123 external bool
124 fetchUsersAddr string
gio43b0f422024-08-21 10:40:13 +0400125 reconciler tasks.Reconciler
gio183e8342024-08-20 06:01:24 +0400126 logs map[string]string
gio9f6b27d2024-10-14 10:08:40 +0400127 schemaTmpl *ttemplate.Template
giod8ab4f52024-07-26 16:58:34 +0400128}
129
130type appConfig struct {
131 Namespace string `json:"namespace"`
132 Network string `json:"network"`
gio0eaf2712024-04-14 13:08:46 +0400133}
134
gio33059762024-07-05 13:19:07 +0400135// TODO(gio): Initialize appNs on startup
gio59946282024-10-07 12:55:51 +0400136func NewServer(
gioa60f0de2024-07-08 10:49:48 +0400137 st Store,
gio11617ac2024-07-15 16:09:04 +0400138 nf NetworkFilter,
139 ug UserGetter,
gio0eaf2712024-04-14 13:08:46 +0400140 port int,
gioa60f0de2024-07-08 10:49:48 +0400141 apiPort int,
gio33059762024-07-05 13:19:07 +0400142 self string,
gioc81a8472024-09-24 13:06:19 +0200143 selfPublic string,
gio11617ac2024-07-15 16:09:04 +0400144 repoPublicAddr string,
gio0eaf2712024-04-14 13:08:46 +0400145 sshKey string,
gio33059762024-07-05 13:19:07 +0400146 gitRepoPublicKey string,
gio0eaf2712024-04-14 13:08:46 +0400147 client soft.Client,
148 namespace string,
giocb34ad22024-07-11 08:01:13 +0400149 envAppManagerAddr string,
gio33059762024-07-05 13:19:07 +0400150 nsc installer.NamespaceCreator,
giof8843412024-05-22 16:38:05 +0400151 jc installer.JobCreator,
gio864b4332024-09-05 13:56:47 +0400152 vpnKeyGen installer.VPNAPIClient,
giof6ad2982024-08-23 17:42:49 +0400153 cnc installer.ClusterNetworkConfigurator,
gio0eaf2712024-04-14 13:08:46 +0400154 env installer.EnvConfig,
giocafd4e62024-07-31 10:53:40 +0400155 external bool,
156 fetchUsersAddr string,
gio43b0f422024-08-21 10:40:13 +0400157 reconciler tasks.Reconciler,
gio59946282024-10-07 12:55:51 +0400158) (*Server, error) {
159 tmplts, err := parseTemplates(templates)
gio23bdc1b2024-07-11 16:07:47 +0400160 if err != nil {
161 return nil, err
162 }
gio5e4d1a72024-10-09 15:25:29 +0400163 apps, err := fs.Sub(appTmplsFS, "app-templates")
gio5e49bb62024-07-20 10:43:19 +0400164 if err != nil {
165 return nil, err
166 }
167 appTmpls, err := NewAppTmplStoreFS(apps)
168 if err != nil {
169 return nil, err
170 }
gio9f6b27d2024-10-14 10:08:40 +0400171 schemaTmpl, err := ttemplate.New("app.schema.json").Parse(dodoAppJsonSchema)
172 if err != nil {
173 return nil, err
174 }
gio59946282024-10-07 12:55:51 +0400175 s := &Server{
gio9d66f322024-07-06 13:45:10 +0400176 &sync.Mutex{},
gioa60f0de2024-07-08 10:49:48 +0400177 st,
gio11617ac2024-07-15 16:09:04 +0400178 nf,
179 ug,
gio0eaf2712024-04-14 13:08:46 +0400180 port,
gioa60f0de2024-07-08 10:49:48 +0400181 apiPort,
gio33059762024-07-05 13:19:07 +0400182 self,
gioc81a8472024-09-24 13:06:19 +0200183 selfPublic,
gio11617ac2024-07-15 16:09:04 +0400184 repoPublicAddr,
gio0eaf2712024-04-14 13:08:46 +0400185 sshKey,
gio33059762024-07-05 13:19:07 +0400186 gitRepoPublicKey,
gio0eaf2712024-04-14 13:08:46 +0400187 client,
188 namespace,
giocb34ad22024-07-11 08:01:13 +0400189 envAppManagerAddr,
gio0eaf2712024-04-14 13:08:46 +0400190 env,
gio33059762024-07-05 13:19:07 +0400191 nsc,
giof8843412024-05-22 16:38:05 +0400192 jc,
gio36b23b32024-08-25 12:20:54 +0400193 vpnKeyGen,
giof6ad2982024-08-23 17:42:49 +0400194 cnc,
gio266c04f2024-07-03 14:18:45 +0400195 map[string]map[string]struct{}{},
giod8ab4f52024-07-26 16:58:34 +0400196 map[string]appConfig{},
gio23bdc1b2024-07-11 16:07:47 +0400197 tmplts,
gio5e49bb62024-07-20 10:43:19 +0400198 appTmpls,
giocafd4e62024-07-31 10:53:40 +0400199 external,
200 fetchUsersAddr,
gio43b0f422024-08-21 10:40:13 +0400201 reconciler,
gio183e8342024-08-20 06:01:24 +0400202 map[string]string{},
gio9f6b27d2024-10-14 10:08:40 +0400203 schemaTmpl,
gio0eaf2712024-04-14 13:08:46 +0400204 }
gioa60f0de2024-07-08 10:49:48 +0400205 config, err := client.GetRepo(ConfigRepoName)
gio9d66f322024-07-06 13:45:10 +0400206 if err != nil {
207 return nil, err
208 }
giod8ab4f52024-07-26 16:58:34 +0400209 r, err := config.Reader(appConfigsFile)
gio9d66f322024-07-06 13:45:10 +0400210 if err == nil {
211 defer r.Close()
giod8ab4f52024-07-26 16:58:34 +0400212 if err := json.NewDecoder(r).Decode(&s.appConfigs); err != nil {
gio9d66f322024-07-06 13:45:10 +0400213 return nil, err
214 }
215 } else if !errors.Is(err, fs.ErrNotExist) {
216 return nil, err
217 }
218 return s, nil
gio0eaf2712024-04-14 13:08:46 +0400219}
220
gio59946282024-10-07 12:55:51 +0400221func (s *Server) getAppConfig(app, branch string) appConfig {
gio7fbd4ad2024-08-27 10:06:39 +0400222 return s.appConfigs[fmt.Sprintf("%s-%s", app, branch)]
223}
224
gio59946282024-10-07 12:55:51 +0400225func (s *Server) setAppConfig(app, branch string, cfg appConfig) {
gio7fbd4ad2024-08-27 10:06:39 +0400226 s.appConfigs[fmt.Sprintf("%s-%s", app, branch)] = cfg
227}
228
gio59946282024-10-07 12:55:51 +0400229func (s *Server) Start() error {
gio7fbd4ad2024-08-27 10:06:39 +0400230 // if err := s.client.DisableKeyless(); err != nil {
231 // return err
232 // }
233 // if err := s.client.DisableAnonAccess(); err != nil {
234 // return err
235 // }
gioa60f0de2024-07-08 10:49:48 +0400236 e := make(chan error)
237 go func() {
238 r := mux.NewRouter()
gio81246f02024-07-10 12:02:15 +0400239 r.Use(s.mwAuth)
gio9f6b27d2024-10-14 10:08:40 +0400240 r.HandleFunc(schemasPath+"{user}/app.schema.json", s.handleSchema).Methods(http.MethodGet)
gio59946282024-10-07 12:55:51 +0400241 r.PathPrefix(staticPath).Handler(server.NewCachingHandler(http.FileServer(http.FS(staticAssets))))
gio81246f02024-07-10 12:02:15 +0400242 r.HandleFunc(logoutPath, s.handleLogout).Methods(http.MethodGet)
gio8fae3af2024-07-25 13:43:31 +0400243 r.HandleFunc(apiPublicData, s.handleAPIPublicData)
244 r.HandleFunc(apiCreateApp, s.handleAPICreateApp).Methods(http.MethodPost)
gio81246f02024-07-10 12:02:15 +0400245 r.HandleFunc("/{app-name}"+loginPath, s.handleLoginForm).Methods(http.MethodGet)
246 r.HandleFunc("/{app-name}"+loginPath, s.handleLogin).Methods(http.MethodPost)
gio183e8342024-08-20 06:01:24 +0400247 r.HandleFunc("/{app-name}/logs", s.handleAppLogs).Methods(http.MethodGet)
giob4a3a192024-08-19 09:55:47 +0400248 r.HandleFunc("/{app-name}/{hash}", s.handleAppCommit).Methods(http.MethodGet)
gio7fbd4ad2024-08-27 10:06:39 +0400249 r.HandleFunc("/{app-name}/dev-branch/create", s.handleCreateDevBranch).Methods(http.MethodPost)
250 r.HandleFunc("/{app-name}/branch/{branch}", s.handleAppStatus).Methods(http.MethodGet)
gio5887caa2024-10-03 15:07:23 +0400251 r.HandleFunc("/{app-name}/branch/{branch}/delete", s.handleBranchDelete).Methods(http.MethodPost)
gio81246f02024-07-10 12:02:15 +0400252 r.HandleFunc("/{app-name}", s.handleAppStatus).Methods(http.MethodGet)
gio5887caa2024-10-03 15:07:23 +0400253 r.HandleFunc("/{app-name}/delete", s.handleAppDelete).Methods(http.MethodPost)
gio81246f02024-07-10 12:02:15 +0400254 r.HandleFunc("/", s.handleStatus).Methods(http.MethodGet)
gio11617ac2024-07-15 16:09:04 +0400255 r.HandleFunc("/", s.handleCreateApp).Methods(http.MethodPost)
gioa60f0de2024-07-08 10:49:48 +0400256 e <- http.ListenAndServe(fmt.Sprintf(":%d", s.port), r)
257 }()
258 go func() {
259 r := mux.NewRouter()
gio8fae3af2024-07-25 13:43:31 +0400260 r.HandleFunc("/update", s.handleAPIUpdate)
261 r.HandleFunc("/api/apps/{app-name}/workers", s.handleAPIRegisterWorker).Methods(http.MethodPost)
gio7fbd4ad2024-08-27 10:06:39 +0400262 r.HandleFunc("/api/add-public-key", s.handleAPIAddPublicKey).Methods(http.MethodPost)
giocfb228c2024-09-06 15:44:31 +0400263 r.HandleFunc("/api/apps/{app-name}/branch/{branch}/env-profile", s.handleBranchEnvProfile).Methods(http.MethodGet)
giocafd4e62024-07-31 10:53:40 +0400264 if !s.external {
265 r.HandleFunc("/api/sync-users", s.handleAPISyncUsers).Methods(http.MethodGet)
266 }
gioa60f0de2024-07-08 10:49:48 +0400267 e <- http.ListenAndServe(fmt.Sprintf(":%d", s.apiPort), r)
268 }()
giocafd4e62024-07-31 10:53:40 +0400269 if !s.external {
270 go func() {
271 s.syncUsers()
Davit Tabidzea5ea5092024-08-01 15:28:09 +0400272 for {
273 delay := time.Duration(rand.Intn(60)+60) * time.Second
274 time.Sleep(delay)
giocafd4e62024-07-31 10:53:40 +0400275 s.syncUsers()
276 }
277 }()
278 }
gioa60f0de2024-07-08 10:49:48 +0400279 return <-e
280}
281
gio11617ac2024-07-15 16:09:04 +0400282type UserGetter interface {
283 Get(r *http.Request) string
gio8fae3af2024-07-25 13:43:31 +0400284 Encode(w http.ResponseWriter, user string) error
gio11617ac2024-07-15 16:09:04 +0400285}
286
287type externalUserGetter struct {
288 sc *securecookie.SecureCookie
289}
290
291func NewExternalUserGetter() UserGetter {
gio8fae3af2024-07-25 13:43:31 +0400292 return &externalUserGetter{securecookie.New(
293 securecookie.GenerateRandomKey(64),
294 securecookie.GenerateRandomKey(32),
295 )}
gio11617ac2024-07-15 16:09:04 +0400296}
297
298func (ug *externalUserGetter) Get(r *http.Request) string {
299 cookie, err := r.Cookie(sessionCookie)
300 if err != nil {
301 return ""
302 }
303 var user string
304 if err := ug.sc.Decode(sessionCookie, cookie.Value, &user); err != nil {
305 return ""
306 }
307 return user
308}
309
gio8fae3af2024-07-25 13:43:31 +0400310func (ug *externalUserGetter) Encode(w http.ResponseWriter, user string) error {
311 if encoded, err := ug.sc.Encode(sessionCookie, user); err == nil {
312 cookie := &http.Cookie{
313 Name: sessionCookie,
314 Value: encoded,
315 Path: "/",
316 Secure: true,
317 HttpOnly: true,
318 }
319 http.SetCookie(w, cookie)
320 return nil
321 } else {
322 return err
323 }
324}
325
gio11617ac2024-07-15 16:09:04 +0400326type internalUserGetter struct{}
327
328func NewInternalUserGetter() UserGetter {
329 return internalUserGetter{}
330}
331
332func (ug internalUserGetter) Get(r *http.Request) string {
giodd213152024-09-27 11:26:59 +0200333 return r.Header.Get("X-Forwarded-User")
gio11617ac2024-07-15 16:09:04 +0400334}
335
gio8fae3af2024-07-25 13:43:31 +0400336func (ug internalUserGetter) Encode(w http.ResponseWriter, user string) error {
337 return nil
338}
339
gio59946282024-10-07 12:55:51 +0400340func (s *Server) mwAuth(next http.Handler) http.Handler {
gio81246f02024-07-10 12:02:15 +0400341 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gio8fae3af2024-07-25 13:43:31 +0400342 if strings.HasSuffix(r.URL.Path, loginPath) ||
343 strings.HasPrefix(r.URL.Path, logoutPath) ||
344 strings.HasPrefix(r.URL.Path, staticPath) ||
gioc81a8472024-09-24 13:06:19 +0200345 strings.HasPrefix(r.URL.Path, schemasPath) ||
gio8fae3af2024-07-25 13:43:31 +0400346 strings.HasPrefix(r.URL.Path, apiPublicData) ||
347 strings.HasPrefix(r.URL.Path, apiCreateApp) {
gio81246f02024-07-10 12:02:15 +0400348 next.ServeHTTP(w, r)
349 return
350 }
gio11617ac2024-07-15 16:09:04 +0400351 user := s.ug.Get(r)
352 if user == "" {
gio81246f02024-07-10 12:02:15 +0400353 vars := mux.Vars(r)
354 appName, ok := vars["app-name"]
355 if !ok || appName == "" {
356 http.Error(w, "missing app-name", http.StatusBadRequest)
357 return
358 }
359 http.Redirect(w, r, fmt.Sprintf("/%s%s", appName, loginPath), http.StatusSeeOther)
360 return
361 }
gio81246f02024-07-10 12:02:15 +0400362 next.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), userCtx, user)))
363 })
364}
365
gio9f6b27d2024-10-14 10:08:40 +0400366type schemaNetwork struct {
367 Value string `json:"const"`
368}
369
gio59946282024-10-07 12:55:51 +0400370func (s *Server) handleSchema(w http.ResponseWriter, r *http.Request) {
gioc81a8472024-09-24 13:06:19 +0200371 w.Header().Set("Content-Type", "application/schema+json")
gio9f6b27d2024-10-14 10:08:40 +0400372 vars := mux.Vars(r)
373 user, ok := vars["user"]
374 if !ok {
375 http.Error(w, "no user", http.StatusBadRequest)
376 return
377 }
378 networks, err := s.getNetworks(user)
379 if err != nil {
380 http.Error(w, err.Error(), http.StatusInternalServerError)
381 return
382 }
383 var names []schemaNetwork
384 for _, n := range networks {
385 names = append(names, schemaNetwork{n.Name})
386 }
387 var tmp strings.Builder
388 if err := json.NewEncoder(&tmp).Encode(names); err != nil {
389 http.Error(w, err.Error(), http.StatusInternalServerError)
390 return
391 }
392 if err := s.schemaTmpl.Execute(w, map[string]any{
393 "Networks": tmp.String(),
394 }); err != nil {
395 http.Error(w, err.Error(), http.StatusInternalServerError)
396 return
397 }
gioc81a8472024-09-24 13:06:19 +0200398}
399
gio59946282024-10-07 12:55:51 +0400400func (s *Server) handleLogout(w http.ResponseWriter, r *http.Request) {
gio8fae3af2024-07-25 13:43:31 +0400401 // TODO(gio): move to UserGetter
gio81246f02024-07-10 12:02:15 +0400402 http.SetCookie(w, &http.Cookie{
403 Name: sessionCookie,
404 Value: "",
405 Path: "/",
406 HttpOnly: true,
407 Secure: true,
408 })
409 http.Redirect(w, r, "/", http.StatusSeeOther)
410}
411
gio59946282024-10-07 12:55:51 +0400412func (s *Server) handleLoginForm(w http.ResponseWriter, r *http.Request) {
gio81246f02024-07-10 12:02:15 +0400413 vars := mux.Vars(r)
414 appName, ok := vars["app-name"]
415 if !ok || appName == "" {
416 http.Error(w, "missing app-name", http.StatusBadRequest)
417 return
418 }
419 fmt.Fprint(w, `
420<!DOCTYPE html>
421<html lang='en'>
422 <head>
423 <title>dodo: app - login</title>
424 <meta charset='utf-8'>
425 </head>
426 <body>
427 <form action="" method="POST">
428 <input type="password" placeholder="Password" name="password" required />
429 <button type="submit">Login</button>
430 </form>
431 </body>
432</html>
433`)
434}
435
gio59946282024-10-07 12:55:51 +0400436func (s *Server) handleLogin(w http.ResponseWriter, r *http.Request) {
gio81246f02024-07-10 12:02:15 +0400437 vars := mux.Vars(r)
438 appName, ok := vars["app-name"]
439 if !ok || appName == "" {
440 http.Error(w, "missing app-name", http.StatusBadRequest)
441 return
442 }
443 password := r.FormValue("password")
444 if password == "" {
445 http.Error(w, "missing password", http.StatusBadRequest)
446 return
447 }
448 user, err := s.st.GetAppOwner(appName)
449 if err != nil {
450 http.Error(w, err.Error(), http.StatusInternalServerError)
451 return
452 }
453 hashed, err := s.st.GetUserPassword(user)
454 if err != nil {
455 http.Error(w, err.Error(), http.StatusInternalServerError)
456 return
457 }
458 if err := bcrypt.CompareHashAndPassword(hashed, []byte(password)); err != nil {
459 http.Redirect(w, r, r.URL.Path, http.StatusSeeOther)
460 return
461 }
gio8fae3af2024-07-25 13:43:31 +0400462 if err := s.ug.Encode(w, user); err != nil {
463 http.Error(w, err.Error(), http.StatusInternalServerError)
464 return
gio81246f02024-07-10 12:02:15 +0400465 }
466 http.Redirect(w, r, fmt.Sprintf("/%s", appName), http.StatusSeeOther)
467}
468
giob4a3a192024-08-19 09:55:47 +0400469type navItem struct {
470 Name string
471 Address string
472}
473
gio23bdc1b2024-07-11 16:07:47 +0400474type statusData struct {
giob4a3a192024-08-19 09:55:47 +0400475 Navigation []navItem
476 Apps []string
477 Networks []installer.Network
478 Types []string
gio23bdc1b2024-07-11 16:07:47 +0400479}
480
gio59946282024-10-07 12:55:51 +0400481func (s *Server) handleStatus(w http.ResponseWriter, r *http.Request) {
gio81246f02024-07-10 12:02:15 +0400482 user := r.Context().Value(userCtx)
483 if user == nil {
484 http.Error(w, "unauthorized", http.StatusUnauthorized)
485 return
486 }
gio281aa512024-10-25 23:25:53 +0400487 var apps []string
488 var err error
489 if s.external {
490 apps, err = s.st.GetUserApps(user.(string))
491 } else {
492 apps, err = s.st.GetApps()
493 }
gioa60f0de2024-07-08 10:49:48 +0400494 if err != nil {
495 http.Error(w, err.Error(), http.StatusInternalServerError)
496 return
497 }
gio11617ac2024-07-15 16:09:04 +0400498 networks, err := s.getNetworks(user.(string))
499 if err != nil {
500 http.Error(w, err.Error(), http.StatusInternalServerError)
501 return
502 }
giob54db242024-07-30 18:49:33 +0400503 var types []string
504 for _, t := range s.appTmpls.Types() {
505 types = append(types, strings.Replace(t, "-", ":", 1))
506 }
giob4a3a192024-08-19 09:55:47 +0400507 n := []navItem{navItem{"Home", "/"}}
508 data := statusData{n, apps, networks, types}
gio23bdc1b2024-07-11 16:07:47 +0400509 if err := s.tmplts.index.Execute(w, data); err != nil {
510 http.Error(w, err.Error(), http.StatusInternalServerError)
511 return
gioa60f0de2024-07-08 10:49:48 +0400512 }
513}
514
gio5e49bb62024-07-20 10:43:19 +0400515type appStatusData struct {
giob4a3a192024-08-19 09:55:47 +0400516 Navigation []navItem
gio5e49bb62024-07-20 10:43:19 +0400517 Name string
gio5887caa2024-10-03 15:07:23 +0400518 Branch string
gio5e49bb62024-07-20 10:43:19 +0400519 GitCloneCommand string
giob4a3a192024-08-19 09:55:47 +0400520 Commits []CommitMeta
gio183e8342024-08-20 06:01:24 +0400521 LastCommit resourceData
gio7fbd4ad2024-08-27 10:06:39 +0400522 Branches []string
gio5e49bb62024-07-20 10:43:19 +0400523}
524
gio59946282024-10-07 12:55:51 +0400525func (s *Server) handleAppStatus(w http.ResponseWriter, r *http.Request) {
gioa60f0de2024-07-08 10:49:48 +0400526 vars := mux.Vars(r)
527 appName, ok := vars["app-name"]
528 if !ok || appName == "" {
529 http.Error(w, "missing app-name", http.StatusBadRequest)
530 return
531 }
gio7fbd4ad2024-08-27 10:06:39 +0400532 branch, ok := vars["branch"]
533 if !ok || branch == "" {
534 branch = "master"
535 }
gio94904702024-07-26 16:58:34 +0400536 u := r.Context().Value(userCtx)
537 if u == nil {
538 http.Error(w, "unauthorized", http.StatusUnauthorized)
539 return
540 }
541 user, ok := u.(string)
542 if !ok {
543 http.Error(w, "could not get user", http.StatusInternalServerError)
544 return
545 }
546 owner, err := s.st.GetAppOwner(appName)
547 if err != nil {
548 http.Error(w, err.Error(), http.StatusInternalServerError)
549 return
550 }
gio281aa512024-10-25 23:25:53 +0400551 if s.external && owner != user {
gio94904702024-07-26 16:58:34 +0400552 http.Error(w, "unauthorized", http.StatusUnauthorized)
553 return
554 }
gio7fbd4ad2024-08-27 10:06:39 +0400555 commits, err := s.st.GetCommitHistory(appName, branch)
gioa60f0de2024-07-08 10:49:48 +0400556 if err != nil {
557 http.Error(w, err.Error(), http.StatusInternalServerError)
558 return
559 }
gio183e8342024-08-20 06:01:24 +0400560 var lastCommitResources resourceData
561 if len(commits) > 0 {
562 lastCommit, err := s.st.GetCommit(commits[len(commits)-1].Hash)
563 if err != nil {
564 http.Error(w, err.Error(), http.StatusInternalServerError)
565 return
566 }
567 r, err := extractResourceData(lastCommit.Resources.Helm)
568 if err != nil {
569 http.Error(w, err.Error(), http.StatusInternalServerError)
570 return
571 }
572 lastCommitResources = r
573 }
gio7fbd4ad2024-08-27 10:06:39 +0400574 branches, err := s.st.GetBranches(appName)
575 if err != nil {
576 http.Error(w, err.Error(), http.StatusInternalServerError)
577 return
578 }
gio5e49bb62024-07-20 10:43:19 +0400579 data := appStatusData{
giob4a3a192024-08-19 09:55:47 +0400580 Navigation: []navItem{
581 navItem{"Home", "/"},
582 navItem{appName, "/" + appName},
583 },
gio5e49bb62024-07-20 10:43:19 +0400584 Name: appName,
gio5887caa2024-10-03 15:07:23 +0400585 Branch: branch,
gio5e49bb62024-07-20 10:43:19 +0400586 GitCloneCommand: fmt.Sprintf("git clone %s/%s\n\n\n", s.repoPublicAddr, appName),
587 Commits: commits,
gio183e8342024-08-20 06:01:24 +0400588 LastCommit: lastCommitResources,
gio7fbd4ad2024-08-27 10:06:39 +0400589 Branches: branches,
590 }
591 if branch != "master" {
592 data.Navigation = append(data.Navigation, navItem{branch, fmt.Sprintf("/%s/branch/%s", appName, branch)})
gio5e49bb62024-07-20 10:43:19 +0400593 }
594 if err := s.tmplts.appStatus.Execute(w, data); err != nil {
595 http.Error(w, err.Error(), http.StatusInternalServerError)
596 return
gioa60f0de2024-07-08 10:49:48 +0400597 }
gio0eaf2712024-04-14 13:08:46 +0400598}
599
giocfb228c2024-09-06 15:44:31 +0400600type appEnv struct {
601 Profile string `json:"envProfile"`
602}
603
gio59946282024-10-07 12:55:51 +0400604func (s *Server) handleBranchEnvProfile(w http.ResponseWriter, r *http.Request) {
giocfb228c2024-09-06 15:44:31 +0400605 vars := mux.Vars(r)
606 appName, ok := vars["app-name"]
607 if !ok || appName == "" {
608 http.Error(w, "missing app-name", http.StatusBadRequest)
609 return
610 }
611 branch, ok := vars["branch"]
612 if !ok || branch == "" {
613 branch = "master"
614 }
615 info, err := s.st.GetLastCommitInfo(appName, branch)
616 if err != nil {
617 http.Error(w, err.Error(), http.StatusInternalServerError)
618 return
619 }
620 var e appEnv
621 if err := json.NewDecoder(bytes.NewReader(info.Resources.RenderedRaw)).Decode(&e); err != nil {
622 http.Error(w, err.Error(), http.StatusInternalServerError)
623 return
624 }
625 fmt.Fprintln(w, e.Profile)
626}
627
giob4a3a192024-08-19 09:55:47 +0400628type volume struct {
629 Name string
630 Size string
631}
632
633type postgresql struct {
634 Name string
635 Version string
636 Volume string
637}
638
gio07eb1082024-10-25 14:35:56 +0400639type mongodb struct {
640 Name string
641 Version string
642 Volume string
643}
644
giob4a3a192024-08-19 09:55:47 +0400645type ingress struct {
giof078f462024-10-14 09:07:33 +0400646 Name string
giob4a3a192024-08-19 09:55:47 +0400647 Host string
giof078f462024-10-14 09:07:33 +0400648 Home string
giob4a3a192024-08-19 09:55:47 +0400649}
650
gio7fbd4ad2024-08-27 10:06:39 +0400651type vm struct {
652 Name string
653 User string
654 CPUCores int
655 Memory string
656}
657
giob4a3a192024-08-19 09:55:47 +0400658type resourceData struct {
gio7fbd4ad2024-08-27 10:06:39 +0400659 Volume []volume
660 PostgreSQL []postgresql
gio07eb1082024-10-25 14:35:56 +0400661 MongoDB []mongodb
gio7fbd4ad2024-08-27 10:06:39 +0400662 Ingress []ingress
663 VirtualMachine []vm
giob4a3a192024-08-19 09:55:47 +0400664}
665
666type commitStatusData struct {
667 Navigation []navItem
668 AppName string
669 Commit Commit
670 Resources resourceData
671}
672
gio59946282024-10-07 12:55:51 +0400673func (s *Server) handleAppCommit(w http.ResponseWriter, r *http.Request) {
giob4a3a192024-08-19 09:55:47 +0400674 vars := mux.Vars(r)
675 appName, ok := vars["app-name"]
676 if !ok || appName == "" {
677 http.Error(w, "missing app-name", http.StatusBadRequest)
678 return
679 }
680 hash, ok := vars["hash"]
681 if !ok || appName == "" {
682 http.Error(w, "missing app-name", http.StatusBadRequest)
683 return
684 }
685 u := r.Context().Value(userCtx)
686 if u == nil {
687 http.Error(w, "unauthorized", http.StatusUnauthorized)
688 return
689 }
690 user, ok := u.(string)
691 if !ok {
692 http.Error(w, "could not get user", http.StatusInternalServerError)
693 return
694 }
695 owner, err := s.st.GetAppOwner(appName)
696 if err != nil {
697 http.Error(w, err.Error(), http.StatusInternalServerError)
698 return
699 }
gio281aa512024-10-25 23:25:53 +0400700 if s.external && owner != user {
giob4a3a192024-08-19 09:55:47 +0400701 http.Error(w, "unauthorized", http.StatusUnauthorized)
702 return
703 }
704 commit, err := s.st.GetCommit(hash)
705 if err != nil {
706 // TODO(gio): not-found ?
707 http.Error(w, err.Error(), http.StatusInternalServerError)
708 return
709 }
710 var res strings.Builder
711 if err := json.NewEncoder(&res).Encode(commit.Resources.Helm); err != nil {
712 http.Error(w, err.Error(), http.StatusInternalServerError)
713 return
714 }
715 resData, err := extractResourceData(commit.Resources.Helm)
716 if err != nil {
717 http.Error(w, err.Error(), http.StatusInternalServerError)
718 return
719 }
720 data := commitStatusData{
721 Navigation: []navItem{
722 navItem{"Home", "/"},
723 navItem{appName, "/" + appName},
724 navItem{hash, "/" + appName + "/" + hash},
725 },
726 AppName: appName,
727 Commit: commit,
728 Resources: resData,
729 }
730 if err := s.tmplts.commitStatus.Execute(w, data); err != nil {
731 http.Error(w, err.Error(), http.StatusInternalServerError)
732 return
733 }
734}
735
gio183e8342024-08-20 06:01:24 +0400736type logData struct {
737 Navigation []navItem
738 AppName string
739 Logs template.HTML
740}
741
gio59946282024-10-07 12:55:51 +0400742func (s *Server) handleAppLogs(w http.ResponseWriter, r *http.Request) {
gio183e8342024-08-20 06:01:24 +0400743 vars := mux.Vars(r)
744 appName, ok := vars["app-name"]
745 if !ok || appName == "" {
746 http.Error(w, "missing app-name", http.StatusBadRequest)
747 return
748 }
749 u := r.Context().Value(userCtx)
750 if u == nil {
751 http.Error(w, "unauthorized", http.StatusUnauthorized)
752 return
753 }
754 user, ok := u.(string)
755 if !ok {
756 http.Error(w, "could not get user", http.StatusInternalServerError)
757 return
758 }
759 owner, err := s.st.GetAppOwner(appName)
760 if err != nil {
761 http.Error(w, err.Error(), http.StatusInternalServerError)
762 return
763 }
gio281aa512024-10-25 23:25:53 +0400764 if s.external && owner != user {
gio183e8342024-08-20 06:01:24 +0400765 http.Error(w, "unauthorized", http.StatusUnauthorized)
766 return
767 }
768 data := logData{
769 Navigation: []navItem{
770 navItem{"Home", "/"},
771 navItem{appName, "/" + appName},
772 navItem{"Logs", "/" + appName + "/logs"},
773 },
774 AppName: appName,
775 Logs: template.HTML(strings.ReplaceAll(s.logs[appName], "\n", "<br/>")),
776 }
777 if err := s.tmplts.logs.Execute(w, data); err != nil {
778 fmt.Println(err)
779 http.Error(w, err.Error(), http.StatusInternalServerError)
780 return
781 }
782}
783
gio81246f02024-07-10 12:02:15 +0400784type apiUpdateReq struct {
gio266c04f2024-07-03 14:18:45 +0400785 Ref string `json:"ref"`
786 Repository struct {
787 Name string `json:"name"`
788 } `json:"repository"`
gioe2e31e12024-08-18 08:20:56 +0400789 After string `json:"after"`
790 Commits []struct {
791 Id string `json:"id"`
792 Message string `json:"message"`
793 } `json:"commits"`
gio0eaf2712024-04-14 13:08:46 +0400794}
795
gio59946282024-10-07 12:55:51 +0400796func (s *Server) handleAPIUpdate(w http.ResponseWriter, r *http.Request) {
gio0eaf2712024-04-14 13:08:46 +0400797 fmt.Println("update")
gio81246f02024-07-10 12:02:15 +0400798 var req apiUpdateReq
gio0eaf2712024-04-14 13:08:46 +0400799 var contents strings.Builder
800 io.Copy(&contents, r.Body)
801 c := contents.String()
802 fmt.Println(c)
803 if err := json.NewDecoder(strings.NewReader(c)).Decode(&req); err != nil {
gio23bdc1b2024-07-11 16:07:47 +0400804 http.Error(w, err.Error(), http.StatusBadRequest)
gio0eaf2712024-04-14 13:08:46 +0400805 return
806 }
gio7fbd4ad2024-08-27 10:06:39 +0400807 if strings.HasPrefix(req.Ref, "refs/heads/dodo_") || req.Repository.Name == ConfigRepoName {
808 return
809 }
810 branch, ok := strings.CutPrefix(req.Ref, "refs/heads/")
811 if !ok {
812 http.Error(w, "invalid branch", http.StatusBadRequest)
gio0eaf2712024-04-14 13:08:46 +0400813 return
814 }
gioa60f0de2024-07-08 10:49:48 +0400815 // TODO(gio): Create commit record on app init as well
gio0eaf2712024-04-14 13:08:46 +0400816 go func() {
gio11617ac2024-07-15 16:09:04 +0400817 owner, err := s.st.GetAppOwner(req.Repository.Name)
818 if err != nil {
819 return
820 }
821 networks, err := s.getNetworks(owner)
giocb34ad22024-07-11 08:01:13 +0400822 if err != nil {
823 return
824 }
giof15b9da2024-09-19 06:59:16 +0400825 // TODO(gio): get only available ones by owner
826 clusters, err := s.getClusters()
827 if err != nil {
828 return
829 }
gio94904702024-07-26 16:58:34 +0400830 apps := installer.NewInMemoryAppRepository(installer.CreateAllApps())
831 instanceAppStatus, err := installer.FindEnvApp(apps, "dodo-app-instance-status")
832 if err != nil {
833 return
834 }
gioe2e31e12024-08-18 08:20:56 +0400835 found := false
836 commitMsg := ""
837 for _, c := range req.Commits {
838 if c.Id == req.After {
839 found = true
840 commitMsg = c.Message
841 break
gioa60f0de2024-07-08 10:49:48 +0400842 }
843 }
gioe2e31e12024-08-18 08:20:56 +0400844 if !found {
845 fmt.Printf("Error: could not find commit message")
846 return
847 }
gio7fbd4ad2024-08-27 10:06:39 +0400848 s.l.Lock()
849 defer s.l.Unlock()
giof15b9da2024-09-19 06:59:16 +0400850 resources, err := s.updateDodoApp(instanceAppStatus, req.Repository.Name, branch, s.getAppConfig(req.Repository.Name, branch).Namespace, networks, clusters, owner)
gio7fbd4ad2024-08-27 10:06:39 +0400851 if err = s.createCommit(req.Repository.Name, branch, req.After, commitMsg, err, resources); err != nil {
gio12e887d2024-08-18 16:09:47 +0400852 fmt.Printf("Error: %s\n", err.Error())
gioe2e31e12024-08-18 08:20:56 +0400853 return
854 }
gioa60f0de2024-07-08 10:49:48 +0400855 for addr, _ := range s.workers[req.Repository.Name] {
856 go func() {
857 // TODO(gio): make port configurable
858 http.Get(fmt.Sprintf("http://%s/update", addr))
859 }()
gio0eaf2712024-04-14 13:08:46 +0400860 }
861 }()
gio0eaf2712024-04-14 13:08:46 +0400862}
863
gio81246f02024-07-10 12:02:15 +0400864type apiRegisterWorkerReq struct {
gio0eaf2712024-04-14 13:08:46 +0400865 Address string `json:"address"`
gio183e8342024-08-20 06:01:24 +0400866 Logs string `json:"logs"`
gio0eaf2712024-04-14 13:08:46 +0400867}
868
gio59946282024-10-07 12:55:51 +0400869func (s *Server) handleAPIRegisterWorker(w http.ResponseWriter, r *http.Request) {
gio7fbd4ad2024-08-27 10:06:39 +0400870 // TODO(gio): lock
gioa60f0de2024-07-08 10:49:48 +0400871 vars := mux.Vars(r)
872 appName, ok := vars["app-name"]
873 if !ok || appName == "" {
874 http.Error(w, "missing app-name", http.StatusBadRequest)
875 return
876 }
gio81246f02024-07-10 12:02:15 +0400877 var req apiRegisterWorkerReq
gio0eaf2712024-04-14 13:08:46 +0400878 if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
879 http.Error(w, err.Error(), http.StatusInternalServerError)
880 return
881 }
gioa60f0de2024-07-08 10:49:48 +0400882 if _, ok := s.workers[appName]; !ok {
883 s.workers[appName] = map[string]struct{}{}
gio266c04f2024-07-03 14:18:45 +0400884 }
gioa60f0de2024-07-08 10:49:48 +0400885 s.workers[appName][req.Address] = struct{}{}
gio183e8342024-08-20 06:01:24 +0400886 s.logs[appName] = req.Logs
gio0eaf2712024-04-14 13:08:46 +0400887}
888
gio59946282024-10-07 12:55:51 +0400889func (s *Server) handleCreateApp(w http.ResponseWriter, r *http.Request) {
gio11617ac2024-07-15 16:09:04 +0400890 u := r.Context().Value(userCtx)
891 if u == nil {
892 http.Error(w, "unauthorized", http.StatusUnauthorized)
893 return
894 }
895 user, ok := u.(string)
896 if !ok {
897 http.Error(w, "could not get user", http.StatusInternalServerError)
898 return
899 }
900 network := r.FormValue("network")
901 if network == "" {
902 http.Error(w, "missing network", http.StatusBadRequest)
903 return
904 }
gio5e49bb62024-07-20 10:43:19 +0400905 subdomain := r.FormValue("subdomain")
906 if subdomain == "" {
907 http.Error(w, "missing subdomain", http.StatusBadRequest)
908 return
909 }
910 appType := r.FormValue("type")
911 if appType == "" {
912 http.Error(w, "missing type", http.StatusBadRequest)
913 return
914 }
gio5cc6afc2024-10-06 09:33:44 +0400915 appName := r.FormValue("name")
916 var err error
917 if appName == "" {
918 g := installer.NewFixedLengthRandomNameGenerator(3)
919 appName, err = g.Generate()
920 }
gio11617ac2024-07-15 16:09:04 +0400921 if err != nil {
922 http.Error(w, err.Error(), http.StatusInternalServerError)
923 return
924 }
925 if ok, err := s.client.UserExists(user); err != nil {
926 http.Error(w, err.Error(), http.StatusInternalServerError)
927 return
928 } else if !ok {
giocafd4e62024-07-31 10:53:40 +0400929 http.Error(w, "user sync has not finished, please try again in few minutes", http.StatusFailedDependency)
930 return
gio11617ac2024-07-15 16:09:04 +0400931 }
giocafd4e62024-07-31 10:53:40 +0400932 if err := s.st.CreateUser(user, nil, network); err != nil && !errors.Is(err, ErrorAlreadyExists) {
gio11617ac2024-07-15 16:09:04 +0400933 http.Error(w, err.Error(), http.StatusInternalServerError)
934 return
935 }
936 if err := s.st.CreateApp(appName, user); err != nil {
937 http.Error(w, err.Error(), http.StatusInternalServerError)
938 return
939 }
giod8ab4f52024-07-26 16:58:34 +0400940 if err := s.createApp(user, appName, appType, network, subdomain); err != nil {
gio11617ac2024-07-15 16:09:04 +0400941 http.Error(w, err.Error(), http.StatusInternalServerError)
942 return
943 }
944 http.Redirect(w, r, fmt.Sprintf("/%s", appName), http.StatusSeeOther)
945}
946
gio59946282024-10-07 12:55:51 +0400947func (s *Server) handleCreateDevBranch(w http.ResponseWriter, r *http.Request) {
gio7fbd4ad2024-08-27 10:06:39 +0400948 u := r.Context().Value(userCtx)
949 if u == nil {
950 http.Error(w, "unauthorized", http.StatusUnauthorized)
951 return
952 }
953 user, ok := u.(string)
954 if !ok {
955 http.Error(w, "could not get user", http.StatusInternalServerError)
956 return
957 }
958 vars := mux.Vars(r)
959 appName, ok := vars["app-name"]
960 if !ok || appName == "" {
961 http.Error(w, "missing app-name", http.StatusBadRequest)
962 return
963 }
964 branch := r.FormValue("branch")
965 if branch == "" {
gio5887caa2024-10-03 15:07:23 +0400966 http.Error(w, "missing branch", http.StatusBadRequest)
gio7fbd4ad2024-08-27 10:06:39 +0400967 return
968 }
969 if err := s.createDevBranch(appName, "master", branch, user); err != nil {
970 http.Error(w, err.Error(), http.StatusInternalServerError)
971 return
972 }
973 http.Redirect(w, r, fmt.Sprintf("/%s/branch/%s", appName, branch), http.StatusSeeOther)
974}
975
gio59946282024-10-07 12:55:51 +0400976func (s *Server) handleBranchDelete(w http.ResponseWriter, r *http.Request) {
gio5887caa2024-10-03 15:07:23 +0400977 u := r.Context().Value(userCtx)
978 if u == nil {
979 http.Error(w, "unauthorized", http.StatusUnauthorized)
980 return
981 }
982 vars := mux.Vars(r)
983 appName, ok := vars["app-name"]
984 if !ok || appName == "" {
985 http.Error(w, "missing app-name", http.StatusBadRequest)
986 return
987 }
988 branch, ok := vars["branch"]
989 if !ok || branch == "" {
990 http.Error(w, "missing branch", http.StatusBadRequest)
991 return
992 }
993 if err := s.deleteBranch(appName, branch); err != nil {
994 http.Error(w, err.Error(), http.StatusInternalServerError)
995 return
996 }
997 http.Redirect(w, r, fmt.Sprintf("/%s", appName), http.StatusSeeOther)
998}
999
gio59946282024-10-07 12:55:51 +04001000func (s *Server) handleAppDelete(w http.ResponseWriter, r *http.Request) {
gio5887caa2024-10-03 15:07:23 +04001001 u := r.Context().Value(userCtx)
1002 if u == nil {
1003 http.Error(w, "unauthorized", http.StatusUnauthorized)
1004 return
1005 }
1006 vars := mux.Vars(r)
1007 appName, ok := vars["app-name"]
1008 if !ok || appName == "" {
1009 http.Error(w, "missing app-name", http.StatusBadRequest)
1010 return
1011 }
1012 if err := s.deleteApp(appName); err != nil {
1013 http.Error(w, err.Error(), http.StatusInternalServerError)
1014 return
1015 }
gioe44c1512024-10-06 14:13:55 +04001016 http.Redirect(w, r, "/", http.StatusSeeOther)
gio5887caa2024-10-03 15:07:23 +04001017}
1018
gio81246f02024-07-10 12:02:15 +04001019type apiCreateAppReq struct {
gio5e49bb62024-07-20 10:43:19 +04001020 AppType string `json:"type"`
gio33059762024-07-05 13:19:07 +04001021 AdminPublicKey string `json:"adminPublicKey"`
gio11617ac2024-07-15 16:09:04 +04001022 Network string `json:"network"`
gio5e49bb62024-07-20 10:43:19 +04001023 Subdomain string `json:"subdomain"`
gio33059762024-07-05 13:19:07 +04001024}
1025
gio81246f02024-07-10 12:02:15 +04001026type apiCreateAppResp struct {
1027 AppName string `json:"appName"`
1028 Password string `json:"password"`
gio33059762024-07-05 13:19:07 +04001029}
1030
gio59946282024-10-07 12:55:51 +04001031func (s *Server) handleAPICreateApp(w http.ResponseWriter, r *http.Request) {
giod8ab4f52024-07-26 16:58:34 +04001032 w.Header().Set("Access-Control-Allow-Origin", "*")
gio81246f02024-07-10 12:02:15 +04001033 var req apiCreateAppReq
gio33059762024-07-05 13:19:07 +04001034 if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
1035 http.Error(w, err.Error(), http.StatusBadRequest)
1036 return
1037 }
1038 g := installer.NewFixedLengthRandomNameGenerator(3)
1039 appName, err := g.Generate()
1040 if err != nil {
1041 http.Error(w, err.Error(), http.StatusInternalServerError)
1042 return
1043 }
gio11617ac2024-07-15 16:09:04 +04001044 user, err := s.client.FindUser(req.AdminPublicKey)
gio81246f02024-07-10 12:02:15 +04001045 if err != nil {
gio33059762024-07-05 13:19:07 +04001046 http.Error(w, err.Error(), http.StatusInternalServerError)
1047 return
1048 }
gio11617ac2024-07-15 16:09:04 +04001049 if user != "" {
1050 http.Error(w, "public key already registered", http.StatusBadRequest)
1051 return
1052 }
1053 user = appName
1054 if err := s.client.AddUser(user, req.AdminPublicKey); err != nil {
1055 http.Error(w, err.Error(), http.StatusInternalServerError)
1056 return
1057 }
1058 password := generatePassword()
1059 hashed, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
1060 if err != nil {
1061 http.Error(w, err.Error(), http.StatusInternalServerError)
1062 return
1063 }
giocafd4e62024-07-31 10:53:40 +04001064 if err := s.st.CreateUser(user, hashed, req.Network); err != nil {
gio11617ac2024-07-15 16:09:04 +04001065 http.Error(w, err.Error(), http.StatusInternalServerError)
1066 return
1067 }
1068 if err := s.st.CreateApp(appName, user); err != nil {
1069 http.Error(w, err.Error(), http.StatusInternalServerError)
1070 return
1071 }
giod8ab4f52024-07-26 16:58:34 +04001072 if err := s.createApp(user, appName, req.AppType, req.Network, req.Subdomain); err != nil {
gio11617ac2024-07-15 16:09:04 +04001073 http.Error(w, err.Error(), http.StatusInternalServerError)
1074 return
1075 }
gio81246f02024-07-10 12:02:15 +04001076 resp := apiCreateAppResp{
1077 AppName: appName,
1078 Password: password,
1079 }
gio33059762024-07-05 13:19:07 +04001080 if err := json.NewEncoder(w).Encode(resp); err != nil {
1081 http.Error(w, err.Error(), http.StatusInternalServerError)
1082 return
1083 }
1084}
1085
gio59946282024-10-07 12:55:51 +04001086func (s *Server) isNetworkUseAllowed(network string) bool {
giocafd4e62024-07-31 10:53:40 +04001087 if !s.external {
giod8ab4f52024-07-26 16:58:34 +04001088 return true
1089 }
1090 for _, cfg := range s.appConfigs {
1091 if strings.ToLower(cfg.Network) == network {
1092 return false
1093 }
1094 }
1095 return true
1096}
1097
gio59946282024-10-07 12:55:51 +04001098func (s *Server) createApp(user, appName, appType, network, subdomain string) error {
gio9d66f322024-07-06 13:45:10 +04001099 s.l.Lock()
1100 defer s.l.Unlock()
gio33059762024-07-05 13:19:07 +04001101 fmt.Printf("Creating app: %s\n", appName)
giod8ab4f52024-07-26 16:58:34 +04001102 network = strings.ToLower(network)
1103 if !s.isNetworkUseAllowed(network) {
1104 return fmt.Errorf("network already used: %s", network)
1105 }
gio33059762024-07-05 13:19:07 +04001106 if ok, err := s.client.RepoExists(appName); err != nil {
gio11617ac2024-07-15 16:09:04 +04001107 return err
gio33059762024-07-05 13:19:07 +04001108 } else if ok {
gio11617ac2024-07-15 16:09:04 +04001109 return nil
gioa60f0de2024-07-08 10:49:48 +04001110 }
gio5e49bb62024-07-20 10:43:19 +04001111 networks, err := s.getNetworks(user)
1112 if err != nil {
1113 return err
1114 }
giod8ab4f52024-07-26 16:58:34 +04001115 n, ok := installer.NetworkMap(networks)[network]
gio5e49bb62024-07-20 10:43:19 +04001116 if !ok {
1117 return fmt.Errorf("network not found: %s\n", network)
1118 }
gio33059762024-07-05 13:19:07 +04001119 if err := s.client.AddRepository(appName); err != nil {
gio11617ac2024-07-15 16:09:04 +04001120 return err
gio33059762024-07-05 13:19:07 +04001121 }
1122 appRepo, err := s.client.GetRepo(appName)
1123 if err != nil {
gio11617ac2024-07-15 16:09:04 +04001124 return err
gio33059762024-07-05 13:19:07 +04001125 }
gio9f6b27d2024-10-14 10:08:40 +04001126 files, err := s.renderAppConfigTemplate(user, appType, n, subdomain)
gio7fbd4ad2024-08-27 10:06:39 +04001127 if err != nil {
1128 return err
1129 }
1130 return s.createAppForBranch(appRepo, appName, "master", user, network, files)
1131}
1132
gio59946282024-10-07 12:55:51 +04001133func (s *Server) createDevBranch(appName, fromBranch, toBranch, user string) error {
gio7fbd4ad2024-08-27 10:06:39 +04001134 s.l.Lock()
1135 defer s.l.Unlock()
1136 fmt.Printf("Creating dev branch app: %s %s %s\n", appName, fromBranch, toBranch)
1137 appRepo, err := s.client.GetRepoBranch(appName, fromBranch)
1138 if err != nil {
1139 return err
1140 }
gioc81a8472024-09-24 13:06:19 +02001141 appCfg, err := soft.ReadFile(appRepo, "app.json")
gio7fbd4ad2024-08-27 10:06:39 +04001142 if err != nil {
1143 return err
1144 }
gio9f6b27d2024-10-14 10:08:40 +04001145 network, branchCfg, err := createDevBranchAppConfig(appCfg, toBranch, user, s.selfPublic)
gio7fbd4ad2024-08-27 10:06:39 +04001146 if err != nil {
1147 return err
1148 }
gioc81a8472024-09-24 13:06:19 +02001149 return s.createAppForBranch(appRepo, appName, toBranch, user, network, map[string][]byte{"app.json": branchCfg})
gio7fbd4ad2024-08-27 10:06:39 +04001150}
1151
gio59946282024-10-07 12:55:51 +04001152func (s *Server) deleteBranch(appName string, branch string) error {
gio5887caa2024-10-03 15:07:23 +04001153 appBranch := fmt.Sprintf("dodo_%s", branch)
1154 hf := installer.NewGitHelmFetcher()
1155 if err := func() error {
1156 repo, err := s.client.GetRepoBranch(appName, appBranch)
1157 if err != nil {
1158 return err
1159 }
1160 m, err := installer.NewAppManager(repo, s.nsc, s.jc, hf, s.vpnKeyGen, s.cnc, "/.dodo")
1161 if err != nil {
1162 return err
1163 }
1164 return m.Remove("app")
1165 }(); err != nil {
1166 return err
1167 }
1168 configRepo, err := s.client.GetRepo(ConfigRepoName)
1169 if err != nil {
1170 return err
1171 }
1172 m, err := installer.NewAppManager(configRepo, s.nsc, s.jc, hf, s.vpnKeyGen, s.cnc, "/")
1173 if err != nil {
1174 return err
1175 }
1176 appPath := fmt.Sprintf("%s/%s", appName, branch)
gio829b1b72024-10-05 21:50:56 +04001177 if err := m.Remove(appPath); err != nil {
gio5887caa2024-10-03 15:07:23 +04001178 return err
1179 }
1180 if err := s.client.DeleteRepoBranch(appName, appBranch); err != nil {
1181 return err
1182 }
1183 if branch != "master" {
gioe44c1512024-10-06 14:13:55 +04001184 if err := s.client.DeleteRepoBranch(appName, branch); err != nil {
1185 return err
1186 }
gio5887caa2024-10-03 15:07:23 +04001187 }
gioe44c1512024-10-06 14:13:55 +04001188 return s.st.DeleteBranch(appName, branch)
gio5887caa2024-10-03 15:07:23 +04001189}
1190
gio59946282024-10-07 12:55:51 +04001191func (s *Server) deleteApp(appName string) error {
gio5887caa2024-10-03 15:07:23 +04001192 configRepo, err := s.client.GetRepo(ConfigRepoName)
1193 if err != nil {
1194 return err
1195 }
1196 branches, err := configRepo.ListDir(fmt.Sprintf("/%s", appName))
1197 if err != nil {
1198 return err
1199 }
1200 for _, b := range branches {
1201 if !b.IsDir() || strings.HasPrefix(b.Name(), "dodo_") {
1202 continue
1203 }
1204 if err := s.deleteBranch(appName, b.Name()); err != nil {
1205 return err
1206 }
1207 }
gioe44c1512024-10-06 14:13:55 +04001208 if err := s.client.DeleteRepo(appName); err != nil {
1209 return err
1210 }
1211 return s.st.DeleteApp(appName)
gio5887caa2024-10-03 15:07:23 +04001212}
1213
gio59946282024-10-07 12:55:51 +04001214func (s *Server) createAppForBranch(
gio7fbd4ad2024-08-27 10:06:39 +04001215 repo soft.RepoIO,
1216 appName string,
1217 branch string,
1218 user string,
1219 network string,
1220 files map[string][]byte,
1221) error {
1222 commit, err := repo.Do(func(fs soft.RepoFS) (string, error) {
1223 for path, contents := range files {
1224 if err := soft.WriteFile(fs, path, string(contents)); err != nil {
1225 return "", err
1226 }
1227 }
1228 return "init", nil
1229 }, soft.WithCommitToBranch(branch))
1230 if err != nil {
1231 return err
1232 }
1233 networks, err := s.getNetworks(user)
giob4a3a192024-08-19 09:55:47 +04001234 if err != nil {
gio11617ac2024-07-15 16:09:04 +04001235 return err
gio33059762024-07-05 13:19:07 +04001236 }
giof15b9da2024-09-19 06:59:16 +04001237 // TODO(gio): get only available ones by owner
1238 clusters, err := s.getClusters()
1239 if err != nil {
1240 return err
1241 }
gio33059762024-07-05 13:19:07 +04001242 apps := installer.NewInMemoryAppRepository(installer.CreateAllApps())
gio94904702024-07-26 16:58:34 +04001243 instanceApp, err := installer.FindEnvApp(apps, "dodo-app-instance")
1244 if err != nil {
1245 return err
1246 }
1247 instanceAppStatus, err := installer.FindEnvApp(apps, "dodo-app-instance-status")
gio33059762024-07-05 13:19:07 +04001248 if err != nil {
gio11617ac2024-07-15 16:09:04 +04001249 return err
gio33059762024-07-05 13:19:07 +04001250 }
1251 suffixGen := installer.NewFixedLengthRandomSuffixGenerator(3)
1252 suffix, err := suffixGen.Generate()
1253 if err != nil {
gio11617ac2024-07-15 16:09:04 +04001254 return err
gio33059762024-07-05 13:19:07 +04001255 }
gio94904702024-07-26 16:58:34 +04001256 namespace := fmt.Sprintf("%s%s%s", s.env.NamespacePrefix, instanceApp.Namespace(), suffix)
gio7fbd4ad2024-08-27 10:06:39 +04001257 s.setAppConfig(appName, branch, appConfig{namespace, network})
giof15b9da2024-09-19 06:59:16 +04001258 resources, err := s.updateDodoApp(instanceAppStatus, appName, branch, namespace, networks, clusters, user)
giob4a3a192024-08-19 09:55:47 +04001259 if err != nil {
gio7fbd4ad2024-08-27 10:06:39 +04001260 fmt.Printf("Error: %s\n", err.Error())
giob4a3a192024-08-19 09:55:47 +04001261 return err
1262 }
gio7fbd4ad2024-08-27 10:06:39 +04001263 if err = s.createCommit(appName, branch, commit, initCommitMsg, err, resources); err != nil {
giob4a3a192024-08-19 09:55:47 +04001264 fmt.Printf("Error: %s\n", err.Error())
gio11617ac2024-07-15 16:09:04 +04001265 return err
gio33059762024-07-05 13:19:07 +04001266 }
giod8ab4f52024-07-26 16:58:34 +04001267 configRepo, err := s.client.GetRepo(ConfigRepoName)
gio33059762024-07-05 13:19:07 +04001268 if err != nil {
gio11617ac2024-07-15 16:09:04 +04001269 return err
gio33059762024-07-05 13:19:07 +04001270 }
1271 hf := installer.NewGitHelmFetcher()
giof6ad2982024-08-23 17:42:49 +04001272 m, err := installer.NewAppManager(configRepo, s.nsc, s.jc, hf, s.vpnKeyGen, s.cnc, "/")
gio33059762024-07-05 13:19:07 +04001273 if err != nil {
gio11617ac2024-07-15 16:09:04 +04001274 return err
gio33059762024-07-05 13:19:07 +04001275 }
gio7fbd4ad2024-08-27 10:06:39 +04001276 appPath := fmt.Sprintf("/%s/%s", appName, branch)
giob4a3a192024-08-19 09:55:47 +04001277 _, err = configRepo.Do(func(fs soft.RepoFS) (string, error) {
giod8ab4f52024-07-26 16:58:34 +04001278 w, err := fs.Writer(appConfigsFile)
gio9d66f322024-07-06 13:45:10 +04001279 if err != nil {
1280 return "", err
1281 }
1282 defer w.Close()
giod8ab4f52024-07-26 16:58:34 +04001283 if err := json.NewEncoder(w).Encode(s.appConfigs); err != nil {
gio9d66f322024-07-06 13:45:10 +04001284 return "", err
1285 }
1286 if _, err := m.Install(
gio94904702024-07-26 16:58:34 +04001287 instanceApp,
gio9d66f322024-07-06 13:45:10 +04001288 appName,
gio7fbd4ad2024-08-27 10:06:39 +04001289 appPath,
gio9d66f322024-07-06 13:45:10 +04001290 namespace,
1291 map[string]any{
1292 "repoAddr": s.client.GetRepoAddress(appName),
1293 "repoHost": strings.Split(s.client.Address(), ":")[0],
gio7fbd4ad2024-08-27 10:06:39 +04001294 "branch": fmt.Sprintf("dodo_%s", branch),
gio9d66f322024-07-06 13:45:10 +04001295 "gitRepoPublicKey": s.gitRepoPublicKey,
1296 },
1297 installer.WithConfig(&s.env),
gio23bdc1b2024-07-11 16:07:47 +04001298 installer.WithNoNetworks(),
gio9d66f322024-07-06 13:45:10 +04001299 installer.WithNoPublish(),
1300 installer.WithNoLock(),
1301 ); err != nil {
1302 return "", err
1303 }
1304 return fmt.Sprintf("Installed app: %s", appName), nil
giob4a3a192024-08-19 09:55:47 +04001305 })
1306 if err != nil {
gio11617ac2024-07-15 16:09:04 +04001307 return err
gio33059762024-07-05 13:19:07 +04001308 }
gio7fbd4ad2024-08-27 10:06:39 +04001309 return s.initAppACLs(m, appPath, appName, branch, user)
1310}
1311
gio59946282024-10-07 12:55:51 +04001312func (s *Server) initAppACLs(m *installer.AppManager, path, appName, branch, user string) error {
gio7fbd4ad2024-08-27 10:06:39 +04001313 cfg, err := m.GetInstance(path)
gio33059762024-07-05 13:19:07 +04001314 if err != nil {
gio11617ac2024-07-15 16:09:04 +04001315 return err
gio33059762024-07-05 13:19:07 +04001316 }
1317 fluxKeys, ok := cfg.Input["fluxKeys"]
1318 if !ok {
gio11617ac2024-07-15 16:09:04 +04001319 return fmt.Errorf("Fluxcd keys not found")
gio33059762024-07-05 13:19:07 +04001320 }
1321 fluxPublicKey, ok := fluxKeys.(map[string]any)["public"]
1322 if !ok {
gio11617ac2024-07-15 16:09:04 +04001323 return fmt.Errorf("Fluxcd keys not found")
gio33059762024-07-05 13:19:07 +04001324 }
1325 if ok, err := s.client.UserExists("fluxcd"); err != nil {
gio11617ac2024-07-15 16:09:04 +04001326 return err
gio33059762024-07-05 13:19:07 +04001327 } else if ok {
1328 if err := s.client.AddPublicKey("fluxcd", fluxPublicKey.(string)); err != nil {
gio11617ac2024-07-15 16:09:04 +04001329 return err
gio33059762024-07-05 13:19:07 +04001330 }
1331 } else {
1332 if err := s.client.AddUser("fluxcd", fluxPublicKey.(string)); err != nil {
gio11617ac2024-07-15 16:09:04 +04001333 return err
gio33059762024-07-05 13:19:07 +04001334 }
1335 }
gio7fbd4ad2024-08-27 10:06:39 +04001336 if branch != "master" {
1337 return nil
1338 }
gio33059762024-07-05 13:19:07 +04001339 if err := s.client.AddReadOnlyCollaborator(appName, "fluxcd"); err != nil {
gio11617ac2024-07-15 16:09:04 +04001340 return err
gio33059762024-07-05 13:19:07 +04001341 }
gio7fbd4ad2024-08-27 10:06:39 +04001342 if err := s.client.AddReadWriteCollaborator(appName, user); err != nil {
gio11617ac2024-07-15 16:09:04 +04001343 return err
gio33059762024-07-05 13:19:07 +04001344 }
gio7fbd4ad2024-08-27 10:06:39 +04001345 if err := s.client.AddWebhook(appName, fmt.Sprintf("http://%s/update", s.self), "--active=true", "--events=push", "--content-type=json"); err != nil {
gio11617ac2024-07-15 16:09:04 +04001346 return err
gio33059762024-07-05 13:19:07 +04001347 }
gio2ccb6e32024-08-15 12:01:33 +04001348 if !s.external {
1349 go func() {
1350 users, err := s.client.GetAllUsers()
1351 if err != nil {
1352 fmt.Println(err)
1353 return
1354 }
1355 for _, user := range users {
1356 // TODO(gio): fluxcd should have only read access
1357 if err := s.client.AddReadWriteCollaborator(appName, user); err != nil {
1358 fmt.Println(err)
1359 }
1360 }
1361 }()
1362 }
gio43b0f422024-08-21 10:40:13 +04001363 ctx, _ := context.WithTimeout(context.Background(), 2*time.Minute)
1364 go s.reconciler.Reconcile(ctx, s.namespace, "config")
gio11617ac2024-07-15 16:09:04 +04001365 return nil
gio33059762024-07-05 13:19:07 +04001366}
1367
gio81246f02024-07-10 12:02:15 +04001368type apiAddAdminKeyReq struct {
gio7fbd4ad2024-08-27 10:06:39 +04001369 User string `json:"user"`
1370 PublicKey string `json:"publicKey"`
gio70be3e52024-06-26 18:27:19 +04001371}
1372
gio59946282024-10-07 12:55:51 +04001373func (s *Server) handleAPIAddPublicKey(w http.ResponseWriter, r *http.Request) {
gio81246f02024-07-10 12:02:15 +04001374 var req apiAddAdminKeyReq
gio70be3e52024-06-26 18:27:19 +04001375 if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
1376 http.Error(w, err.Error(), http.StatusBadRequest)
1377 return
1378 }
gio7fbd4ad2024-08-27 10:06:39 +04001379 if req.User == "" {
1380 http.Error(w, "invalid user", http.StatusBadRequest)
1381 return
1382 }
1383 if req.PublicKey == "" {
1384 http.Error(w, "invalid public key", http.StatusBadRequest)
1385 return
1386 }
1387 if err := s.client.AddPublicKey(req.User, req.PublicKey); err != nil {
gio70be3e52024-06-26 18:27:19 +04001388 http.Error(w, err.Error(), http.StatusInternalServerError)
1389 return
1390 }
1391}
1392
gio94904702024-07-26 16:58:34 +04001393type dodoAppRendered struct {
1394 App struct {
1395 Ingress struct {
1396 Network string `json:"network"`
1397 Subdomain string `json:"subdomain"`
1398 } `json:"ingress"`
1399 } `json:"app"`
1400 Input struct {
1401 AppId string `json:"appId"`
1402 } `json:"input"`
1403}
1404
gio7fbd4ad2024-08-27 10:06:39 +04001405// TODO(gio): must not require owner, now we need it to bootstrap dev vm.
gio59946282024-10-07 12:55:51 +04001406func (s *Server) updateDodoApp(
gio43b0f422024-08-21 10:40:13 +04001407 appStatus installer.EnvApp,
gio7fbd4ad2024-08-27 10:06:39 +04001408 name string,
1409 branch string,
1410 namespace string,
gio43b0f422024-08-21 10:40:13 +04001411 networks []installer.Network,
giof15b9da2024-09-19 06:59:16 +04001412 clusters []installer.Cluster,
gio7fbd4ad2024-08-27 10:06:39 +04001413 owner string,
gio43b0f422024-08-21 10:40:13 +04001414) (installer.ReleaseResources, error) {
gio7fbd4ad2024-08-27 10:06:39 +04001415 repo, err := s.client.GetRepoBranch(name, branch)
gio0eaf2712024-04-14 13:08:46 +04001416 if err != nil {
giob4a3a192024-08-19 09:55:47 +04001417 return installer.ReleaseResources{}, err
gio0eaf2712024-04-14 13:08:46 +04001418 }
giof8843412024-05-22 16:38:05 +04001419 hf := installer.NewGitHelmFetcher()
giof6ad2982024-08-23 17:42:49 +04001420 m, err := installer.NewAppManager(repo, s.nsc, s.jc, hf, s.vpnKeyGen, s.cnc, "/.dodo")
gio0eaf2712024-04-14 13:08:46 +04001421 if err != nil {
giob4a3a192024-08-19 09:55:47 +04001422 return installer.ReleaseResources{}, err
gio0eaf2712024-04-14 13:08:46 +04001423 }
gioc81a8472024-09-24 13:06:19 +02001424 appCfg, err := soft.ReadFile(repo, "app.json")
gio0eaf2712024-04-14 13:08:46 +04001425 if err != nil {
giob4a3a192024-08-19 09:55:47 +04001426 return installer.ReleaseResources{}, err
gio0eaf2712024-04-14 13:08:46 +04001427 }
1428 app, err := installer.NewDodoApp(appCfg)
1429 if err != nil {
giob4a3a192024-08-19 09:55:47 +04001430 return installer.ReleaseResources{}, err
gio0eaf2712024-04-14 13:08:46 +04001431 }
giof8843412024-05-22 16:38:05 +04001432 lg := installer.GitRepositoryLocalChartGenerator{"app", namespace}
giob4a3a192024-08-19 09:55:47 +04001433 var ret installer.ReleaseResources
1434 if _, err := repo.Do(func(r soft.RepoFS) (string, error) {
1435 ret, err = m.Install(
gio94904702024-07-26 16:58:34 +04001436 app,
1437 "app",
1438 "/.dodo/app",
1439 namespace,
1440 map[string]any{
gio7fbd4ad2024-08-27 10:06:39 +04001441 "repoAddr": repo.FullAddress(),
1442 "repoPublicAddr": s.repoPublicAddr,
1443 "managerAddr": fmt.Sprintf("http://%s", s.self),
1444 "appId": name,
1445 "branch": branch,
1446 "sshPrivateKey": s.sshKey,
1447 "username": owner,
gio94904702024-07-26 16:58:34 +04001448 },
1449 installer.WithNoPull(),
1450 installer.WithNoPublish(),
1451 installer.WithConfig(&s.env),
1452 installer.WithNetworks(networks),
giof15b9da2024-09-19 06:59:16 +04001453 installer.WithClusters(clusters),
gio94904702024-07-26 16:58:34 +04001454 installer.WithLocalChartGenerator(lg),
1455 installer.WithNoLock(),
1456 )
1457 if err != nil {
1458 return "", err
1459 }
1460 var rendered dodoAppRendered
giob4a3a192024-08-19 09:55:47 +04001461 if err := json.NewDecoder(bytes.NewReader(ret.RenderedRaw)).Decode(&rendered); err != nil {
gio94904702024-07-26 16:58:34 +04001462 return "", nil
1463 }
1464 if _, err := m.Install(
1465 appStatus,
1466 "status",
1467 "/.dodo/status",
1468 s.namespace,
1469 map[string]any{
1470 "appName": rendered.Input.AppId,
1471 "network": rendered.App.Ingress.Network,
1472 "appSubdomain": rendered.App.Ingress.Subdomain,
1473 },
1474 installer.WithNoPull(),
1475 installer.WithNoPublish(),
1476 installer.WithConfig(&s.env),
1477 installer.WithNetworks(networks),
giof15b9da2024-09-19 06:59:16 +04001478 installer.WithClusters(clusters),
gio94904702024-07-26 16:58:34 +04001479 installer.WithLocalChartGenerator(lg),
1480 installer.WithNoLock(),
1481 ); err != nil {
1482 return "", err
1483 }
1484 return "install app", nil
1485 },
gio7fbd4ad2024-08-27 10:06:39 +04001486 soft.WithCommitToBranch(fmt.Sprintf("dodo_%s", branch)),
gio94904702024-07-26 16:58:34 +04001487 soft.WithForce(),
giob4a3a192024-08-19 09:55:47 +04001488 ); err != nil {
1489 return installer.ReleaseResources{}, err
1490 }
gio43b0f422024-08-21 10:40:13 +04001491 ctx, _ := context.WithTimeout(context.Background(), 2*time.Minute)
1492 go s.reconciler.Reconcile(ctx, namespace, "app")
giob4a3a192024-08-19 09:55:47 +04001493 return ret, nil
gio0eaf2712024-04-14 13:08:46 +04001494}
gio33059762024-07-05 13:19:07 +04001495
gio9f6b27d2024-10-14 10:08:40 +04001496func (s *Server) renderAppConfigTemplate(user, appType string, network installer.Network, subdomain string) (map[string][]byte, error) {
giob54db242024-07-30 18:49:33 +04001497 appType = strings.Replace(appType, ":", "-", 1)
gio5e49bb62024-07-20 10:43:19 +04001498 appTmpl, err := s.appTmpls.Find(appType)
1499 if err != nil {
gio7fbd4ad2024-08-27 10:06:39 +04001500 return nil, err
gio33059762024-07-05 13:19:07 +04001501 }
gio9f6b27d2024-10-14 10:08:40 +04001502 return appTmpl.Render(fmt.Sprintf("%s/schemas/%s/app.schema.json", s.selfPublic, user), network, subdomain)
gio33059762024-07-05 13:19:07 +04001503}
gio81246f02024-07-10 12:02:15 +04001504
1505func generatePassword() string {
1506 return "foo"
1507}
giocb34ad22024-07-11 08:01:13 +04001508
gio59946282024-10-07 12:55:51 +04001509func (s *Server) getNetworks(user string) ([]installer.Network, error) {
gio23bdc1b2024-07-11 16:07:47 +04001510 addr := fmt.Sprintf("%s/api/networks", s.envAppManagerAddr)
giocb34ad22024-07-11 08:01:13 +04001511 resp, err := http.Get(addr)
1512 if err != nil {
1513 return nil, err
1514 }
gio23bdc1b2024-07-11 16:07:47 +04001515 networks := []installer.Network{}
1516 if json.NewDecoder(resp.Body).Decode(&networks); err != nil {
giocb34ad22024-07-11 08:01:13 +04001517 return nil, err
1518 }
gio11617ac2024-07-15 16:09:04 +04001519 return s.nf.Filter(user, networks)
1520}
1521
gio59946282024-10-07 12:55:51 +04001522func (s *Server) getClusters() ([]installer.Cluster, error) {
giof15b9da2024-09-19 06:59:16 +04001523 addr := fmt.Sprintf("%s/api/clusters", s.envAppManagerAddr)
1524 resp, err := http.Get(addr)
1525 if err != nil {
1526 return nil, err
1527 }
1528 clusters := []installer.Cluster{}
1529 if json.NewDecoder(resp.Body).Decode(&clusters); err != nil {
1530 return nil, err
1531 }
1532 fmt.Printf("CLUSTERS %+v\n", clusters)
1533 return clusters, nil
1534}
1535
gio8fae3af2024-07-25 13:43:31 +04001536type publicNetworkData struct {
1537 Name string `json:"name"`
1538 Domain string `json:"domain"`
1539}
1540
1541type publicData struct {
1542 Networks []publicNetworkData `json:"networks"`
1543 Types []string `json:"types"`
1544}
1545
gio59946282024-10-07 12:55:51 +04001546func (s *Server) handleAPIPublicData(w http.ResponseWriter, r *http.Request) {
giod8ab4f52024-07-26 16:58:34 +04001547 w.Header().Set("Access-Control-Allow-Origin", "*")
1548 s.l.Lock()
1549 defer s.l.Unlock()
gio8fae3af2024-07-25 13:43:31 +04001550 networks, err := s.getNetworks("")
1551 if err != nil {
1552 http.Error(w, err.Error(), http.StatusInternalServerError)
1553 return
1554 }
1555 var ret publicData
1556 for _, n := range networks {
giod8ab4f52024-07-26 16:58:34 +04001557 if s.isNetworkUseAllowed(strings.ToLower(n.Name)) {
1558 ret.Networks = append(ret.Networks, publicNetworkData{n.Name, n.Domain})
1559 }
gio8fae3af2024-07-25 13:43:31 +04001560 }
1561 for _, t := range s.appTmpls.Types() {
giob54db242024-07-30 18:49:33 +04001562 ret.Types = append(ret.Types, strings.Replace(t, "-", ":", 1))
gio8fae3af2024-07-25 13:43:31 +04001563 }
gio8fae3af2024-07-25 13:43:31 +04001564 if err := json.NewEncoder(w).Encode(ret); err != nil {
1565 http.Error(w, err.Error(), http.StatusInternalServerError)
1566 return
1567 }
1568}
1569
gio59946282024-10-07 12:55:51 +04001570func (s *Server) createCommit(name, branch, hash, message string, err error, resources installer.ReleaseResources) error {
giob4a3a192024-08-19 09:55:47 +04001571 if err != nil {
1572 fmt.Printf("Error: %s\n", err.Error())
gio7fbd4ad2024-08-27 10:06:39 +04001573 if err := s.st.CreateCommit(name, branch, hash, message, "FAILED", err.Error(), nil); err != nil {
giob4a3a192024-08-19 09:55:47 +04001574 fmt.Printf("Error: %s\n", err.Error())
1575 return err
1576 }
1577 return err
1578 }
1579 var resB bytes.Buffer
1580 if err := json.NewEncoder(&resB).Encode(resources); err != nil {
gio7fbd4ad2024-08-27 10:06:39 +04001581 if err := s.st.CreateCommit(name, branch, hash, message, "FAILED", err.Error(), nil); err != nil {
giob4a3a192024-08-19 09:55:47 +04001582 fmt.Printf("Error: %s\n", err.Error())
1583 return err
1584 }
1585 return err
1586 }
gio7fbd4ad2024-08-27 10:06:39 +04001587 if err := s.st.CreateCommit(name, branch, hash, message, "OK", "", resB.Bytes()); err != nil {
giob4a3a192024-08-19 09:55:47 +04001588 fmt.Printf("Error: %s\n", err.Error())
1589 return err
1590 }
1591 return nil
1592}
1593
gio11617ac2024-07-15 16:09:04 +04001594func pickNetwork(networks []installer.Network, network string) []installer.Network {
1595 for _, n := range networks {
1596 if n.Name == network {
1597 return []installer.Network{n}
1598 }
1599 }
1600 return []installer.Network{}
1601}
1602
1603type NetworkFilter interface {
1604 Filter(user string, networks []installer.Network) ([]installer.Network, error)
1605}
1606
1607type noNetworkFilter struct{}
1608
1609func NewNoNetworkFilter() NetworkFilter {
1610 return noNetworkFilter{}
1611}
1612
gio8fae3af2024-07-25 13:43:31 +04001613func (f noNetworkFilter) Filter(user string, networks []installer.Network) ([]installer.Network, error) {
gio11617ac2024-07-15 16:09:04 +04001614 return networks, nil
1615}
1616
1617type filterByOwner struct {
1618 st Store
1619}
1620
1621func NewNetworkFilterByOwner(st Store) NetworkFilter {
1622 return &filterByOwner{st}
1623}
1624
1625func (f *filterByOwner) Filter(user string, networks []installer.Network) ([]installer.Network, error) {
gio8fae3af2024-07-25 13:43:31 +04001626 if user == "" {
1627 return networks, nil
1628 }
gio11617ac2024-07-15 16:09:04 +04001629 network, err := f.st.GetUserNetwork(user)
1630 if err != nil {
1631 return nil, err
gio23bdc1b2024-07-11 16:07:47 +04001632 }
1633 ret := []installer.Network{}
1634 for _, n := range networks {
gio11617ac2024-07-15 16:09:04 +04001635 if n.Name == network {
gio23bdc1b2024-07-11 16:07:47 +04001636 ret = append(ret, n)
1637 }
1638 }
giocb34ad22024-07-11 08:01:13 +04001639 return ret, nil
1640}
gio11617ac2024-07-15 16:09:04 +04001641
1642type allowListFilter struct {
1643 allowed []string
1644}
1645
1646func NewAllowListFilter(allowed []string) NetworkFilter {
1647 return &allowListFilter{allowed}
1648}
1649
gio8fae3af2024-07-25 13:43:31 +04001650func (f *allowListFilter) Filter(user string, networks []installer.Network) ([]installer.Network, error) {
gio11617ac2024-07-15 16:09:04 +04001651 ret := []installer.Network{}
1652 for _, n := range networks {
1653 if slices.Contains(f.allowed, n.Name) {
1654 ret = append(ret, n)
1655 }
1656 }
1657 return ret, nil
1658}
1659
1660type combinedNetworkFilter struct {
1661 filters []NetworkFilter
1662}
1663
1664func NewCombinedFilter(filters ...NetworkFilter) NetworkFilter {
1665 return &combinedNetworkFilter{filters}
1666}
1667
gio8fae3af2024-07-25 13:43:31 +04001668func (f *combinedNetworkFilter) Filter(user string, networks []installer.Network) ([]installer.Network, error) {
gio11617ac2024-07-15 16:09:04 +04001669 ret := networks
1670 var err error
1671 for _, f := range f.filters {
gio8fae3af2024-07-25 13:43:31 +04001672 ret, err = f.Filter(user, ret)
gio11617ac2024-07-15 16:09:04 +04001673 if err != nil {
1674 return nil, err
1675 }
1676 }
1677 return ret, nil
1678}
giocafd4e62024-07-31 10:53:40 +04001679
1680type user struct {
1681 Username string `json:"username"`
1682 Email string `json:"email"`
1683 SSHPublicKeys []string `json:"sshPublicKeys,omitempty"`
1684}
1685
gio59946282024-10-07 12:55:51 +04001686func (s *Server) handleAPISyncUsers(_ http.ResponseWriter, _ *http.Request) {
giocafd4e62024-07-31 10:53:40 +04001687 go s.syncUsers()
1688}
1689
gio59946282024-10-07 12:55:51 +04001690func (s *Server) syncUsers() {
giocafd4e62024-07-31 10:53:40 +04001691 if s.external {
1692 panic("MUST NOT REACH!")
1693 }
1694 resp, err := http.Get(fmt.Sprintf("%s?selfAddress=%s/api/sync-users", s.fetchUsersAddr, s.self))
1695 if err != nil {
1696 return
1697 }
1698 users := []user{}
1699 if err := json.NewDecoder(resp.Body).Decode(&users); err != nil {
1700 fmt.Println(err)
1701 return
1702 }
Davit Tabidzea5ea5092024-08-01 15:28:09 +04001703 validUsernames := make(map[string]user)
1704 for _, u := range users {
1705 validUsernames[u.Username] = u
1706 }
1707 allClientUsers, err := s.client.GetAllUsers()
1708 if err != nil {
1709 fmt.Println(err)
1710 return
1711 }
1712 keyToUser := make(map[string]string)
1713 for _, clientUser := range allClientUsers {
Davit Tabidze4aaa27b2024-08-05 20:23:50 +04001714 if clientUser == "admin" || clientUser == "fluxcd" {
1715 continue
1716 }
Davit Tabidzea5ea5092024-08-01 15:28:09 +04001717 userData, ok := validUsernames[clientUser]
1718 if !ok {
1719 if err := s.client.RemoveUser(clientUser); err != nil {
1720 fmt.Println(err)
1721 return
1722 }
1723 } else {
1724 existingKeys, err := s.client.GetUserPublicKeys(clientUser)
1725 if err != nil {
1726 fmt.Println(err)
1727 return
1728 }
1729 for _, existingKey := range existingKeys {
Davit Tabidze4aaa27b2024-08-05 20:23:50 +04001730 cleanKey := soft.CleanKey(existingKey)
Davit Tabidzea5ea5092024-08-01 15:28:09 +04001731 keyOk := slices.ContainsFunc(userData.SSHPublicKeys, func(key string) bool {
Davit Tabidze4aaa27b2024-08-05 20:23:50 +04001732 return cleanKey == soft.CleanKey(key)
Davit Tabidzea5ea5092024-08-01 15:28:09 +04001733 })
1734 if !keyOk {
1735 if err := s.client.RemovePublicKey(clientUser, existingKey); err != nil {
1736 fmt.Println(err)
1737 }
1738 } else {
1739 keyToUser[cleanKey] = clientUser
1740 }
1741 }
1742 }
1743 }
giocafd4e62024-07-31 10:53:40 +04001744 for _, u := range users {
Davit Tabidze4aaa27b2024-08-05 20:23:50 +04001745 if err := s.st.CreateUser(u.Username, nil, ""); err != nil && !errors.Is(err, ErrorAlreadyExists) {
1746 fmt.Println(err)
1747 return
1748 }
giocafd4e62024-07-31 10:53:40 +04001749 if len(u.SSHPublicKeys) == 0 {
1750 continue
1751 }
Davit Tabidzea5ea5092024-08-01 15:28:09 +04001752 ok, err := s.client.UserExists(u.Username)
1753 if err != nil {
giocafd4e62024-07-31 10:53:40 +04001754 fmt.Println(err)
1755 return
Davit Tabidzea5ea5092024-08-01 15:28:09 +04001756 }
1757 if !ok {
1758 if err := s.client.AddUser(u.Username, u.SSHPublicKeys[0]); err != nil {
1759 fmt.Println(err)
1760 return
1761 }
1762 } else {
1763 for _, key := range u.SSHPublicKeys {
Davit Tabidze4aaa27b2024-08-05 20:23:50 +04001764 cleanKey := soft.CleanKey(key)
1765 if user, ok := keyToUser[cleanKey]; ok {
1766 if u.Username != user {
1767 panic("MUST NOT REACH! IMPOSSIBLE KEY USER RECORD")
1768 }
1769 continue
Davit Tabidzea5ea5092024-08-01 15:28:09 +04001770 }
Davit Tabidze4aaa27b2024-08-05 20:23:50 +04001771 if err := s.client.AddPublicKey(u.Username, cleanKey); err != nil {
Davit Tabidzea5ea5092024-08-01 15:28:09 +04001772 fmt.Println(err)
1773 return
giocafd4e62024-07-31 10:53:40 +04001774 }
1775 }
1776 }
1777 }
1778 repos, err := s.client.GetAllRepos()
1779 if err != nil {
1780 return
1781 }
1782 for _, r := range repos {
1783 if r == ConfigRepoName {
1784 continue
1785 }
1786 for _, u := range users {
1787 if err := s.client.AddReadWriteCollaborator(r, u.Username); err != nil {
1788 fmt.Println(err)
Davit Tabidze4aaa27b2024-08-05 20:23:50 +04001789 continue
giocafd4e62024-07-31 10:53:40 +04001790 }
1791 }
1792 }
1793}
giob4a3a192024-08-19 09:55:47 +04001794
1795func extractResourceData(resources []installer.Resource) (resourceData, error) {
1796 var ret resourceData
1797 for _, r := range resources {
1798 t, ok := r.Annotations["dodo.cloud/resource-type"]
1799 if !ok {
1800 continue
1801 }
giof078f462024-10-14 09:07:33 +04001802 internal, ok := r.Annotations["dodo.cloud/internal"]
1803 if ok && strings.ToLower(internal) == "true" {
1804 continue
1805 }
giob4a3a192024-08-19 09:55:47 +04001806 switch t {
1807 case "volume":
1808 name, ok := r.Annotations["dodo.cloud/resource.volume.name"]
1809 if !ok {
1810 return resourceData{}, fmt.Errorf("no name")
1811 }
1812 size, ok := r.Annotations["dodo.cloud/resource.volume.size"]
1813 if !ok {
1814 return resourceData{}, fmt.Errorf("no size")
1815 }
1816 ret.Volume = append(ret.Volume, volume{name, size})
1817 case "postgresql":
1818 name, ok := r.Annotations["dodo.cloud/resource.postgresql.name"]
1819 if !ok {
1820 return resourceData{}, fmt.Errorf("no name")
1821 }
1822 version, ok := r.Annotations["dodo.cloud/resource.postgresql.version"]
1823 if !ok {
1824 return resourceData{}, fmt.Errorf("no version")
1825 }
1826 volume, ok := r.Annotations["dodo.cloud/resource.postgresql.volume"]
1827 if !ok {
1828 return resourceData{}, fmt.Errorf("no volume")
1829 }
1830 ret.PostgreSQL = append(ret.PostgreSQL, postgresql{name, version, volume})
gio07eb1082024-10-25 14:35:56 +04001831 case "mongodb":
1832 name, ok := r.Annotations["dodo.cloud/resource.mongodb.name"]
1833 if !ok {
1834 return resourceData{}, fmt.Errorf("no name")
1835 }
1836 version, ok := r.Annotations["dodo.cloud/resource.mongodb.version"]
1837 if !ok {
1838 return resourceData{}, fmt.Errorf("no version")
1839 }
1840 volume, ok := r.Annotations["dodo.cloud/resource.mongodb.volume"]
1841 if !ok {
1842 return resourceData{}, fmt.Errorf("no volume")
1843 }
1844 ret.MongoDB = append(ret.MongoDB, mongodb{name, version, volume})
giob4a3a192024-08-19 09:55:47 +04001845 case "ingress":
giof078f462024-10-14 09:07:33 +04001846 name, ok := r.Annotations["dodo.cloud/resource.ingress.name"]
1847 if !ok {
1848 return resourceData{}, fmt.Errorf("no name")
1849 }
1850 home, ok := r.Annotations["dodo.cloud/resource.ingress.home"]
1851 if !ok {
1852 home = ""
1853 }
giob4a3a192024-08-19 09:55:47 +04001854 host, ok := r.Annotations["dodo.cloud/resource.ingress.host"]
1855 if !ok {
1856 return resourceData{}, fmt.Errorf("no host")
1857 }
giof078f462024-10-14 09:07:33 +04001858 ret.Ingress = append(ret.Ingress, ingress{name, host, home})
gio7fbd4ad2024-08-27 10:06:39 +04001859 case "virtual-machine":
1860 name, ok := r.Annotations["dodo.cloud/resource.virtual-machine.name"]
1861 if !ok {
1862 return resourceData{}, fmt.Errorf("no name")
1863 }
1864 user, ok := r.Annotations["dodo.cloud/resource.virtual-machine.user"]
1865 if !ok {
1866 return resourceData{}, fmt.Errorf("no user")
1867 }
1868 cpuCoresS, ok := r.Annotations["dodo.cloud/resource.virtual-machine.cpu-cores"]
1869 if !ok {
1870 return resourceData{}, fmt.Errorf("no cpu cores")
1871 }
1872 cpuCores, err := strconv.Atoi(cpuCoresS)
1873 if err != nil {
1874 return resourceData{}, fmt.Errorf("invalid cpu cores: %s", cpuCoresS)
1875 }
1876 memory, ok := r.Annotations["dodo.cloud/resource.virtual-machine.memory"]
1877 if !ok {
1878 return resourceData{}, fmt.Errorf("no memory")
1879 }
1880 ret.VirtualMachine = append(ret.VirtualMachine, vm{name, user, cpuCores, memory})
giob4a3a192024-08-19 09:55:47 +04001881 default:
1882 fmt.Printf("Unknown resource: %+v\n", r.Annotations)
1883 }
1884 }
giof078f462024-10-14 09:07:33 +04001885 sort.Slice(ret.Ingress, func(i, j int) bool {
1886 return strings.Compare(ret.Ingress[i].Name, ret.Ingress[j].Name) < 0
1887 })
giob4a3a192024-08-19 09:55:47 +04001888 return ret, nil
1889}
gio7fbd4ad2024-08-27 10:06:39 +04001890
gio9f6b27d2024-10-14 10:08:40 +04001891func createDevBranchAppConfig(from []byte, branch, username, publicAddr string) (string, []byte, error) {
gioc81a8472024-09-24 13:06:19 +02001892 cfg, err := installer.ParseCueAppConfig(installer.CueAppData{
1893 "app.cue": from,
1894 })
gio7fbd4ad2024-08-27 10:06:39 +04001895 if err != nil {
1896 return "", nil, err
1897 }
1898 if err := cfg.Err(); err != nil {
1899 return "", nil, err
1900 }
1901 if err := cfg.Validate(); err != nil {
1902 return "", nil, err
1903 }
1904 subdomain := cfg.LookupPath(cue.ParsePath("app.ingress.subdomain"))
1905 if err := subdomain.Err(); err != nil {
1906 return "", nil, err
1907 }
1908 subdomainStr, err := subdomain.String()
1909 network := cfg.LookupPath(cue.ParsePath("app.ingress.network"))
1910 if err := network.Err(); err != nil {
1911 return "", nil, err
1912 }
1913 networkStr, err := network.String()
1914 if err != nil {
1915 return "", nil, err
1916 }
1917 newCfg := map[string]any{}
1918 if err := cfg.Decode(&newCfg); err != nil {
1919 return "", nil, err
1920 }
1921 app, ok := newCfg["app"].(map[string]any)
1922 if !ok {
1923 return "", nil, fmt.Errorf("not a map")
1924 }
1925 app["ingress"].(map[string]any)["subdomain"] = fmt.Sprintf("%s-%s", branch, subdomainStr)
1926 app["dev"] = map[string]any{
1927 "enabled": true,
1928 "username": username,
1929 }
gio9f6b27d2024-10-14 10:08:40 +04001930 newCfg["$schema"] = fmt.Sprintf("%s/schemas/%s/app.schema.json", publicAddr, username)
gio7fbd4ad2024-08-27 10:06:39 +04001931 buf, err := json.MarshalIndent(newCfg, "", "\t")
1932 if err != nil {
1933 return "", nil, err
1934 }
1935 return networkStr, buf, nil
1936}