2019-02-21 15:03:17 +00:00
|
|
|
// Copyright 2019 Sorint.lab
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package gitserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
2022-02-22 14:01:29 +00:00
|
|
|
"agola.io/agola/internal/errors"
|
2019-07-01 09:40:20 +00:00
|
|
|
handlers "agola.io/agola/internal/git-handler"
|
|
|
|
"agola.io/agola/internal/services/config"
|
|
|
|
"agola.io/agola/internal/util"
|
2019-02-21 15:03:17 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2022-02-21 17:07:58 +00:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
2019-02-21 15:03:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
gitSuffix = ".git"
|
|
|
|
)
|
|
|
|
|
|
|
|
func repoPathIsValid(reposDir, repoPath string) (bool, error) {
|
|
|
|
// a parent cannot end with .git
|
|
|
|
parts := strings.Split(repoPath, "/")
|
|
|
|
for _, part := range parts[:len(parts)-1] {
|
|
|
|
if strings.HasSuffix(part, gitSuffix) {
|
|
|
|
return false, errors.Errorf("path %q contains a parent directory with .git suffix", repoPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check that a subdirectory doesn't exists
|
|
|
|
reposDir, err := filepath.Abs(reposDir)
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return false, errors.WithStack(err)
|
2019-02-21 15:03:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
path := repoPath
|
|
|
|
_, err = os.Stat(path)
|
2022-03-02 12:23:32 +00:00
|
|
|
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
2022-02-22 14:01:29 +00:00
|
|
|
return false, errors.WithStack(err)
|
2019-02-21 15:03:17 +00:00
|
|
|
}
|
2022-03-02 12:23:32 +00:00
|
|
|
if !errors.Is(err, os.ErrNotExist) {
|
2019-02-21 15:03:17 +00:00
|
|
|
// if it exists assume it's valid
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
path = filepath.Dir(path)
|
|
|
|
if len(path) <= len(reposDir) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := os.Stat(path)
|
2022-03-02 12:23:32 +00:00
|
|
|
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
2022-02-22 14:01:29 +00:00
|
|
|
return false, errors.WithStack(err)
|
2019-02-21 15:03:17 +00:00
|
|
|
}
|
|
|
|
// a parent path cannot end with .git
|
|
|
|
if strings.HasSuffix(path, gitSuffix) {
|
|
|
|
return false, nil
|
|
|
|
}
|
2022-03-02 12:23:32 +00:00
|
|
|
if !errors.Is(err, os.ErrNotExist) {
|
2019-02-21 15:03:17 +00:00
|
|
|
// if a parent exists return not valid
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func repoExists(repoAbsPath string) (bool, error) {
|
|
|
|
_, err := os.Stat(repoAbsPath)
|
2022-03-02 12:23:32 +00:00
|
|
|
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
2022-02-22 14:01:29 +00:00
|
|
|
return false, errors.WithStack(err)
|
2019-02-21 15:03:17 +00:00
|
|
|
}
|
2022-03-02 12:23:32 +00:00
|
|
|
return !errors.Is(err, os.ErrNotExist), nil
|
2019-02-21 15:03:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func repoAbsPath(reposDir, repoPath string) (string, bool, error) {
|
|
|
|
valid, err := repoPathIsValid(reposDir, repoPath)
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return "", false, errors.WithStack(err)
|
2019-02-21 15:03:17 +00:00
|
|
|
}
|
|
|
|
if !valid {
|
|
|
|
return "", false, handlers.ErrWrongRepoPath
|
|
|
|
}
|
|
|
|
|
|
|
|
repoFSPath, err := filepath.Abs(filepath.Join(reposDir, repoPath))
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return "", false, errors.WithStack(err)
|
2019-02-21 15:03:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
exists, err := repoExists(repoFSPath)
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return "", false, errors.WithStack(err)
|
2019-02-21 15:03:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return repoFSPath, exists, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Matcher(matchRegexp *regexp.Regexp) mux.MatcherFunc {
|
|
|
|
return func(r *http.Request, rm *mux.RouteMatch) bool {
|
|
|
|
return matchRegexp.MatchString(r.URL.Path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-08 13:23:13 +00:00
|
|
|
type Gitserver struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
|
|
|
c *config.Gitserver
|
2019-02-21 15:03:17 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewGitserver(ctx context.Context, log zerolog.Logger, c *config.Gitserver) (*Gitserver, error) {
|
2019-02-21 15:03:17 +00:00
|
|
|
if c.Debug {
|
2022-02-21 17:07:58 +00:00
|
|
|
log = log.Level(zerolog.DebugLevel)
|
2019-02-21 15:03:17 +00:00
|
|
|
}
|
|
|
|
|
2019-05-08 13:23:13 +00:00
|
|
|
return &Gitserver{
|
2022-02-21 17:07:58 +00:00
|
|
|
log: log,
|
|
|
|
c: c,
|
2019-02-21 15:03:17 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-05-08 13:23:13 +00:00
|
|
|
func (s *Gitserver) Run(ctx context.Context) error {
|
2022-02-21 17:07:58 +00:00
|
|
|
gitSmartHandler := handlers.NewGitSmartHandler(s.log, s.c.DataDir, true, repoAbsPath, nil)
|
|
|
|
fetchFileHandler := handlers.NewFetchFileHandler(s.log, s.c.DataDir, repoAbsPath)
|
2019-02-21 15:03:17 +00:00
|
|
|
|
|
|
|
router := mux.NewRouter()
|
|
|
|
router.MatcherFunc(Matcher(handlers.InfoRefsRegExp)).Handler(gitSmartHandler)
|
|
|
|
router.MatcherFunc(Matcher(handlers.UploadPackRegExp)).Handler(gitSmartHandler)
|
|
|
|
router.MatcherFunc(Matcher(handlers.ReceivePackRegExp)).Handler(gitSmartHandler)
|
|
|
|
router.MatcherFunc(Matcher(handlers.FetchFileRegExp)).Handler(fetchFileHandler)
|
|
|
|
|
|
|
|
var tlsConfig *tls.Config
|
|
|
|
if s.c.Web.TLS {
|
|
|
|
var err error
|
|
|
|
tlsConfig, err = util.NewTLSConfig(s.c.Web.TLSCertFile, s.c.Web.TLSKeyFile, "", false)
|
|
|
|
if err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
s.log.Err(err).Send()
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-02-21 15:03:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
httpServer := http.Server{
|
|
|
|
Addr: s.c.Web.ListenAddress,
|
|
|
|
Handler: router,
|
|
|
|
TLSConfig: tlsConfig,
|
|
|
|
}
|
|
|
|
|
|
|
|
lerrCh := make(chan error)
|
|
|
|
go func() {
|
2021-03-19 09:52:32 +00:00
|
|
|
if !s.c.Web.TLS {
|
|
|
|
lerrCh <- httpServer.ListenAndServe()
|
|
|
|
} else {
|
|
|
|
lerrCh <- httpServer.ListenAndServeTLS("", "")
|
|
|
|
}
|
2019-02-21 15:03:17 +00:00
|
|
|
}()
|
|
|
|
|
2021-11-22 13:46:06 +00:00
|
|
|
//TODO a lock is needed or it'll cause some concurrency issues if repo cleaner runs when someone at the same time is pushing
|
|
|
|
go s.repoCleanerLoop(ctx)
|
|
|
|
|
2019-02-21 15:03:17 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2022-02-21 17:07:58 +00:00
|
|
|
log.Info().Msgf("gitserver exiting")
|
2019-02-21 15:03:17 +00:00
|
|
|
httpServer.Close()
|
|
|
|
case err := <-lerrCh:
|
2019-07-04 13:50:37 +00:00
|
|
|
if err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
s.log.Err(err).Msgf("http server listen error")
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-07-04 13:50:37 +00:00
|
|
|
}
|
2019-02-21 15:03:17 +00:00
|
|
|
}
|
2019-07-04 13:50:37 +00:00
|
|
|
|
|
|
|
return nil
|
2019-02-21 15:03:17 +00:00
|
|
|
}
|