As a user I'd like to sign a commit using the Golang OpenPGP library implementation and push to Bitbucket Server

XMLWordPrintable

    • Type: Suggestion
    • Resolution: Unresolved
    • None
    • Component/s: Administration
    • None
    • 1

      Problem Definition

      Using the following Golang Code I am able to sign and push a commit to Github but not Bitbucket Server

      package main
      
      import (
      	"crypto/tls"
      	"github.com/go-git/go-git/v5"
      	. "github.com/go-git/go-git/v5/_examples"
      	"github.com/go-git/go-git/v5/plumbing/object"
      	"github.com/go-git/go-git/v5/plumbing/transport/client"
      	githttp "github.com/go-git/go-git/v5/plumbing/transport/http"
      	"golang.org/x/crypto/openpgp"
      	"net/http"
      	"os"
      	"time"
      )
      
      func getPGPIdentity() (*openpgp.Entity, error) {
      
      	secKeyRing, err := os.Open("/<path>/<to>/.gnupg/secring.gpg")
      	if err != nil { return nil, err }
      
      	entities, err := openpgp.ReadKeyRing(secKeyRing)
      	if err != nil { return nil, err }
      	e := entities[0]
      	return e, nil
      }
      
      
      // Example of how to open a repository in a specific path, and push to
      // its default remote (origin).
      func main() {
      	CheckArgs("<repository-path>", "<uname>", "<pass>")
      	path := os.Args[1]
      	uname := os.Args[2]
      	password := os.Args[3]
      	auth := &githttp.BasicAuth{uname, password}
      
      	r, err := git.PlainOpen(path)
      	CheckIfError(err)
      	Info("opening repo..")
      
      	wt, err := r.Worktree()
      	CheckIfError(err)
      	Info("creating working tree")
      
      	err = wt.AddGlob(".")
      	CheckIfError(err)
      	Info("adding changes to staging")
      
      	pgpEntity, err := getPGPIdentity()
      	CheckIfError(err)
      
      	err = pgpEntity.PrivateKey.Decrypt([]byte("<gpg_prviate_key_pass>"))
      	CheckIfError(err)
      
      	_, err = wt.Commit("dark commit", &git.CommitOptions{
      		Author: &object.Signature{ Name: "git_user", Email: "git_user@git_user_email", When: time.Now(), },
      		Committer: &object.Signature{ Name: "git_user", Email: "git_user@git_user_email", When: time.Now(), },
      		SignKey: pgpEntity,
      	})
      	
      	Info("making commit")
      
      	// Create a custom http(s) client with your config
      	customClient := &http.Client{
      		// accept any certificate (might be useful for testing)
      		Transport: &http.Transport{
      			TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
      		},
      
      		// 15 second timeout
      		Timeout: 15 * time.Second,
      
      		// don't follow redirect
      		CheckRedirect: func(req *http.Request, via []*http.Request) error {
      			return http.ErrUseLastResponse
      		},
      	}
      
      	// Override http(s) default protocol to use our custom client
      	client.InstallProtocol("https", githttp.NewClient(customClient))
      
      	Info("git push")
      
      
      
      	// push using default options
      	err = r.Push(&git.PushOptions{ Auth: auth})
      	CheckIfError(err)
      }
      

      To use the code above:

      • First create a gpg key
      • A .kbx file is created which must be exported to a (now deprecated by gpg) secring.gpg file that the Go Lang library can use
      • In a working directory with a remote repo (origin) hosted by Bitbucket Server, modify a file and then run the code above against that repo (path to repo is one of the args the code accepts)

      Suggested Resolution

      Bitbucket Server should be able to accept the signed commit as Github is able to.

            Assignee:
            Unassigned
            Reporter:
            Nate Hansberry (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: