Skip to content

Commit cfd2861

Browse files
authored
we are terraform devloprs! (#804)
1 parent fefa5b4 commit cfd2861

File tree

5 files changed

+30
-23
lines changed

5 files changed

+30
-23
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## Unreleased
2+
3+
BREAKING CHANGES:
4+
5+
- Allow GitHub App PEM data to be passed directly ([#803](https://github.com/integrations/terraform-provider-github/issues/803))
6+
17
## 4.10.1 (May 25, 2021)
28

39
BUG FIXES:

github/apps.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,20 @@ import (
44
"crypto/x509"
55
"encoding/json"
66
"encoding/pem"
7+
"errors"
78
"fmt"
8-
"gopkg.in/square/go-jose.v2"
9-
"gopkg.in/square/go-jose.v2/jwt"
109
"io/ioutil"
1110
"net/http"
1211
"time"
13-
)
1412

15-
// GenerateOAuthTokenFromApp generates a GitHub OAuth access token from a set of valid GitHub App credentials. The
16-
// returned token can be used to interact with both GitHub's REST and GraphQL APIs.
17-
func GenerateOAuthTokenFromApp(baseURL, appID, appInstallationID, appPemFile string) (string, error) {
18-
pemData, err := ioutil.ReadFile(appPemFile)
19-
if err != nil {
20-
return "", err
21-
}
13+
"gopkg.in/square/go-jose.v2"
14+
"gopkg.in/square/go-jose.v2/jwt"
15+
)
2216

23-
appJWT, err := generateAppJWT(appID, time.Now(), pemData)
17+
// GenerateOAuthTokenFromApp generates a GitHub OAuth access token from a set of valid GitHub App credentials.
18+
// The returned token can be used to interact with both GitHub's REST and GraphQL APIs.
19+
func GenerateOAuthTokenFromApp(baseURL, appID, appInstallationID, pemData string) (string, error) {
20+
appJWT, err := generateAppJWT(appID, time.Now(), []byte(pemData))
2421
if err != nil {
2522
return "", err
2623
}
@@ -73,6 +70,10 @@ func getInstallationAccessToken(baseURL string, jwt string, installationID strin
7370

7471
func generateAppJWT(appID string, now time.Time, pemData []byte) (string, error) {
7572
block, _ := pem.Decode(pemData)
73+
if block == nil {
74+
return "", errors.New("No decodeable PEM data found")
75+
}
76+
7677
privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
7778
if err != nil {
7879
return "", err

github/apps_test.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,30 @@ import (
55
"crypto/x509"
66
"encoding/pem"
77
"fmt"
8-
"gopkg.in/square/go-jose.v2"
9-
"gopkg.in/square/go-jose.v2/jwt"
108
"io/ioutil"
119
"strings"
1210
"testing"
1311
"time"
12+
13+
"gopkg.in/square/go-jose.v2"
14+
"gopkg.in/square/go-jose.v2/jwt"
1415
)
1516

1617
const (
1718
testGitHubAppID string = "123456789"
1819
testGitHubAppInstallationID string = "987654321"
19-
testGitHubAppPrivateKeyFile string = "test-fixtures/github-app-key.pem"
2020
testGitHubAppPublicKeyFile string = "test-fixtures/github-app-key.pub"
21+
testGitHubAppPrivateKeyFile string = "test-fixtures/github-app-key.pem"
2122
)
2223

2324
var (
2425
testEpochTime = time.Unix(0, 0)
26+
27+
testGitHubAppPrivateKeyPemData, _ = ioutil.ReadFile(testGitHubAppPrivateKeyFile)
2528
)
2629

2730
func TestGenerateAppJWT(t *testing.T) {
28-
pemData, err := ioutil.ReadFile(testGitHubAppPrivateKeyFile)
29-
if err != nil {
30-
t.Logf("Failed to read private key file '%s': %s", testGitHubAppPrivateKeyFile, err)
31-
t.FailNow()
32-
}
33-
34-
appJWT, err := generateAppJWT(testGitHubAppID, testEpochTime, pemData)
31+
appJWT, err := generateAppJWT(testGitHubAppID, testEpochTime, testGitHubAppPrivateKeyPemData)
3532
t.Log(appJWT)
3633
if err != nil {
3734
t.Logf("Failed to generate GitHub app JWT: %s", err)

github/provider.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package github
22

33
import (
44
"fmt"
5+
56
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
67
"github.com/hashicorp/terraform-plugin-sdk/terraform"
78
)
@@ -150,7 +151,7 @@ func init() {
150151
"`token`. Anonymous mode is enabled if both `token` and `app_auth` are not set.",
151152
"app_auth.id": "The GitHub App ID.",
152153
"app_auth.installation_id": "The GitHub App installation instance ID.",
153-
"app_auth.pem_file": "The GitHub App PEM file path.",
154+
"app_auth.pem_file": "The GitHub App PEM file contents.",
154155
}
155156
}
156157

website/docs/index.html.markdown

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ The following arguments are supported in the `provider` block:
104104
* `app_auth` - (Optional) Configuration block to use GitHub App installation token. When not provided, the provider can only access resources available anonymously.
105105
* `id` - (Required) This is the ID of the GitHub App. It can sourced from the `GITHUB_APP_ID` environment variable.
106106
* `installation_id` - (Required) This is the ID of the GitHub App installation. It can sourced from the `GITHUB_APP_INSTALLATION_ID` environment variable.
107-
* `pem_file` - (Required) This is the path to the GitHub App private key file. It can sourced from the `GITHUB_APP_PEM_FILE` environment variable.
107+
* `pem_file` - (Required) This is the contents of the GitHub App private key PEM file. It can also be sourced from the `GITHUB_APP_PEM_FILE` environment variable.
108+
109+
Note: If you have a PEM file on disk, you can pass it in via `pem_file = file("path/to/file.pem")`.
108110

109111
For backwards compatibility, if more than one of `owner`, `organization`,
110112
`GITHUB_OWNER` and `GITHUB_ORGANIZATION` are set, the first in this

0 commit comments

Comments
 (0)