MCPcopy Create free account
hub / github.com/cloudbase/garm / migrateCredentialsToDB

Method migrateCredentialsToDB

database/sql/sql.go:315–451  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

313}
314
315func (s *sqlDatabase) migrateCredentialsToDB() (err error) {
316 s.conn.Exec("PRAGMA foreign_keys = OFF")
317 defer s.conn.Exec("PRAGMA foreign_keys = ON")
318
319 adminUser, err := s.GetAdminUser(s.ctx)
320 if err != nil {
321 if errors.Is(err, runnerErrors.ErrNotFound) {
322 // Admin user doesn't exist. This is a new deploy. Nothing to migrate.
323 return nil
324 }
325 return fmt.Errorf("error getting admin user: %w", err)
326 }
327
328 // Impersonate the admin user. We're migrating from config credentials to
329 // database credentials. At this point, there is no other user than the admin
330 // user. GARM is not yet multi-user, so it's safe to assume we only have this
331 // one user.
332 adminCtx := context.Background()
333 adminCtx = auth.PopulateContext(adminCtx, adminUser, nil)
334
335 slog.Info("migrating credentials to DB")
336 slog.Info("creating github endpoints table")
337 if err := s.conn.AutoMigrate(&GithubEndpoint{}); err != nil {
338 return fmt.Errorf("error migrating github endpoints: %w", err)
339 }
340
341 defer func() {
342 if err != nil {
343 slog.With(slog.Any("error", err)).Error("rolling back github github endpoints table")
344 s.conn.Migrator().DropTable(&GithubEndpoint{})
345 }
346 }()
347
348 slog.Info("creating github credentials table")
349 if err := s.conn.AutoMigrate(&GithubCredentials{}); err != nil {
350 return fmt.Errorf("error migrating github credentials: %w", err)
351 }
352
353 defer func() {
354 if err != nil {
355 slog.With(slog.Any("error", err)).Error("rolling back github github credentials table")
356 s.conn.Migrator().DropTable(&GithubCredentials{})
357 }
358 }()
359
360 // Nothing to migrate.
361 if len(s.cfg.MigrateCredentials) == 0 {
362 return nil
363 }
364
365 slog.Info("importing credentials from config")
366 for _, cred := range s.cfg.MigrateCredentials {
367 slog.Info("importing credential", "name", cred.Name)
368 parsed, err := url.Parse(cred.BaseEndpoint())
369 if err != nil {
370 return fmt.Errorf("error parsing base URL: %w", err)
371 }
372

Callers 1

migrateDBMethod · 0.95

Calls 14

GetAdminUserMethod · 0.95
GetGithubEndpointMethod · 0.95
CreateGithubEndpointMethod · 0.95
PopulateContextFunction · 0.92
ForgeAuthTypeTypeAlias · 0.92
BaseEndpointMethod · 0.80
CACertBundleMethod · 0.80
APIEndpointMethod · 0.80
UploadEndpointMethod · 0.80
GetAuthTypeMethod · 0.80
PrivateKeyBytesMethod · 0.80

Tested by

no test coverage detected