Skip to content

Commit

Permalink
Merge pull request #17 from ubclaunchpad/chad/#14-poc-deploy-key
Browse files Browse the repository at this point in the history
Initial POC for deploy keys
  • Loading branch information
chadlagore committed Dec 10, 2017
2 parents 0752741 + 82849eb commit a0184ef
Showing 1 changed file with 96 additions and 0 deletions.
96 changes: 96 additions & 0 deletions poc/deploy/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Proof-of-concept for programmatically generating a deploy key to register
// with GitHub. To test, run go main.go github.com/<yourname>/<yourrepo>.
package main

import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"

"golang.org/x/crypto/ssh"
)

var (
sshDir = "/Users/chadlagore/.ssh/"
)

func main() {
repo := os.Args[1]
CreateDeployKey(repo)
}

// CreateDeployKey creates and saves deploy keys to disk, prompts
// user with the public key (so they can copy paste to GitHub).
// Alternatively, this function can push the deploy key to GitHub,
// but we need user logon.
func CreateDeployKey(repo string) {
repoName := filepath.Base(repo) + "-inertia"

pemFile := filepath.Join(sshDir, repoName)
pubFile := filepath.Join(sshDir, repoName+".pub")

fmt.Println("New PEM file: " + pemFile)
fmt.Println("New PUB file: " + pubFile)

err := SSHKeyGen(pubFile, pemFile, 2014)
if err != nil {
fmt.Println(err)
return
}

pub, err := os.Open(pubFile)
defer pub.Close()
if err != nil {
fmt.Println(err)
return
}

deployKeyURL := "https://" + repo + "/settings/keys/new"
fmt.Println("\n😏 Your new inertia public deploy key:\n")

io.Copy(os.Stdout, pub)

fmt.Println("\nAdd it here: " + deployKeyURL)
}

// SSHKeyGen creates a public-private key pair much like ssh-keygen
// command line utility, except it does not prompt for pw. Writes
// result to disk.
// https://stackoverflow.com/a/34347463.
func SSHKeyGen(pubLoc, pemLoc string, size int) error {
privateKey, err := rsa.GenerateKey(rand.Reader, size)
if err != nil {
return err
}

// Create private key file at pemLoc.
privateKeyFile, err := os.Create(pemLoc)
defer privateKeyFile.Close()
if err != nil {
return err
}

// Use PEM encoding.
privateKeyPEM := &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
}
if err := pem.Encode(privateKeyFile, privateKeyPEM); err != nil {
return err
}

// Create a public key from private.
pub, err := ssh.NewPublicKey(&privateKey.PublicKey)
if err != nil {
return err
}

// Write to public file locatio.
return ioutil.WriteFile(pubLoc, ssh.MarshalAuthorizedKey(pub), 0655)
}

0 comments on commit a0184ef

Please sign in to comment.