Skip to content

Commit

Permalink
Updating documentation, code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanejohnson committed May 24, 2018
1 parent 531b459 commit fb64429
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bbgoget
*~
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Quick and dirty hack to make nice with bitbucket privately hosted repositories that only allow ssh access. In order to set this up,

go get github.com/myENA/bbgoget

Alternately you can download the rpm or the tarball from the Releases tab.

As an example configuration, with Apache httpd running on 443 proxying for bitbucket, you could add the following to
httpd.conf section before the redirects to bitbucket's http service:

RewriteEngine on
RewriteCond "%{QUERY_STRING}" "go-get"
RewriteRule "^/(.*)" "http://localhost:8800/$1" [P]

This assumes bbgoget running locally on port 8800.

The integration with bitbucket is minimal / dumb. It has no knowledge whether or not you actually have a repository
at the requested location. It doesn't really need to, go get will figure out soon enough if the response ends up
going nowhere. The up side is this makes configuration easier. No API access required, minimal configuration.
As an added benefit of being unconcerned with tight integration with bitbucket, there is a very good chance this
would work with other git servers. Also - and most importantly - this made it very easy to author.


We've only tested it with our specific configuration.
22 changes: 10 additions & 12 deletions bbgoget.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,16 @@ type BBHandler struct {
}

func (bbh *BBHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
fmt.Printf("got request: %s\n", req.URL.String())
goGet := req.URL.Query()["go-get"]
isGoGet := false
if len(goGet) >= 1 {
if goGet[0] == "1" {
isGoGet = true
}
}

if !isGoGet {
if req.URL.Query().Get("go-get") != "1" {
resp.WriteHeader(http.StatusBadRequest)
resp.Write([]byte("not a go-get request, investigate proxy config\n"))
return
}

// Since path should begin with '/', strings.split
// will return the first element as blank, second
// as project, third as our actual repository.
pathParts := strings.Split(req.URL.Path, "/")

if len(pathParts) < 3 {
Expand All @@ -72,12 +67,13 @@ func (bbh *BBHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
return
}

// Strip everything after the repository portion of the URL
prefix := strings.Join(pathParts[:3], "/")

// Try to find the host
host := bbh.serverNameOverride
if len(host) == 0 {
if len(host) == 0 {
host = req.Header.Get("X-Forwarded-Host")
}
host = req.Header.Get("X-Forwarded-Host")
if len(host) == 0 {
host, _ = splitHostPort(req.URL.Host)
}
Expand All @@ -88,8 +84,10 @@ func (bbh *BBHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
resp.WriteHeader(http.StatusBadRequest)
resp.Write([]byte("blank host"))
}

repoURL := fmt.Sprintf("ssh://git@%s:%d%s.git", host, bbh.sshPort, prefix)
importPrefix := host + prefix

resp.WriteHeader(http.StatusOK)
w := bufio.NewWriter(resp)
_, err := w.WriteString(fmt.Sprintf("<html><head><meta name=\"go-import\" content=\"%s %s %s\"></head><body>go get</body></html>", importPrefix, "git", repoURL))
Expand Down
82 changes: 82 additions & 0 deletions bbgoget.spec
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
%define debug_package %{nil}

Name: bbgoget
Version: 0.1.1
Release: 1%{?dist}
Summary: This makes bitbucket server private repos work with go get
BuildRoot: %{_tmppath}/%{name}-%{version}-build


Group: System Environment/Daemons
License: MIT

%define pkgpath github.com/myENA/%{name}
URL: https://github.com/myENA/bbgoget

%undefine _disable_source_fetch
Source0: https://%{pkgpath}/archive/v%{version}.tar.gz
%define SHA256SUM0 66bd46fd6f8c73f2413f5c3dd9a9fcc1581953c2121813ca5a4089e37e717df8
BuildRequires: systemd-units golang

%define bbgoget_user bbgoget
%define bbgoget_group bbgoget
%define bbgoget_home /etc/sysconfig

Requires(pre): shadow-utils
Requires(post): systemd
Requires(preun): systemd
Requires(postun): systemd

%description
bitbucket server private repos + go get = gravy

%pre
getent group %{bbgoget_group} >/dev/null || \
groupadd -r %{bbgoget_group}
getent passwd %{bbgoget_user} >/dev/null || \
useradd -r -g %{bbgoget_user} -d %{bbgoget_home} \
-s /sbin/nologin -c %{name} %{bbgoget_user}
exit 0


%prep
echo "%{SHA256SUM0} %{SOURCE0}" | sha256sum -c -

%setup -q
mkdir -p go/{src,pkg,bin}
mkdir -p go/src/$(dirname %{pkgpath})
ln -s $(pwd) go/src/%{pkgpath}


%build
export GOPATH=$(pwd)/go
cd go/src/%{pkgpath}
pwd
ls -la
go build


%install
%{__install} -p -D -m 0644 go/src/%{pkgpath}/%{name}.service %{buildroot}%{_unitdir}/%{name}.service

%{__install} -p -D -m 0644 go/src/%{pkgpath}/%{name}.sysconfig %{buildroot}%{_sysconfdir}/sysconfig/%{name}

%{__install} -p -D -m 0755 go/src/%{pkgpath}/%{name} %{buildroot}%{_bindir}/%{name}


%post
%systemd_post %{name}.service

%preun
%systemd_preun %{name}.service

%postun
%systemd_postun_with_restart %{name}.service

%files
%defattr(-,root,root,-)
%{_unitdir}/%{name}.service
%{_bindir}/%{name}
%config(noreplace) %{_sysconfdir}/sysconfig/%{name}

%changelog

0 comments on commit fb64429

Please sign in to comment.