Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion cmd/frontend/auth/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//cmd/frontend/backend",
"//cmd/frontend/globals",
"//cmd/frontend/internal/app/router",
"//cmd/frontend/internal/app/ui/router",
"//internal/actor",
Expand Down
11 changes: 9 additions & 2 deletions cmd/frontend/auth/reset_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package auth

import (
"context"
"net/url"

"github.com/sourcegraph/log"

"github.com/sourcegraph/sourcegraph/cmd/frontend/backend"
"github.com/sourcegraph/sourcegraph/cmd/frontend/globals"
"github.com/sourcegraph/sourcegraph/internal/auth/userpasswd"
"github.com/sourcegraph/sourcegraph/internal/conf"
"github.com/sourcegraph/sourcegraph/internal/database"
Expand Down Expand Up @@ -41,6 +41,13 @@ func ResetPasswordURL(ctx context.Context, db database.DB, logger log.Logger, us
return nil, errors.Wrap(err, msg)
}

ru := globals.ExternalURL().ResolveReference(resetURL).String()
externalURL, err := url.Parse(conf.Get().ExternalURL)
if err != nil {
msg := "failed to parse External URL"
logger.Error(msg, log.Error(err))
return nil, errors.Wrap(err, msg)
}

ru := externalURL.ResolveReference(resetURL).String()
return &ru, nil
}
1 change: 0 additions & 1 deletion cmd/frontend/backend/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//cmd/frontend/envvar",
"//cmd/frontend/globals",
"//cmd/frontend/internal/app/router",
"//internal/actor",
"//internal/api",
Expand Down
25 changes: 20 additions & 5 deletions cmd/frontend/backend/user_emails.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/sourcegraph/log"

"github.com/sourcegraph/sourcegraph/cmd/frontend/envvar"
"github.com/sourcegraph/sourcegraph/cmd/frontend/globals"
"github.com/sourcegraph/sourcegraph/cmd/frontend/internal/app/router"
"github.com/sourcegraph/sourcegraph/internal/actor"
"github.com/sourcegraph/sourcegraph/internal/auth"
Expand Down Expand Up @@ -330,6 +329,11 @@ func (e *userEmails) SendUserEmailOnFieldUpdate(ctx context.Context, id int32, c
return err
}

externalURL, err := url.Parse(conf.Get().ExternalURL)
if err != nil {
return err
}

return txemail.Send(ctx, "user_account_update", txemail.Message{
To: []string{email},
Template: updateAccountEmailTemplate,
Expand All @@ -342,7 +346,7 @@ func (e *userEmails) SendUserEmailOnFieldUpdate(ctx context.Context, id int32, c
Email: email,
Change: change,
Username: usr.Username,
Host: globals.ExternalURL().Host,
Host: externalURL.Host,
},
})
}
Expand Down Expand Up @@ -371,6 +375,11 @@ func (e *userEmails) SendUserEmailOnAccessTokenChange(ctx context.Context, id in
return err
}

externalURL, err := url.Parse(conf.Get().ExternalURL)
if err != nil {
return err
}

