mirror of
1
Fork 0

implement storage of federated_repo

This commit is contained in:
Michael Jerger 2024-03-22 20:14:20 +01:00
parent 6b767684a7
commit 950dcd959e
3 changed files with 73 additions and 63 deletions

View File

@ -16,6 +16,7 @@ func init() {
db.RegisterModel(new(FederatedRepo)) db.RegisterModel(new(FederatedRepo))
} }
// TODO: do we need this?
func GetFederatedRepo(ctx context.Context, ID int64) (*FederatedRepo, error) { func GetFederatedRepo(ctx context.Context, ID int64) (*FederatedRepo, error) {
repo := new(FederatedRepo) repo := new(FederatedRepo)
has, err := db.GetEngine(ctx).Where("id=?", ID).Get(repo) has, err := db.GetEngine(ctx).Where("id=?", ID).Get(repo)
@ -30,6 +31,7 @@ func GetFederatedRepo(ctx context.Context, ID int64) (*FederatedRepo, error) {
return repo, nil return repo, nil
} }
// TODO: do we need this?
func FindFederatedRepoByFQDN(ctx context.Context, fqdn string) (*FederatedRepo, error) { func FindFederatedRepoByFQDN(ctx context.Context, fqdn string) (*FederatedRepo, error) {
repo := new(FederatedRepo) repo := new(FederatedRepo)
has, err := db.GetEngine(ctx).Where("host_fqdn=?", strings.ToLower(fqdn)).Get(repo) has, err := db.GetEngine(ctx).Where("host_fqdn=?", strings.ToLower(fqdn)).Get(repo)
@ -44,6 +46,7 @@ func FindFederatedRepoByFQDN(ctx context.Context, fqdn string) (*FederatedRepo,
return repo, nil return repo, nil
} }
// TODO: do we need this?
func CreateFederatedRepo(ctx context.Context, repo *FederatedRepo) error { func CreateFederatedRepo(ctx context.Context, repo *FederatedRepo) error {
if res, err := validation.IsValid(repo); !res { if res, err := validation.IsValid(repo); !res {
return fmt.Errorf("FederationInfo is not valid: %v", err) return fmt.Errorf("FederationInfo is not valid: %v", err)
@ -52,10 +55,31 @@ func CreateFederatedRepo(ctx context.Context, repo *FederatedRepo) error {
return err return err
} }
func UpdateFederatedRepo(ctx context.Context, repo *FederatedRepo) error { func UpdateFederatedRepo(ctx context.Context, localRepoId int64, federatedRepoList []*FederatedRepo) error {
if res, err := validation.IsValid(repo); !res { for _, federatedRepo := range federatedRepoList {
if res, err := validation.IsValid(federatedRepo); !res {
return fmt.Errorf("FederationInfo is not valid: %v", err) return fmt.Errorf("FederationInfo is not valid: %v", err)
} }
_, err := db.GetEngine(ctx).ID(repo.ID).Update(repo) }
// Begin transaction
ctx, committer, err := db.TxContext((ctx))
if err != nil {
return err return err
}
defer committer.Close()
_, err = db.GetEngine(ctx).Where("repo_id=?", localRepoId).Delete(FederatedRepo{})
if err != nil {
return err
}
for _, federatedRepo := range federatedRepoList {
_, err = db.GetEngine(ctx).Insert(federatedRepo)
if err != nil {
return err
}
}
// Commit transaction
return committer.Commit()
} }

View File

@ -30,7 +30,7 @@ import (
// Validation of incoming RepositoryID against Local RepositoryID // Validation of incoming RepositoryID against Local RepositoryID
// Star the repo if it wasn't already stared // Star the repo if it wasn't already stared
// Do some mitigation against out of order attacks // Do some mitigation against out of order attacks
func LikeActivity(ctx *context.APIContext, form any, repositoryID int64) (int, string, error) { func LikeActivity(ctx *context.Context, form any, repositoryID int64) (int, string, error) {
activity := form.(*forgefed.ForgeLike) activity := form.(*forgefed.ForgeLike)
if res, err := validation.IsValid(activity); !res { if res, err := validation.IsValid(activity); !res {
return http.StatusNotAcceptable, "Invalid activity", err return http.StatusNotAcceptable, "Invalid activity", err
@ -39,20 +39,9 @@ func LikeActivity(ctx *context.APIContext, form any, repositoryID int64) (int, s
// parse actorID (person) // parse actorID (person)
actorURI := activity.Actor.GetID().String() actorURI := activity.Actor.GetID().String()
rawActorID, err := forgefed.NewActorID(actorURI) federationHost, err := GetFederationHostForUri(ctx, actorURI)
if err != nil { if err != nil {
return http.StatusInternalServerError, "Invalid ActorID", err return http.StatusInternalServerError, "Wrong FederationHost", err
}
federationHost, err := forgefed.FindFederationHostByFqdn(ctx, rawActorID.Host)
if err != nil {
return http.StatusInternalServerError, "Could not load FederationHost", err
}
if federationHost == nil {
result, err := CreateFederationHostFromAP(ctx, rawActorID)
if err != nil {
return http.StatusNotAcceptable, "Invalid FederationHost", err
}
federationHost = result
} }
if !activity.IsNewer(federationHost.LatestActivity) { if !activity.IsNewer(federationHost.LatestActivity) {
return http.StatusNotAcceptable, "Activity out of order.", fmt.Errorf("Activity already processed") return http.StatusNotAcceptable, "Activity out of order.", fmt.Errorf("Activity already processed")
@ -106,7 +95,7 @@ func LikeActivity(ctx *context.APIContext, form any, repositoryID int64) (int, s
return 0, "", nil return 0, "", nil
} }
func CreateFederationHostFromAP(ctx *context.APIContext, actorID forgefed.ActorID) (*forgefed.FederationHost, error) { func CreateFederationHostFromAP(ctx *context.Context, actorID forgefed.ActorID) (*forgefed.FederationHost, error) {
actionsUser := user.NewActionsUser() actionsUser := user.NewActionsUser()
client, err := activitypub.NewClient(ctx, actionsUser, "no idea where to get key material.") client, err := activitypub.NewClient(ctx, actionsUser, "no idea where to get key material.")
if err != nil { if err != nil {
@ -139,7 +128,27 @@ func CreateFederationHostFromAP(ctx *context.APIContext, actorID forgefed.ActorI
return &result, nil return &result, nil
} }
func CreateUserFromAP(ctx *context.APIContext, personID forgefed.PersonID, federationHostID int64) (*user.User, *user.FederatedUser, error) { func GetFederationHostForUri(ctx *context.Context, actorURI string) (*forgefed.FederationHost, error) {
// parse actorID (person)
rawActorID, err := forgefed.NewActorID(actorURI)
if err != nil {
return nil, err
}
federationHost, err := forgefed.FindFederationHostByFqdn(ctx, rawActorID.Host)
if err != nil {
return nil, err
}
if federationHost == nil {
result, err := CreateFederationHostFromAP(ctx, rawActorID)
if err != nil {
return nil, err
}
federationHost = result
}
return federationHost, nil
}
func CreateUserFromAP(ctx *context.Context, personID forgefed.PersonID, federationHostID int64) (*user.User, *user.FederatedUser, error) {
// ToDo: Do we get a publicKeyId from server, repo or owner or repo? // ToDo: Do we get a publicKeyId from server, repo or owner or repo?
actionsUser := user.NewActionsUser() actionsUser := user.NewActionsUser()
client, err := activitypub.NewClient(ctx, actionsUser, "no idea where to get key material.") client, err := activitypub.NewClient(ctx, actionsUser, "no idea where to get key material.")
@ -202,34 +211,26 @@ func CreateUserFromAP(ctx *context.APIContext, personID forgefed.PersonID, feder
return &newUser, &federatedUser, nil return &newUser, &federatedUser, nil
} }
func CreateFederatedRepo(ctx *context.APIContext, federatedRepoID forgefed.RepositoryID, federationHostID int64) (repo.FederatedRepo, error) { // Create or update a list of FederatedRepo structs
// ToDo func UpdateFederatedRepoList(ctx *context.Context, localRepoId int64, federatedRepoList []string) (int, string, error) {
// Note: We may want to discuss about side effects here federatedRepos := make([]*repo.FederatedRepo, len(federatedRepoList))
} for _, uri := range federatedRepoList {
federationHost, err := GetFederationHostForUri(ctx, uri)
// Create a list of FederatedRepo structs if err != nil {
func CreateFederadedRepoList(ctx *context.APIContext, repoList []string, localRepoId int64) ([]repo.FederatedRepo, error) { return http.StatusInternalServerError, "Wrong FederationHost", err
}
federatedRepos := make([]repo.FederatedRepo, len(repoList)) federatedRepoID, err := forgefed.NewRepositoryID(uri, string(federationHost.NodeInfo.Source))
for _, uri := range repoList { if err != nil {
return http.StatusNotAcceptable, "Invalid federated repo", err
federatedRepoID, err := forgefed.NewRepositoryID(uri, "forgejo") // ToDo: Don't hardcode this but where do we get this from }
if err != nil { federatedRepo, err := repo.NewFederatedRepo(localRepoId, federatedRepoID.ID, federationHost.ID)
return make([]repo.FederatedRepo, 0), err if err != nil {
} return http.StatusNotAcceptable, "Invalid federated repo", err
}
federationHost, err := forgefed.FindFederationHostByFqdn(ctx, federatedRepoID.Host) federatedRepos = append(federatedRepos, &federatedRepo)
if err != nil { }
return make([]repo.FederatedRepo, 0), err
} repo.UpdateFederatedRepo(ctx, localRepoId, federatedRepos)
federatedRepo, err := CreateFederatedRepo(ctx, federatedRepoID, federationHost.ID) return 0, "", nil
if err != nil {
return make([]repo.FederatedRepo, 0), err
}
federatedRepos = append(federatedRepos, federatedRepo)
}
return federatedRepos, nil
} }

View File

@ -20,6 +20,7 @@ import (
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/forgefed"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/indexer/code" "code.gitea.io/gitea/modules/indexer/code"
"code.gitea.io/gitea/modules/indexer/stats" "code.gitea.io/gitea/modules/indexer/stats"
@ -192,33 +193,17 @@ func SettingsPost(ctx *context.Context) {
return return
} }
// Create Federated Repo Structs & commit to DB federationRepos := form.FederationRepos
// ToDo: Implement in federation_service.go
// ToDo: validation.ValidateMaxLen() seems not to be suited for this case. We may need to approach differently here
// ToDo: Use Federated Repo Struct & Update Federated Repo Table errs := validation.ValidateMaxLen(federationRepos, 2048, "federationRepos")
// TODO: move as much functions to some kind of service in order to keep controller clean an simple if len(errs) > 0 {
switch {
// Allow clearing the field
case form.FederationRepos == "":
repo.FederationRepos = ""
// Validate
case !validation.IsOfValidLength(form.FederationRepos): // ToDo: Use for public testing only. In production we might need longer strings.
ctx.Data["ERR_FederationRepos"] = true ctx.Data["ERR_FederationRepos"] = true
ctx.Flash.Error("The given string was larger than 2048 bytes") ctx.Flash.Error("The given string was larger than 2048 bytes")
ctx.Redirect(repo.Link() + "/settings") ctx.Redirect(repo.Link() + "/settings")
return return
case validation.IsValidFederatedRepoURL(form.FederationRepos):
// TODO: Move this validation to Domain!!
repo.FederationRepos = form.FederationRepos
default:
ctx.Data["ERR_FederationRepos"] = true
ctx.Flash.Error("The given Repo URL was not valid")
ctx.Redirect(repo.Link() + "/settings")
return
} }
if err := repo_service.UpdateRepository(ctx, repo, false); err != nil { if _, _, err := forgefed.UpdateFederatedRepoList(ctx, ctx.Repo.Repository.ID, strings.Split(federationRepos, ";")); err != nil {
ctx.ServerError("UpdateRepository", err) ctx.ServerError("UpdateRepository", err)
return return
} }