Skip to content

Commit

Permalink
get the project running and cleanly working
Browse files Browse the repository at this point in the history
  • Loading branch information
lyondhill committed Jan 13, 2016
1 parent 33fbdf1 commit f463375
Show file tree
Hide file tree
Showing 16 changed files with 331 additions and 152 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

## Butter

A small, git-based deployment service with pluggable authentication and deployment strategies.
A small, version controll based deployment service with pluggable authentication and deployment strategies.

### Status
Experimental/Unstable/Incomplete
Expand All @@ -19,3 +19,8 @@ Experimental/Unstable/Incomplete
| `/commits/{commit}` | Get details about a specific commit | nil | `[{"id":"sha","author":"me","message":"this is a message","author_date":"jan","author_email":"[email protected]"}]` |

[![butter logo](http://nano-assets.gopagoda.io/open-src/nanobox-open-src.png)](http://nanobox.io/open-source)


## TODO
build a cli
Write tests
42 changes: 33 additions & 9 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,51 @@ package api

import (
"net/http"


"encoding/json"
"github.com/gorilla/pat"
"github.com/nanobox-io/nanoauth"
"github.com/nanopack/butter/config"
"encoding/json"
)

func Start() error {
router := pat.New()

router.Get("/branches", showBranches)
router.Get("/branches/{branch}", showBranchDetails)
router.Get("/commits", showCommits)
router.Get("/commits/{commit}", showCommitDetails)
router.Get("/files", listFiles)
router.Get("/files/{file}", getFileContents)
router.Get("/branches/{branch}", handleRequest(showBranchDetails))
router.Get("/branches", handleRequest(showBranches))
router.Get("/commits/{commit}", handleRequest(showCommitDetails))
router.Get("/commits", handleRequest(showCommits))
router.Get("/files/{file:.*}", handleRequest(getFileContents))
router.Get("/files", handleRequest(listFiles))

// blocking...
config.Log.Info("Api Listening on %s", config.HttpListenAddress)
return nanoauth.ListenAndServeTLS(config.HttpListenAddress, config.Token, router)
}

// handleRequest
func handleRequest(fn func(http.ResponseWriter, *http.Request)) http.HandlerFunc {
return func(rw http.ResponseWriter, req *http.Request) {

config.Log.Debug(`
Request:
--------------------------------------------------------------------------------
%+v
`, req)

//
fn(rw, req)

config.Log.Debug(`
Response:
--------------------------------------------------------------------------------
%+v
`, rw)
}
}

// writeBody
func writeBody(v interface{}, rw http.ResponseWriter, status int) error {
b, err := json.Marshal(v)
Expand All @@ -35,4 +59,4 @@ func writeBody(v interface{}, rw http.ResponseWriter, status int) error {
rw.Write(b)

return nil
}
}
4 changes: 2 additions & 2 deletions api/branch.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package api

import (
"net/http"
"github.com/nanopack/butter/repo"
"net/http"
)

func showBranches(rw http.ResponseWriter, req *http.Request) {
Expand All @@ -17,5 +17,5 @@ func showBranches(rw http.ResponseWriter, req *http.Request) {

// there arent branch details yet... as far as i know
func showBranchDetails(rw http.ResponseWriter, req *http.Request) {
writeBody(nil, rw, http.StatusOK)
rw.Write([]byte(req.URL.Query().Get(":branch")))
}
6 changes: 5 additions & 1 deletion api/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import (

func showCommits(rw http.ResponseWriter, req *http.Request) {
page, _ := strconv.Atoi(req.FormValue("page"))
commits, err := repo.ListCommits(req.FormValue("page"), page)
branch := req.FormValue("branch")
if branch == "" {
branch = "master"
}
commits, err := repo.ListCommits(branch, page)
if err != nil {
rw.Write([]byte(err.Error()))
rw.WriteHeader(http.StatusInternalServerError)
Expand Down
9 changes: 6 additions & 3 deletions api/file.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package api

import (
"net/http"
"fmt"
"io"

"net/http"

"github.com/nanopack/butter/repo"
)

func listFiles(rw http.ResponseWriter, req *http.Request) {
files, err := repo.ListFiles(req.FormValue("commit"))
comm := req.FormValue("commit")
fmt.Println("commmmm",comm)
files, err := repo.ListFiles(comm)
if err != nil {
rw.Write([]byte(err.Error()))
rw.WriteHeader(http.StatusInternalServerError)
Expand Down
16 changes: 8 additions & 8 deletions auth/auth.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package auth

import (
"golang.org/x/crypto/ssh"
"github.com/nanopack/butter/config"
"golang.org/x/crypto/ssh"
)

type (
Expand All @@ -15,18 +15,16 @@ type (
Initialize() error
Auth(conn ssh.ConnMetadata, password []byte) (*ssh.Permissions, error)
}

)

var (
var (
availableKeyAuthers = map[string]KeyAuther{}
defaultKeyAuther KeyAuther
defaultKeyAuther KeyAuther

availablePassAuthers = map[string]PassAuther{}
defaultPassAuther PassAuther
defaultPassAuther PassAuther
)


func KeyRegister(name string, k KeyAuther) {
availableKeyAuthers[name] = k
}
Expand All @@ -38,13 +36,15 @@ func PassRegister(name string, p PassAuther) {
func Setup() error {
keyauth, ok := availableKeyAuthers[config.KeyAuthType]
if ok {
config.Log.Info("setting up key auth(%s) location: %s", config.KeyAuthType, config.KeyAuthLocation)
defaultKeyAuther = keyauth
if err := keyauth.Initialize(); err != nil {
return err
}
}
passauth, ok := availablePassAuthers[config.PassAuthType]
if ok {
config.Log.Info("setting up pass auth(%s) location: %s", config.PassAuthType, config.PassAuthLocation)
defaultPassAuther = passauth
if err := passauth.Initialize(); err != nil {
return err
Expand All @@ -53,14 +53,14 @@ func Setup() error {
return nil
}

func KeyAuth() (func(conn ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error)) {
func KeyAuth() func(conn ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) {
if defaultKeyAuther == nil {
return nil
}
return defaultKeyAuther.Auth
}

func PassAuth() (func(conn ssh.ConnMetadata, password []byte) (*ssh.Permissions, error)) {
func PassAuth() func(conn ssh.ConnMetadata, password []byte) (*ssh.Permissions, error) {
if defaultPassAuther == nil {
return nil
}
Expand Down
57 changes: 54 additions & 3 deletions auth/script.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package auth

import (
"bytes"
"encoding/base64"
"fmt"
"golang.org/x/crypto/ssh"
"os"
"os/exec"

"github.com/nanopack/butter/config"
)

type (
ScriptPassAuth struct {}
ScriptKeyAuth struct {}
ScriptPassAuth struct{}
ScriptKeyAuth struct{}
)

func init() {
Expand All @@ -15,16 +22,60 @@ func init() {
}

func (s ScriptPassAuth) Initialize() error {
file, err := os.Open(config.PassAuthLocation)
if err != nil {
return fmt.Errorf("ScriptPassAuth: %+v", err)
}
fi, err := file.Stat()
if err != nil {
return fmt.Errorf("ScriptPassAuth: %+v", err)
}
if fi.IsDir() {
return fmt.Errorf("file given is a directory")
}
return nil
}

func (s ScriptKeyAuth) Initialize() error {
file, err := os.Open(config.KeyAuthLocation)
if err != nil {
return fmt.Errorf("ScriptKeyAuth: %+v", err)
}
fi, err := file.Stat()
if err != nil {
return fmt.Errorf("ScriptKeyAuth: %+v", err)
}
if fi.IsDir() {
return fmt.Errorf("file given is a directory")
}
return nil
}

func (s ScriptPassAuth) Auth(conn ssh.ConnMetadata, password []byte) (*ssh.Permissions, error) {
cmd := exec.Command(config.PassAuthLocation, conn.User(), conn.RemoteAddr().String())
passReader := bytes.NewReader(password)
cmd.Stdin = passReader
output, err := cmd.CombinedOutput()
if err != nil {
config.Log.Error("password authentication: %s\n%v", output, err)
return nil, err
}

// nil permissions is success?
return nil, nil
}

func (s ScriptKeyAuth) Auth(conn ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) {
cmd := exec.Command(config.KeyAuthLocation, conn.User(), conn.RemoteAddr().String())
k := fmt.Sprintf("%s\n",base64.StdEncoding.EncodeToString(key.Marshal()))
keyReader := bytes.NewReader([]byte(k))
cmd.Stdin = keyReader
output, err := cmd.CombinedOutput()
if err != nil {
config.Log.Error("key authentication: %s\n%v", output, err)
return nil, err
}

// nil permissions is success?
return nil, nil
}
}
Binary file modified butter
Binary file not shown.
27 changes: 15 additions & 12 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,31 @@ package config
import (
"io/ioutil"

"github.com/spf13/cobra"
"github.com/ghodss/yaml"
"github.com/jcelliott/lumber"
"github.com/spf13/cobra"
)

var (
SshListenAddress string
HttpListenAddress string
KeyPath string
RepoType string
RepoType string
RepoLocation string
KeyAuthType string
KeyAuthType string
KeyAuthLocation string
PassAuthType string
PassAuthType string
PassAuthLocation string
DeployType string
DeployLocation string
DeployType string
DeployLocation string
Token string
Log lumber.Logger
Log lumber.Logger
)

func init() {
Log = lumber.NewConsoleLogger(lumber.DEBUG)
}

func AddFlags(cmd *cobra.Command) {
cmd.Flags().StringVarP(&SshListenAddress, "ssh-address", "", ":2222", "[server] SshListenAddress")
cmd.Flags().StringVarP(&HttpListenAddress, "http-address", "", ":8080", "[server] HttpListenAddress")
Expand All @@ -34,17 +38,16 @@ func AddFlags(cmd *cobra.Command) {
cmd.Flags().StringVarP(&KeyAuthLocation, "key-auth-location", "", "", "[server] KeyAuthLocation")
cmd.Flags().StringVarP(&PassAuthType, "pass-auth-type", "", "", "[server] PassAuthType")
cmd.Flags().StringVarP(&PassAuthLocation, "pass-auth-location", "", "", "[server] PassAuthLocation")
cmd.Flags().StringVarP(&DeployType, "pass-auth-type", "", "", "[server] DeployType")
cmd.Flags().StringVarP(&DeployLocation, "pass-auth-location", "", "", "[server] DeployLocation")
cmd.PersistentFlags().StringVarP(&Token, "token", "", "secret" , "Token security")
cmd.Flags().StringVarP(&DeployType, "deploy-type", "", "", "[server] DeployType")
cmd.Flags().StringVarP(&DeployLocation, "deploy-location", "", "", "[server] DeployLocation")
cmd.PersistentFlags().StringVarP(&Token, "token", "", "secret", "Token security")
}

func Parse(configFile string) {
c := map[string]string{}

bytes, err := ioutil.ReadFile(configFile)
if err != nil {
Log = lumber.NewConsoleLogger(lumber.INFO)
Log.Error("unable to read config file: %v\n", err)
}
err = yaml.Unmarshal(bytes, &c)
Expand Down Expand Up @@ -96,4 +99,4 @@ func Parse(configFile string) {
if c["token"] != "" {
Token = c["token"]
}
}
}
Loading

0 comments on commit f463375

Please sign in to comment.