2023-09-25 17:07:07 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-09-26 06:40:41 +00:00
|
|
|
garage "git.deuxfleurs.fr/garage-sdk/garage-admin-sdk-golang"
|
2023-09-25 17:07:07 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-09-26 06:40:41 +00:00
|
|
|
ErrWebsiteNotFound = fmt.Errorf("Website not found")
|
|
|
|
ErrFetchBucketInfo = fmt.Errorf("Failed to fetch bucket information")
|
|
|
|
ErrWebsiteQuotaReached = fmt.Errorf("Can't create additional websites, quota reached")
|
|
|
|
ErrEmptyBucketName = fmt.Errorf("You can't create a website with an empty name")
|
|
|
|
ErrCantCreateBucket = fmt.Errorf("Can't create this bucket. Maybe another bucket already exists with this name or you have an invalid character")
|
|
|
|
ErrCantAllowKey = fmt.Errorf("Can't allow given key on the target bucket")
|
|
|
|
ErrCantConfigureBucket = fmt.Errorf("Unable to configure the bucket (activating website, adding quotas, etc.)")
|
|
|
|
ErrBucketDeleteNotEmpty = fmt.Errorf("You must remove all the files before deleting a bucket")
|
2023-09-25 20:00:46 +00:00
|
|
|
ErrBucketDeleteUnfinishedUpload = fmt.Errorf("You must remove all the unfinished multipart uploads before deleting a bucket")
|
2023-09-25 17:07:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type WebsiteId struct {
|
2023-09-26 06:40:41 +00:00
|
|
|
Pretty string `json:"name"`
|
|
|
|
Internal string `json:"-"`
|
|
|
|
Alt []string `json:"alt_name"`
|
|
|
|
Expanded bool `json:"expanded"`
|
|
|
|
Url string `json:"domain"`
|
2023-09-25 17:07:07 +00:00
|
|
|
}
|
2023-09-26 06:40:41 +00:00
|
|
|
|
2023-09-25 17:07:07 +00:00
|
|
|
func NewWebsiteId(id string, aliases []string) *WebsiteId {
|
|
|
|
pretty := id
|
|
|
|
var alt []string
|
|
|
|
if len(aliases) > 0 {
|
|
|
|
pretty = aliases[0]
|
|
|
|
alt = aliases[1:]
|
|
|
|
}
|
|
|
|
expanded := strings.Contains(pretty, ".")
|
|
|
|
|
|
|
|
url := pretty
|
|
|
|
if !expanded {
|
|
|
|
url = fmt.Sprintf("%s.web.deuxfleurs.fr", pretty)
|
|
|
|
}
|
|
|
|
|
2023-09-26 06:40:41 +00:00
|
|
|
return &WebsiteId{pretty, id, alt, expanded, url}
|
2023-09-25 17:07:07 +00:00
|
|
|
}
|
|
|
|
func NewWebsiteIdFromBucketInfo(binfo *garage.BucketInfo) *WebsiteId {
|
|
|
|
return NewWebsiteId(*binfo.Id, binfo.GlobalAliases)
|
|
|
|
}
|
|
|
|
|
|
|
|
type WebsiteController struct {
|
2023-09-26 06:40:41 +00:00
|
|
|
User *LoggedUser
|
|
|
|
WebsiteIdx map[string]*WebsiteId
|
|
|
|
PrettyList []string
|
2023-09-25 17:07:07 +00:00
|
|
|
WebsiteCount QuotaStat
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewWebsiteController(user *LoggedUser) (*WebsiteController, error) {
|
|
|
|
idx := map[string]*WebsiteId{}
|
|
|
|
var wlist []string
|
|
|
|
|
|
|
|
keyInfo, err := user.S3KeyInfo()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-09-26 06:40:41 +00:00
|
|
|
for _, bckt := range keyInfo.Buckets {
|
2023-09-25 17:07:07 +00:00
|
|
|
if len(bckt.GlobalAliases) > 0 {
|
|
|
|
wid := NewWebsiteId(*bckt.Id, bckt.GlobalAliases)
|
|
|
|
idx[wid.Pretty] = wid
|
|
|
|
wlist = append(wlist, wid.Pretty)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(wlist)
|
|
|
|
|
|
|
|
maxW := user.Quota.WebsiteCount
|
|
|
|
quota := NewQuotaStat(int64(len(wlist)), maxW, true)
|
|
|
|
|
2023-09-26 06:40:41 +00:00
|
|
|
return &WebsiteController{user, idx, wlist, quota}, nil
|
2023-09-25 17:07:07 +00:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:00:57 +00:00
|
|
|
type WebsiteDescribe struct {
|
2023-09-26 06:40:41 +00:00
|
|
|
AccessKeyId string `json:"access_key_id"`
|
|
|
|
SecretAccessKey string `json:"secret_access_key"`
|
|
|
|
AllowedWebsites *QuotaStat `json:"quota_website_count"`
|
|
|
|
BurstBucketQuotaSize string `json:"burst_bucket_quota_size"`
|
|
|
|
Websites []*WebsiteId `json:"vhosts"`
|
2023-09-25 21:00:57 +00:00
|
|
|
}
|
|
|
|
|
2023-09-26 06:40:30 +00:00
|
|
|
func (w *WebsiteController) Describe() (*WebsiteDescribe, error) {
|
|
|
|
s3key, err := w.User.S3KeyInfo()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-09-25 17:07:07 +00:00
|
|
|
r := make([]*WebsiteId, 0, len(w.PrettyList))
|
|
|
|
for _, k := range w.PrettyList {
|
2023-09-26 06:40:41 +00:00
|
|
|
r = append(r, w.WebsiteIdx[k])
|
2023-09-25 17:07:07 +00:00
|
|
|
}
|
2023-09-26 06:40:41 +00:00
|
|
|
return &WebsiteDescribe{
|
|
|
|
*s3key.AccessKeyId,
|
|
|
|
*s3key.SecretAccessKey,
|
|
|
|
&w.WebsiteCount,
|
2023-09-26 06:40:30 +00:00
|
|
|
w.User.Quota.WebsiteSizeBurstedPretty(),
|
2023-09-26 06:40:41 +00:00
|
|
|
r}, nil
|
2023-09-25 17:07:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WebsiteController) Inspect(pretty string) (*WebsiteView, error) {
|
|
|
|
website, ok := w.WebsiteIdx[pretty]
|
|
|
|
if !ok {
|
|
|
|
return nil, ErrWebsiteNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
binfo, err := grgGetBucket(website.Internal)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrFetchBucketInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewWebsiteView(binfo), nil
|
|
|
|
}
|
|
|
|
|
2023-09-25 20:00:46 +00:00
|
|
|
func (w *WebsiteController) Patch(pretty string, patch *WebsitePatch) (*WebsiteView, error) {
|
|
|
|
website, ok := w.WebsiteIdx[pretty]
|
|
|
|
if !ok {
|
|
|
|
return nil, ErrWebsiteNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
binfo, err := grgGetBucket(website.Internal)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrFetchBucketInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
// Patch the max size
|
|
|
|
urQuota := garage.NewUpdateBucketRequestQuotas()
|
|
|
|
urQuota.SetMaxSize(w.User.Quota.WebsiteSizeAdjust(binfo.Quotas.GetMaxSize()))
|
|
|
|
urQuota.SetMaxObjects(w.User.Quota.WebsiteObjectAdjust(binfo.Quotas.GetMaxObjects()))
|
2023-09-25 21:00:57 +00:00
|
|
|
if patch.Size != nil {
|
|
|
|
urQuota.SetMaxSize(w.User.Quota.WebsiteSizeAdjust(*patch.Size))
|
2023-09-25 20:00:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build the update
|
|
|
|
ur := garage.NewUpdateBucketRequest()
|
|
|
|
ur.SetQuotas(*urQuota)
|
|
|
|
|
|
|
|
// Call garage
|
|
|
|
binfo, err = grgUpdateBucket(website.Internal, ur)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrCantConfigureBucket
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewWebsiteView(binfo), nil
|
2023-09-25 17:07:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WebsiteController) Create(pretty string) (*WebsiteView, error) {
|
|
|
|
if pretty == "" {
|
|
|
|
return nil, ErrEmptyBucketName
|
|
|
|
}
|
|
|
|
|
|
|
|
if w.WebsiteCount.IsFull() {
|
|
|
|
return nil, ErrWebsiteQuotaReached
|
|
|
|
}
|
|
|
|
|
|
|
|
binfo, err := grgCreateBucket(pretty)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrCantCreateBucket
|
|
|
|
}
|
|
|
|
|
|
|
|
s3key, err := w.User.S3KeyInfo()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
binfo, err = grgAllowKeyOnBucket(*binfo.Id, *s3key.AccessKeyId)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrCantAllowKey
|
|
|
|
}
|
|
|
|
|
|
|
|
qr := w.User.Quota.DefaultWebsiteQuota()
|
|
|
|
wr := allowWebsiteDefault()
|
|
|
|
|
|
|
|
ur := garage.NewUpdateBucketRequest()
|
|
|
|
ur.SetWebsiteAccess(*wr)
|
|
|
|
ur.SetQuotas(*qr)
|
|
|
|
|
|
|
|
binfo, err = grgUpdateBucket(*binfo.Id, ur)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrCantConfigureBucket
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewWebsiteView(binfo), nil
|
|
|
|
}
|
|
|
|
|
2023-09-25 20:00:46 +00:00
|
|
|
func (w *WebsiteController) Delete(pretty string) error {
|
|
|
|
if pretty == "" {
|
|
|
|
return ErrEmptyBucketName
|
|
|
|
}
|
|
|
|
|
|
|
|
website, ok := w.WebsiteIdx[pretty]
|
|
|
|
if !ok {
|
|
|
|
return ErrWebsiteNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
binfo, err := grgGetBucket(website.Internal)
|
|
|
|
if err != nil {
|
|
|
|
return ErrFetchBucketInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
if *binfo.Objects > int64(0) {
|
|
|
|
return ErrBucketDeleteNotEmpty
|
2023-09-26 06:40:41 +00:00
|
|
|
}
|
2023-09-25 20:00:46 +00:00
|
|
|
|
|
|
|
if *binfo.UnfinishedUploads > int32(0) {
|
|
|
|
return ErrBucketDeleteUnfinishedUpload
|
|
|
|
}
|
|
|
|
|
|
|
|
err = grgDeleteBucket(website.Internal)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-25 17:07:07 +00:00
|
|
|
type WebsiteView struct {
|
2023-09-26 07:17:33 +00:00
|
|
|
Name *WebsiteId `json:"vhost"`
|
2023-09-26 06:40:41 +00:00
|
|
|
Size QuotaStat `json:"quota_size"`
|
|
|
|
Files QuotaStat `json:"quota_files"`
|
2023-09-25 17:07:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewWebsiteView(binfo *garage.BucketInfo) *WebsiteView {
|
|
|
|
q := binfo.GetQuotas()
|
|
|
|
|
|
|
|
wid := NewWebsiteIdFromBucketInfo(binfo)
|
|
|
|
size := NewQuotaStat(*binfo.Bytes, (&q).GetMaxSize(), true)
|
|
|
|
objects := NewQuotaStat(*binfo.Objects, (&q).GetMaxObjects(), false)
|
2023-09-26 06:40:41 +00:00
|
|
|
return &WebsiteView{wid, size, objects}
|
2023-09-25 17:07:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type WebsitePatch struct {
|
2023-09-25 21:00:57 +00:00
|
|
|
Size *int64 `json:"quota_size"`
|
2023-09-25 17:07:07 +00:00
|
|
|
}
|