Skip to content

Commit 8cc62ff

Browse files
authored
Merge branch 'main' into fix-doc
2 parents 9dd318b + 17b2079 commit 8cc62ff

File tree

11 files changed

+215
-14
lines changed

11 files changed

+215
-14
lines changed

cmd/admin.go

+181
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"code.gitea.io/gitea/modules/storage"
2828
auth_service "code.gitea.io/gitea/services/auth"
2929
"code.gitea.io/gitea/services/auth/source/oauth2"
30+
"code.gitea.io/gitea/services/auth/source/smtp"
3031
repo_service "code.gitea.io/gitea/services/repository"
3132
user_service "code.gitea.io/gitea/services/user"
3233

@@ -190,6 +191,8 @@ var (
190191
cmdAuthUpdateLdapBindDn,
191192
cmdAuthAddLdapSimpleAuth,
192193
cmdAuthUpdateLdapSimpleAuth,
194+
microcmdAuthAddSMTP,
195+
microcmdAuthUpdateSMTP,
193196
microcmdAuthList,
194197
microcmdAuthDelete,
195198
},
@@ -366,6 +369,72 @@ var (
366369
},
367370
},
368371
}
372+
373+
smtpCLIFlags = []cli.Flag{
374+
cli.StringFlag{
375+
Name: "name",
376+
Value: "",
377+
Usage: "Application Name",
378+
},
379+
cli.StringFlag{
380+
Name: "auth-type",
381+
Value: "PLAIN",
382+
Usage: "SMTP Authentication Type (PLAIN/LOGIN/CRAM-MD5) default PLAIN",
383+
},
384+
cli.StringFlag{
385+
Name: "host",
386+
Value: "",
387+
Usage: "SMTP Host",
388+
},
389+
cli.IntFlag{
390+
Name: "port",
391+
Usage: "SMTP Port",
392+
},
393+
cli.BoolTFlag{
394+
Name: "force-smtps",
395+
Usage: "SMTPS is always used on port 465. Set this to force SMTPS on other ports.",
396+
},
397+
cli.BoolTFlag{
398+
Name: "skip-verify",
399+
Usage: "Skip TLS verify.",
400+
},
401+
cli.StringFlag{
402+
Name: "helo-hostname",
403+
Value: "",
404+
Usage: "Hostname sent with HELO. Leave blank to send current hostname",
405+
},
406+
cli.BoolTFlag{
407+
Name: "disable-helo",
408+
Usage: "Disable SMTP helo.",
409+
},
410+
cli.StringFlag{
411+
Name: "allowed-domains",
412+
Value: "",
413+
Usage: "Leave empty to allow all domains. Separate multiple domains with a comma (',')",
414+
},
415+
cli.BoolTFlag{
416+
Name: "skip-local-2fa",
417+
Usage: "Skip 2FA to log on.",
418+
},
419+
cli.BoolTFlag{
420+
Name: "active",
421+
Usage: "This Authentication Source is Activated.",
422+
},
423+
}
424+
425+
microcmdAuthAddSMTP = cli.Command{
426+
Name: "add-smtp",
427+
Usage: "Add new SMTP authentication source",
428+
Action: runAddSMTP,
429+
Flags: smtpCLIFlags,
430+
}
431+
432+
microcmdAuthUpdateSMTP = cli.Command{
433+
Name: "update-smtp",
434+
Usage: "Update existing SMTP authentication source",
435+
Action: runUpdateSMTP,
436+
Flags: append(smtpCLIFlags[:1], append([]cli.Flag{idFlag}, smtpCLIFlags[1:]...)...),
437+
}
369438
)
370439

