Path + format

This commit is contained in:
Quentin 2021-11-20 09:53:05 +01:00
parent 514731cf4b
commit 4448577ec1
4 changed files with 33 additions and 22 deletions

View File

@ -21,7 +21,7 @@ func (l LdapPreAuth) WithCreds(username, password string) http.Handler {
var e *LdapWrongPasswordError var e *LdapWrongPasswordError
access_key, secret_key, err := LdapGetS3(l.WithConfig, username, password) access_key, secret_key, err := LdapGetS3(l.WithConfig, username, password)
if err == nil { if err == nil {
l.OnCreds.WithCreds(access_key, secret_key).ServeHTTP(w, r) l.OnCreds.WithCreds(access_key, secret_key).ServeHTTP(w, r)
} else if errors.As(err, &e) { } else if errors.As(err, &e) {
@ -44,16 +44,18 @@ type ldapConnector struct {
type LdapError struct { type LdapError struct {
Username string Username string
Err error Err error
} }
func (e *LdapError) Error() string { return "ldap error for "+e.Username+": "+e.Err.Error() }
type LdapWrongPasswordError struct { LdapError } func (e *LdapError) Error() string { return "ldap error for " + e.Username + ": " + e.Err.Error() }
type LdapWrongPasswordError struct{ LdapError }
func LdapGetS3(c *Config, username, password string) (access_key, secret_key string, werr error) { func LdapGetS3(c *Config, username, password string) (access_key, secret_key string, werr error) {
// 1. Connect to the server // 1. Connect to the server
conn, err := ldapConnect(c) conn, err := ldapConnect(c)
if err != nil { if err != nil {
werr = &LdapError { username, err } werr = &LdapError{username, err}
return return
} }
defer conn.Close() defer conn.Close()
@ -62,14 +64,14 @@ func LdapGetS3(c *Config, username, password string) (access_key, secret_key str
// @FIXME we should better check the error, it could also be due to an LDAP error // @FIXME we should better check the error, it could also be due to an LDAP error
err = conn.auth(username, password) err = conn.auth(username, password)
if err != nil { if err != nil {
werr = &LdapWrongPasswordError { LdapError { username, err } } werr = &LdapWrongPasswordError{LdapError{username, err}}
return return
} }
// 3. Fetch user's profile // 3. Fetch user's profile
profile, err := conn.profile() profile, err := conn.profile()
if err != nil { if err != nil {
werr = &LdapError { username, err } werr = &LdapError{username, err}
return return
} }
@ -78,7 +80,7 @@ func LdapGetS3(c *Config, username, password string) (access_key, secret_key str
secret_key = profile.GetAttributeValue("garage_s3_secret_key") secret_key = profile.GetAttributeValue("garage_s3_secret_key")
if access_key == "" || secret_key == "" { if access_key == "" || secret_key == "" {
err = errors.New(fmt.Sprintf("Either access key or secret key is missing in LDAP for %s", conn.userDn)) err = errors.New(fmt.Sprintf("Either access key or secret key is missing in LDAP for %s", conn.userDn))
werr = &LdapError { username, err } werr = &LdapError{username, err}
return return
} }

25
main.go
View File

@ -3,15 +3,15 @@ package main
import ( import (
"context" "context"
"fmt" "fmt"
"git.deuxfleurs.fr/Deuxfleurs/bagage/s3"
"git.deuxfleurs.fr/Deuxfleurs/bagage/sftp"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"golang.org/x/crypto/ssh"
"io" "io"
"io/ioutil" "io/ioutil"
"net"
"git.deuxfleurs.fr/Deuxfleurs/bagage/sftp"
"git.deuxfleurs.fr/Deuxfleurs/bagage/s3"
"github.com/minio/minio-go/v7/pkg/credentials"
"github.com/minio/minio-go/v7"
"golang.org/x/crypto/ssh"
"log" "log"
"net"
"net/http" "net/http"
) )
@ -26,7 +26,7 @@ func main() {
go httpServer(config, done) go httpServer(config, done)
go sshServer(config, done) go sshServer(config, done)
err := <- done err := <-done
if err != nil { if err != nil {
log.Fatalf("A component failed: %v", err) log.Fatalf("A component failed: %v", err)
} }
@ -36,9 +36,10 @@ type s3creds struct {
accessKey string accessKey string
secretKey string secretKey string
} }
var keychain map[string]s3creds var keychain map[string]s3creds
func sshServer(dconfig* Config, done chan error) { func sshServer(dconfig *Config, done chan error) {
keychain = make(map[string]s3creds) keychain = make(map[string]s3creds)
config := &ssh.ServerConfig{ config := &ssh.ServerConfig{
@ -46,7 +47,7 @@ func sshServer(dconfig* Config, done chan error) {
log.Printf("Login: %s\n", c.User()) log.Printf("Login: %s\n", c.User())
access_key, secret_key, err := LdapGetS3(dconfig, c.User(), string(pass)) access_key, secret_key, err := LdapGetS3(dconfig, c.User(), string(pass))
if err == nil { if err == nil {
keychain[c.User()] = s3creds{ access_key, secret_key } keychain[c.User()] = s3creds{access_key, secret_key}
} }
return nil, err return nil, err
}, },
@ -82,7 +83,7 @@ func sshServer(dconfig* Config, done chan error) {
} }
} }
func handleSSHConn(nConn net.Conn, dconfig* Config, config *ssh.ServerConfig) { func handleSSHConn(nConn net.Conn, dconfig *Config, config *ssh.ServerConfig) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
@ -138,7 +139,7 @@ func handleSSHConn(nConn net.Conn, dconfig* Config, config *ssh.ServerConfig) {
} }
}(requests) }(requests)
creds := keychain[user] creds := keychain[user]
mc, err := minio.New(dconfig.Endpoint, &minio.Options{ mc, err := minio.New(dconfig.Endpoint, &minio.Options{
Creds: credentials.NewStaticV4(creds.accessKey, creds.secretKey, ""), Creds: credentials.NewStaticV4(creds.accessKey, creds.secretKey, ""),
Secure: dconfig.UseSSL, Secure: dconfig.UseSSL,
@ -163,7 +164,7 @@ func handleSSHConn(nConn net.Conn, dconfig* Config, config *ssh.ServerConfig) {
} }
} }
func httpServer(config* Config, done chan error) { func httpServer(config *Config, done chan error) {
// Assemble components to handle WebDAV requests // Assemble components to handle WebDAV requests
http.Handle(config.DavPath+"/", http.Handle(config.DavPath+"/",
BasicAuthExtract{ BasicAuthExtract{

View File

@ -19,6 +19,7 @@ type S3File struct {
objw *io.PipeWriter objw *io.PipeWriter
donew chan error donew chan error
pos int64 pos int64
eof bool
entries []fs.FileInfo entries []fs.FileInfo
Path S3Path Path S3Path
} }
@ -85,12 +86,19 @@ func (f *S3File) Read(p []byte) (n int, err error) {
} }
func (f *S3File) ReadAt(p []byte, off int64) (n int, err error) { func (f *S3File) ReadAt(p []byte, off int64) (n int, err error) {
if f.eof {
return 0, io.EOF
}
log.Printf("s3 ReadAt %v\n", off) log.Printf("s3 ReadAt %v\n", off)
if err := f.loadObject(); err != nil { if err := f.loadObject(); err != nil {
return 0, err return 0, err
} }
return f.obj.ReadAt(p, off) n, err = f.obj.ReadAt(p, off)
f.eof = err == io.EOF
return
} }
func (f *S3File) WriteAt(p []byte, off int64) (n int, err error) { func (f *S3File) WriteAt(p []byte, off int64) (n int, err error) {

View File

@ -1,8 +1,8 @@
package main package main
import ( import (
"github.com/minio/minio-go/v7"
"git.deuxfleurs.fr/Deuxfleurs/bagage/s3" "git.deuxfleurs.fr/Deuxfleurs/bagage/s3"
"github.com/minio/minio-go/v7"
"golang.org/x/net/webdav" "golang.org/x/net/webdav"
"log" "log"
"net/http" "net/http"