Merge pull request #484 from jola5/master

Abort serve command if rootDir is inaccessible
This commit is contained in:
Andrey Smirnov
2017-02-15 23:54:42 +03:00
committed by GitHub
6 changed files with 61 additions and 0 deletions

View File

@@ -26,6 +26,7 @@ gom 'github.com/ugorji/go/codec', :commit => '71c2886f5a673a35f909803f38ece58101
gom 'github.com/vaughan0/go-ini', :commit => 'a98ad7ee00ec53921f08832bc06ecf7fd600e6a1'
gom 'github.com/wsxiaoys/terminal/color', :commit => '5668e431776a7957528361f90ce828266c69ed08'
gom 'golang.org/x/crypto/ssh/terminal', :commit => 'a7ead6ddf06233883deca151dffaef2effbf498f'
gom 'golang.org/x/sys/unix', :commit => '7a6e5648d140666db5d920909e082ca00a87ba2c'
group :test do
gom 'gopkg.in/check.v1'

View File

@@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"github.com/smira/aptly/api"
"github.com/smira/aptly/utils"
"github.com/smira/commander"
"github.com/smira/flag"
"net/http"
@@ -18,6 +19,17 @@ func aptlyAPIServe(cmd *commander.Command, args []string) error {
return commander.ErrCommandError
}
// There are only two working options for aptly's rootDir:
// 1. rootDir does not exist, then we'll create it
// 2. rootDir exists and is writable
// anything else must fail.
// E.g.: Running the service under a different user may lead to a rootDir
// that exists but is not usable due to access permissions.
err = utils.DirIsAccessible(context.Config().RootDir)
if err != nil {
return err
}
listen := context.Flags().Lookup("listen").Value.String()
fmt.Printf("\nStarting web server at: %s (press Ctrl+C to quit)...\n", listen)

View File

@@ -22,6 +22,17 @@ func aptlyServe(cmd *commander.Command, args []string) error {
return commander.ErrCommandError
}
// There are only two working options for aptly's rootDir:
// 1. rootDir does not exist, then we'll create it
// 2. rootDir exists and is writable
// anything else must fail.
// E.g.: Running the service under a different user may lead to a rootDir
// that exists but is not usable due to access permissions.
err = utils.DirIsAccessible(context.Config().RootDir)
if err != nil {
return err
}
if context.CollectionFactory().PublishedRepoCollection().Len() == 0 {
fmt.Printf("No published repositories, unable to serve.\n")
return nil

View File

@@ -0,0 +1 @@
ERROR: '/root' is inaccessible, check access rights

View File

@@ -8,9 +8,24 @@ import signal
import subprocess
import shlex
import time
import errno
from lib import BaseTest
from socket import error as socket_error
class RootDirInaccessible(BaseTest):
"""
serve command aborts if rootDir is inaccessible
"""
fixtureDB = False
fixturePool = False
configOverride = {
"rootDir": "/root" # any directory that exists but is not writable
}
runCmd = "aptly serve -listen=127.0.0.1:8765"
expectedCode = 1
class Serve1Test(BaseTest):
"""

View File

@@ -1,2 +1,23 @@
// Package utils collects various services: simple operations, compression, etc.
package utils
import (
"fmt"
"os"
"golang.org/x/sys/unix"
)
// check if directory exists and is accessible
func DirIsAccessible(filename string) error {
_, err := os.Stat(filename);
if err != nil {
if ! os.IsNotExist(err) {
return fmt.Errorf("Something went wrong, %v", err)
}
} else {
if unix.Access(filename, unix.W_OK) != nil {
return fmt.Errorf("'%s' is inaccessible, check access rights", filename)
}
}
return nil
}