Files
ocis/services/proxy/pkg/middleware/create_home.go
T
Roman PerekhodandMartin 39e3d33820 [full-ci] feat: ocisdev-533 graph (#12108)
* feat: Implemet the vault-storage

feat: Separate the storage-users and graph to handle

feat: move the space create cache

configurate the vault srorage Postprocessing

hide the assignment of the MountID to VaultStorageProviderID behind a flag to avoid the ID mismatch

configurate the proxy to create the vault home space

update the filter

Make the graph able to handle vault prefix. no extra service needed

apply the vault filter to sharedbyme and sharedwithme

added vault-storage docker

use squashed reva

fix the finishUpload event

restict webdav copy/move from the vault

feat: provide the mfa to webdav and grpc

feat: refactoring: provide the mfa to webdav and grpc

mfa collaboration

fix GetRootDriveChildren and more

fix: send the MFA header in HTTP clients from the collaboration service

provide the default storage user vault configuration

update create_home middleware

Update deployments/examples/ocis_full/vault-storage.yml

Co-authored-by: Martin <github@diemattels.at>

Update services/gateway/pkg/config/config.go

Co-authored-by: Martin <github@diemattels.at>

Update services/graph/pkg/config/config.go

Co-authored-by: Martin <github@diemattels.at>

Update services/graph/pkg/config/config.go

Co-authored-by: Martin <github@diemattels.at>

Update services/storage-users/pkg/config/config.go

Co-authored-by: Martin <github@diemattels.at>

Update services/proxy/pkg/config/config.go

Co-authored-by: Martin <github@diemattels.at>

Update services/storage-users/pkg/config/config.go

Co-authored-by: Martin <github@diemattels.at>

Update deployments/examples/ocis_full/vault-storage.yml

Co-authored-by: Martin <github@diemattels.at>

bump reva

deduplicate the requireMFA middeware on vault routes. fix AppendPlainToOpaque.

added the changlog and comments

* feat: add the vault mode search

* Update changelog/unreleased/enhancement-vault-storage.md

Co-authored-by: Martin <github@diemattels.at>

* update the readme. the reva vault storage provider configuration disabled by default

* feat: [OCISDEV-531] add vault capabilities to the OCS capabilities endpoint

* the port range allocated. the configuration updated.

---------

Co-authored-by: Martin <github@diemattels.at>
2026-05-15 16:53:05 +02:00

159 lines
5.7 KiB
Go

package middleware
import (
"net/http"
"strconv"
"sync"
gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
userv1beta1 "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
"github.com/owncloud/ocis/v2/ocis-pkg/log"
"github.com/owncloud/ocis/v2/ocis-pkg/mfa"
"github.com/owncloud/ocis/v2/services/graph/pkg/errorcode"
ctxpkg "github.com/owncloud/reva/v2/pkg/ctx"
revactx "github.com/owncloud/reva/v2/pkg/ctx"
"github.com/owncloud/reva/v2/pkg/rgrpc/todo/pool"
"github.com/owncloud/reva/v2/pkg/storagespace"
"github.com/owncloud/reva/v2/pkg/utils"
"google.golang.org/grpc/metadata"
)
// CreateHome provides a middleware which sends a CreateHome request to the reva gateway
func CreateHome(optionSetters ...Option) func(next http.Handler) http.Handler {
options := newOptions(optionSetters...)
logger := options.Logger
return func(next http.Handler) http.Handler {
return &createHome{
next: next,
logger: logger,
revaGatewaySelector: options.RevaGatewaySelector,
roleQuotas: options.RoleQuotas,
enableVaultMode: options.EnableVaultMode,
cache: sync.Map{},
}
}
}
type createHome struct {
next http.Handler
logger log.Logger
revaGatewaySelector pool.Selectable[gateway.GatewayAPIClient]
roleQuotas map[string]uint64
enableVaultMode bool
cache sync.Map // Store users for which personal space has been in memory indefinitely. Persistence isn't critical.
}
func (m *createHome) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if !m.shouldServe(req) {
m.next.ServeHTTP(w, req)
return
}
token := req.Header.Get("x-access-token")
// we need to pass the token to authenticate the CreateHome request.
ctx := metadata.AppendToOutgoingContext(req.Context(), revactx.TokenHeader, token)
createHomeReq := &provider.CreateHomeRequest{}
u, ok := revactx.ContextGetUser(ctx)
if !ok || u == nil {
m.logger.Error().Msg("no user in context")
m.next.ServeHTTP(w, req)
return
}
roleIDs, err := m.getUserRoles(u)
if err != nil {
m.logger.Error().Err(err).Str("userid", u.Id.OpaqueId).Msg("failed to get roles for user")
errorcode.GeneralException.Render(w, req, http.StatusInternalServerError, "Unauthorized")
return
}
if limit, hasLimit := m.checkRoleQuotaLimit(roleIDs); hasLimit {
createHomeReq.Opaque = utils.AppendPlainToOpaque(nil, "quota", strconv.FormatUint(limit, 10))
}
client, err := m.revaGatewaySelector.Next()
if err != nil {
m.logger.Err(err).Msg("error selecting next gateway client")
} else {
key := u.GetId().GetOpaqueId()
if _, exists := m.cache.Load(key); !exists {
createHomeRes, err := client.CreateHome(ctx, createHomeReq)
switch {
case err != nil:
m.logger.Err(err).Msg("error calling CreateHome")
case createHomeRes.GetStatus().GetCode() == rpc.Code_CODE_OK:
m.logger.Debug().Interface("userID", u.GetId().GetOpaqueId()).Msg("personal space created")
m.cache.Store(key, struct{}{})
case createHomeRes.GetStatus().GetCode() == rpc.Code_CODE_ALREADY_EXISTS:
m.logger.Info().Interface("userID", u.GetId().GetOpaqueId()).Interface("status", createHomeRes.GetStatus()).Msg("personal space already exists")
m.cache.Store(key, struct{}{})
default:
m.logger.Error().Interface("userID", u.GetId().GetOpaqueId()).Interface("status", createHomeRes.GetStatus()).Msg("personal space creation failed")
}
}
// TODO Perekhod: Create the vault home based on User permission
if m.enableVaultMode && mfa.IsMFAHeaderTrue(req) {
// Force MFA=true for vault home creation
vctx := metadata.AppendToOutgoingContext(ctx, ctxpkg.MFAOutgoingHeader, "true")
vaultKey := storagespace.FormatStorageID(utils.VaultStorageProviderID, u.GetId().GetOpaqueId())
if _, exists := m.cache.Load(vaultKey); !exists {
// Create vault personal space
// Inject storage_id into opaque for vault personal space
createHomeReq.Opaque = utils.AppendPlainToOpaque(createHomeReq.Opaque, "storage_id", utils.VaultStorageProviderID)
cpsRes, err := client.CreateHome(vctx, createHomeReq)
switch {
case err != nil:
m.logger.Err(err).Msg("error calling CreateHome for vault personal")
case cpsRes.GetStatus().GetCode() == rpc.Code_CODE_OK:
m.logger.Debug().Interface("userID", u.GetId().GetOpaqueId()).Msg("vault personal space created")
m.cache.Store(vaultKey, struct{}{})
case cpsRes.GetStatus().GetCode() == rpc.Code_CODE_ALREADY_EXISTS:
m.logger.Info().Interface("userID", u.GetId().GetOpaqueId()).Interface("status", cpsRes.GetStatus()).Msg("vault personal space already exists")
m.cache.Store(vaultKey, struct{}{})
default:
m.logger.Error().Interface("userID", u.GetId().GetOpaqueId()).Interface("status", cpsRes.GetStatus()).Msg("vault personal space creation failed")
}
}
}
}
m.next.ServeHTTP(w, req)
}
func (m *createHome) shouldServe(req *http.Request) bool {
return req.Header.Get("x-access-token") != ""
}
func (m *createHome) getUserRoles(user *userv1beta1.User) ([]string, error) {
var roleIDs []string
if err := utils.ReadJSONFromOpaque(user.Opaque, "roles", &roleIDs); err != nil {
return nil, err
}
tmp := make(map[string]struct{})
for _, id := range roleIDs {
tmp[id] = struct{}{}
}
dedup := make([]string, 0, len(tmp))
for k := range tmp {
dedup = append(dedup, k)
}
return dedup, nil
}
func (m *createHome) checkRoleQuotaLimit(roleIDs []string) (uint64, bool) {
if len(roleIDs) == 0 {
return 0, false
}
id := roleIDs[0] // At the moment a user can only have one role.
quota, ok := m.roleQuotas[id]
return quota, ok
}