From 22debd29935246bf8013d0f0e9f07c2f396b5406 Mon Sep 17 00:00:00 2001 From: TANABE Ken-ichi Date: Sat, 8 Aug 2020 12:27:54 +0900 Subject: [PATCH] initial import --- .gitignore | 1 + README.md | 34 +++++++++++++++++++++++++++++++++ go.mod | 9 +++++++++ go.sum | 23 +++++++++++++++++++++++ main.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 122 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cdf19ed --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +go-github-apps diff --git a/README.md b/README.md new file mode 100644 index 0000000..10be0d5 --- /dev/null +++ b/README.md @@ -0,0 +1,34 @@ +# go-github-apps + +`go-github-apps` is a command-line tool to retrieve a github access token for your Github Apps. + +When you want to call Github APIs from machines, you would want an access token which independs of a real account. +Github provides several ways to issue tokens, for example: +- Issue Personal Access Token via machine-user: Before Github Apps exists, this is typical method to issue a token but it consumes one user seats. +- Create Github Apps and issue a token for the app: This is a new way and recommended way. The problem is [it's not that easy to issue a token](https://docs.github.com/en/developers/apps/authenticating-with-github-apps#authenticating-as-a-github-app) just to automate small stuff. + +This command-line tool allows you to get a token with just providing `App ID`, `Installation ID` and the private key. + +## Usage + +```sh +Usage of go-github-apps: + -app-id int + App ID + -export + show token as 'export GITHUB_TOKEN=...' + -inst-id int + Installation ID +``` + +**Example**: +```sh +export GITHUB_PRIV_KEY=$(cat your-apps-2020-08-07.private-key.pem) +eval $(go-github-apps -export -app-id 12345 -inst-id 123456) + +# github token is now exported to GITHUB_TOKEN environment variable +``` + +## AppID and Installation ID + +You can find how to get those ID at https://github.com/bradleyfalzon/ghinstallation#what-is-app-id-and-installation-id diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..40bbc14 --- /dev/null +++ b/go.mod @@ -0,0 +1,9 @@ +module github.com/nabeken/go-github-apps + +go 1.14 + +require ( + github.com/bradleyfalzon/ghinstallation v1.1.1 + github.com/google/go-github/v29 v29.0.3 + golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..491b994 --- /dev/null +++ b/go.sum @@ -0,0 +1,23 @@ +github.com/bradleyfalzon/ghinstallation v1.1.1 h1:pmBXkxgM1WeF8QYvDLT5kuQiHMcmf+X015GI0KM/E3I= +github.com/bradleyfalzon/ghinstallation v1.1.1/go.mod h1:vyCmHTciHx/uuyN82Zc3rXN3X2KTK8nUTCrTMwAhcug= +github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= +github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-github/v29 v29.0.2 h1:opYN6Wc7DOz7Ku3Oh4l7prmkOMwEcQxpFtxdU8N8Pts= +github.com/google/go-github/v29 v29.0.2/go.mod h1:CHKiKKPHJ0REzfwc14QMklvtHwCveD0PxlMjLlzAM5E= +github.com/google/go-github/v29 v29.0.3 h1:IktKCTwU//aFHnpA+2SLIi7Oo9uhAzgsdZNbcAqhgdc= +github.com/google/go-github/v29 v29.0.3/go.mod h1:CHKiKKPHJ0REzfwc14QMklvtHwCveD0PxlMjLlzAM5E= +github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk= +github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de h1:ikNHVSjEfnvz6sxdSPCaPt572qowuyMDMJLLm3Db3ig= +golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= diff --git a/main.go b/main.go new file mode 100644 index 0000000..ea87a86 --- /dev/null +++ b/main.go @@ -0,0 +1,55 @@ +package main + +import ( + "context" + "flag" + "fmt" + "log" + "net/http" + "os" + "time" + + "github.com/bradleyfalzon/ghinstallation" +) + +func main() { + appID := flag.Int64("app-id", 0, "App ID") + instID := flag.Int64("inst-id", 0, "Installation ID") + export := flag.Bool("export", false, "show token as 'export GITHUB_TOKEN=...'") + flag.Parse() + + if *appID == 0 || *instID == 0 { + fmt.Fprintf(os.Stderr, "App ID and Installation ID are required.\n\n") + flag.Usage() + os.Exit(1) + } + + key := os.Getenv("GITHUB_PRIV_KEY") + if key == "" { + log.Fatal("Please populate GITHUB_PRIV_KEY environment variable with the private key for the App") + } + + // Wrap the shared transport for use with the app ID 1 authenticating with installation ID 99. + itr, err := ghinstallation.New(http.DefaultTransport, *appID, *instID, []byte(key)) + if err != nil { + log.Fatal(err) + } + + ctx, cancel := context.WithTimeout(context.Background(), time.Minute) + defer cancel() + + token, err := itr.Token(ctx) + if err != nil { + log.Fatalf("unable to get github token: %s", err) + } + + if *export { + showExport(token) + } else { + fmt.Println(token) + } +} + +func showExport(token string) { + fmt.Printf("export GITHUB_TOKEN=%s\n", token) +}