summaryrefslogtreecommitdiff
path: root/internal/federation
diff options
context:
space:
mode:
authorLibravatar tobi <31960611+tsmethurst@users.noreply.github.com>2021-12-07 13:31:39 +0100
committerLibravatar GitHub <noreply@github.com>2021-12-07 13:31:39 +0100
commit0884f89431cd26bcc9674b3b7ab628b090f5881e (patch)
treecdd3b3f77f780a8b59d075dbcc3d4d013811e405 /internal/federation
parentUpdate dependencies (#333) (diff)
downloadgotosocial-0884f89431cd26bcc9674b3b7ab628b090f5881e.tar.xz
Implement Cobra CLI tooling, Viper config tooling (#336)
* start pulling out + replacing urfave and config * replace many many instances of config * move more stuff => viper * properly remove urfave * move some flags to root command * add testrig commands to root * alias config file keys * start adding cli parsing tests * reorder viper init * remove config path alias * fmt * change config file keys to non-nested * we're more or less in business now * tidy up the common func * go fmt * get tests passing again * add note about the cliparsing tests * reorganize * update docs with changes * structure cmd dir better * rename + move some files around * fix dangling comma
Diffstat (limited to 'internal/federation')
-rw-r--r--internal/federation/authenticate.go5
-rw-r--r--internal/federation/dereferencing/dereferencer.go5
-rw-r--r--internal/federation/dereferencing/dereferencer_test.go9
-rw-r--r--internal/federation/dereferencing/thread.go14
-rw-r--r--internal/federation/federatingdb/db.go7
-rw-r--r--internal/federation/federatingdb/federatingdb_test.go6
-rw-r--r--internal/federation/federatingdb/owns.go7
-rw-r--r--internal/federation/federatingdb/reject_test.go2
-rw-r--r--internal/federation/federatingdb/update.go5
-rw-r--r--internal/federation/federatingdb/util.go9
-rw-r--r--internal/federation/federator.go7
-rw-r--r--internal/federation/federator_test.go14
12 files changed, 48 insertions, 42 deletions
diff --git a/internal/federation/authenticate.go b/internal/federation/authenticate.go
index fea5a765a..9d715c549 100644
--- a/internal/federation/authenticate.go
+++ b/internal/federation/authenticate.go
@@ -29,11 +29,13 @@ import (
"strings"
"github.com/sirupsen/logrus"
+ "github.com/spf13/viper"
"github.com/go-fed/httpsig"
"github.com/superseriousbusiness/activity/pub"
"github.com/superseriousbusiness/activity/streams"
"github.com/superseriousbusiness/activity/streams/vocab"
+ "github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/util"
@@ -155,7 +157,8 @@ func (f *federator) AuthenticateFederatedRequest(ctx context.Context, requestedU
requestingRemoteAccount := &gtsmodel.Account{}
requestingLocalAccount := &gtsmodel.Account{}
requestingHost := requestingPublicKeyID.Host
- if strings.EqualFold(requestingHost, f.config.Host) {
+ host := viper.GetString(config.Keys.Host)
+ if strings.EqualFold(requestingHost, host) {
// LOCAL ACCOUNT REQUEST
// the request is coming from INSIDE THE HOUSE so skip the remote dereferencing
l.Tracef("proceeding without dereference for local public key %s", requestingPublicKeyID)
diff --git a/internal/federation/dereferencing/dereferencer.go b/internal/federation/dereferencing/dereferencer.go
index 76fd830a2..c105665f3 100644
--- a/internal/federation/dereferencing/dereferencer.go
+++ b/internal/federation/dereferencing/dereferencer.go
@@ -24,7 +24,6 @@ import (
"sync"
"github.com/superseriousbusiness/gotosocial/internal/ap"
- "github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/media"
@@ -82,19 +81,17 @@ type deref struct {
typeConverter typeutils.TypeConverter
transportController transport.Controller
mediaHandler media.Handler
- config *config.Config
handshakes map[string][]*url.URL
handshakeSync *sync.Mutex // mutex to lock/unlock when checking or updating the handshakes map
}
// NewDereferencer returns a Dereferencer initialized with the given parameters.
-func NewDereferencer(config *config.Config, db db.DB, typeConverter typeutils.TypeConverter, transportController transport.Controller, mediaHandler media.Handler) Dereferencer {
+func NewDereferencer(db db.DB, typeConverter typeutils.TypeConverter, transportController transport.Controller, mediaHandler media.Handler) Dereferencer {
return &deref{
db: db,
typeConverter: typeConverter,
transportController: transportController,
mediaHandler: mediaHandler,
- config: config,
handshakeSync: &sync.Mutex{},
}
}
diff --git a/internal/federation/dereferencing/dereferencer_test.go b/internal/federation/dereferencing/dereferencer_test.go
index d4bf3396d..8bcee8615 100644
--- a/internal/federation/dereferencing/dereferencer_test.go
+++ b/internal/federation/dereferencing/dereferencer_test.go
@@ -29,7 +29,6 @@ import (
"github.com/stretchr/testify/suite"
"github.com/superseriousbusiness/activity/streams"
"github.com/superseriousbusiness/activity/streams/vocab"
- "github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/federation/dereferencing"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
@@ -39,7 +38,6 @@ import (
type DereferencerStandardTestSuite struct {
suite.Suite
- config *config.Config
db db.DB
storage *kv.KVStore
@@ -61,11 +59,12 @@ func (suite *DereferencerStandardTestSuite) SetupSuite() {
}
func (suite *DereferencerStandardTestSuite) SetupTest() {
- suite.config = testrig.NewTestConfig()
- suite.db = testrig.NewTestDB()
testrig.InitTestLog()
+ testrig.InitTestConfig()
+
+ suite.db = testrig.NewTestDB()
suite.storage = testrig.NewTestStorage()
- suite.dereferencer = dereferencing.NewDereferencer(suite.config, suite.db, testrig.NewTestTypeConverter(suite.db), suite.mockTransportController(), testrig.NewTestMediaHandler(suite.db, suite.storage))
+ suite.dereferencer = dereferencing.NewDereferencer(suite.db, testrig.NewTestTypeConverter(suite.db), suite.mockTransportController(), testrig.NewTestMediaHandler(suite.db, suite.storage))
testrig.StandardDBSetup(suite.db, nil)
}
diff --git a/internal/federation/dereferencing/thread.go b/internal/federation/dereferencing/thread.go
index db1d336c6..209df32c4 100644
--- a/internal/federation/dereferencing/thread.go
+++ b/internal/federation/dereferencing/thread.go
@@ -24,7 +24,9 @@ import (
"net/url"
"github.com/sirupsen/logrus"
+ "github.com/spf13/viper"
"github.com/superseriousbusiness/gotosocial/internal/ap"
+ "github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/util"
)
@@ -43,7 +45,8 @@ func (d *deref) DereferenceThread(ctx context.Context, username string, statusIR
l.Debug("entering DereferenceThread")
// if it's our status we already have everything stashed so we can bail early
- if statusIRI.Host == d.config.Host {
+ host := viper.GetString(config.Keys.Host)
+ if statusIRI.Host == host {
l.Debug("iri belongs to us, bailing")
return nil
}
@@ -77,7 +80,8 @@ func (d *deref) iterateAncestors(ctx context.Context, username string, statusIRI
l.Debug("entering iterateAncestors")
// if it's our status we don't need to dereference anything so we can immediately move up the chain
- if statusIRI.Host == d.config.Host {
+ host := viper.GetString(config.Keys.Host)
+ if statusIRI.Host == host {
l.Debug("iri belongs to us, moving up to next ancestor")
// since this is our status, we know we can extract the id from the status path
@@ -129,7 +133,8 @@ func (d *deref) iterateDescendants(ctx context.Context, username string, statusI
l.Debug("entering iterateDescendants")
// if it's our status we already have descendants stashed so we can bail early
- if statusIRI.Host == d.config.Host {
+ host := viper.GetString(config.Keys.Host)
+ if statusIRI.Host == host {
l.Debug("iri belongs to us, bailing")
return nil
}
@@ -205,7 +210,8 @@ pageLoop:
continue
}
- if itemURI.Host == d.config.Host {
+ host := viper.GetString(config.Keys.Host)
+ if itemURI.Host == host {
// skip if the reply is from us -- we already have it then
continue
}
diff --git a/internal/federation/federatingdb/db.go b/internal/federation/federatingdb/db.go
index 4b2f92fd4..64e5ad359 100644
--- a/internal/federation/federatingdb/db.go
+++ b/internal/federation/federatingdb/db.go
@@ -25,7 +25,6 @@ import (
"github.com/superseriousbusiness/activity/pub"
"github.com/superseriousbusiness/activity/streams/vocab"
- "github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/typeutils"
)
@@ -46,19 +45,17 @@ type federatingDB struct {
locks map[string]*mutex
pool sync.Pool
db db.DB
- config *config.Config
typeConverter typeutils.TypeConverter
}
// New returns a DB interface using the given database and config
-func New(db db.DB, config *config.Config) DB {
+func New(db db.DB) DB {
fdb := federatingDB{
mutex: sync.Mutex{},
locks: make(map[string]*mutex, 100),
pool: sync.Pool{New: func() interface{} { return &mutex{} }},
db: db,
- config: config,
- typeConverter: typeutils.NewConverter(config, db),
+ typeConverter: typeutils.NewConverter(db),
}
go fdb.cleanupLocks()
return &fdb
diff --git a/internal/federation/federatingdb/federatingdb_test.go b/internal/federation/federatingdb/federatingdb_test.go
index 24cbfed98..d51e0a825 100644
--- a/internal/federation/federatingdb/federatingdb_test.go
+++ b/internal/federation/federatingdb/federatingdb_test.go
@@ -22,7 +22,6 @@ import (
"context"
"github.com/stretchr/testify/suite"
- "github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/federation/federatingdb"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
@@ -34,7 +33,6 @@ import (
type FederatingDBTestSuite struct {
suite.Suite
- config *config.Config
db db.DB
tc typeutils.TypeConverter
federatingDB federatingdb.DB
@@ -59,13 +57,13 @@ func (suite *FederatingDBTestSuite) SetupSuite() {
suite.testAttachments = testrig.NewTestAttachments()
suite.testStatuses = testrig.NewTestStatuses()
suite.testBlocks = testrig.NewTestBlocks()
- suite.testActivities = testrig.NewTestActivities(suite.testAccounts)
}
func (suite *FederatingDBTestSuite) SetupTest() {
testrig.InitTestLog()
- suite.config = testrig.NewTestConfig()
+ testrig.InitTestConfig()
suite.db = testrig.NewTestDB()
+ suite.testActivities = testrig.NewTestActivities(suite.testAccounts)
suite.tc = testrig.NewTestTypeConverter(suite.db)
suite.federatingDB = testrig.NewTestFederatingDB(suite.db)
testrig.StandardDBSetup(suite.db, suite.testAccounts)
diff --git a/internal/federation/federatingdb/owns.go b/internal/federation/federatingdb/owns.go
index 8846c52bb..2603c9aa2 100644
--- a/internal/federation/federatingdb/owns.go
+++ b/internal/federation/federatingdb/owns.go
@@ -24,6 +24,8 @@ import (
"net/url"
"github.com/sirupsen/logrus"
+ "github.com/spf13/viper"
+ "github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/util"
@@ -42,8 +44,9 @@ func (f *federatingDB) Owns(ctx context.Context, id *url.URL) (bool, error) {
l.Debug("entering Owns")
// if the id host isn't this instance host, we don't own this IRI
- if id.Host != f.config.Host {
- l.Tracef("we DO NOT own activity because the host is %s not %s", id.Host, f.config.Host)
+ host := viper.GetString(config.Keys.Host)
+ if id.Host != host {
+ l.Tracef("we DO NOT own activity because the host is %s not %s", id.Host, host)
return false, nil
}
diff --git a/internal/federation/federatingdb/reject_test.go b/internal/federation/federatingdb/reject_test.go
index 2b213a3f0..9930d83d2 100644
--- a/internal/federation/federatingdb/reject_test.go
+++ b/internal/federation/federatingdb/reject_test.go
@@ -48,7 +48,7 @@ func (suite *RejectTestSuite) TestRejectFollowRequest() {
ID: "01FJ1S8DX3STJJ6CEYPMZ1M0R3",
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
- URI: util.GenerateURIForFollow(followingAccount.Username, "http", "localhost:8080", "01FJ1S8DX3STJJ6CEYPMZ1M0R3"),
+ URI: util.GenerateURIForFollow(followingAccount.Username, "01FJ1S8DX3STJJ6CEYPMZ1M0R3"),
AccountID: followingAccount.ID,
TargetAccountID: followedAccount.ID,
}
diff --git a/internal/federation/federatingdb/update.go b/internal/federation/federatingdb/update.go
index 8f318ce71..1d56b931f 100644
--- a/internal/federation/federatingdb/update.go
+++ b/internal/federation/federatingdb/update.go
@@ -24,8 +24,10 @@ import (
"fmt"
"github.com/sirupsen/logrus"
+ "github.com/spf13/viper"
"github.com/superseriousbusiness/activity/streams/vocab"
"github.com/superseriousbusiness/gotosocial/internal/ap"
+ "github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/messages"
"github.com/superseriousbusiness/gotosocial/internal/util"
@@ -124,7 +126,8 @@ func (f *federatingDB) Update(ctx context.Context, asType vocab.Type) error {
return fmt.Errorf("UPDATE: error converting to account: %s", err)
}
- if updatedAcct.Domain == f.config.Host {
+ host := viper.GetString(config.Keys.Host)
+ if updatedAcct.Domain == host {
// no need to update local accounts
// in fact, if we do this will break the shit out of things so do NOT
return nil
diff --git a/internal/federation/federatingdb/util.go b/internal/federation/federatingdb/util.go
index f35fbbb2d..afa09e39d 100644
--- a/internal/federation/federatingdb/util.go
+++ b/internal/federation/federatingdb/util.go
@@ -26,9 +26,11 @@ import (
"net/url"
"github.com/sirupsen/logrus"
+ "github.com/spf13/viper"
"github.com/superseriousbusiness/activity/streams"
"github.com/superseriousbusiness/activity/streams/vocab"
"github.com/superseriousbusiness/gotosocial/internal/ap"
+ "github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/id"
@@ -104,7 +106,7 @@ func (f *federatingDB) NewID(ctx context.Context, t vocab.Type) (idURL *url.URL,
if err != nil {
return nil, err
}
- return url.Parse(util.GenerateURIForFollow(actorAccount.Username, f.config.Protocol, f.config.Host, newID))
+ return url.Parse(util.GenerateURIForFollow(actorAccount.Username, newID))
}
}
}
@@ -207,7 +209,10 @@ func (f *federatingDB) NewID(ctx context.Context, t vocab.Type) (idURL *url.URL,
if err != nil {
return nil, err
}
- return url.Parse(fmt.Sprintf("%s://%s/%s", f.config.Protocol, f.config.Host, newID))
+
+ protocol := viper.GetString(config.Keys.Protocol)
+ host := viper.GetString(config.Keys.Host)
+ return url.Parse(fmt.Sprintf("%s://%s/%s", protocol, host, newID))
}
// ActorForOutbox fetches the actor's IRI for the given outbox IRI.
diff --git a/internal/federation/federator.go b/internal/federation/federator.go
index 280375d7d..8ccae9427 100644
--- a/internal/federation/federator.go
+++ b/internal/federation/federator.go
@@ -24,7 +24,6 @@ import (
"github.com/superseriousbusiness/activity/pub"
"github.com/superseriousbusiness/gotosocial/internal/ap"
- "github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/federation/dereferencing"
"github.com/superseriousbusiness/gotosocial/internal/federation/federatingdb"
@@ -73,7 +72,6 @@ type Federator interface {
}
type federator struct {
- config *config.Config
db db.DB
federatingDB federatingdb.DB
clock pub.Clock
@@ -85,12 +83,11 @@ type federator struct {
}
// NewFederator returns a new federator
-func NewFederator(db db.DB, federatingDB federatingdb.DB, transportController transport.Controller, config *config.Config, typeConverter typeutils.TypeConverter, mediaHandler media.Handler) Federator {
- dereferencer := dereferencing.NewDereferencer(config, db, typeConverter, transportController, mediaHandler)
+func NewFederator(db db.DB, federatingDB federatingdb.DB, transportController transport.Controller, typeConverter typeutils.TypeConverter, mediaHandler media.Handler) Federator {
+ dereferencer := dereferencing.NewDereferencer(db, typeConverter, transportController, mediaHandler)
clock := &Clock{}
f := &federator{
- config: config,
db: db,
federatingDB: federatingDB,
clock: &Clock{},
diff --git a/internal/federation/federator_test.go b/internal/federation/federator_test.go
index 7ac149c0d..f907ac00b 100644
--- a/internal/federation/federator_test.go
+++ b/internal/federation/federator_test.go
@@ -30,7 +30,6 @@ import (
"github.com/stretchr/testify/suite"
"github.com/superseriousbusiness/activity/pub"
- "github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/federation"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
@@ -41,7 +40,6 @@ import (
type ProtocolTestSuite struct {
suite.Suite
- config *config.Config
db db.DB
storage *kv.KVStore
typeConverter typeutils.TypeConverter
@@ -52,16 +50,16 @@ type ProtocolTestSuite struct {
// SetupSuite sets some variables on the suite that we can use as consts (more or less) throughout
func (suite *ProtocolTestSuite) SetupSuite() {
// setup standard items
- suite.config = testrig.NewTestConfig()
- suite.db = testrig.NewTestDB()
- testrig.InitTestLog()
suite.storage = testrig.NewTestStorage()
suite.typeConverter = testrig.NewTestTypeConverter(suite.db)
suite.accounts = testrig.NewTestAccounts()
- suite.activities = testrig.NewTestActivities(suite.accounts)
}
func (suite *ProtocolTestSuite) SetupTest() {
+ testrig.InitTestLog()
+ testrig.InitTestConfig()
+ suite.db = testrig.NewTestDB()
+ suite.activities = testrig.NewTestActivities(suite.accounts)
testrig.StandardDBSetup(suite.db, suite.accounts)
}
@@ -80,7 +78,7 @@ func (suite *ProtocolTestSuite) TestPostInboxRequestBodyHook() {
return nil, nil
}), suite.db)
// setup module being tested
- federator := federation.NewFederator(suite.db, testrig.NewTestFederatingDB(suite.db), tc, suite.config, suite.typeConverter, testrig.NewTestMediaHandler(suite.db, suite.storage))
+ federator := federation.NewFederator(suite.db, testrig.NewTestFederatingDB(suite.db), tc, suite.typeConverter, testrig.NewTestMediaHandler(suite.db, suite.storage))
// setup request
ctx := context.Background()
@@ -109,7 +107,7 @@ func (suite *ProtocolTestSuite) TestAuthenticatePostInbox() {
tc := testrig.NewTestTransportController(testrig.NewMockHTTPClient(nil), suite.db)
// now setup module being tested, with the mock transport controller
- federator := federation.NewFederator(suite.db, testrig.NewTestFederatingDB(suite.db), tc, suite.config, suite.typeConverter, testrig.NewTestMediaHandler(suite.db, suite.storage))
+ federator := federation.NewFederator(suite.db, testrig.NewTestFederatingDB(suite.db), tc, suite.typeConverter, testrig.NewTestMediaHandler(suite.db, suite.storage))
request := httptest.NewRequest(http.MethodPost, "http://localhost:8080/users/the_mighty_zork/inbox", nil)
// we need these headers for the request to be validated