371440
func runChangePassword(c *cli.Context) error {
@@ -804,6 +873,118 @@ func runUpdateOauth(c *cli.Context) error {
804873
return auth.UpdateSource(source)
805874
}
806875

876+
func parseSMTPConfig(c *cli.Context, conf *smtp.Source) error {
877+
if c.IsSet("auth-type") {
878+
conf.Auth = c.String("auth-type")
879+
validAuthTypes := []string{"PLAIN", "LOGIN", "CRAM-MD5"}
880+
if !contains(validAuthTypes, strings.ToUpper(c.String("auth-type"))) {
881+
return errors.New("Auth must be one of PLAIN/LOGIN/CRAM-MD5")
882+
}
883+
conf.Auth = c.String("auth-type")
884+
}
885+
if c.IsSet("host") {
886+
conf.Host = c.String("host")
887+
}
888+
if c.IsSet("port") {
889+
conf.Port = c.Int("port")
890+
}
891+
if c.IsSet("allowed-domains") {
892+
conf.AllowedDomains = c.String("allowed-domains")
893+
}
894+
if c.IsSet("force-smtps") {
895+
conf.ForceSMTPS = c.BoolT("force-smtps")
896+
}
897+
if c.IsSet("skip-verify") {
898+
conf.SkipVerify = c.BoolT("skip-verify")
899+
}
900+
if c.IsSet("helo-hostname") {
901+
conf.HeloHostname = c.String("helo-hostname")
902+
}
903+
if c.IsSet("disable-helo") {
904+
conf.DisableHelo = c.BoolT("disable-helo")
905+
}
906+
if c.IsSet("skip-local-2fa") {
907+
conf.SkipLocalTwoFA = c.BoolT("skip-local-2fa")
908+
}
909+
return nil
910+
}
911+
912+
func runAddSMTP(c *cli.Context) error {
913+
ctx, cancel := installSignals()
914+
defer cancel()
915+
916+
if err := initDB(ctx); err != nil {
917+
return err
918+
}
919+
920+
if !c.IsSet("name") || len(c.String("name")) == 0 {
921+
return errors.New("name must be set")
922+
}
923+
if !c.IsSet("host") || len(c.String("host")) == 0 {
924+
return errors.New("host must be set")
925+
}
926+
if !c.IsSet("port") {
927+
return errors.New("port must be set")
928+
}
929+
var active = true
930+
if c.IsSet("active") {
931+
active = c.BoolT("active")
932+
}
933+
934+
var smtpConfig smtp.Source
935+
if err := parseSMTPConfig(c, &smtpConfig); err != nil {
936+
return err
937+
}
938+
939+
// If not set default to PLAIN
940+
if len(smtpConfig.Auth) == 0 {
941+
smtpConfig.Auth = "PLAIN"
942+
}
943+
944+
return auth.CreateSource(&auth.Source{
945+
Type: auth.SMTP,
946+
Name: c.String("name"),
947+
IsActive: active,
948+
Cfg: &smtpConfig,
949+
})
950+
}
951+
952+
func runUpdateSMTP(c *cli.Context) error {
953+
if !c.IsSet("id") {
954+
return fmt.Errorf("--id flag is missing")
955+
}
956+
957+
ctx, cancel := installSignals()
958+
defer cancel()
959+
960+
if err := initDB(ctx); err != nil {
961+
return err
962+
}
963+
964+
source, err := auth.GetSourceByID(c.Int64("id"))
965+
if err != nil {
966+
return err
967+
}
968+
969+
smtpConfig := source.Cfg.(*smtp.Source)
970+
971+
if err := parseSMTPConfig(c, smtpConfig); err != nil {
972+
return err
973+
}
974+
975+
if c.IsSet("name") {
976+
source.Name = c.String("name")
977+
}
978+
979+
if c.IsSet("active") {
980+
source.IsActive = c.BoolT("active")
981+
}
982+
983+
source.Cfg = smtpConfig
984+
985+
return auth.UpdateSource(source)
986+
}
987+
807988
func runListAuth(c *cli.Context) error {
808989
ctx, cancel := installSignals()
809990
defer cancel()

docs/content/doc/usage/command-line.en-us.md

+27
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,33 @@ Admin operations:
161161
- `--restricted-group`: Group Claim value for restricted users. (Optional)
162162
- Examples:
163163
- `gitea admin auth update-oauth --id 1 --name external-github-updated`
164+
- `add-smtp`:
165+
- Options:
166+
- `--name`: Application Name. Required.
167+
- `--auth-type`: SMTP Authentication Type (PLAIN/LOGIN/CRAM-MD5). Default to PLAIN.
168+
- `--host`: SMTP host. Required.
169+
- `--port`: SMTP port. Required.
170+
- `--force-smtps`: SMTPS is always used on port 465. Set this to force SMTPS on other ports.
171+
- `--skip-verify`: Skip TLS verify.
172+
- `--helo-hostname`: Hostname sent with HELO. Leave blank to send current hostname.
173+
- `--disable-helo`: Disable SMTP helo.
174+
- `--allowed-domains`: Leave empty to allow all domains. Separate multiple domains with a comma (',').
175+
- `--skip-local-2fa`: Skip 2FA to log on.
176+
- `--active`: This Authentication Source is Activated.
177+
Remarks:
178+
`--force-smtps`, `--skip-verify`, `--disable-helo`, `--skip-loca-2fs` and `--active` options can be used in form:
179+
- `--option`, `--option=true` to enable
180+
- `--option=false` to disable
181+
If those options are not specified value would not be changed in `update-smtp` or would use default `false` value in `add-smtp`
182+
- Examples:
183+
- `gitea admin auth add-smtp --name ldap --host smtp.mydomain.org --port 587 --skip-verify --active`
184+
- `update-smtp`:
185+
- Options:
186+
- `--id`: ID of source to be updated. Required.
187+
- other options are shared with `add-smtp`
188+
- Examples:
189+
- `gitea admin auth update-smtp --id 1 --host smtp.mydomain.org --port 587 --skip-verify=false`
190+
- `gitea admin auth update-smtp --id 1 --active=false`
164191
- `add-ldap`: Add new LDAP (via Bind DN) authentication source
165192
- Options:
166193
- `--name value`: Authentication name. Required.

modules/git/repo_commit_nogogit.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,8 @@ func (repo *Repository) getCommitFromBatchReader(rd *bufio.Reader, id SHA1) (*Co
103103
if err != nil {
104104
return nil, err
105105
}
106-
tag.repo = repo
107106

108-
commit, err := tag.Commit()
107+
commit, err := tag.Commit(repo)
109108
if err != nil {
110109
return nil, err
111110
}

modules/git/repo_tag.go

-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ func (repo *Repository) getTag(tagID SHA1, name string) (*Tag, error) {
7272
Type: tp,
7373
Tagger: commit.Committer,
7474
Message: commit.Message(),
75-
repo: repo,
7675
}
7776

7877
repo.tagCache.Set(tagID.String(), tag)
@@ -92,7 +91,6 @@ func (repo *Repository) getTag(tagID SHA1, name string) (*Tag, error) {
9291

9392
tag.Name = name
9493
tag.ID = tagID
95-
tag.repo = repo
9694
tag.Type = tp
9795

9896
repo.tagCache.Set(tagID.String(), tag)

modules/git/repo_tag_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ func TestRepository_GetTag(t *testing.T) {
5555
if lTag == nil {
5656
assert.FailNow(t, "nil lTag: %s", lTagName)
5757
}
58-
lTag.repo = nil
5958
assert.EqualValues(t, lTagName, lTag.Name)
6059
assert.EqualValues(t, lTagCommitID, lTag.ID.String())
6160
assert.EqualValues(t, lTagCommitID, lTag.Object.String())

modules/git/repo_tree_nogogit.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (repo *Repository) getTree(id SHA1) (*Tree, error) {
3434
if err != nil {
3535
return nil, err
3636
}
37-
commit, err := tag.Commit()
37+
commit, err := tag.Commit(repo)
3838
if err != nil {
3939
return nil, err
4040
}

modules/git/tag.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ const endpgp = "\n-----END PGP SIGNATURE-----"
1717
type Tag struct {
1818
Name string
1919
ID SHA1
20-
repo *Repository
2120
Object SHA1 // The id of this commit object
2221
Type string
2322
Tagger *Signature
@@ -26,8 +25,8 @@ type Tag struct {
2625
}
2726

2827
// Commit return the commit of the tag reference
29-
func (tag *Tag) Commit() (*Commit, error) {
30-
return tag.repo.getCommit(tag.Object)
28+
func (tag *Tag) Commit(gitRepo *Repository) (*Commit, error) {
29+
return gitRepo.getCommit(tag.Object)
3130
}
3231

3332
// Parse commit information from the (uncompressed) raw

modules/git/tag_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ tagger Lucas Michot <[email protected]> 1484491741 +0100
2424
`), tag: Tag{
2525
Name: "",
2626
ID: SHA1{},
27-
repo: nil,
2827
Object: SHA1{0x3b, 0x11, 0x4a, 0xb8, 0x0, 0xc6, 0x43, 0x2a, 0xd4, 0x23, 0x87, 0xcc, 0xf6, 0xbc, 0x8d, 0x43, 0x88, 0xa2, 0x88, 0x5a},
2928
Type: "commit",
3029
Tagger: &Signature{Name: "Lucas Michot", Email: "[email protected]", When: time.Unix(1484491741, 0)},
@@ -42,7 +41,6 @@ o
4241
ono`), tag: Tag{
4342
Name: "",
4443
ID: SHA1{},
45-
repo: nil,
4644
Object: SHA1{0x7c, 0xdf, 0x42, 0xc0, 0xb1, 0xcc, 0x76, 0x3a, 0xb7, 0xe4, 0xc3, 0x3c, 0x47, 0xa2, 0x4e, 0x27, 0xc6, 0x6b, 0xfc, 0xcc},
4745
Type: "commit",
4846
Tagger: &Signature{Name: "Lucas Michot", Email: "[email protected]", When: time.Unix(1484553735, 0)},

modules/repository/repo.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ func PushUpdateAddTag(repo *repo_model.Repository, gitRepo *git.Repository, tagN
296296
if err != nil {
297297
return fmt.Errorf("GetTag: %v", err)
298298
}
299-
commit, err := tag.Commit()
299+
commit, err := tag.Commit(gitRepo)
300300
if err != nil {
301301
return fmt.Errorf("Commit: %v", err)
302302
}

routers/api/v1/repo/tag.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func GetAnnotatedTag(ctx *context.APIContext) {
103103
if tag, err := ctx.Repo.GitRepo.GetAnnotatedTag(sha); err != nil {
104104
ctx.Error(http.StatusBadRequest, "GetAnnotatedTag", err)
105105
} else {
106-
commit, err := tag.Commit()
106+
commit, err := tag.Commit(ctx.Repo.GitRepo)
107107
if err != nil {
108108
ctx.Error(http.StatusBadRequest, "GetAnnotatedTag", err)
109109
}

services/repository/push.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ func pushUpdateAddTags(ctx context.Context, repo *repo_model.Repository, gitRepo
294294
if err != nil {
295295
return fmt.Errorf("GetTag: %v", err)
296296
}
297-
commit, err := tag.Commit()
297+
commit, err := tag.Commit(gitRepo)
298298
if err != nil {
299299
return fmt.Errorf("Commit: %v", err)
300300
}

0 commit comments

Comments
 (0)