-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
161 lines (142 loc) · 3.63 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"strconv"
"time"
jwt "github.com/dgrijalva/jwt-go"
)
func main() {
privateKey := flag.String("private-key-file", "", "Path to file containing GitHub app private key PEM")
appIDStr := flag.String("app-id", "", "GitHub Application ID")
gitURL := flag.String("git-url", "https://api.github.com", "Github api base usr (ent: https://github.example.com/api/v3/)")
repo := flag.String("repo", "", "GitHub repository (owner/project)")
jwtOnly := flag.Bool("jwt-only", false, "Only generate the JWT (workaround for proxy issue)")
flag.Parse()
if *privateKey == "" {
panic("private-key-file is required")
}
if *appIDStr == "" {
panic("app-id is required")
}
if !*jwtOnly && *repo == "" {
panic("repo is required")
}
if !*jwtOnly && *gitURL == "" {
panic("git-url is required")
}
appID := -1
if i, err := strconv.Atoi(*appIDStr); err == nil {
appID = i
} else {
panic(err)
}
// Get RSA Private key
pkBytes, err := ioutil.ReadFile(*privateKey)
if err != nil {
panic(err)
}
pk, err := jwt.ParseRSAPrivateKeyFromPEM(pkBytes)
if err != nil {
panic(err)
}
token := jwt.NewWithClaims(jwt.SigningMethodRS256, jwt.MapClaims{
// issued at time
"iat": time.Now().Unix(),
// expiration time - 10mins max
"exp": time.Now().Add(time.Minute * 8).Unix(), // 8mins allows for clock skew 2 mins into the future
// GitHub App ID
"iss": appID,
})
// Sign and get the complete encoded token as a string using the private key
jwt, err := token.SignedString(pk)
if err != nil {
panic(err)
}
if *jwtOnly {
fmt.Println("jwt:", jwt)
return
}
// Get Installation ID
insResp, err := reqGithub(
"GET",
*gitURL,
fmt.Sprintf("/repos/%v/installation", *repo),
nil,
jwt,
)
if err != nil {
panic(err)
}
// Get Installation Token
insTokResp, err := reqGithub(
"POST",
*gitURL,
fmt.Sprintf("/app/installations/%v/access_tokens", insResp["id"]),
nil,
jwt,
)
if err != nil {
panic(err)
}
fmt.Println("token:", insTokResp["token"])
}
func reqGithub(method string, gitURL string, path string, postBody io.Reader, jwt string) (map[string]interface{}, error) {
url := fmt.Sprintf("%v%v", gitURL, path)
req, err := http.NewRequest(method, url, postBody)
if err != nil {
return nil, err
}
tr := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
req.Header.Set("Host", "github.dxc.com")
req.Header.Set("Accept", "application/vnd.github.machine-man-preview+json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %v", jwt))
client := &http.Client{Transport: tr}
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
if len(via) > 15 {
return fmt.Errorf("%d consecutive redirects", len(via))
}
if len(via) == 0 {
return nil
}
for key, val := range via[0].Header {
req.Header[key] = val
}
fmt.Printf("Redirect header: %v\n", req.Header)
return nil
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 && resp.StatusCode != 201 {
return nil, fmt.Errorf("Error: Invalid response from github, resp: %v", resp)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
subResp := make(map[string]interface{})
err = json.Unmarshal(body, &subResp)
if err != nil {
return nil, err
}
return subResp, nil
}