Unable to Parse Private Key
Hi, I am trying to use the ghinstallation ghinstallation.New() method, as referenced in the docs, to create a transport for use in my github client, but I am having trouble with my RSA key being parsed. When I used ghinstallation.NewKeyFromFile() everything worked fine, but swapping to New() and feeding in a string of the key as a byte gives me the following error: could not parse private key: Invalid Key: Key must be PEM encoded PKCS1 or PKCS8 private key. I am confused by this because in their transport_test.go they are doing it the exact same way and I have to assume the tests are passing. I tried using go-githubauth as well, but ran into the same issue so it seems like the problem may exist somewhere in here for reading the key value in this way. Am I missing something?
- key declaration: https://github.com/bradleyfalzon/ghinstallation/blob/d680810648e94347929da00376d1e7067f3572dd/transport_test.go#L27-L53
- using key in
New(): https://github.com/bradleyfalzon/ghinstallation/blob/d680810648e94347929da00376d1e7067f3572dd/transport_test.go#L167
My code is like this:
var key = `-----BEGIN RSA PRIVATE KEY-----
KEY GOES HERE
-----END RSA PRIVATE KEY-----`
itr, err := ghinstallation.New(
http.DefaultTransport,
99,
123,
[]byte(key),
)
if err != nil {
log.Fatal("github authentication error: ", err)
}
githubClient := github.NewClient(&http.Client{Transport: itr})
@bradleyfalzon - can you please comment?
Is there a native way to handle the auth without either of the other packages? I just need something to get me by for now and I would rather not have to read from a file for this value if I can help it.
Did you take a look at this basic auth example? https://github.com/google/go-github/blob/master/example/basicauth/main.go
Did you take a look at this basic auth example? https://github.com/google/go-github/blob/master/example/basicauth/main.go
Does that work for GitHub app auth?
Sorry, I don't know. We'll need someone who has worked with GitHub app auth to respond.
Both implementations seem to call jwt.ParseRSAPrivateKeyFromPEM -> pem.Decode.
Maybe the content of key []byte is just wrong.
refs: https://github.com/jferrl/go-githubauth/blob/main/auth.go#L61 https://github.com/bradleyfalzon/ghinstallation/blob/d680810648e94347929da00376d1e7067f3572dd/appsTransport.go#L48 https://github.com/golang-jwt/jwt/blob/main/rsa_utils.go#L11
Im trying to reproduce the issue with go-githubauth
package main
import (
"fmt"
"github.com/jferrl/go-githubauth"
)
const key = `-----BEGIN RSA PRIVATE KEY-----
KEY GOES HERE
-----END RSA PRIVATE KEY-----`
func main() {
_, err := githubauth.NewApplicationTokenSource(123456, []byte(key))
if err != nil {
fmt.Println("Error creating application token source:", err)
return
}
}
and seems to be ok. @coleglencairn
Closing due to inactivity.