An API for Guichet #23

Merged
quentin merged 14 commits from api into main 2023-09-26 06:44:36 +00:00
8 changed files with 139 additions and 147 deletions
Showing only changes of commit 706ff58a6f - Show all commits

2
api.go
View file

@ -1,8 +1,8 @@
package main package main
import ( import (
"errors"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"net/http" "net/http"

2
cli.go
View file

@ -3,9 +3,9 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"golang.org/x/term"
"os" "os"
"syscall" "syscall"
"golang.org/x/term"
) )
var fsCli = flag.NewFlagSet("cli", flag.ContinueOnError) var fsCli = flag.NewFlagSet("cli", flag.ContinueOnError)

View file

@ -10,7 +10,6 @@ import (
"strings" "strings"
) )
func gadmin() (*garage.APIClient, context.Context) { func gadmin() (*garage.APIClient, context.Context) {
// Set Host and other parameters // Set Host and other parameters
configuration := garage.NewConfiguration() configuration := garage.NewConfiguration()
@ -47,8 +46,6 @@ func grgGetKey(accessKey string) (*garage.KeyInfo, error) {
return resp, nil return resp, nil
} }
func grgCreateBucket(bucket string) (*garage.BucketInfo, error) { func grgCreateBucket(bucket string) (*garage.BucketInfo, error) {
client, ctx := gadmin() client, ctx := gadmin()
@ -267,10 +264,10 @@ func handleWebsiteInspect(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" { if r.Method == "POST" {
r.ParseForm() r.ParseForm()
action := strings.Join(r.Form["action"],"") action := strings.Join(r.Form["action"], "")
switch action { switch action {
case "increase_quota": case "increase_quota":
_, processErr = ctrl.Patch(bucketName, &WebsitePatch { Size: &user.Quota.WebsiteSizeBursted }) _, processErr = ctrl.Patch(bucketName, &WebsitePatch{Size: &user.Quota.WebsiteSizeBursted})
case "delete_bucket": case "delete_bucket":
processErr = ctrl.Delete(bucketName) processErr = ctrl.Delete(bucketName)
http.Redirect(w, r, "/website", http.StatusFound) http.Redirect(w, r, "/website", http.StatusFound)
@ -293,7 +290,7 @@ func handleWebsiteInspect(w http.ResponseWriter, r *http.Request) {
return return
} }
tpl := &WebsiteInspectTpl{ describe, view, processErr } tpl := &WebsiteInspectTpl{describe, view, processErr}
tWebsiteInspect := getTemplate("garage_website_inspect.html") tWebsiteInspect := getTemplate("garage_website_inspect.html")
tWebsiteInspect.Execute(w, &tpl) tWebsiteInspect.Execute(w, &tpl)

View file

@ -112,6 +112,7 @@ type Capabilities struct {
CanAdmin bool CanAdmin bool
CanInvite bool CanInvite bool
} }
func NewCapabilities(login *LoginStatus, entry *ldap.Entry) *Capabilities { func NewCapabilities(login *LoginStatus, entry *ldap.Entry) *Capabilities {
// Initialize // Initialize
canAdmin := false canAdmin := false
@ -148,6 +149,7 @@ type LoggedUser struct {
Quota *UserQuota Quota *UserQuota
s3key *garage.KeyInfo s3key *garage.KeyInfo
} }
func NewLoggedUser(login *LoginStatus) (*LoggedUser, error) { func NewLoggedUser(login *LoginStatus) (*LoggedUser, error) {
requestKind := "(objectClass=organizationalPerson)" requestKind := "(objectClass=organizationalPerson)"
if strings.EqualFold(login.Info.DN(), config.AdminAccount) { if strings.EqualFold(login.Info.DN(), config.AdminAccount) {
@ -184,7 +186,7 @@ func NewLoggedUser(login *LoginStatus) (*LoggedUser, error) {
} }
entry := sr.Entries[0] entry := sr.Entries[0]
lu := &LoggedUser { lu := &LoggedUser{
Login: login, Login: login,
Entry: entry, Entry: entry,
Capabilities: NewCapabilities(login, entry), Capabilities: NewCapabilities(login, entry),
@ -251,7 +253,6 @@ func RequireUser(r *http.Request) (*LoggedUser, error) {
return nil, ErrNotAuthenticated return nil, ErrNotAuthenticated
} }
loginStatus, err := NewLoginStatus(r, login_info) loginStatus, err := NewLoginStatus(r, login_info)
if err != nil { if err != nil {
return nil, err return nil, err

View file

@ -192,7 +192,6 @@ func logRequest(handler http.Handler) http.Handler {
// Page handlers ---- // Page handlers ----
// --- Home Controller // --- Home Controller
type HomePageData struct { type HomePageData struct {
User *LoggedUser User *LoggedUser
@ -254,7 +253,7 @@ func handleLogin(w http.ResponseWriter, r *http.Request) {
username := strings.Join(r.Form["username"], "") username := strings.Join(r.Form["username"], "")
password := strings.Join(r.Form["password"], "") password := strings.Join(r.Form["password"], "")
loginInfo := LoginInfo { username, password } loginInfo := LoginInfo{username, password}
l, err := NewLdapCon() l, err := NewLdapCon()
if err != nil { if err != nil {

View file

@ -5,8 +5,8 @@ import (
"fmt" "fmt"
"strconv" "strconv"
"github.com/go-ldap/ldap/v3"
garage "git.deuxfleurs.fr/garage-sdk/garage-admin-sdk-golang" garage "git.deuxfleurs.fr/garage-sdk/garage-admin-sdk-golang"
"github.com/go-ldap/ldap/v3"
) )
const ( const (
@ -29,7 +29,7 @@ type UserQuota struct {
} }
func NewUserQuota() *UserQuota { func NewUserQuota() *UserQuota {
return &UserQuota { return &UserQuota{
WebsiteCount: QUOTA_WEBSITE_COUNT, WebsiteCount: QUOTA_WEBSITE_COUNT,
WebsiteSizeDefault: QUOTA_WEBSITE_SIZE_DEFAULT, WebsiteSizeDefault: QUOTA_WEBSITE_SIZE_DEFAULT,
WebsiteSizeBursted: QUOTA_WEBSITE_SIZE_BURSTED, WebsiteSizeBursted: QUOTA_WEBSITE_SIZE_BURSTED,
@ -107,8 +107,9 @@ type QuotaStat struct {
Ratio float64 `json:"ratio"` Ratio float64 `json:"ratio"`
Burstable bool `json:"burstable"` Burstable bool `json:"burstable"`
} }
func NewQuotaStat(current, max int64, burstable bool) QuotaStat { func NewQuotaStat(current, max int64, burstable bool) QuotaStat {
return QuotaStat { return QuotaStat{
Current: current, Current: current,
Max: max, Max: max,
Ratio: float64(current) / float64(max), Ratio: float64(current) / float64(max),

View file

@ -2,9 +2,9 @@ package main
import ( import (
"fmt" "fmt"
garage "git.deuxfleurs.fr/garage-sdk/garage-admin-sdk-golang"
"sort" "sort"
"strings" "strings"
garage "git.deuxfleurs.fr/garage-sdk/garage-admin-sdk-golang"
) )
var ( var (
@ -19,16 +19,14 @@ var (
ErrBucketDeleteUnfinishedUpload = fmt.Errorf("You must remove all the unfinished multipart uploads before deleting a bucket") ErrBucketDeleteUnfinishedUpload = fmt.Errorf("You must remove all the unfinished multipart uploads before deleting a bucket")
) )
type WebsiteId struct { type WebsiteId struct {
Pretty string `json:"name"` Pretty string `json:"name"`
Internal string `json:"-"` Internal string `json:"-"`
Alt []string `json:"alt_name"` Alt []string `json:"alt_name"`
Expanded bool `json:"expanded"` Expanded bool `json:"expanded"`
Url string `json:"domain"` Url string `json:"domain"`
} }
func NewWebsiteId(id string, aliases []string) *WebsiteId { func NewWebsiteId(id string, aliases []string) *WebsiteId {
pretty := id pretty := id
var alt []string var alt []string
@ -43,7 +41,7 @@ func NewWebsiteId(id string, aliases []string) *WebsiteId {
url = fmt.Sprintf("%s.web.deuxfleurs.fr", pretty) url = fmt.Sprintf("%s.web.deuxfleurs.fr", pretty)
} }
return &WebsiteId { pretty, id, alt, expanded, url } return &WebsiteId{pretty, id, alt, expanded, url}
} }
func NewWebsiteIdFromBucketInfo(binfo *garage.BucketInfo) *WebsiteId { func NewWebsiteIdFromBucketInfo(binfo *garage.BucketInfo) *WebsiteId {
return NewWebsiteId(*binfo.Id, binfo.GlobalAliases) return NewWebsiteId(*binfo.Id, binfo.GlobalAliases)
@ -65,7 +63,7 @@ func NewWebsiteController(user *LoggedUser) (*WebsiteController, error) {
return nil, err return nil, err
} }
for _, bckt := range(keyInfo.Buckets) { for _, bckt := range keyInfo.Buckets {
if len(bckt.GlobalAliases) > 0 { if len(bckt.GlobalAliases) > 0 {
wid := NewWebsiteId(*bckt.Id, bckt.GlobalAliases) wid := NewWebsiteId(*bckt.Id, bckt.GlobalAliases)
idx[wid.Pretty] = wid idx[wid.Pretty] = wid
@ -77,7 +75,7 @@ func NewWebsiteController(user *LoggedUser) (*WebsiteController, error) {
maxW := user.Quota.WebsiteCount maxW := user.Quota.WebsiteCount
quota := NewQuotaStat(int64(len(wlist)), maxW, true) quota := NewQuotaStat(int64(len(wlist)), maxW, true)
return &WebsiteController { user, idx, wlist, quota }, nil return &WebsiteController{user, idx, wlist, quota}, nil
} }
type WebsiteDescribe struct { type WebsiteDescribe struct {
@ -98,12 +96,12 @@ func (w *WebsiteController) Describe() (*WebsiteDescribe, error) {
for _, k := range w.PrettyList { for _, k := range w.PrettyList {
r = append(r, w.WebsiteIdx[k]) r = append(r, w.WebsiteIdx[k])
} }
return &WebsiteDescribe { return &WebsiteDescribe{
*s3key.AccessKeyId, *s3key.AccessKeyId,
*s3key.SecretAccessKey, *s3key.SecretAccessKey,
&w.WebsiteCount, &w.WebsiteCount,
w.User.Quota.WebsiteSizeBurstedPretty(), w.User.Quota.WebsiteSizeBurstedPretty(),
r }, nil r}, nil
} }
func (w *WebsiteController) Inspect(pretty string) (*WebsiteView, error) { func (w *WebsiteController) Inspect(pretty string) (*WebsiteView, error) {
@ -183,7 +181,6 @@ func (w *WebsiteController) Create(pretty string) (*WebsiteView, error) {
ur.SetWebsiteAccess(*wr) ur.SetWebsiteAccess(*wr)
ur.SetQuotas(*qr) ur.SetQuotas(*qr)
binfo, err = grgUpdateBucket(*binfo.Id, ur) binfo, err = grgUpdateBucket(*binfo.Id, ur)
if err != nil { if err != nil {
return nil, ErrCantConfigureBucket return nil, ErrCantConfigureBucket
@ -219,9 +216,6 @@ func (w *WebsiteController) Delete(pretty string) error {
return err return err
} }
type WebsiteView struct { type WebsiteView struct {
Name *WebsiteId `json:"identified_as"` Name *WebsiteId `json:"identified_as"`
Size QuotaStat `json:"quota_size"` Size QuotaStat `json:"quota_size"`
@ -234,7 +228,7 @@ func NewWebsiteView(binfo *garage.BucketInfo) *WebsiteView {
wid := NewWebsiteIdFromBucketInfo(binfo) wid := NewWebsiteIdFromBucketInfo(binfo)
size := NewQuotaStat(*binfo.Bytes, (&q).GetMaxSize(), true) size := NewQuotaStat(*binfo.Bytes, (&q).GetMaxSize(), true)
objects := NewQuotaStat(*binfo.Objects, (&q).GetMaxObjects(), false) objects := NewQuotaStat(*binfo.Objects, (&q).GetMaxObjects(), false)
return &WebsiteView { wid, size, objects } return &WebsiteView{wid, size, objects}
} }
type WebsitePatch struct { type WebsitePatch struct {