loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

move the `gitea admin` subcommands into separate files (#27307)

As title.
Probably it's better to put those sub cmd to different dirs. Will do
that in the future.

authored by

Nanguan Lin and committed by
GitHub
70f8d10f 5600504e

+670 -619
+7 -610
cmd/admin.go
··· 6 6 7 7 import ( 8 8 "context" 9 - "errors" 10 9 "fmt" 11 - "net/url" 12 - "os" 13 - "strings" 14 - "text/tabwriter" 15 10 16 - asymkey_model "code.gitea.io/gitea/models/asymkey" 17 - auth_model "code.gitea.io/gitea/models/auth" 18 11 "code.gitea.io/gitea/models/db" 19 12 repo_model "code.gitea.io/gitea/models/repo" 20 13 "code.gitea.io/gitea/modules/git" 21 - "code.gitea.io/gitea/modules/graceful" 22 14 "code.gitea.io/gitea/modules/log" 23 15 repo_module "code.gitea.io/gitea/modules/repository" 24 - "code.gitea.io/gitea/modules/util" 25 - auth_service "code.gitea.io/gitea/services/auth" 26 - "code.gitea.io/gitea/services/auth/source/oauth2" 27 - "code.gitea.io/gitea/services/auth/source/smtp" 28 - repo_service "code.gitea.io/gitea/services/repository" 29 16 30 17 "github.com/urfave/cli/v2" 31 18 ) ··· 59 46 }, 60 47 } 61 48 62 - microcmdRegenHooks = &cli.Command{ 63 - Name: "hooks", 64 - Usage: "Regenerate git-hooks", 65 - Action: runRegenerateHooks, 66 - } 67 - 68 - microcmdRegenKeys = &cli.Command{ 69 - Name: "keys", 70 - Usage: "Regenerate authorized_keys file", 71 - Action: runRegenerateKeys, 72 - } 73 - 74 49 subcmdAuth = &cli.Command{ 75 50 Name: "auth", 76 51 Usage: "Modify external auth providers", 77 52 Subcommands: []*cli.Command{ 78 53 microcmdAuthAddOauth, 79 54 microcmdAuthUpdateOauth, 80 - cmdAuthAddLdapBindDn, 81 - cmdAuthUpdateLdapBindDn, 82 - cmdAuthAddLdapSimpleAuth, 83 - cmdAuthUpdateLdapSimpleAuth, 55 + microcmdAuthAddLdapBindDn, 56 + microcmdAuthUpdateLdapBindDn, 57 + microcmdAuthAddLdapSimpleAuth, 58 + microcmdAuthUpdateLdapSimpleAuth, 84 59 microcmdAuthAddSMTP, 85 60 microcmdAuthUpdateSMTP, 86 61 microcmdAuthList, ··· 88 63 }, 89 64 } 90 65 91 - microcmdAuthList = &cli.Command{ 92 - Name: "list", 93 - Usage: "List auth sources", 94 - Action: runListAuth, 95 - Flags: []cli.Flag{ 96 - &cli.IntFlag{ 97 - Name: "min-width", 98 - Usage: "Minimal cell width including any padding for the formatted table", 99 - Value: 0, 100 - }, 101 - &cli.IntFlag{ 102 - Name: "tab-width", 103 - Usage: "width of tab characters in formatted table (equivalent number of spaces)", 104 - Value: 8, 105 - }, 106 - &cli.IntFlag{ 107 - Name: "padding", 108 - Usage: "padding added to a cell before computing its width", 109 - Value: 1, 110 - }, 111 - &cli.StringFlag{ 112 - Name: "pad-char", 113 - Usage: `ASCII char used for padding if padchar == '\\t', the Writer will assume that the width of a '\\t' in the formatted output is tabwidth, and cells are left-aligned independent of align_left (for correct-looking results, tabwidth must correspond to the tab width in the viewer displaying the result)`, 114 - Value: "\t", 115 - }, 116 - &cli.BoolFlag{ 117 - Name: "vertical-bars", 118 - Usage: "Set to true to print vertical bars between columns", 119 - }, 120 - }, 121 - } 122 - 123 - idFlag = &cli.Int64Flag{ 124 - Name: "id", 125 - Usage: "ID of authentication source", 126 - } 127 - 128 - microcmdAuthDelete = &cli.Command{ 129 - Name: "delete", 130 - Usage: "Delete specific auth source", 131 - Flags: []cli.Flag{idFlag}, 132 - Action: runDeleteAuth, 133 - } 134 - 135 - oauthCLIFlags = []cli.Flag{ 136 - &cli.StringFlag{ 137 - Name: "name", 138 - Value: "", 139 - Usage: "Application Name", 140 - }, 141 - &cli.StringFlag{ 142 - Name: "provider", 143 - Value: "", 144 - Usage: "OAuth2 Provider", 145 - }, 146 - &cli.StringFlag{ 147 - Name: "key", 148 - Value: "", 149 - Usage: "Client ID (Key)", 150 - }, 151 - &cli.StringFlag{ 152 - Name: "secret", 153 - Value: "", 154 - Usage: "Client Secret", 155 - }, 156 - &cli.StringFlag{ 157 - Name: "auto-discover-url", 158 - Value: "", 159 - Usage: "OpenID Connect Auto Discovery URL (only required when using OpenID Connect as provider)", 160 - }, 161 - &cli.StringFlag{ 162 - Name: "use-custom-urls", 163 - Value: "false", 164 - Usage: "Use custom URLs for GitLab/GitHub OAuth endpoints", 165 - }, 166 - &cli.StringFlag{ 167 - Name: "custom-tenant-id", 168 - Value: "", 169 - Usage: "Use custom Tenant ID for OAuth endpoints", 170 - }, 171 - &cli.StringFlag{ 172 - Name: "custom-auth-url", 173 - Value: "", 174 - Usage: "Use a custom Authorization URL (option for GitLab/GitHub)", 175 - }, 176 - &cli.StringFlag{ 177 - Name: "custom-token-url", 178 - Value: "", 179 - Usage: "Use a custom Token URL (option for GitLab/GitHub)", 180 - }, 181 - &cli.StringFlag{ 182 - Name: "custom-profile-url", 183 - Value: "", 184 - Usage: "Use a custom Profile URL (option for GitLab/GitHub)", 185 - }, 186 - &cli.StringFlag{ 187 - Name: "custom-email-url", 188 - Value: "", 189 - Usage: "Use a custom Email URL (option for GitHub)", 190 - }, 191 - &cli.StringFlag{ 192 - Name: "icon-url", 193 - Value: "", 194 - Usage: "Custom icon URL for OAuth2 login source", 195 - }, 196 - &cli.BoolFlag{ 197 - Name: "skip-local-2fa", 198 - Usage: "Set to true to skip local 2fa for users authenticated by this source", 199 - }, 200 - &cli.StringSliceFlag{ 201 - Name: "scopes", 202 - Value: nil, 203 - Usage: "Scopes to request when to authenticate against this OAuth2 source", 204 - }, 205 - &cli.StringFlag{ 206 - Name: "required-claim-name", 207 - Value: "", 208 - Usage: "Claim name that has to be set to allow users to login with this source", 209 - }, 210 - &cli.StringFlag{ 211 - Name: "required-claim-value", 212 - Value: "", 213 - Usage: "Claim value that has to be set to allow users to login with this source", 214 - }, 215 - &cli.StringFlag{ 216 - Name: "group-claim-name", 217 - Value: "", 218 - Usage: "Claim name providing group names for this source", 219 - }, 220 - &cli.StringFlag{ 221 - Name: "admin-group", 222 - Value: "", 223 - Usage: "Group Claim value for administrator users", 224 - }, 225 - &cli.StringFlag{ 226 - Name: "restricted-group", 227 - Value: "", 228 - Usage: "Group Claim value for restricted users", 229 - }, 230 - &cli.StringFlag{ 231 - Name: "group-team-map", 232 - Value: "", 233 - Usage: "JSON mapping between groups and org teams", 234 - }, 235 - &cli.BoolFlag{ 236 - Name: "group-team-map-removal", 237 - Usage: "Activate automatic team membership removal depending on groups", 238 - }, 239 - } 240 - 241 - microcmdAuthUpdateOauth = &cli.Command{ 242 - Name: "update-oauth", 243 - Usage: "Update existing Oauth authentication source", 244 - Action: runUpdateOauth, 245 - Flags: append(oauthCLIFlags[:1], append([]cli.Flag{idFlag}, oauthCLIFlags[1:]...)...), 246 - } 247 - 248 - microcmdAuthAddOauth = &cli.Command{ 249 - Name: "add-oauth", 250 - Usage: "Add new Oauth authentication source", 251 - Action: runAddOauth, 252 - Flags: oauthCLIFlags, 253 - } 254 - 255 66 subcmdSendMail = &cli.Command{ 256 67 Name: "sendmail", 257 68 Usage: "Send a message to all users", ··· 275 86 }, 276 87 } 277 88 278 - smtpCLIFlags = []cli.Flag{ 279 - &cli.StringFlag{ 280 - Name: "name", 281 - Value: "", 282 - Usage: "Application Name", 283 - }, 284 - &cli.StringFlag{ 285 - Name: "auth-type", 286 - Value: "PLAIN", 287 - Usage: "SMTP Authentication Type (PLAIN/LOGIN/CRAM-MD5) default PLAIN", 288 - }, 289 - &cli.StringFlag{ 290 - Name: "host", 291 - Value: "", 292 - Usage: "SMTP Host", 293 - }, 294 - &cli.IntFlag{ 295 - Name: "port", 296 - Usage: "SMTP Port", 297 - }, 298 - &cli.BoolFlag{ 299 - Name: "force-smtps", 300 - Usage: "SMTPS is always used on port 465. Set this to force SMTPS on other ports.", 301 - Value: true, 302 - }, 303 - &cli.BoolFlag{ 304 - Name: "skip-verify", 305 - Usage: "Skip TLS verify.", 306 - Value: true, 307 - }, 308 - &cli.StringFlag{ 309 - Name: "helo-hostname", 310 - Value: "", 311 - Usage: "Hostname sent with HELO. Leave blank to send current hostname", 312 - }, 313 - &cli.BoolFlag{ 314 - Name: "disable-helo", 315 - Usage: "Disable SMTP helo.", 316 - Value: true, 317 - }, 318 - &cli.StringFlag{ 319 - Name: "allowed-domains", 320 - Value: "", 321 - Usage: "Leave empty to allow all domains. Separate multiple domains with a comma (',')", 322 - }, 323 - &cli.BoolFlag{ 324 - Name: "skip-local-2fa", 325 - Usage: "Skip 2FA to log on.", 326 - Value: true, 327 - }, 328 - &cli.BoolFlag{ 329 - Name: "active", 330 - Usage: "This Authentication Source is Activated.", 331 - Value: true, 332 - }, 333 - } 334 - 335 - microcmdAuthAddSMTP = &cli.Command{ 336 - Name: "add-smtp", 337 - Usage: "Add new SMTP authentication source", 338 - Action: runAddSMTP, 339 - Flags: smtpCLIFlags, 340 - } 341 - 342 - microcmdAuthUpdateSMTP = &cli.Command{ 343 - Name: "update-smtp", 344 - Usage: "Update existing SMTP authentication source", 345 - Action: runUpdateSMTP, 346 - Flags: append(smtpCLIFlags[:1], append([]cli.Flag{idFlag}, smtpCLIFlags[1:]...)...), 89 + idFlag = &cli.Int64Flag{ 90 + Name: "id", 91 + Usage: "ID of authentication source", 347 92 } 348 93 ) 349 94 ··· 420 165 }, 421 166 ) 422 167 } 423 - 424 - func runRegenerateHooks(_ *cli.Context) error { 425 - ctx, cancel := installSignals() 426 - defer cancel() 427 - 428 - if err := initDB(ctx); err != nil { 429 - return err 430 - } 431 - return repo_service.SyncRepositoryHooks(graceful.GetManager().ShutdownContext()) 432 - } 433 - 434 - func runRegenerateKeys(_ *cli.Context) error { 435 - ctx, cancel := installSignals() 436 - defer cancel() 437 - 438 - if err := initDB(ctx); err != nil { 439 - return err 440 - } 441 - return asymkey_model.RewriteAllPublicKeys(ctx) 442 - } 443 - 444 - func parseOAuth2Config(c *cli.Context) *oauth2.Source { 445 - var customURLMapping *oauth2.CustomURLMapping 446 - if c.IsSet("use-custom-urls") { 447 - customURLMapping = &oauth2.CustomURLMapping{ 448 - TokenURL: c.String("custom-token-url"), 449 - AuthURL: c.String("custom-auth-url"), 450 - ProfileURL: c.String("custom-profile-url"), 451 - EmailURL: c.String("custom-email-url"), 452 - Tenant: c.String("custom-tenant-id"), 453 - } 454 - } else { 455 - customURLMapping = nil 456 - } 457 - return &oauth2.Source{ 458 - Provider: c.String("provider"), 459 - ClientID: c.String("key"), 460 - ClientSecret: c.String("secret"), 461 - OpenIDConnectAutoDiscoveryURL: c.String("auto-discover-url"), 462 - CustomURLMapping: customURLMapping, 463 - IconURL: c.String("icon-url"), 464 - SkipLocalTwoFA: c.Bool("skip-local-2fa"), 465 - Scopes: c.StringSlice("scopes"), 466 - RequiredClaimName: c.String("required-claim-name"), 467 - RequiredClaimValue: c.String("required-claim-value"), 468 - GroupClaimName: c.String("group-claim-name"), 469 - AdminGroup: c.String("admin-group"), 470 - RestrictedGroup: c.String("restricted-group"), 471 - GroupTeamMap: c.String("group-team-map"), 472 - GroupTeamMapRemoval: c.Bool("group-team-map-removal"), 473 - } 474 - } 475 - 476 - func runAddOauth(c *cli.Context) error { 477 - ctx, cancel := installSignals() 478 - defer cancel() 479 - 480 - if err := initDB(ctx); err != nil { 481 - return err 482 - } 483 - 484 - config := parseOAuth2Config(c) 485 - if config.Provider == "openidConnect" { 486 - discoveryURL, err := url.Parse(config.OpenIDConnectAutoDiscoveryURL) 487 - if err != nil || (discoveryURL.Scheme != "http" && discoveryURL.Scheme != "https") { 488 - return fmt.Errorf("invalid Auto Discovery URL: %s (this must be a valid URL starting with http:// or https://)", config.OpenIDConnectAutoDiscoveryURL) 489 - } 490 - } 491 - 492 - return auth_model.CreateSource(&auth_model.Source{ 493 - Type: auth_model.OAuth2, 494 - Name: c.String("name"), 495 - IsActive: true, 496 - Cfg: config, 497 - }) 498 - } 499 - 500 - func runUpdateOauth(c *cli.Context) error { 501 - if !c.IsSet("id") { 502 - return fmt.Errorf("--id flag is missing") 503 - } 504 - 505 - ctx, cancel := installSignals() 506 - defer cancel() 507 - 508 - if err := initDB(ctx); err != nil { 509 - return err 510 - } 511 - 512 - source, err := auth_model.GetSourceByID(c.Int64("id")) 513 - if err != nil { 514 - return err 515 - } 516 - 517 - oAuth2Config := source.Cfg.(*oauth2.Source) 518 - 519 - if c.IsSet("name") { 520 - source.Name = c.String("name") 521 - } 522 - 523 - if c.IsSet("provider") { 524 - oAuth2Config.Provider = c.String("provider") 525 - } 526 - 527 - if c.IsSet("key") { 528 - oAuth2Config.ClientID = c.String("key") 529 - } 530 - 531 - if c.IsSet("secret") { 532 - oAuth2Config.ClientSecret = c.String("secret") 533 - } 534 - 535 - if c.IsSet("auto-discover-url") { 536 - oAuth2Config.OpenIDConnectAutoDiscoveryURL = c.String("auto-discover-url") 537 - } 538 - 539 - if c.IsSet("icon-url") { 540 - oAuth2Config.IconURL = c.String("icon-url") 541 - } 542 - 543 - if c.IsSet("scopes") { 544 - oAuth2Config.Scopes = c.StringSlice("scopes") 545 - } 546 - 547 - if c.IsSet("required-claim-name") { 548 - oAuth2Config.RequiredClaimName = c.String("required-claim-name") 549 - } 550 - if c.IsSet("required-claim-value") { 551 - oAuth2Config.RequiredClaimValue = c.String("required-claim-value") 552 - } 553 - 554 - if c.IsSet("group-claim-name") { 555 - oAuth2Config.GroupClaimName = c.String("group-claim-name") 556 - } 557 - if c.IsSet("admin-group") { 558 - oAuth2Config.AdminGroup = c.String("admin-group") 559 - } 560 - if c.IsSet("restricted-group") { 561 - oAuth2Config.RestrictedGroup = c.String("restricted-group") 562 - } 563 - if c.IsSet("group-team-map") { 564 - oAuth2Config.GroupTeamMap = c.String("group-team-map") 565 - } 566 - if c.IsSet("group-team-map-removal") { 567 - oAuth2Config.GroupTeamMapRemoval = c.Bool("group-team-map-removal") 568 - } 569 - 570 - // update custom URL mapping 571 - customURLMapping := &oauth2.CustomURLMapping{} 572 - 573 - if oAuth2Config.CustomURLMapping != nil { 574 - customURLMapping.TokenURL = oAuth2Config.CustomURLMapping.TokenURL 575 - customURLMapping.AuthURL = oAuth2Config.CustomURLMapping.AuthURL 576 - customURLMapping.ProfileURL = oAuth2Config.CustomURLMapping.ProfileURL 577 - customURLMapping.EmailURL = oAuth2Config.CustomURLMapping.EmailURL 578 - customURLMapping.Tenant = oAuth2Config.CustomURLMapping.Tenant 579 - } 580 - if c.IsSet("use-custom-urls") && c.IsSet("custom-token-url") { 581 - customURLMapping.TokenURL = c.String("custom-token-url") 582 - } 583 - 584 - if c.IsSet("use-custom-urls") && c.IsSet("custom-auth-url") { 585 - customURLMapping.AuthURL = c.String("custom-auth-url") 586 - } 587 - 588 - if c.IsSet("use-custom-urls") && c.IsSet("custom-profile-url") { 589 - customURLMapping.ProfileURL = c.String("custom-profile-url") 590 - } 591 - 592 - if c.IsSet("use-custom-urls") && c.IsSet("custom-email-url") { 593 - customURLMapping.EmailURL = c.String("custom-email-url") 594 - } 595 - 596 - if c.IsSet("use-custom-urls") && c.IsSet("custom-tenant-id") { 597 - customURLMapping.Tenant = c.String("custom-tenant-id") 598 - } 599 - 600 - oAuth2Config.CustomURLMapping = customURLMapping 601 - source.Cfg = oAuth2Config 602 - 603 - return auth_model.UpdateSource(source) 604 - } 605 - 606 - func parseSMTPConfig(c *cli.Context, conf *smtp.Source) error { 607 - if c.IsSet("auth-type") { 608 - conf.Auth = c.String("auth-type") 609 - validAuthTypes := []string{"PLAIN", "LOGIN", "CRAM-MD5"} 610 - if !util.SliceContainsString(validAuthTypes, strings.ToUpper(c.String("auth-type"))) { 611 - return errors.New("Auth must be one of PLAIN/LOGIN/CRAM-MD5") 612 - } 613 - conf.Auth = c.String("auth-type") 614 - } 615 - if c.IsSet("host") { 616 - conf.Host = c.String("host") 617 - } 618 - if c.IsSet("port") { 619 - conf.Port = c.Int("port") 620 - } 621 - if c.IsSet("allowed-domains") { 622 - conf.AllowedDomains = c.String("allowed-domains") 623 - } 624 - if c.IsSet("force-smtps") { 625 - conf.ForceSMTPS = c.Bool("force-smtps") 626 - } 627 - if c.IsSet("skip-verify") { 628 - conf.SkipVerify = c.Bool("skip-verify") 629 - } 630 - if c.IsSet("helo-hostname") { 631 - conf.HeloHostname = c.String("helo-hostname") 632 - } 633 - if c.IsSet("disable-helo") { 634 - conf.DisableHelo = c.Bool("disable-helo") 635 - } 636 - if c.IsSet("skip-local-2fa") { 637 - conf.SkipLocalTwoFA = c.Bool("skip-local-2fa") 638 - } 639 - return nil 640 - } 641 - 642 - func runAddSMTP(c *cli.Context) error { 643 - ctx, cancel := installSignals() 644 - defer cancel() 645 - 646 - if err := initDB(ctx); err != nil { 647 - return err 648 - } 649 - 650 - if !c.IsSet("name") || len(c.String("name")) == 0 { 651 - return errors.New("name must be set") 652 - } 653 - if !c.IsSet("host") || len(c.String("host")) == 0 { 654 - return errors.New("host must be set") 655 - } 656 - if !c.IsSet("port") { 657 - return errors.New("port must be set") 658 - } 659 - active := true 660 - if c.IsSet("active") { 661 - active = c.Bool("active") 662 - } 663 - 664 - var smtpConfig smtp.Source 665 - if err := parseSMTPConfig(c, &smtpConfig); err != nil { 666 - return err 667 - } 668 - 669 - // If not set default to PLAIN 670 - if len(smtpConfig.Auth) == 0 { 671 - smtpConfig.Auth = "PLAIN" 672 - } 673 - 674 - return auth_model.CreateSource(&auth_model.Source{ 675 - Type: auth_model.SMTP, 676 - Name: c.String("name"), 677 - IsActive: active, 678 - Cfg: &smtpConfig, 679 - }) 680 - } 681 - 682 - func runUpdateSMTP(c *cli.Context) error { 683 - if !c.IsSet("id") { 684 - return fmt.Errorf("--id flag is missing") 685 - } 686 - 687 - ctx, cancel := installSignals() 688 - defer cancel() 689 - 690 - if err := initDB(ctx); err != nil { 691 - return err 692 - } 693 - 694 - source, err := auth_model.GetSourceByID(c.Int64("id")) 695 - if err != nil { 696 - return err 697 - } 698 - 699 - smtpConfig := source.Cfg.(*smtp.Source) 700 - 701 - if err := parseSMTPConfig(c, smtpConfig); err != nil { 702 - return err 703 - } 704 - 705 - if c.IsSet("name") { 706 - source.Name = c.String("name") 707 - } 708 - 709 - if c.IsSet("active") { 710 - source.IsActive = c.Bool("active") 711 - } 712 - 713 - source.Cfg = smtpConfig 714 - 715 - return auth_model.UpdateSource(source) 716 - } 717 - 718 - func runListAuth(c *cli.Context) error { 719 - ctx, cancel := installSignals() 720 - defer cancel() 721 - 722 - if err := initDB(ctx); err != nil { 723 - return err 724 - } 725 - 726 - authSources, err := auth_model.Sources() 727 - if err != nil { 728 - return err 729 - } 730 - 731 - flags := tabwriter.AlignRight 732 - if c.Bool("vertical-bars") { 733 - flags |= tabwriter.Debug 734 - } 735 - 736 - padChar := byte('\t') 737 - if len(c.String("pad-char")) > 0 { 738 - padChar = c.String("pad-char")[0] 739 - } 740 - 741 - // loop through each source and print 742 - w := tabwriter.NewWriter(os.Stdout, c.Int("min-width"), c.Int("tab-width"), c.Int("padding"), padChar, flags) 743 - fmt.Fprintf(w, "ID\tName\tType\tEnabled\n") 744 - for _, source := range authSources { 745 - fmt.Fprintf(w, "%d\t%s\t%s\t%t\n", source.ID, source.Name, source.Type.String(), source.IsActive) 746 - } 747 - w.Flush() 748 - 749 - return nil 750 - } 751 - 752 - func runDeleteAuth(c *cli.Context) error { 753 - if !c.IsSet("id") { 754 - return fmt.Errorf("--id flag is missing") 755 - } 756 - 757 - ctx, cancel := installSignals() 758 - defer cancel() 759 - 760 - if err := initDB(ctx); err != nil { 761 - return err 762 - } 763 - 764 - source, err := auth_model.GetSourceByID(c.Int64("id")) 765 - if err != nil { 766 - return err 767 - } 768 - 769 - return auth_service.DeleteSource(source) 770 - }
+109
cmd/admin_auth.go
··· 1 + // Copyright 2023 The Gitea Authors. All rights reserved. 2 + // SPDX-License-Identifier: MIT 3 + 4 + package cmd 5 + 6 + import ( 7 + "fmt" 8 + "os" 9 + "text/tabwriter" 10 + 11 + auth_model "code.gitea.io/gitea/models/auth" 12 + auth_service "code.gitea.io/gitea/services/auth" 13 + 14 + "github.com/urfave/cli/v2" 15 + ) 16 + 17 + var ( 18 + microcmdAuthDelete = &cli.Command{ 19 + Name: "delete", 20 + Usage: "Delete specific auth source", 21 + Flags: []cli.Flag{idFlag}, 22 + Action: runDeleteAuth, 23 + } 24 + microcmdAuthList = &cli.Command{ 25 + Name: "list", 26 + Usage: "List auth sources", 27 + Action: runListAuth, 28 + Flags: []cli.Flag{ 29 + &cli.IntFlag{ 30 + Name: "min-width", 31 + Usage: "Minimal cell width including any padding for the formatted table", 32 + Value: 0, 33 + }, 34 + &cli.IntFlag{ 35 + Name: "tab-width", 36 + Usage: "width of tab characters in formatted table (equivalent number of spaces)", 37 + Value: 8, 38 + }, 39 + &cli.IntFlag{ 40 + Name: "padding", 41 + Usage: "padding added to a cell before computing its width", 42 + Value: 1, 43 + }, 44 + &cli.StringFlag{ 45 + Name: "pad-char", 46 + Usage: `ASCII char used for padding if padchar == '\\t', the Writer will assume that the width of a '\\t' in the formatted output is tabwidth, and cells are left-aligned independent of align_left (for correct-looking results, tabwidth must correspond to the tab width in the viewer displaying the result)`, 47 + Value: "\t", 48 + }, 49 + &cli.BoolFlag{ 50 + Name: "vertical-bars", 51 + Usage: "Set to true to print vertical bars between columns", 52 + }, 53 + }, 54 + } 55 + ) 56 + 57 + func runListAuth(c *cli.Context) error { 58 + ctx, cancel := installSignals() 59 + defer cancel() 60 + 61 + if err := initDB(ctx); err != nil { 62 + return err 63 + } 64 + 65 + authSources, err := auth_model.Sources() 66 + if err != nil { 67 + return err 68 + } 69 + 70 + flags := tabwriter.AlignRight 71 + if c.Bool("vertical-bars") { 72 + flags |= tabwriter.Debug 73 + } 74 + 75 + padChar := byte('\t') 76 + if len(c.String("pad-char")) > 0 { 77 + padChar = c.String("pad-char")[0] 78 + } 79 + 80 + // loop through each source and print 81 + w := tabwriter.NewWriter(os.Stdout, c.Int("min-width"), c.Int("tab-width"), c.Int("padding"), padChar, flags) 82 + fmt.Fprintf(w, "ID\tName\tType\tEnabled\n") 83 + for _, source := range authSources { 84 + fmt.Fprintf(w, "%d\t%s\t%s\t%t\n", source.ID, source.Name, source.Type.String(), source.IsActive) 85 + } 86 + w.Flush() 87 + 88 + return nil 89 + } 90 + 91 + func runDeleteAuth(c *cli.Context) error { 92 + if !c.IsSet("id") { 93 + return fmt.Errorf("--id flag is missing") 94 + } 95 + 96 + ctx, cancel := installSignals() 97 + defer cancel() 98 + 99 + if err := initDB(ctx); err != nil { 100 + return err 101 + } 102 + 103 + source, err := auth_model.GetSourceByID(c.Int64("id")) 104 + if err != nil { 105 + return err 106 + } 107 + 108 + return auth_service.DeleteSource(source) 109 + }
+5 -5
cmd/admin_auth_ldap.go
··· 132 132 ldapSimpleAuthCLIFlags = append(commonLdapCLIFlags, 133 133 &cli.StringFlag{ 134 134 Name: "user-dn", 135 - Usage: "The user’s DN.", 135 + Usage: "The user's DN.", 136 136 }) 137 137 138 - cmdAuthAddLdapBindDn = &cli.Command{ 138 + microcmdAuthAddLdapBindDn = &cli.Command{ 139 139 Name: "add-ldap", 140 140 Usage: "Add new LDAP (via Bind DN) authentication source", 141 141 Action: func(c *cli.Context) error { ··· 144 144 Flags: ldapBindDnCLIFlags, 145 145 } 146 146 147 - cmdAuthUpdateLdapBindDn = &cli.Command{ 147 + microcmdAuthUpdateLdapBindDn = &cli.Command{ 148 148 Name: "update-ldap", 149 149 Usage: "Update existing LDAP (via Bind DN) authentication source", 150 150 Action: func(c *cli.Context) error { ··· 153 153 Flags: append([]cli.Flag{idFlag}, ldapBindDnCLIFlags...), 154 154 } 155 155 156 - cmdAuthAddLdapSimpleAuth = &cli.Command{ 156 + microcmdAuthAddLdapSimpleAuth = &cli.Command{ 157 157 Name: "add-ldap-simple", 158 158 Usage: "Add new LDAP (simple auth) authentication source", 159 159 Action: func(c *cli.Context) error { ··· 162 162 Flags: ldapSimpleAuthCLIFlags, 163 163 } 164 164 165 - cmdAuthUpdateLdapSimpleAuth = &cli.Command{ 165 + microcmdAuthUpdateLdapSimpleAuth = &cli.Command{ 166 166 Name: "update-ldap-simple", 167 167 Usage: "Update existing LDAP (simple auth) authentication source", 168 168 Action: func(c *cli.Context) error {
+4 -4
cmd/admin_auth_ldap_test.go
··· 226 226 227 227 // Create a copy of command to test 228 228 app := cli.NewApp() 229 - app.Flags = cmdAuthAddLdapBindDn.Flags 229 + app.Flags = microcmdAuthAddLdapBindDn.Flags 230 230 app.Action = service.addLdapBindDn 231 231 232 232 // Run it ··· 457 457 458 458 // Create a copy of command to test 459 459 app := cli.NewApp() 460 - app.Flags = cmdAuthAddLdapSimpleAuth.Flags 460 + app.Flags = microcmdAuthAddLdapSimpleAuth.Flags 461 461 app.Action = service.addLdapSimpleAuth 462 462 463 463 // Run it ··· 920 920 921 921 // Create a copy of command to test 922 922 app := cli.NewApp() 923 - app.Flags = cmdAuthUpdateLdapBindDn.Flags 923 + app.Flags = microcmdAuthUpdateLdapBindDn.Flags 924 924 app.Action = service.updateLdapBindDn 925 925 926 926 // Run it ··· 1310 1310 1311 1311 // Create a copy of command to test 1312 1312 app := cli.NewApp() 1313 - app.Flags = cmdAuthUpdateLdapSimpleAuth.Flags 1313 + app.Flags = microcmdAuthUpdateLdapSimpleAuth.Flags 1314 1314 app.Action = service.updateLdapSimpleAuth 1315 1315 1316 1316 // Run it
+298
cmd/admin_auth_oauth.go
··· 1 + // Copyright 2023 The Gitea Authors. All rights reserved. 2 + // SPDX-License-Identifier: MIT 3 + 4 + package cmd 5 + 6 + import ( 7 + "fmt" 8 + "net/url" 9 + 10 + auth_model "code.gitea.io/gitea/models/auth" 11 + "code.gitea.io/gitea/services/auth/source/oauth2" 12 + 13 + "github.com/urfave/cli/v2" 14 + ) 15 + 16 + var ( 17 + oauthCLIFlags = []cli.Flag{ 18 + &cli.StringFlag{ 19 + Name: "name", 20 + Value: "", 21 + Usage: "Application Name", 22 + }, 23 + &cli.StringFlag{ 24 + Name: "provider", 25 + Value: "", 26 + Usage: "OAuth2 Provider", 27 + }, 28 + &cli.StringFlag{ 29 + Name: "key", 30 + Value: "", 31 + Usage: "Client ID (Key)", 32 + }, 33 + &cli.StringFlag{ 34 + Name: "secret", 35 + Value: "", 36 + Usage: "Client Secret", 37 + }, 38 + &cli.StringFlag{ 39 + Name: "auto-discover-url", 40 + Value: "", 41 + Usage: "OpenID Connect Auto Discovery URL (only required when using OpenID Connect as provider)", 42 + }, 43 + &cli.StringFlag{ 44 + Name: "use-custom-urls", 45 + Value: "false", 46 + Usage: "Use custom URLs for GitLab/GitHub OAuth endpoints", 47 + }, 48 + &cli.StringFlag{ 49 + Name: "custom-tenant-id", 50 + Value: "", 51 + Usage: "Use custom Tenant ID for OAuth endpoints", 52 + }, 53 + &cli.StringFlag{ 54 + Name: "custom-auth-url", 55 + Value: "", 56 + Usage: "Use a custom Authorization URL (option for GitLab/GitHub)", 57 + }, 58 + &cli.StringFlag{ 59 + Name: "custom-token-url", 60 + Value: "", 61 + Usage: "Use a custom Token URL (option for GitLab/GitHub)", 62 + }, 63 + &cli.StringFlag{ 64 + Name: "custom-profile-url", 65 + Value: "", 66 + Usage: "Use a custom Profile URL (option for GitLab/GitHub)", 67 + }, 68 + &cli.StringFlag{ 69 + Name: "custom-email-url", 70 + Value: "", 71 + Usage: "Use a custom Email URL (option for GitHub)", 72 + }, 73 + &cli.StringFlag{ 74 + Name: "icon-url", 75 + Value: "", 76 + Usage: "Custom icon URL for OAuth2 login source", 77 + }, 78 + &cli.BoolFlag{ 79 + Name: "skip-local-2fa", 80 + Usage: "Set to true to skip local 2fa for users authenticated by this source", 81 + }, 82 + &cli.StringSliceFlag{ 83 + Name: "scopes", 84 + Value: nil, 85 + Usage: "Scopes to request when to authenticate against this OAuth2 source", 86 + }, 87 + &cli.StringFlag{ 88 + Name: "required-claim-name", 89 + Value: "", 90 + Usage: "Claim name that has to be set to allow users to login with this source", 91 + }, 92 + &cli.StringFlag{ 93 + Name: "required-claim-value", 94 + Value: "", 95 + Usage: "Claim value that has to be set to allow users to login with this source", 96 + }, 97 + &cli.StringFlag{ 98 + Name: "group-claim-name", 99 + Value: "", 100 + Usage: "Claim name providing group names for this source", 101 + }, 102 + &cli.StringFlag{ 103 + Name: "admin-group", 104 + Value: "", 105 + Usage: "Group Claim value for administrator users", 106 + }, 107 + &cli.StringFlag{ 108 + Name: "restricted-group", 109 + Value: "", 110 + Usage: "Group Claim value for restricted users", 111 + }, 112 + &cli.StringFlag{ 113 + Name: "group-team-map", 114 + Value: "", 115 + Usage: "JSON mapping between groups and org teams", 116 + }, 117 + &cli.BoolFlag{ 118 + Name: "group-team-map-removal", 119 + Usage: "Activate automatic team membership removal depending on groups", 120 + }, 121 + } 122 + 123 + microcmdAuthAddOauth = &cli.Command{ 124 + Name: "add-oauth", 125 + Usage: "Add new Oauth authentication source", 126 + Action: runAddOauth, 127 + Flags: oauthCLIFlags, 128 + } 129 + 130 + microcmdAuthUpdateOauth = &cli.Command{ 131 + Name: "update-oauth", 132 + Usage: "Update existing Oauth authentication source", 133 + Action: runUpdateOauth, 134 + Flags: append(oauthCLIFlags[:1], append([]cli.Flag{idFlag}, oauthCLIFlags[1:]...)...), 135 + } 136 + ) 137 + 138 + func parseOAuth2Config(c *cli.Context) *oauth2.Source { 139 + var customURLMapping *oauth2.CustomURLMapping 140 + if c.IsSet("use-custom-urls") { 141 + customURLMapping = &oauth2.CustomURLMapping{ 142 + TokenURL: c.String("custom-token-url"), 143 + AuthURL: c.String("custom-auth-url"), 144 + ProfileURL: c.String("custom-profile-url"), 145 + EmailURL: c.String("custom-email-url"), 146 + Tenant: c.String("custom-tenant-id"), 147 + } 148 + } else { 149 + customURLMapping = nil 150 + } 151 + return &oauth2.Source{ 152 + Provider: c.String("provider"), 153 + ClientID: c.String("key"), 154 + ClientSecret: c.String("secret"), 155 + OpenIDConnectAutoDiscoveryURL: c.String("auto-discover-url"), 156 + CustomURLMapping: customURLMapping, 157 + IconURL: c.String("icon-url"), 158 + SkipLocalTwoFA: c.Bool("skip-local-2fa"), 159 + Scopes: c.StringSlice("scopes"), 160 + RequiredClaimName: c.String("required-claim-name"), 161 + RequiredClaimValue: c.String("required-claim-value"), 162 + GroupClaimName: c.String("group-claim-name"), 163 + AdminGroup: c.String("admin-group"), 164 + RestrictedGroup: c.String("restricted-group"), 165 + GroupTeamMap: c.String("group-team-map"), 166 + GroupTeamMapRemoval: c.Bool("group-team-map-removal"), 167 + } 168 + } 169 + 170 + func runAddOauth(c *cli.Context) error { 171 + ctx, cancel := installSignals() 172 + defer cancel() 173 + 174 + if err := initDB(ctx); err != nil { 175 + return err 176 + } 177 + 178 + config := parseOAuth2Config(c) 179 + if config.Provider == "openidConnect" { 180 + discoveryURL, err := url.Parse(config.OpenIDConnectAutoDiscoveryURL) 181 + if err != nil || (discoveryURL.Scheme != "http" && discoveryURL.Scheme != "https") { 182 + return fmt.Errorf("invalid Auto Discovery URL: %s (this must be a valid URL starting with http:// or https://)", config.OpenIDConnectAutoDiscoveryURL) 183 + } 184 + } 185 + 186 + return auth_model.CreateSource(&auth_model.Source{ 187 + Type: auth_model.OAuth2, 188 + Name: c.String("name"), 189 + IsActive: true, 190 + Cfg: config, 191 + }) 192 + } 193 + 194 + func runUpdateOauth(c *cli.Context) error { 195 + if !c.IsSet("id") { 196 + return fmt.Errorf("--id flag is missing") 197 + } 198 + 199 + ctx, cancel := installSignals() 200 + defer cancel() 201 + 202 + if err := initDB(ctx); err != nil { 203 + return err 204 + } 205 + 206 + source, err := auth_model.GetSourceByID(c.Int64("id")) 207 + if err != nil { 208 + return err 209 + } 210 + 211 + oAuth2Config := source.Cfg.(*oauth2.Source) 212 + 213 + if c.IsSet("name") { 214 + source.Name = c.String("name") 215 + } 216 + 217 + if c.IsSet("provider") { 218 + oAuth2Config.Provider = c.String("provider") 219 + } 220 + 221 + if c.IsSet("key") { 222 + oAuth2Config.ClientID = c.String("key") 223 + } 224 + 225 + if c.IsSet("secret") { 226 + oAuth2Config.ClientSecret = c.String("secret") 227 + } 228 + 229 + if c.IsSet("auto-discover-url") { 230 + oAuth2Config.OpenIDConnectAutoDiscoveryURL = c.String("auto-discover-url") 231 + } 232 + 233 + if c.IsSet("icon-url") { 234 + oAuth2Config.IconURL = c.String("icon-url") 235 + } 236 + 237 + if c.IsSet("scopes") { 238 + oAuth2Config.Scopes = c.StringSlice("scopes") 239 + } 240 + 241 + if c.IsSet("required-claim-name") { 242 + oAuth2Config.RequiredClaimName = c.String("required-claim-name") 243 + } 244 + if c.IsSet("required-claim-value") { 245 + oAuth2Config.RequiredClaimValue = c.String("required-claim-value") 246 + } 247 + 248 + if c.IsSet("group-claim-name") { 249 + oAuth2Config.GroupClaimName = c.String("group-claim-name") 250 + } 251 + if c.IsSet("admin-group") { 252 + oAuth2Config.AdminGroup = c.String("admin-group") 253 + } 254 + if c.IsSet("restricted-group") { 255 + oAuth2Config.RestrictedGroup = c.String("restricted-group") 256 + } 257 + if c.IsSet("group-team-map") { 258 + oAuth2Config.GroupTeamMap = c.String("group-team-map") 259 + } 260 + if c.IsSet("group-team-map-removal") { 261 + oAuth2Config.GroupTeamMapRemoval = c.Bool("group-team-map-removal") 262 + } 263 + 264 + // update custom URL mapping 265 + customURLMapping := &oauth2.CustomURLMapping{} 266 + 267 + if oAuth2Config.CustomURLMapping != nil { 268 + customURLMapping.TokenURL = oAuth2Config.CustomURLMapping.TokenURL 269 + customURLMapping.AuthURL = oAuth2Config.CustomURLMapping.AuthURL 270 + customURLMapping.ProfileURL = oAuth2Config.CustomURLMapping.ProfileURL 271 + customURLMapping.EmailURL = oAuth2Config.CustomURLMapping.EmailURL 272 + customURLMapping.Tenant = oAuth2Config.CustomURLMapping.Tenant 273 + } 274 + if c.IsSet("use-custom-urls") && c.IsSet("custom-token-url") { 275 + customURLMapping.TokenURL = c.String("custom-token-url") 276 + } 277 + 278 + if c.IsSet("use-custom-urls") && c.IsSet("custom-auth-url") { 279 + customURLMapping.AuthURL = c.String("custom-auth-url") 280 + } 281 + 282 + if c.IsSet("use-custom-urls") && c.IsSet("custom-profile-url") { 283 + customURLMapping.ProfileURL = c.String("custom-profile-url") 284 + } 285 + 286 + if c.IsSet("use-custom-urls") && c.IsSet("custom-email-url") { 287 + customURLMapping.EmailURL = c.String("custom-email-url") 288 + } 289 + 290 + if c.IsSet("use-custom-urls") && c.IsSet("custom-tenant-id") { 291 + customURLMapping.Tenant = c.String("custom-tenant-id") 292 + } 293 + 294 + oAuth2Config.CustomURLMapping = customURLMapping 295 + source.Cfg = oAuth2Config 296 + 297 + return auth_model.UpdateSource(source) 298 + }
+201
cmd/admin_auth_stmp.go
··· 1 + // Copyright 2023 The Gitea Authors. All rights reserved. 2 + // SPDX-License-Identifier: MIT 3 + 4 + package cmd 5 + 6 + import ( 7 + "errors" 8 + "fmt" 9 + "strings" 10 + 11 + auth_model "code.gitea.io/gitea/models/auth" 12 + "code.gitea.io/gitea/modules/util" 13 + "code.gitea.io/gitea/services/auth/source/smtp" 14 + 15 + "github.com/urfave/cli/v2" 16 + ) 17 + 18 + var ( 19 + smtpCLIFlags = []cli.Flag{ 20 + &cli.StringFlag{ 21 + Name: "name", 22 + Value: "", 23 + Usage: "Application Name", 24 + }, 25 + &cli.StringFlag{ 26 + Name: "auth-type", 27 + Value: "PLAIN", 28 + Usage: "SMTP Authentication Type (PLAIN/LOGIN/CRAM-MD5) default PLAIN", 29 + }, 30 + &cli.StringFlag{ 31 + Name: "host", 32 + Value: "", 33 + Usage: "SMTP Host", 34 + }, 35 + &cli.IntFlag{ 36 + Name: "port", 37 + Usage: "SMTP Port", 38 + }, 39 + &cli.BoolFlag{ 40 + Name: "force-smtps", 41 + Usage: "SMTPS is always used on port 465. Set this to force SMTPS on other ports.", 42 + Value: true, 43 + }, 44 + &cli.BoolFlag{ 45 + Name: "skip-verify", 46 + Usage: "Skip TLS verify.", 47 + Value: true, 48 + }, 49 + &cli.StringFlag{ 50 + Name: "helo-hostname", 51 + Value: "", 52 + Usage: "Hostname sent with HELO. Leave blank to send current hostname", 53 + }, 54 + &cli.BoolFlag{ 55 + Name: "disable-helo", 56 + Usage: "Disable SMTP helo.", 57 + Value: true, 58 + }, 59 + &cli.StringFlag{ 60 + Name: "allowed-domains", 61 + Value: "", 62 + Usage: "Leave empty to allow all domains. Separate multiple domains with a comma (',')", 63 + }, 64 + &cli.BoolFlag{ 65 + Name: "skip-local-2fa", 66 + Usage: "Skip 2FA to log on.", 67 + Value: true, 68 + }, 69 + &cli.BoolFlag{ 70 + Name: "active", 71 + Usage: "This Authentication Source is Activated.", 72 + Value: true, 73 + }, 74 + } 75 + 76 + microcmdAuthAddSMTP = &cli.Command{ 77 + Name: "add-smtp", 78 + Usage: "Add new SMTP authentication source", 79 + Action: runAddSMTP, 80 + Flags: smtpCLIFlags, 81 + } 82 + 83 + microcmdAuthUpdateSMTP = &cli.Command{ 84 + Name: "update-smtp", 85 + Usage: "Update existing SMTP authentication source", 86 + Action: runUpdateSMTP, 87 + Flags: append(smtpCLIFlags[:1], append([]cli.Flag{idFlag}, smtpCLIFlags[1:]...)...), 88 + } 89 + ) 90 + 91 + func parseSMTPConfig(c *cli.Context, conf *smtp.Source) error { 92 + if c.IsSet("auth-type") { 93 + conf.Auth = c.String("auth-type") 94 + validAuthTypes := []string{"PLAIN", "LOGIN", "CRAM-MD5"} 95 + if !util.SliceContainsString(validAuthTypes, strings.ToUpper(c.String("auth-type"))) { 96 + return errors.New("Auth must be one of PLAIN/LOGIN/CRAM-MD5") 97 + } 98 + conf.Auth = c.String("auth-type") 99 + } 100 + if c.IsSet("host") { 101 + conf.Host = c.String("host") 102 + } 103 + if c.IsSet("port") { 104 + conf.Port = c.Int("port") 105 + } 106 + if c.IsSet("allowed-domains") { 107 + conf.AllowedDomains = c.String("allowed-domains") 108 + } 109 + if c.IsSet("force-smtps") { 110 + conf.ForceSMTPS = c.Bool("force-smtps") 111 + } 112 + if c.IsSet("skip-verify") { 113 + conf.SkipVerify = c.Bool("skip-verify") 114 + } 115 + if c.IsSet("helo-hostname") { 116 + conf.HeloHostname = c.String("helo-hostname") 117 + } 118 + if c.IsSet("disable-helo") { 119 + conf.DisableHelo = c.Bool("disable-helo") 120 + } 121 + if c.IsSet("skip-local-2fa") { 122 + conf.SkipLocalTwoFA = c.Bool("skip-local-2fa") 123 + } 124 + return nil 125 + } 126 + 127 + func runAddSMTP(c *cli.Context) error { 128 + ctx, cancel := installSignals() 129 + defer cancel() 130 + 131 + if err := initDB(ctx); err != nil { 132 + return err 133 + } 134 + 135 + if !c.IsSet("name") || len(c.String("name")) == 0 { 136 + return errors.New("name must be set") 137 + } 138 + if !c.IsSet("host") || len(c.String("host")) == 0 { 139 + return errors.New("host must be set") 140 + } 141 + if !c.IsSet("port") { 142 + return errors.New("port must be set") 143 + } 144 + active := true 145 + if c.IsSet("active") { 146 + active = c.Bool("active") 147 + } 148 + 149 + var smtpConfig smtp.Source 150 + if err := parseSMTPConfig(c, &smtpConfig); err != nil { 151 + return err 152 + } 153 + 154 + // If not set default to PLAIN 155 + if len(smtpConfig.Auth) == 0 { 156 + smtpConfig.Auth = "PLAIN" 157 + } 158 + 159 + return auth_model.CreateSource(&auth_model.Source{ 160 + Type: auth_model.SMTP, 161 + Name: c.String("name"), 162 + IsActive: active, 163 + Cfg: &smtpConfig, 164 + }) 165 + } 166 + 167 + func runUpdateSMTP(c *cli.Context) error { 168 + if !c.IsSet("id") { 169 + return fmt.Errorf("--id flag is missing") 170 + } 171 + 172 + ctx, cancel := installSignals() 173 + defer cancel() 174 + 175 + if err := initDB(ctx); err != nil { 176 + return err 177 + } 178 + 179 + source, err := auth_model.GetSourceByID(c.Int64("id")) 180 + if err != nil { 181 + return err 182 + } 183 + 184 + smtpConfig := source.Cfg.(*smtp.Source) 185 + 186 + if err := parseSMTPConfig(c, smtpConfig); err != nil { 187 + return err 188 + } 189 + 190 + if c.IsSet("name") { 191 + source.Name = c.String("name") 192 + } 193 + 194 + if c.IsSet("active") { 195 + source.IsActive = c.Bool("active") 196 + } 197 + 198 + source.Cfg = smtpConfig 199 + 200 + return auth_model.UpdateSource(source) 201 + }
+46
cmd/admin_regenerate.go
··· 1 + // Copyright 2023 The Gitea Authors. All rights reserved. 2 + // SPDX-License-Identifier: MIT 3 + 4 + package cmd 5 + 6 + import ( 7 + asymkey_model "code.gitea.io/gitea/models/asymkey" 8 + "code.gitea.io/gitea/modules/graceful" 9 + repo_service "code.gitea.io/gitea/services/repository" 10 + 11 + "github.com/urfave/cli/v2" 12 + ) 13 + 14 + var ( 15 + microcmdRegenHooks = &cli.Command{ 16 + Name: "hooks", 17 + Usage: "Regenerate git-hooks", 18 + Action: runRegenerateHooks, 19 + } 20 + 21 + microcmdRegenKeys = &cli.Command{ 22 + Name: "keys", 23 + Usage: "Regenerate authorized_keys file", 24 + Action: runRegenerateKeys, 25 + } 26 + ) 27 + 28 + func runRegenerateHooks(_ *cli.Context) error { 29 + ctx, cancel := installSignals() 30 + defer cancel() 31 + 32 + if err := initDB(ctx); err != nil { 33 + return err 34 + } 35 + return repo_service.SyncRepositoryHooks(graceful.GetManager().ShutdownContext()) 36 + } 37 + 38 + func runRegenerateKeys(_ *cli.Context) error { 39 + ctx, cancel := installSignals() 40 + defer cancel() 41 + 42 + if err := initDB(ctx); err != nil { 43 + return err 44 + } 45 + return asymkey_model.RewriteAllPublicKeys(ctx) 46 + }