var tmpl txtypes.Templates
if deleted {
tmpl = accessTokenDeletedEmailTemplate
Expand All @@ -389,7 +398,7 @@ func (e *userEmails) SendUserEmailOnAccessTokenChange(ctx context.Context, id in
Email: email,
TokenName: tokenName,
Username: usr.Username,
Host: globals.ExternalURL().Host,
Host: externalURL.Host,
},
})
}
Expand Down Expand Up @@ -522,6 +531,12 @@ func SendUserEmailVerificationEmail(ctx context.Context, username, email, code s
q.Set("code", code)
q.Set("email", email)
verifyEmailPath, _ := router.Router().Get(router.VerifyEmail).URLPath()

externalURL, err := url.Parse(conf.Get().ExternalURL)
if err != nil {
return err
}

return txemail.Send(ctx, "user_email_verification", txemail.Message{
To: []string{email},
Template: verifyEmailTemplates,
Expand All @@ -531,11 +546,11 @@ func SendUserEmailVerificationEmail(ctx context.Context, username, email, code s
Host string
}{
Username: username,
URL: globals.ExternalURL().ResolveReference(&url.URL{
URL: externalURL.ResolveReference(&url.URL{
Path: verifyEmailPath.Path,
RawQuery: q.Encode(),
}).String(),
Host: globals.ExternalURL().Host,
Host: externalURL.Host,
},
})
}
Expand Down
21 changes: 21 additions & 0 deletions cmd/frontend/backend/user_emails_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ func TestCheckEmailAbuse(t *testing.T) {
}

func TestSendUserEmailVerificationEmail(t *testing.T) {
conf.Mock(&conf.Unified{
SiteConfiguration: schema.SiteConfiguration{
ExternalURL: "http://example.com",
},
})
defer conf.Mock(nil)

var sent *txemail.Message
txemail.MockSend = func(ctx context.Context, message txemail.Message) error {
sent = &message
Expand Down Expand Up @@ -177,6 +184,13 @@ func TestSendUserEmailOnFieldUpdate(t *testing.T) {
}
defer func() { txemail.MockSend = nil }()

conf.Mock(&conf.Unified{
SiteConfiguration: schema.SiteConfiguration{
ExternalURL: "http://example.com",
},
})
defer conf.Mock(nil)

userEmails := dbmocks.NewMockUserEmailsStore()
userEmails.GetPrimaryEmailFunc.SetDefaultReturn("a@example.com", true, nil)

Expand Down Expand Up @@ -225,6 +239,13 @@ func TestSendUserEmailOnTokenChange(t *testing.T) {
}
defer func() { txemail.MockSend = nil }()

conf.Mock(&conf.Unified{
SiteConfiguration: schema.SiteConfiguration{
ExternalURL: "http://example.com",
},
})
defer conf.Mock(nil)

userEmails := dbmocks.NewMockUserEmailsStore()
userEmails.GetPrimaryEmailFunc.SetDefaultReturn("a@example.com", true, nil)

Expand Down
9 changes: 0 additions & 9 deletions cmd/frontend/external/globals/BUILD.bazel

This file was deleted.

13 changes: 0 additions & 13 deletions cmd/frontend/external/globals/globals.go

This file was deleted.

54 changes: 0 additions & 54 deletions cmd/frontend/globals/globals.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
package globals

import (
"net/url"
"reflect"
"sync"
"sync/atomic"
Expand All @@ -13,59 +12,6 @@ import (
"github.com/sourcegraph/sourcegraph/schema"
)

var defaultExternalURL = &url.URL{
Scheme: "http",
Host: "example.com",
}

var externalURL = func() atomic.Value {
var v atomic.Value
v.Store(defaultExternalURL)
return v
}()

var watchExternalURLOnce sync.Once

// WatchExternalURL watches for changes in the `externalURL` site configuration
// so that changes are reflected in what is returned by the ExternalURL function.
func WatchExternalURL() {
watchExternalURLOnce.Do(func() {
conf.Watch(func() {
after := defaultExternalURL
if val := conf.Get().ExternalURL; val != "" {
var err error
if after, err = url.Parse(val); err != nil {
log15.Error("globals.ExternalURL", "value", val, "error", err)
return
}
}

if before := ExternalURL(); !reflect.DeepEqual(before, after) {
SetExternalURL(after)
if before.Host != "example.com" {
log15.Info(
"globals.ExternalURL",
"updated", true,
"before", before,
"after", after,
)
}
}
})
})
}

// ExternalURL returns the fully-resolved, externally accessible frontend URL.
// Callers must not mutate the returned pointer.
func ExternalURL() *url.URL {
return externalURL.Load().(*url.URL)
}

// SetExternalURL sets the fully-resolved, externally accessible frontend URL.
func SetExternalURL(u *url.URL) {
externalURL.Store(u)
}

var defaultPermissionsUserMapping = &schema.PermissionsUserMapping{
Enabled: false,
BindID: "email",
Expand Down
12 changes: 10 additions & 2 deletions cmd/frontend/graphqlbackend/email_invitation.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"github.com/sourcegraph/sourcegraph/lib/errors"

"github.com/sourcegraph/sourcegraph/cmd/frontend/envvar"
"github.com/sourcegraph/sourcegraph/cmd/frontend/globals"
"github.com/sourcegraph/sourcegraph/internal/actor"
"github.com/sourcegraph/sourcegraph/internal/conf"
"github.com/sourcegraph/sourcegraph/internal/env"
"github.com/sourcegraph/sourcegraph/internal/goroutine"
"github.com/sourcegraph/sourcegraph/internal/rcache"
Expand Down Expand Up @@ -68,6 +68,14 @@ func (r *schemaResolver) InviteEmailToSourcegraph(ctx context.Context, args *str
}

urlSignUp, _ := url.Parse("/sign-up?invitedBy=" + invitedBy.Username)

externalURL, err := url.Parse(conf.Get().ExternalURL)
if err != nil {
log15.Warn("email invites: failed parse external url", "error", err)
invitedEmailsLimiter.Delete(args.Email) // allow attempting to invite this email again without waiting 24h
return
}

if err := txemail.Send(ctx, "user_invite", txemail.Message{
To: []string{args.Email},
Template: emailTemplateEmailInvitation,
Expand All @@ -76,7 +84,7 @@ func (r *schemaResolver) InviteEmailToSourcegraph(ctx context.Context, args *str
URL string
}{
FromName: invitedBy.Username,
URL: globals.ExternalURL().ResolveReference(urlSignUp).String(),
URL: externalURL.ResolveReference(urlSignUp).String(),
},
}); err != nil {
log15.Warn("email invites: failed to send email", "error", err)
Expand Down
9 changes: 7 additions & 2 deletions cmd/frontend/graphqlbackend/git_tree_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import (
"github.com/inconshreveable/log15"
"go.opentelemetry.io/otel/attribute"

"github.com/sourcegraph/sourcegraph/cmd/frontend/globals"
"github.com/sourcegraph/sourcegraph/cmd/frontend/graphqlbackend/externallink"
"github.com/sourcegraph/sourcegraph/internal/api"
"github.com/sourcegraph/sourcegraph/internal/authz"
"github.com/sourcegraph/sourcegraph/internal/binary"
"github.com/sourcegraph/sourcegraph/internal/cloneurls"
resolverstubs "github.com/sourcegraph/sourcegraph/internal/codeintel/resolvers"
"github.com/sourcegraph/sourcegraph/internal/conf"
"github.com/sourcegraph/sourcegraph/internal/database"
"github.com/sourcegraph/sourcegraph/internal/gitserver"
"github.com/sourcegraph/sourcegraph/internal/gitserver/gitdomain"
Expand Down Expand Up @@ -280,7 +280,12 @@ func (r *GitTreeEntryResolver) ExternalURLs(ctx context.Context) ([]*externallin
}

func (r *GitTreeEntryResolver) RawZipArchiveURL() string {
return globals.ExternalURL().ResolveReference(&url.URL{
externalURL, err := url.Parse(conf.Get().ExternalURL)
if err != nil {
return ""
}

return externalURL.ResolveReference(&url.URL{
Path: path.Join(r.Repository().URL(), "-/raw/", r.Path()),
RawQuery: "format=zip",
}).String()
Expand Down
13 changes: 13 additions & 0 deletions cmd/frontend/graphqlbackend/git_tree_entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import (
"testing"

"github.com/google/go-cmp/cmp"

"github.com/sourcegraph/sourcegraph/internal/authz"
"github.com/sourcegraph/sourcegraph/internal/conf"
"github.com/sourcegraph/sourcegraph/internal/database/dbmocks"
"github.com/sourcegraph/sourcegraph/schema"

"github.com/sourcegraph/sourcegraph/internal/api"
"github.com/sourcegraph/sourcegraph/internal/gitserver"
Expand All @@ -23,6 +26,11 @@ func TestGitTreeEntry_RawZipArchiveURL(t *testing.T) {
},
Stat: CreateFileInfo("a/b", true),
}
conf.Mock(&conf.Unified{
SiteConfiguration: schema.SiteConfiguration{
ExternalURL: "http://example.com",
},
})
got := NewGitTreeEntryResolver(db, gitserverClient, opts).RawZipArchiveURL()
want := "http://example.com/my/repo/-/raw/a/b?format=zip"
if got != want {
Expand All @@ -36,6 +44,11 @@ func TestGitTreeEntry_Content(t *testing.T) {

db := dbmocks.NewMockDB()
gitserverClient := gitserver.NewMockClient()
conf.Mock(&conf.Unified{
SiteConfiguration: schema.SiteConfiguration{
ExternalURL: "http://example.com",
},
})

gitserverClient.ReadFileFunc.SetDefaultHook(func(_ context.Context, _ authz.SubRepoPermissionChecker, _ api.RepoName, _ api.CommitID, name string) ([]byte, error) {
if name != wantPath {
Expand Down
17 changes: 13 additions & 4 deletions cmd/frontend/graphqlbackend/org_invitations.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/graph-gophers/graphql-go/relay"

"github.com/sourcegraph/sourcegraph/cmd/frontend/envvar"
"github.com/sourcegraph/sourcegraph/cmd/frontend/globals"
sgactor "github.com/sourcegraph/sourcegraph/internal/actor"
"github.com/sourcegraph/sourcegraph/internal/auth"
"github.com/sourcegraph/sourcegraph/internal/authz/permssync"
Expand Down Expand Up @@ -453,7 +452,12 @@ func orgInvitationURLLegacy(org *types.Org, relative bool) string {
if relative {
return path
}
return globals.ExternalURL().ResolveReference(&url.URL{Path: path}).String()
externalURL, err := url.Parse(conf.Get().ExternalURL)
if err != nil {
return ""
}

return externalURL.ResolveReference(&url.URL{Path: path}).String()
}

func orgInvitationURL(invitation database.OrgInvitation, relative bool) (string, error) {
Expand All @@ -468,7 +472,12 @@ func orgInvitationURL(invitation database.OrgInvitation, relative bool) (string,
if relative {
return path, nil
}
return globals.ExternalURL().ResolveReference(&url.URL{Path: path}).String(), nil
externalURL, err := url.Parse(conf.Get().ExternalURL)
if err != nil {
return "", err
}

return externalURL.ResolveReference(&url.URL{Path: path}).String(), nil
}

func createInvitationJWT(orgID int32, invitationID int64, senderID int32, expiryTime time.Time) (string, error) {
Expand All @@ -479,7 +488,7 @@ func createInvitationJWT(orgID int32, invitationID int64, senderID int32, expiry

token := jwt.NewWithClaims(jwt.SigningMethodHS512, &orgInvitationClaims{
RegisteredClaims: jwt.RegisteredClaims{
Issuer: globals.ExternalURL().String(),
Issuer: conf.Get().ExternalURL,
ExpiresAt: jwt.NewNumericDate(expiryTime),
Subject: strconv.FormatInt(int64(orgID), 10),
},
Expand Down
Loading